Skip to content

Commit 6b4a6d1

Browse files
authored
Fix For AoT build (#20)
Remove Attributes and Generate them instead, resulting in no reference to ReactiveUI.SourceGenerators being required in end App.
1 parent 05fca0a commit 6b4a6d1

File tree

13 files changed

+176
-96
lines changed

13 files changed

+176
-96
lines changed

src/ReactiveUI.SourceGenerators.Execute.Maui/ReactiveUI.SourceGenerators.Execute.Maui.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</ItemGroup>
2525

2626
<ItemGroup>
27-
<ProjectReference Include="..\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" OutputItemType="Analyzer" />
27+
<ProjectReference Include="..\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
2828
</ItemGroup>
2929

3030
</Project>

src/ReactiveUI.SourceGenerators.Execute/ReactiveUI.SourceGenerators.Execute.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<ProjectReference Include="..\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" OutputItemType="Analyzer" />
16+
<ProjectReference Include="..\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

src/ReactiveUI.SourceGenerators/Attributes/IViewForAttribute.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/ReactiveUI.SourceGenerators/Attributes/ObservableAsPropertyAttribute.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/ReactiveUI.SourceGenerators/Attributes/ReactiveAttribute.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/ReactiveUI.SourceGenerators/Attributes/ReactiveCommandAttribute.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/ReactiveUI.SourceGenerators/Attributes/ReactiveObjectAttribute.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
namespace ReactiveUI.SourceGenerators.Helpers;
7+
8+
internal static class AttributeDefinitions
9+
{
10+
public const string ReactiveObjectAttribute = """
11+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
12+
// Licensed to the .NET Foundation under one or more agreements.
13+
// The .NET Foundation licenses this file to you under the MIT license.
14+
// See the LICENSE file in the project root for full license information.
15+
16+
using System;
17+
18+
// <auto-generated/>
19+
#pragma warning disable
20+
#nullable enable
21+
namespace ReactiveUI.SourceGenerators;
22+
23+
/// <summary>
24+
/// ReactiveObjectAttribute.
25+
/// </summary>
26+
/// <seealso cref="System.Attribute" />
27+
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
28+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
29+
public sealed class ReactiveObjectAttribute : Attribute;
30+
#nullable restore
31+
#pragma warning restore
32+
""";
33+
34+
public const string ReactiveCommandAttribute = """
35+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
36+
// Licensed to the .NET Foundation under one or more agreements.
37+
// The .NET Foundation licenses this file to you under the MIT license.
38+
// See the LICENSE file in the project root for full license information.
39+
40+
using System;
41+
42+
// <auto-generated/>
43+
#pragma warning disable
44+
#nullable enable
45+
namespace ReactiveUI.SourceGenerators;
46+
47+
/// <summary>
48+
/// ReativeCommandAttribute.
49+
/// </summary>
50+
/// <seealso cref="Attribute" />
51+
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
52+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
53+
public sealed class ReactiveCommandAttribute : Attribute
54+
{
55+
/// <summary>
56+
/// Gets the can execute method or property.
57+
/// </summary>
58+
/// <value>
59+
/// The name of the CanExecute Observable of bool.
60+
/// </value>
61+
public string? CanExecute { get; init; }
62+
}
63+
#nullable restore
64+
#pragma warning restore
65+
""";
66+
67+
public const string ReactiveAttribute = """
68+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
69+
// Licensed to the .NET Foundation under one or more agreements.
70+
// The .NET Foundation licenses this file to you under the MIT license.
71+
// See the LICENSE file in the project root for full license information.
72+
73+
using System;
74+
75+
// <auto-generated/>
76+
#pragma warning disable
77+
#nullable enable
78+
namespace ReactiveUI.SourceGenerators;
79+
80+
/// <summary>
81+
/// ReactiveAttribute.
82+
/// </summary>
83+
/// <seealso cref="Attribute" />
84+
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
85+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
86+
public sealed class ReactiveAttribute : Attribute;
87+
#nullable restore
88+
#pragma warning restore
89+
""";
90+
91+
public const string ObservableAsPropertyAttribute = """
92+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
93+
// Licensed to the .NET Foundation under one or more agreements.
94+
// The .NET Foundation licenses this file to you under the MIT license.
95+
// See the LICENSE file in the project root for full license information.
96+
97+
using System;
98+
99+
// <auto-generated/>
100+
#pragma warning disable
101+
#nullable enable
102+
namespace ReactiveUI.SourceGenerators;
103+
104+
/// <summary>
105+
/// ReactivePropertyAttribute.
106+
/// </summary>
107+
/// <seealso cref="Attribute" />
108+
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
109+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
110+
public sealed class ObservableAsPropertyAttribute : Attribute;
111+
#nullable restore
112+
#pragma warning restore
113+
""";
114+
115+
public const string IViewForAttribute = """
116+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
117+
// Licensed to the .NET Foundation under one or more agreements.
118+
// The .NET Foundation licenses this file to you under the MIT license.
119+
// See the LICENSE file in the project root for full license information.
120+
121+
using System;
122+
123+
// <auto-generated/>
124+
#pragma warning disable
125+
#nullable enable
126+
namespace ReactiveUI.SourceGenerators;
127+
128+
/// <summary>
129+
/// ReactiveObjectAttribute.
130+
/// </summary>
131+
/// <seealso cref="System.Attribute" />
132+
/// <remarks>
133+
/// Initializes a new instance of the <see cref="IViewForAttribute"/> class.
134+
/// </remarks>
135+
/// <param name="viewModelType">Type of the view model.</param>
136+
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
137+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
138+
public sealed class IViewForAttribute(string? viewModelType) : Attribute;
139+
#nullable restore
140+
#pragma warning restore
141+
""";
142+
}

src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
using System.Collections.Immutable;
88
using System.IO;
99
using System.Linq;
10+
using System.Text;
1011
using System.Threading;
1112
using Microsoft.CodeAnalysis;
1213
using Microsoft.CodeAnalysis.CSharp;
1314
using Microsoft.CodeAnalysis.CSharp.Syntax;
15+
using Microsoft.CodeAnalysis.Text;
1416
using ReactiveUI.SourceGenerators.Extensions;
1517
using ReactiveUI.SourceGenerators.Helpers;
1618
using ReactiveUI.SourceGenerators.Input.Models;
@@ -31,7 +33,10 @@ public sealed partial class IViewForGenerator : IIncrementalGenerator
3133
/// <inheritdoc/>
3234
public void Initialize(IncrementalGeneratorInitializationContext context)
3335
{
34-
// Gather info for all annotated command methods (starting from method declarations with at least one attribute)
36+
context.RegisterPostInitializationOutput(ctx =>
37+
ctx.AddSource($"{IViewForAttribute}.g.cs", SourceText.From(AttributeDefinitions.IViewForAttribute, Encoding.UTF8)));
38+
39+
// Gather info for all annotated IViewFor Classes
3540
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<IViewForInfo> Info)> iViewForInfoWithErrors =
3641
context.SyntaxProvider
3742
.ForAttributeWithMetadataName(

src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
using System.Collections.Immutable;
77
using System.Linq;
8+
using System.Text;
89
using Microsoft.CodeAnalysis;
910
using Microsoft.CodeAnalysis.CSharp.Syntax;
11+
using Microsoft.CodeAnalysis.Text;
1012
using ReactiveUI.SourceGenerators.Extensions;
13+
using ReactiveUI.SourceGenerators.Helpers;
1114
using ReactiveUI.SourceGenerators.Models;
1215

1316
namespace ReactiveUI.SourceGenerators;
@@ -18,14 +21,19 @@ namespace ReactiveUI.SourceGenerators;
1821
[Generator(LanguageNames.CSharp)]
1922
public sealed partial class ObservableAsPropertyGenerator : IIncrementalGenerator
2023
{
24+
private const string ObservableAsPropertyAttribute = "ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute";
25+
2126
/// <inheritdoc/>
2227
public void Initialize(IncrementalGeneratorInitializationContext context)
2328
{
29+
context.RegisterPostInitializationOutput(ctx =>
30+
ctx.AddSource($"{ObservableAsPropertyAttribute}.g.cs", SourceText.From(AttributeDefinitions.ObservableAsPropertyAttribute, Encoding.UTF8)));
31+
2432
// Gather info for all annotated command methods (starting from method declarations with at least one attribute)
2533
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<PropertyInfo> Info)> propertyInfoWithErrors =
2634
context.SyntaxProvider
2735
.ForAttributeWithMetadataName(
28-
"ReactiveUI.SourceGenerators.ObservableAsPropertyAttribute",
36+
ObservableAsPropertyAttribute,
2937
static (node, _) => node is VariableDeclaratorSyntax { Parent: VariableDeclarationSyntax { Parent: FieldDeclarationSyntax { Parent: ClassDeclarationSyntax or RecordDeclarationSyntax, AttributeLists.Count: > 0 } } },
3038
static (context, token) =>
3139
{

0 commit comments

Comments
 (0)