Просмотр исходного кода

Add feature switch to enable switching compiled bindings on by default.

Jeremy Koritzinsky 3 лет назад
Родитель
Сommit
7639ebebae

+ 6 - 0
packages/Avalonia/AvaloniaBuildTasks.targets

@@ -5,6 +5,7 @@
     <AvaloniaXamlReportImportance Condition="'$(AvaloniaXamlReportImportance)' == ''">low</AvaloniaXamlReportImportance>
     <_AvaloniaPatchComInterop Condition="'$(_AvaloniaPatchComInterop)' == ''">false</_AvaloniaPatchComInterop>
     <_AvaloniaSkipXamlCompilation Condition="'$(_AvaloniaSkipXamlCompilation)' == ''">false</_AvaloniaSkipXamlCompilation>
+    <AvaloniaUseCompiledBindingsByDefault Condition="'$(AvaloniaUseCompiledBindingsByDefault)' == ''">false</AvaloniaUseCompiledBindingsByDefault>
   </PropertyGroup>
 
   <!-- Unfortunately we have to update default items in .targets since custom nuget props are improted before Microsoft.NET.Sdk.DefaultItems.props -->
@@ -20,6 +21,10 @@
     <None Remove="**\*.axaml" />
     <None Remove="**\*.paml" />
   </ItemGroup>
+
+  <ItemGroup Condition="'$(AvaloniaUseCompiledBindingsByDefault)' == 'true'">
+    <RuntimeHostConfigurationOption Include="Avalonia.UseCompiledBindingsByDefault" Value="true" />
+  </ItemGroup>
   
   <UsingTask TaskName="GenerateAvaloniaResourcesTask"
              AssemblyFile="$(AvaloniaBuildTasksLocation)"
@@ -109,6 +114,7 @@
       EnableComInteropPatching="$(_AvaloniaPatchComInterop)"
       SkipXamlCompilation="$(_AvaloniaSkipXamlCompilation)"
       DebuggerLaunch="$(AvaloniaXamlIlDebuggerLaunch)"
+      DefaultCompileBindings="$(AvaloniaUseCompiledBindingsByDefault)"
     />
     <Exec
       Condition="'$(_AvaloniaUseExternalMSBuild)' == 'true'"

+ 3 - 1
src/Avalonia.Build.Tasks/CompileAvaloniaXamlTask.cs

@@ -36,7 +36,7 @@ namespace Avalonia.Build.Tasks
 
             var res = XamlCompilerTaskExecutor.Compile(BuildEngine, input,
                 File.ReadAllLines(ReferencesFilePath).Where(l => !string.IsNullOrWhiteSpace(l)).ToArray(),
-                ProjectDirectory, OutputPath, VerifyIl, outputImportance,
+                ProjectDirectory, OutputPath, VerifyIl, DefaultCompileBindings, outputImportance,
                 (SignAssembly && !DelaySign) ? AssemblyOriginatorKeyFile : null,
                 EnableComInteropPatching, SkipXamlCompilation, DebuggerLaunch);
             if (!res.Success)
@@ -72,6 +72,8 @@ namespace Avalonia.Build.Tasks
         public string OutputPath { get; set; }
 
         public bool VerifyIl { get; set; }
+
+        public bool DefaultCompileBindings { get; set; }
         
         public bool EnableComInteropPatching { get; set; }
         public bool SkipXamlCompilation { get; set; }

+ 7 - 6
src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs

