Skip to content

Commit c7f1642

Browse files
author
krafs
authored
Merge pull request #5 from krafs/reference-replacement
Replace ReferencePaths instead of changing ReferenceAssembly-attribute.
2 parents 40926e8 + 0016d7b commit c7f1642

8 files changed

Lines changed: 52 additions & 91 deletions

File tree

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ dotnet_analyzer_diagnostic.severity = error
2424
# IDE0090: Use 'new(...)'
2525
dotnet_diagnostic.IDE0090.severity = none
2626

27+
# IDE0130: Namespace does not match containing folder.
28+
dotnet_diagnostic.IDE0130.severity = none
29+
2730
# Organize usings
2831
dotnet_separate_import_directive_groups = false
2932
dotnet_sort_system_directives_first = true

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Publicizer is an MSBuild library for allowing direct access to non-public members in referenced assemblies.
33

44
## Installation
5-
Use the Visual Studio package manager and reference [Krafs.Publicizer](https://www.nuget.org/packages/Krafs.Publicizer).
5+
Use your IDE's package manager and reference [Krafs.Publicizer](https://www.nuget.org/packages/Krafs.Publicizer) from nuget.org.
66

77
Or add via the dotnet CLI:
88
```bash
@@ -11,15 +11,13 @@ dotnet add package Krafs.Publicizer
1111
Or add directly to your project file:
1212
```xml
1313
<ItemGroup>
14-
<PackageReference Include="Krafs.Publicizer" Version="1.0.0" />
14+
<PackageReference Include="Krafs.Publicizer" Version="1.0.1" />
1515
</ItemGroup>
1616
```
1717

1818
## Usage
1919
Define _Publicize_-items in your project file to instruct Publicizer what to make public.
2020

21-
Any assembly referenced by the project - through Reference, PackageReference, ProjectReference - can be publicized.
22-
2321
### Publicize an entire assembly:
2422
```xml
2523
<ItemGroup>
@@ -28,6 +26,8 @@ Any assembly referenced by the project - through Reference, PackageReference, Pr
2826
```
2927
Doing this will publicize all the assembly's containing members.
3028

29+
Make sure you type the assembly name without the file extension, i.e. **without** '.dll'.
30+
3131
### Publicize a specific member:
3232
```xml
3333
<ItemGroup>
@@ -53,6 +53,20 @@ As with most Items, you can include multiple patterns with semi-colons:
5353
<Publicize Include="AssemblyOne;AssemblyTwo;AssemblyThree" />
5454
</ItemGroup>
5555
```
56+
### Publicize assemblies from a PackageReference
57+
PackageReferences, like other kinds of References, point towards one or more underlying assemblies. Publicizing these assemblies is just a matter of finding out what the underlying assemblies are called, and then specify them the same way as above.
58+
59+
Below is an example of publicizing two assemblies from the package [Krafs.Rimworld.Ref](https://www.nuget.org/packages/Krafs.Rimworld.Ref/):
60+
```xml
61+
<ItemGroup>
62+
<PackageReference Include="Krafs.Publicizer" Version="1.0.1" />
63+
<PackageReference Include="Krafs.Rimworld.Ref" Version="1.3.3117" />
64+
</ItemGroup>
65+
66+
<ItemGroup>
67+
<Publicize Include="Assembly-CSharp;UnityEngine.CoreModule" />
68+
</ItemGroup>
69+
```
5670

5771
### Publicize All
5872
You can use this shorthand property to publicize all assemblies referenced by the project:
@@ -62,13 +76,16 @@ You can use this shorthand property to publicize all assemblies referenced by th
6276
</PropertyGroup>
6377
```
6478

65-
Save the project file and the changes should take effect shortly.
79+
Save the project file and the changes should take effect shortly. If not, try performing a _Restore_.
6680

6781
## How Publicizer works
6882
Publicizer works by copying the referenced assemblies into memory, rewriting the access modifiers to public, and feeding those assemblies to the compiler instead of the real ones.
83+
Publicized assemblies are cached in _obj_ for future builds.
6984

7085
Additionally, the project's assembly is compiled as [unsafe](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code/). Among other things, this tells the runtime to not enforce access checks. Without this, accessing an unpermitted member at runtime throws a [MemberAccessException](https://docs.microsoft.com/en-us/dotnet/api/system.memberaccessexception/).
7186

87+
This means that you do **NOT** have to specify _AllowUnsafeBlocks=true_ in your project file. Publicizer does this for you under the hood.
88+
7289
By default, Publicizer additionally creates the new assemblies as [reference assemblies](https://docs.microsoft.com/en-us/dotnet/standard/assembly/reference-assemblies/).
7390
This reduces build times and memory usage. However, if you use your IDE's decompilation feature to inspect code, you may want to turn that off, or you will just see empty methods.
7491
Do that by specifying this property:
@@ -78,7 +95,7 @@ Do that by specifying this property:
7895
</PropertyGroup>
7996
```
8097
## Acknowledgements
81-
Project builds upon rwmt's [Publicise](https://github.com/rwmt/Publicise), simplyWiri's [TaskPubliciser](https://github.com/simplyWiri/TaskPubliciser), and [this gist](https://gist.github.com/Zetrith/d86b1d84e993c8117983c09f1a5dcdcd) by Zetrith
98+
This project builds upon rwmt's [Publicise](https://github.com/rwmt/Publicise), simplyWiri's [TaskPubliciser](https://github.com/simplyWiri/TaskPubliciser), and [this gist](https://gist.github.com/Zetrith/d86b1d84e993c8117983c09f1a5dcdcd) by Zetrith.
8299

83100

84101
## License

src/Publicizer.Tests/Publicizer.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
1111
<PackageReference Include="NUnit" Version="3.13.2" />
1212
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
13-
<PackageReference Include="MSBuild.ProjectCreation" Version="4.0.4" />
13+
<PackageReference Include="MSBuild.ProjectCreation" Version="6.2.4" />
1414
</ItemGroup>
1515

1616
<ItemGroup>

src/Publicizer/ItemConstants.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,11 @@ public static class ItemConstants
88
public static class DoNotPublicize
99
{
1010
public const string ItemName = "DoNotPublicize";
11-
public const string Pattern = "Pattern";
12-
public const string Priority = "Priority";
13-
public const string ParameterTypes = "ParameterTypes";
1411
}
1512

1613
public static class Publicize
1714
{
1815
public const string ItemName = "Publicize";
19-
public const string Pattern = "Pattern";
20-
public const string Priority = "Priority";
21-
public const string ParameterTypes = "ParameterTypes";
22-
public const string Ignore = "Ignore";
23-
}
24-
25-
public static class ReferencePath
26-
{
27-
public const string ReferenceAssembly = "ReferenceAssembly";
2816
}
2917
}
3018
}

src/Publicizer/Krafs.Publicizer.targets

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
DoNotPublicizes="@(DoNotPublicize)"
2020
OutputDirectory="$(IntermediateOutputPath)PublicizedAssemblies\"
2121
PublicizeAsReferenceAssemblies="$(PublicizeAsReferenceAssemblies)">
22-
<Output TaskParameter="UpdatedReferencePaths" ItemName="_UpdatedReferencePaths" />
22+
<Output TaskParameter="ReferencePathsToDelete" ItemName="_ReferencePathsToDelete" />
23+
<Output TaskParameter="ReferencePathsToAdd" ItemName="_ReferencePathsToAdd" />
2324
</PublicizeAssemblies>
2425

2526
<ItemGroup>
26-
<ReferencePath Remove="@(_UpdatedReferencePaths)" />
27-
<ReferencePath Include="@(_UpdatedReferencePaths)" />
27+
<ReferencePath Remove="@(_ReferencePathsToDelete)" />
28+
<ReferencePath Include="@(_ReferencePathsToAdd)" />
2829
</ItemGroup>
2930

3031
<PropertyGroup>

src/Publicizer/PublicizeAssemblies.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public class PublicizeAssemblies : Task
1818
public string? PublicizeAsReferenceAssemblies { get; set; }
1919

2020
[Output]
21-
public ITaskItem[]? UpdatedReferencePaths { get; set; }
21+
public ITaskItem[]? ReferencePathsToDelete { get; set; }
22+
23+
[Output]
24+
public ITaskItem[]? ReferencePathsToAdd { get; set; }
2225

2326
public override bool Execute()
2427
{
@@ -101,7 +104,8 @@ public override bool Execute()
101104
doNotPublicizes.Add(pattern);
102105
}
103106

104-
List<ITaskItem> updatedReferencePaths = new List<ITaskItem>();
107+
List<ITaskItem> referencePathsToDelete = new List<ITaskItem>();
108+
List<ITaskItem> referencePathsToAdd = new List<ITaskItem>();
105109

106110
foreach (ITaskItem reference in ReferencePaths)
107111
{
@@ -131,22 +135,26 @@ public override bool Execute()
131135
module.Write(fileStream);
132136
}
133137

134-
reference.SetReferenceAssemblyPath(outputAssemblyPath);
135-
updatedReferencePaths.Add(reference);
138+
referencePathsToDelete.Add(reference);
139+
ITaskItem newReference = new TaskItem(outputAssemblyPath);
140+
reference.CopyMetadataTo(newReference);
141+
referencePathsToAdd.Add(newReference);
136142
}
137143

138-
UpdatedReferencePaths = updatedReferencePaths.ToArray();
144+
ReferencePathsToDelete = referencePathsToDelete.ToArray();
145+
ReferencePathsToAdd = referencePathsToAdd.ToArray();
146+
139147
return true;
140148
}
141149

142-
private static string ComputeHash(string assemblyPath, List<string> publicizes, List<string> assemblyDoNotPublicizes)
150+
private static string ComputeHash(string assemblyPath, List<string> publicizes, List<string> doNotPublicizes)
143151
{
144152
StringBuilder sb = new StringBuilder();
145153
foreach (string publicizePattern in publicizes)
146154
{
147155
sb.Append(publicizePattern);
148156
}
149-
foreach (string doNotPublicizePattern in assemblyDoNotPublicizes)
157+
foreach (string doNotPublicizePattern in doNotPublicizes)
150158
{
151159
sb.Append(doNotPublicizePattern);
152160
}

src/Publicizer/Publicizer.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
<RepositoryUrl>https://github.com/krafs/Publicizer.git</RepositoryUrl>
2121
<Description>MSBuild library for allowing direct access to non-public members in .NET assemblies.</Description>
2222
<PackageTags>msbuild accesschecks public publicizer</PackageTags>
23-
<Version>1.0.0</Version>
23+
<Version>1.0.1</Version>
2424
</PropertyGroup>
2525

2626
<ItemGroup>
27-
<PackageReference Include="dnlib" Version="3.3.3" />
28-
<PackageReference Include="ILRepack.Lib.MSBuild" Version="2.1.16.1" PrivateAssets="All" />
29-
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.10.0" PrivateAssets="All" ExcludeAssets="runtime" />
27+
<PackageReference Include="dnlib" Version="3.3.4" />
28+
<PackageReference Include="ILRepack.Lib.MSBuild" Version="2.1.17.1" PrivateAssets="All" />
29+
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.11.0" PrivateAssets="all" ExcludeAssets="runtime" />
3030
</ItemGroup>
3131

3232
<ItemGroup>
Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,17 @@
1-
using System;
2-
using Microsoft.Build.Framework;
1+
using Microsoft.Build.Framework;
32

43
namespace Publicizer
54
{
65
public static class TaskItemExtensions
76
{
8-
public static string GetPattern(this ITaskItem item)
9-
{
10-
return item.GetMetadata(ItemConstants.Publicize.Pattern);
11-
}
12-
13-
public static int GetPriority(this ITaskItem item)
14-
{
15-
if (!int.TryParse(item.GetMetadata(ItemConstants.Publicize.Priority), out int priority))
16-
{
17-
priority = 0;
18-
}
19-
20-
return priority;
21-
}
22-
237
public static string GetFileName(this ITaskItem item)
248
{
259
return item.GetMetadata(ItemConstants.FileName);
2610
}
2711

28-
public static string[] GetIgnorePatterns(this ITaskItem item)
29-
{
30-
string ignorePatternValue = item.GetMetadata("IgnorePatterns");
31-
if (string.IsNullOrWhiteSpace(ignorePatternValue))
32-
{
33-
return Array.Empty<string>();
34-
}
35-
36-
return ignorePatternValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
37-
}
38-
3912
public static string GetFullPath(this ITaskItem item)
4013
{
4114
return item.GetMetadata(ItemConstants.FullPath);
4215
}
43-
44-
public static bool GetIgnore(this ITaskItem item)
45-
{
46-
string ignore = item.GetMetadata(ItemConstants.Publicize.Ignore);
47-
bool shouldIgnore = ignore?.Equals(bool.TrueString, StringComparison.OrdinalIgnoreCase) ?? false;
48-
49-
return shouldIgnore;
50-
}
51-
52-
public static void SetReferenceAssemblyPath(this ITaskItem item, string path)
53-
{
54-
item.SetMetadata(ItemConstants.ReferencePath.ReferenceAssembly, path);
55-
}
56-
57-
public static string GetRawParameterTypes(this ITaskItem item)
58-
{
59-
return item.GetMetadata(ItemConstants.Publicize.ParameterTypes);
60-
}
61-
62-
public static string[] GetParameterTypes(this ITaskItem item)
63-
{
64-
string parameterTypesValue = item.GetRawParameterTypes();
65-
if (string.IsNullOrWhiteSpace(parameterTypesValue))
66-
{
67-
return Array.Empty<string>();
68-
}
69-
70-
return parameterTypesValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
71-
}
7216
}
7317
}

0 commit comments

Comments
 (0)