Make AngleSharp not strong-named. Change assembly name to Microsoft.AspNetCore.Blazor.AngleSharp to ensure no conflicts with other usages.

This commit is contained in:
Steve Sanderson 2018-02-09 17:37:21 +00:00
parent 15ddcd03b0
commit 5a9c41af0e
3 changed files with 33 additions and 10 deletions

View File

@ -34,7 +34,7 @@
<ItemGroup>
<ProjectReference Include="..\anglesharp\AngleSharpBuilder\AngleSharpBuilder.csproj" ReferenceOutputAssembly="false" />
<Reference Include="AngleSharp" HintPath="..\anglesharp\AngleSharpBuilder\dist\AngleSharp.dll" />
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp" HintPath="..\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll" />
</ItemGroup>
</Project>

View File

@ -18,6 +18,10 @@ namespace AngleSharpBuilder
* specifies [InternalsVisibleTo("Microsoft.AspNetCore.Blazor.Build")]. Longer term we can ask
* AngleSharp to expose HtmlTokenizer as a public API, and if that's not viable, possibly
* replace AngleSharp with a different library for HTML tokenization.
*
* Similarly, we have build-process reasons for needing the assembly not to be strong
* named and be called Microsoft.AspNetCore.Blazor.AngleSharp. These requirements will
* not be permanent but it enables progress in the short term.
*/
public static class Program
@ -25,20 +29,41 @@ namespace AngleSharpBuilder
public static void Main()
{
var outputDir = Path.Combine(Directory.GetCurrentDirectory(), "dist");
var angleSharpAssembly = Assembly.GetAssembly(typeof(HtmlParser));
WriteWithInternalsVisibleTo(
angleSharpAssembly,
"Microsoft.AspNetCore.Blazor.Build",
outputDir);
var inputAssembly = Assembly.GetAssembly(typeof(HtmlParser));
WriteModifiedAssembly(inputAssembly, outputDir);
}
private static void WriteWithInternalsVisibleTo(Assembly assembly, string internalVisibleToArg, string outputDir)
private static void WriteModifiedAssembly(Assembly assembly, string outputDir)
{
Directory.CreateDirectory(outputDir);
var assemblyLocation = assembly.Location;
var moduleDefinition = ModuleDefinition.ReadModule(assemblyLocation);
AddInternalsVisibleTo(moduleDefinition, "Microsoft.AspNetCore.Blazor.Build");
RemoveStrongName(moduleDefinition);
SetAssemblyName(moduleDefinition, "Microsoft.AspNetCore.Blazor.AngleSharp");
moduleDefinition.Write(
Path.Combine(outputDir, $"{moduleDefinition.Name}.dll"));
}
private static void SetAssemblyName(ModuleDefinition moduleDefinition, string name)
{
moduleDefinition.Name = name;
moduleDefinition.Assembly.Name.Name = name;
}
private static void RemoveStrongName(ModuleDefinition moduleDefinition)
{
var assemblyName = moduleDefinition.Assembly.Name;
assemblyName.HasPublicKey = false;
assemblyName.PublicKey = new byte[0];
moduleDefinition.Attributes &= ~ModuleAttributes.StrongNameSigned;
}
private static void AddInternalsVisibleTo(ModuleDefinition moduleDefinition, string internalVisibleToArg)
{
var internalsVisibleToCtor = moduleDefinition.ImportReference(
typeof(InternalsVisibleToAttribute).GetConstructor(new[] { typeof(string) }));
@ -47,8 +72,6 @@ namespace AngleSharpBuilder
new CustomAttributeArgument(moduleDefinition.TypeSystem.String, internalVisibleToArg));
moduleDefinition.Assembly.CustomAttributes.Add(customAttribute);
moduleDefinition.Write(Path.Combine(outputDir, Path.GetFileName(assemblyLocation)));
}
}
}

View File

@ -23,7 +23,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\anglesharp\AngleSharpBuilder\AngleSharpBuilder.csproj" ReferenceOutputAssembly="false" />
<Reference Include="AngleSharp" HintPath="..\..\src\anglesharp\AngleSharpBuilder\dist\AngleSharp.dll" />
<Reference Include="Microsoft.AspNetCore.Blazor.AngleSharp" HintPath="..\..\src\anglesharp\AngleSharpBuilder\dist\Microsoft.AspNetCore.Blazor.AngleSharp.dll" />
</ItemGroup>
</Project>