소스 검색

Patch MSBuild tasks assembly during packaging

Nikita Tsukanov 5 년 전
부모
커밋
8a5dcaddd4
4개의 변경된 파일81개의 추가작업 그리고 19개의 파일을 삭제
  1. 2 0
      nukebuild/Build.cs
  2. 76 0
      nukebuild/BuildTasksPatcher.cs
  3. 3 0
      nukebuild/_build.csproj
  4. 0 19
      src/Avalonia.Build.Tasks/Avalonia.Build.Tasks.csproj

+ 2 - 0
nukebuild/Build.cs

@@ -268,6 +268,8 @@ partial class Build : NukeBuild
         .DependsOn(CreateIntermediateNugetPackages)
         .Executes(() =>
         {
+            BuildTasksPatcher.PatchBuildTasksInPackage(Parameters.NugetIntermediateRoot / "Avalonia.Build.Tasks." +
+                                                       Parameters.Version + ".nupkg");
             var config = Numerge.MergeConfiguration.LoadFile(RootDirectory / "nukebuild" / "numerge.config");
             EnsureCleanDirectory(Parameters.NugetRoot);
             if(!Numerge.NugetPackageMerger.Merge(Parameters.NugetIntermediateRoot, Parameters.NugetRoot, config,

+ 76 - 0
nukebuild/BuildTasksPatcher.cs

@@ -0,0 +1,76 @@
+using System;
+using System.IO;
+using System.IO.Compression;
+using System.Linq;
+using ILRepacking;
+using Mono.Cecil;
+
+public class BuildTasksPatcher
+{
+    public static void PatchBuildTasksInPackage(string packagePath)
+    {
+        using (var archive = new ZipArchive(File.Open(packagePath, FileMode.Open, FileAccess.ReadWrite),
+            ZipArchiveMode.Update))
+        {
+
+            foreach (var entry in archive.Entries.ToList())
+            {
+                if (entry.Name == "Avalonia.Build.Tasks.dll")
+                {
+                    var temp = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".dll");
+                    var output = temp + ".output";
+                    var patched = new MemoryStream();
+                    try
+                    {
+                        entry.ExtractToFile(temp, true);
+                        var repack = new ILRepacking.ILRepack(new RepackOptions()
+                        {
+                            Internalize = true,
+                            InputAssemblies = new[]
+                            {
+                                temp, typeof(Mono.Cecil.AssemblyDefinition).Assembly.GetModules()[0]
+                                    .FullyQualifiedName
+                            },
+                            SearchDirectories = new string[0],
+                            OutputFile = output
+                        });
+                        repack.Repack();
+
+
+                        // 'hurr-durr assembly with the same name is already loaded' prevention
+                        using (var asm = AssemblyDefinition.ReadAssembly(output,
+                            new ReaderParameters { ReadWrite = true, InMemory = true, }))
+                        {
+                            asm.Name = new AssemblyNameDefinition(
+                                "Avalonia.Build.Tasks."
+                                + Guid.NewGuid().ToString().Replace("-", ""),
+                                new Version(0, 0, 0));
+                            asm.Write(patched);
+                            patched.Position = 0;
+                        }
+                    }
+                    finally
+                    {
+                        try
+                        {
+                            if (File.Exists(temp))
+                                File.Delete(temp);
+                            if (File.Exists(output))
+                                File.Delete(output);
+                        }
+                        catch
+                        {
+                            //ignore
+                        }
+                    }
+
+                    var fn = entry.FullName;
+                    entry.Delete();
+                    var newEntry = archive.CreateEntry(fn, CompressionLevel.Optimal);
+                    using (var s = newEntry.Open())
+                        patched.CopyTo(s);
+                }
+            }
+        }
+    }
+}

+ 3 - 0
nukebuild/_build.csproj

@@ -14,6 +14,9 @@
     <PackageReference Include="xunit.runner.console" Version="2.3.1" />
     <PackageReference Include="JetBrains.dotMemoryUnit" Version="3.0.20171219.105559" />
     <PackageReference Include="vswhere" Version="2.6.7" Condition=" '$(OS)' == 'Windows_NT' " />
+    <PackageReference Include="ILRepack.NETStandard" Version="2.0.4" />
+    <!-- Keep in sync with Avalonia.Build.Tasks -->
+    <PackageReference Include="Avalonia.Unofficial.Cecil" Version="20190417.2.0" />
   </ItemGroup>
 
   <ItemGroup>

+ 0 - 19
src/Avalonia.Build.Tasks/Avalonia.Build.Tasks.csproj

@@ -46,25 +46,6 @@
       <Compile Remove="../Markup/Avalonia.Markup.Xaml/XamlIl\xamlil.github\**\obj\**\*.cs" />
       <Compile Remove="../Markup/Avalonia.Markup.Xaml/XamlIl\xamlil.github\src\XamlIl\TypeSystem\SreTypeSystem.cs" />
       <PackageReference Include="Avalonia.Unofficial.Cecil" Version="20190417.2.0" PrivateAssets="All" />
-      <PackageReference Condition="$(TargetFramework) == 'netstandard2.0'" Include="ILRepack.MSBuild.Task" Version="2.0.13" PrivateAssets="All" />
       <PackageReference Include="Microsoft.Build.Framework" Version="15.1.548" PrivateAssets="All" />
     </ItemGroup>
-
-  <Target Name="ILRepack" AfterTargets="Build" Condition="$(TargetFramework) == 'netstandard2.0'">
-
-    <PropertyGroup>
-      <WorkingDirectory>$(MSBuildThisFileDirectory)bin\$(Configuration)\$(TargetFramework)</WorkingDirectory>
-    </PropertyGroup>
-
-    <ItemGroup>
-      <InputAssemblies Include="Mono.Cecil.dll" />
-    </ItemGroup>
-    <ILRepack OutputType="$(OutputType)" MainAssembly="$(AssemblyName).dll" OutputAssembly="$(AssemblyName).dll" InputAssemblies="@(InputAssemblies)" WorkingDirectory="$(WorkingDirectory)" />
-    <ItemGroup>
-      <DeleteNonNeededResults Include="$(WorkingDirectory)\*.dll" />
-      <DeleteNonNeededResults Remove="$(WorkingDirectory)\Avalonia.Build.Tasks.dll" />
-    </ItemGroup>
-    <Delete Files="@(DeleteNonNeededResults)" />
-
-  </Target>
 </Project>