AvaSourceGenerators 0.2.0

dotnet add package AvaSourceGenerators --version 0.2.0
NuGet\Install-Package AvaSourceGenerators -Version 0.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AvaSourceGenerators" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AvaSourceGenerators --version 0.2.0
#r "nuget: AvaSourceGenerators, 0.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AvaSourceGenerators as a Cake Addin
#addin nuget:?package=AvaSourceGenerators&version=0.2.0

// Install AvaSourceGenerators as a Cake Tool
#tool nuget:?package=AvaSourceGenerators&version=0.2.0

AvaSourceGenerators

Source generators for AvaloniaUI boilerplate code.

Example

To use it, simply declare a class as partial and annotate a field with the AvaStyledProperty attribute to generate a StyledProperty, or the AvaDirectProperty to generate a DirectProperty. For example:

using Avalonia.Controls.Primitives;
using Lucdem.Avalonia.SourceGenerators.Attributes;

namespace Lucdem.Avalonia.SourceGenerators.Sample.Controls;

public partial class LabeledButton : TemplatedControl
{
    public LabeledButton() { }

    [AvaStyledProperty]
    private string labelText = string.Empty;
}

The source generator will detect the attribute annotated field and create another partial class declaration with the basic StyledProperty boilerplate:

public static readonly StyledProperty<string> LabelTextProperty = AvaloniaProperty.Register<LabeledButton, string>("LabelText");

public string LabelText 
{ 
	get => GetValue(LabelTextProperty);
	set => SetValue(LabelTextProperty, value);
}

Well, or something equivalent to that at least. The exact output of the code generator is not exactly made for human eyes:

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace Lucdem.Avalonia.SourceGenerators.Sample.Controls
{
    /// <inheritdoc/>
    partial class LabeledButton
    {
        public static readonly global::Avalonia.StyledProperty<string> LabelTextProperty = global::Avalonia.AvaloniaProperty.Register<LabeledButton, string>("LabelText");
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        [global::System.CodeDom.Compiler.GeneratedCode("Lucdem.Avalonia.SourceGenerators.Generators.StyledPropertyGenerator", "0.1.0.0")]
        public string LabelText { get => GetValue(LabelTextProperty); set => SetValue(LabelTextProperty, value); }
    }
}

The same goes for DirectProperties, the following code:

using Avalonia.Controls.Primitives;
using Lucdem.Avalonia.SourceGenerators.Attributes;

namespace Lucdem.Avalonia.SourceGenerators.Sample.Controls;

public partial class SliderWithValueBox : TemplatedControl
{
    [AvaDirectProperty]
    private double _value = 25;
}

Will trigger the source generator to create this partial declaration:

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace Lucdem.Avalonia.SourceGenerators.Sample.Controls
{
    /// <inheritdoc/>
    partial class SliderWithValueBox
    {
        public static readonly global::Avalonia.DirectProperty<SliderWithValueBox, double> ValueProperty = global::Avalonia.AvaloniaProperty.RegisterDirect<SliderWithValueBox, double>("Value", o => o.Value, (o, v) => o.Value = v);
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        [global::System.CodeDom.Compiler.GeneratedCode("Lucdem.Avalonia.SourceGenerators.Generators.DirectPropertyGenerator", "0.1.1.2")]
        public double Value { get => _value; set => SetAndRaise(ValueProperty, ref _value, value); }
    }
}

How to use

The project is available as a nuget package.

How to inspect generated source code in VisualStudio or Rider

After referencing the nuget package in your project:

In VisualStudio, go to {YourProject}->Dependencies->Analyzers->Lucdem.Avalonia.SourceGenerators->{NameOfTheGenerator}.

In Rider, go to {YourProject}->Dependencies->Source Generators->{NameOfTheGenerator}.

TODO list

  • Add Diagnostics (warn user that the class should inherit from AvaloniaObject, that the annotated field must start with lower case character, etc)
  • Add generators for attached properties
  • Maybe use field value as the default value for the property?
  • Add a way to mark a function as the validation function for a property (something like a [AvaPropValidator(nameof(PropertyName))] annotation maybe?)
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.0 116 3/3/2024
0.1.1 91 2/29/2024
0.1.0.11 95 2/29/2024