Skip to content

sundews/Sundew.Xaml.Optimization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sundew.Xaml.Optimization

The interface project for Sundew.Xaml.Optimizer.BuildTask - A framework for creating Xaml optimizers for WPF, Avalonia, Maui, WinUI, UWP and Xamarin.Forms.

Implementing an optimizer:

  1. Create a new .NET Standard 2.0 project.
  2. Reference Sundew.Xaml.Optimization e.g. via Nuget.
  3. Create the optimizer class and implement IXamlOptimizer. Currently WPF, Maui, Avalonia, WinUI, UWP and Xamarin.Forms are supported and each optimizer have to indicate which platforms they support.
  4. If the optimization requires code to work at runtime, this will have to be included in an additional dll or package dependency.
  5. Build and distribute dll(s), e.g. via NuGet

Usage sample:

Refer to the sample: https://github.com/sundews/Sundew.Xaml.Optimizer.Sample

Implementing IXamlOptimizer:

The IXamlOptimizer interface should be implemented to create a xaml optimizer.

Constructors:

Constructors in xaml optimizer can accept an optional settings object:

  • Additionally, a JObject (Json.NET) is accepted. This represents the settings for a particular optimizer in the "settings.sxos" file. Instead of JObject, a custom type, deserializing the settings is also permitted.

Optimize method:

The Optimize method is called gets called with the following information:

  • A list of all xaml files in the project as XDocument.
  • XamlPlatformInfo contains some basic information about the current xaml platform.
  • ProjectInfo, which contains information about the project, e.g. target framework, output type, assembly name etc..

The Optimize method should return an OptimizationResult containing the results of the optimization. When rewriting the xaml, it is recommended to not change the line ordering, because the optimized Xaml will be compiled and after compilation not be visible to the programmer (unless using a decompiler).

Xaml Namespaces:

When modifying Xaml it might be necessary to add xmlns declarations. This can be done via the XNamespaceInserter class, which ensures that prefixes are unique and that the same namespace is not added multiple times.

Returning additional files:

When returning a successful optimization, additional files may also be passed to the result, which allows for creating more advanced optimizations, e.g. with generated code.

Implementing a Xaml Analyzer:

An IXamlOptimizer can implement code analysis, in this case it does not modify the xaml, but returns a list of diagnostics in the OptimizationResult, which are then reported to MSBuild.

Packaging the optimizer:

Generally, an optimizer can be packaged in any way, e.g. as a NuGet package. When packaging the following rules are recommended:

  • Merge assemblies into a single dll to avoid assembly resolution issues. Example with ILRepack:
  • Do not merge Sundew.Xaml.Optimization.dll, System.*dlls as they are already loaded.
  • The analyzer should be packaged into the "build" folder
  • If the optimizer requires runtime code, this should be packaged into the "lib" folder or provided as a NuGet package dependency.

In Solution optimizers:

Adding an optimizer from within the solution is possible similar to how Roslyn analyzers work:
Refer to lines in the sample here

  • Assuming an optionally IL merged analyzer the dll path is determined.
  • Analyzer added to Sxo MSBuild Item
  • Anlyzer project is referenced with ReferenceOutputAssembly="False" to ensure build order, but not depending on the analyzer at runtime.

Sample optimizers:

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages