Quellcode durchsuchen

Inject AvaloniaUseCompiledBindingsByDefault from Cecil

Max Katz vor 3 Jahren
Ursprung
Commit
66ad721763

+ 0 - 10
packages/Avalonia/AvaloniaBuildTasks.targets

@@ -45,16 +45,6 @@
     <BuildAvaloniaResourcesDependsOn>$(BuildAvaloniaResourcesDependsOn);AddAvaloniaResources;ResolveReferences;_GenerateAvaloniaResourcesDependencyCache</BuildAvaloniaResourcesDependsOn>
   </PropertyGroup>
 
-  <Target Name="AddXamlMetadataAssemblyAttributes"
-          BeforeTargets="CoreGenerateAssemblyInfo">
-    <ItemGroup>
-      <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="$(AvaloniaUseCompiledBindingsByDefault) != ''" >
-        <_Parameter1>AvaloniaUseCompiledBindingsByDefault</_Parameter1>
-        <_Parameter2>$(AvaloniaUseCompiledBindingsByDefault)</_Parameter2>
-      </AssemblyAttribute>
-    </ItemGroup>
-  </Target>
-
   <Target Name="_GenerateAvaloniaResourcesDependencyCache" BeforeTargets="GenerateAvaloniaResources">
     <ItemGroup>
       <CustomAdditionalGenerateAvaloniaResourcesInputs Include="$(IntermediateOutputPath)/Avalonia/Resources.Inputs.cache" />

+ 2 - 0
src/Avalonia.Build.Tasks/BuildEngineErrorCode.cs

@@ -5,5 +5,7 @@ namespace Avalonia.Build.Tasks
         InvalidXAML = 1,
         DuplicateXClass = 2,
         LegacyResmScheme = 3,
+
+        Unknown = 9999
     }
 }

+ 36 - 18
src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs

@@ -49,29 +49,37 @@ namespace Avalonia.Build.Tasks
             string projectDirectory,
             string output, bool verifyIl, bool defaultCompileBindings, MessageImportance logImportance, string strongNameKey, bool skipXamlCompilation, bool debuggerLaunch)
         {
-            var typeSystem = new CecilTypeSystem(
-                references.Where(r => !r.ToLowerInvariant().EndsWith("avalonia.build.tasks.dll")),
-                input);
-
-            var asm = typeSystem.TargetAssemblyDefinition;
-
-            if (!skipXamlCompilation)
+            try
             {
-                var compileRes = CompileCore(engine, typeSystem, projectDirectory, verifyIl, defaultCompileBindings, logImportance, debuggerLaunch);
-                if (compileRes == null)
-                    return new CompileResult(true);
-                if (compileRes == false)
-                    return new CompileResult(false);
-            }
+                var typeSystem = new CecilTypeSystem(
+                    references.Where(r => !r.ToLowerInvariant().EndsWith("avalonia.build.tasks.dll")),
+                    input);
 
-            var writerParameters = new WriterParameters { WriteSymbols = asm.MainModule.HasSymbols };
-            if (!string.IsNullOrWhiteSpace(strongNameKey))
-                writerParameters.StrongNameKeyBlob = File.ReadAllBytes(strongNameKey);
+                var asm = typeSystem.TargetAssemblyDefinition;
+
+                if (!skipXamlCompilation)
+                {
+                    var compileRes = CompileCore(engine, typeSystem, projectDirectory, verifyIl, defaultCompileBindings,
+                        logImportance, debuggerLaunch);
+                    if (compileRes == null)
+                        return new CompileResult(true);
+                    if (compileRes == false)
+                        return new CompileResult(false);
+                }
 
-            asm.Write(output, writerParameters);
+                var writerParameters = new WriterParameters { WriteSymbols = asm.MainModule.HasSymbols };
+                if (!string.IsNullOrWhiteSpace(strongNameKey))
+                    writerParameters.StrongNameKeyBlob = File.ReadAllBytes(strongNameKey);
 
-            return new CompileResult(true, true);
+                asm.Write(output, writerParameters);
 
+                return new CompileResult(true, true);
+            }
+            catch (Exception ex)
+            {
+                engine.LogError(BuildEngineErrorCode.Unknown, "", ex.Message);
+                return new CompileResult(false);
+            }
         }
 
         static bool? CompileCore(IBuildEngine engine, CecilTypeSystem typeSystem,
@@ -115,6 +123,16 @@ namespace Avalonia.Build.Tasks
                 // Nothing to do
                 return null;
 
+            if (asm.MainModule.TryGetTypeReference("System.Reflection.AssemblyMetadataAttribute", out var asmMetadata))
+            {
+                var ctor = asm.MainModule.ImportReference(asmMetadata.Resolve()
+                    .GetConstructors().First(c => c.Parameters.Count == 2).Resolve());
+                var strType = asm.MainModule.ImportReference(typeof(string));
+                var arg1 = new CustomAttributeArgument(strType, "AvaloniaUseCompiledBindingsByDefault");
+                var arg2 = new CustomAttributeArgument(strType, defaultCompileBindings.ToString());
+                asm.CustomAttributes.Add(new CustomAttribute(ctor) { ConstructorArguments = { arg1, arg2 } });
+            }
+            
             var clrPropertiesDef = new TypeDefinition("CompiledAvaloniaXaml", "XamlIlHelpers",
                 TypeAttributes.Class, asm.MainModule.TypeSystem.Object);
             asm.MainModule.Types.Add(clrPropertiesDef);