@@ -39,15 +39,15 @@ namespace Avalonia.Build.Tasks
 
         public static CompileResult Compile(IBuildEngine engine, string input, string[] references,
             string projectDirectory,
-            string output, bool verifyIl, MessageImportance logImportance, string strongNameKey, bool patchCom,
+            string output, bool verifyIl, bool defaultCompileBindings, MessageImportance logImportance, string strongNameKey, bool patchCom,
             bool skipXamlCompilation)
         {
-            return Compile(engine, input, references, projectDirectory, output, verifyIl, logImportance, strongNameKey, patchCom, skipXamlCompilation, debuggerLaunch:false);
+            return Compile(engine, input, references, projectDirectory, output, verifyIl, defaultCompileBindings, logImportance, strongNameKey, patchCom, skipXamlCompilation, debuggerLaunch:false);
         }
 
         internal static CompileResult Compile(IBuildEngine engine, string input, string[] references,
             string projectDirectory,
-            string output, bool verifyIl, MessageImportance logImportance, string strongNameKey, bool patchCom, bool skipXamlCompilation, bool debuggerLaunch)
+            string output, bool verifyIl, bool defaultCompileBindings, MessageImportance logImportance, string strongNameKey, bool patchCom, bool skipXamlCompilation, bool debuggerLaunch)
         {
             var typeSystem = new CecilTypeSystem(references
                 .Where(r => !r.ToLowerInvariant().EndsWith("avalonia.build.tasks.dll"))
@@ -57,7 +57,7 @@ namespace Avalonia.Build.Tasks
 
             if (!skipXamlCompilation)
             {
-                var compileRes = CompileCore(engine, typeSystem, projectDirectory, verifyIl, logImportance, debuggerLaunch);
+                var compileRes = CompileCore(engine, typeSystem, projectDirectory, verifyIl, defaultCompileBindings, logImportance, debuggerLaunch);
                 if (compileRes == null && !patchCom)
                     return new CompileResult(true);
                 if (compileRes == false)
@@ -78,7 +78,8 @@ namespace Avalonia.Build.Tasks
         }
 
         static bool? CompileCore(IBuildEngine engine, CecilTypeSystem typeSystem,
-            string projectDirectory, bool verifyIl, 
+            string projectDirectory, bool verifyIl,
+            bool defaultCompileBindings,
             MessageImportance logImportance
             , bool debuggerLaunch = false)
         {
@@ -143,7 +144,7 @@ namespace Avalonia.Build.Tasks
             var contextClass = XamlILContextDefinition.GenerateContextClass(typeSystem.CreateTypeBuilder(contextDef), typeSystem,
                 xamlLanguage, emitConfig);
 
-            var compiler = new AvaloniaXamlIlCompiler(compilerConfig, emitConfig, contextClass) { EnableIlVerification = verifyIl };
+            var compiler = new AvaloniaXamlIlCompiler(compilerConfig, emitConfig, contextClass) { EnableIlVerification = verifyIl, DefaultCompileBindings = defaultCompileBindings };
 
             var editorBrowsableAttribute = typeSystem
                 .GetTypeReference(typeSystem.FindType("System.ComponentModel.EditorBrowsableAttribute"))

+ 13 - 7
src/Markup/Avalonia.Markup.Xaml.Loader/AvaloniaXamlIlRuntimeCompiler.cs

@@ -24,6 +24,8 @@ namespace Avalonia.Markup.Xaml.XamlIl
 {
     static class AvaloniaXamlIlRuntimeCompiler
     {
+        private const string UseCompileBindingsByDefaultConfigSwitch = "Avalonia.UseCompiledBindingsByDefault";
+
 #if !RUNTIME_XAML_CECIL
         private static SreTypeSystem _sreTypeSystem;
         private static Type _ignoresAccessChecksFromAttribute;
@@ -178,13 +180,14 @@ namespace Avalonia.Markup.Xaml.XamlIl
             var clrPropertyBuilder = tb.DefineNestedType("ClrProperties_" + Guid.NewGuid().ToString("N"));
             var indexerClosureType = _sreBuilder.DefineType("IndexerClosure_" + Guid.NewGuid().ToString("N"));
 
+            bool compileBindingsByDefault = AppContext.TryGetSwitch(UseCompileBindingsByDefaultConfigSwitch, out var compileBindingsSwitchValue) && compileBindingsSwitchValue;
+
             var compiler = new AvaloniaXamlIlCompiler(new AvaloniaXamlIlCompilerConfiguration(_sreTypeSystem, asm,
                 _sreMappings, _sreXmlns, AvaloniaXamlIlLanguage.CustomValueConverter,
                 new XamlIlClrPropertyInfoEmitter(_sreTypeSystem.CreateTypeBuilder(clrPropertyBuilder)),
                 new XamlIlPropertyInfoAccessorFactoryEmitter(_sreTypeSystem.CreateTypeBuilder(indexerClosureType))), 
                 _sreEmitMappings,
-                _sreContextType) { EnableIlVerification = true };
-            
+                _sreContextType) { EnableIlVerification = true, DefaultCompileBindings = compileBindingsByDefault };
 
             IXamlType overrideType = null;
             if (rootInstance != null)
@@ -200,8 +203,8 @@ namespace Avalonia.Markup.Xaml.XamlIl
             return LoadOrPopulate(created, rootInstance);
         }
 #endif
-        
-        static object LoadOrPopulate(Type created, object rootInstance)
+
+            static object LoadOrPopulate(Type created, object rootInstance)
         {
             var isp = Expression.Parameter(typeof(IServiceProvider));
 
@@ -299,8 +302,6 @@ namespace Avalonia.Markup.Xaml.XamlIl
             {
                 overrideType = _cecilTypeSystem.GetType(rootInstance.GetType().FullName);
             }
-
-            
            
             var safeUri = uri.ToString()
                 .Replace(":", "_")
@@ -324,13 +325,18 @@ namespace Avalonia.Markup.Xaml.XamlIl
             asm.MainModule.Types.Add(contextDef);
             
             var tb = _cecilTypeSystem.CreateTypeBuilder(def);
+            
+            bool compileBindingsByDefault = AppContext.TryGetSwitch(UseCompileBindingsByDefaultConfigSwitch, out var compileBindingsSwitchValue) && compileBindingsSwitchValue;
 
             var compiler = new AvaloniaXamlIlCompiler(new XamlIlTransformerConfiguration(_cecilTypeSystem,
                     localAssembly == null ? null : _cecilTypeSystem.FindAssembly(localAssembly.GetName().Name),
                     _cecilMappings, XamlIlXmlnsMappings.Resolve(_cecilTypeSystem, _cecilMappings),
                     AvaloniaXamlIlLanguage.CustomValueConverter),
                 _cecilEmitMappings,
-                _cecilTypeSystem.CreateTypeBuilder(contextDef));
+                _cecilTypeSystem.CreateTypeBuilder(contextDef))
+                {
+                    DefaultCompileBindings = compileBindingsByDefault
+                };
             compiler.ParseAndCompile(xaml, uri.ToString(), tb, overrideType);
             var asmPath = Path.Combine(_cecilEmitDir, safeUri + ".dll");
             using(var f = File.Create(asmPath))