1
1
using System . Reflection ;
2
+ using System . Text ;
3
+ using Microsoft . CodeAnalysis . CSharp ;
4
+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
5
+ using Microsoft . CodeAnalysis . Text ;
2
6
3
7
namespace UnityUxmlGenerator ;
4
8
5
9
internal sealed partial class UxmlGenerator
6
10
{
11
+ private const string UxmlElementClassName = "UxmlElementAttribute" ;
12
+ private const string UxmlAttributeClassName = "UxmlAttributeAttribute" ;
13
+
7
14
private static readonly AssemblyName AssemblyName = typeof ( UxmlGenerator ) . Assembly . GetName ( ) ;
8
15
9
- private static readonly string GeneratedCodeAttribute =
10
- $ """ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("{ AssemblyName . Name } ", "{ AssemblyName . Version } ")]""" ;
16
+ private static SourceText GenerateUxmlElementAttribute ( )
17
+ {
18
+ var baseList = SimpleBaseType ( IdentifierName ( "global::System.Attribute" ) ) ;
11
19
12
- private static readonly string UxmlElementAttribute = $$ """
13
- // <auto-generated/>
14
- #pragma warning disable
20
+ var @class = ClassDeclaration ( UxmlElementClassName )
21
+ . WithModifiers ( TokenList ( Token ( SyntaxKind . InternalKeyword ) , Token ( SyntaxKind . SealedKeyword ) ) )
22
+ . WithBaseList ( BaseList ( SingletonSeparatedList < BaseTypeSyntax > ( baseList ) ) ) ;
15
23
16
- #nullable enable
24
+ return GetCompilationUnit ( ( TypeDeclarationSyntax ) ProcessMemberDeclaration ( @class ) , AssemblyName . Name )
25
+ . GetText ( Encoding . UTF8 ) ;
26
+ }
17
27
18
- namespace {{ AssemblyName . Name }}
19
- {
20
- {{ GeneratedCodeAttribute }}
21
- [global::System.AttributeUsageAttribute(global::System.AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
22
- internal sealed class UxmlElementAttribute : global::System.Attribute
28
+ private static SourceText GenerateUxmlAttributeAttribute ( )
23
29
{
24
- }
25
- }
26
- """ ;
30
+ var baseList = SimpleBaseType ( IdentifierName ( "global::System.Attribute" ) ) ;
27
31
28
- private static readonly string UxmlAttributeAttribute = $$ """
29
- // <auto-generated/>
30
- #pragma warning disable
32
+ var @class = ClassDeclaration ( UxmlAttributeClassName )
33
+ . WithModifiers ( TokenList ( Token ( SyntaxKind . InternalKeyword ) , Token ( SyntaxKind . SealedKeyword ) ) )
34
+ . WithBaseList ( BaseList ( SingletonSeparatedList < BaseTypeSyntax > ( baseList ) ) ) ;
31
35
32
- #nullable enable
36
+ var members = GetUxmlAttributeMembers ( ) ;
33
37
34
- namespace {{ AssemblyName . Name }}
35
- {
36
- {{ GeneratedCodeAttribute }}
37
- [global::System.AttributeUsageAttribute(global::System.AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
38
- internal sealed class UxmlAttributeAttribute : global::System.Attribute
39
- {
40
- public UxmlAttributeAttribute(object? defaultValue = default)
41
- {
42
- DefaultValue = defaultValue;
43
- }
38
+ return GetCompilationUnit ( ( TypeDeclarationSyntax ) ProcessMemberDeclaration ( @class ) , AssemblyName . Name , members )
39
+ . GetText ( Encoding . UTF8 ) ;
40
+ }
44
41
45
- public object? DefaultValue { get; }
42
+ private static MemberDeclarationSyntax [ ] GetUxmlAttributeMembers ( )
43
+ {
44
+ var classConstructor =
45
+ ConstructorDeclaration ( Identifier ( UxmlAttributeClassName ) )
46
+ . WithModifiers ( TokenList ( Token ( SyntaxKind . PublicKeyword ) ) )
47
+ . WithParameterList ( ParameterList ( SingletonSeparatedList ( Parameter ( Identifier ( "defaultValue" ) )
48
+ . WithType ( NullableType ( PredefinedType ( Token ( SyntaxKind . ObjectKeyword ) ) ) )
49
+ . WithDefault ( EqualsValueClause ( LiteralExpression ( SyntaxKind . DefaultLiteralExpression , Token ( SyntaxKind . DefaultKeyword ) ) ) ) ) ) )
50
+ . WithBody ( Block ( SingletonList < StatementSyntax > ( ExpressionStatement ( AssignmentExpression ( SyntaxKind . SimpleAssignmentExpression ,
51
+ IdentifierName ( "DefaultValue" ) ,
52
+ IdentifierName ( "defaultValue" ) ) ) ) ) ) ;
53
+
54
+ var defaultProperty =
55
+ PropertyDeclaration ( NullableType ( PredefinedType ( Token ( SyntaxKind . ObjectKeyword ) ) ) , Identifier ( "DefaultValue" ) )
56
+ . WithModifiers ( TokenList ( Token ( SyntaxKind . PublicKeyword ) ) )
57
+ . WithAccessorList ( AccessorList ( SingletonList (
58
+ AccessorDeclaration ( SyntaxKind . GetAccessorDeclaration )
59
+ . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ) ) ) ;
60
+
61
+ return new MemberDeclarationSyntax [ ] { classConstructor , defaultProperty } ;
46
62
}
47
- }
48
- """ ;
49
63
}
0 commit comments