1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Diagnostics ;
3
4
using System . IO ;
4
5
using System . Linq ;
5
6
using System . Reflection ;
6
7
using System . Runtime . InteropServices ;
7
8
using Microsoft . CodeAnalysis ;
8
9
using Uno . Extensions ;
10
+ using Uno . Foundation . Interop ;
9
11
using Uno . UI . SourceGenerators . Helpers ;
10
12
using Uno . Roslyn ;
11
13
@@ -32,7 +34,6 @@ public void Initialize(GeneratorInitializationContext context)
32
34
33
35
public void Execute ( GeneratorExecutionContext context )
34
36
{
35
-
36
37
if ( ! DesignTimeHelper . IsDesignTime ( context ) )
37
38
{
38
39
_bindingsPaths = context . GetMSBuildPropertyValue ( "TSBindingsPath" ) ? . ToString ( ) ;
@@ -59,52 +60,69 @@ from module in sym.Modules
59
60
60
61
internal void GenerateTSMarshallingLayouts ( IEnumerable < IModuleSymbol > modules )
61
62
{
62
- var messageTypes = from module in modules
63
- from type in GetNamespaceTypes ( module )
64
- where (
65
- type . FindAttributeFlattened ( _interopMessageSymbol ) != null
66
- && type . TypeKind == TypeKind . Struct
67
- )
68
- select type ;
63
+ var messages = from module in modules
64
+ from type in GetNamespaceTypes ( module )
65
+ let attr = type . FindAttributeFlattened ( _interopMessageSymbol )
66
+ where attr is not null && type . TypeKind is TypeKind . Struct
67
+ select ( type , attr ) ;
69
68
70
- messageTypes = messageTypes . ToArray ( ) ;
69
+ messages = messages . ToArray ( ) ;
71
70
72
- foreach ( var messageType in messageTypes )
71
+ foreach ( var message in messages )
73
72
{
74
- var packValue = GetStructPack ( messageType ) ;
73
+ var packValue = GetStructPack ( message . type ) ;
75
74
76
75
var sb = new IndentedStringBuilder ( ) ;
77
76
78
77
sb . AppendLineInvariant ( $ "/* { nameof ( TSBindingsGenerator ) } Generated code -- this code is regenerated on each build */") ;
79
78
80
- using ( sb . BlockInvariant ( $ "class { messageType . Name } ") )
79
+ var ns = message . type . ContainingNamespace . ToDisplayString ( ) ;
80
+ if ( message . type . ContainingType ? . Name ? . Contains ( "WindowManagerInterop" , StringComparison . OrdinalIgnoreCase ) ?? false )
81
81
{
82
- sb . AppendLineInvariant ( $ "/* Pack={ packValue } */") ;
82
+ // For backward compatibility, we include the namespace only for types that are not part of the WindowsManagerInterop.
83
+ // We should include the namespace for all messages, but it would require to update all usages.
84
+ ns = null ;
85
+ }
83
86
84
- foreach ( var field in messageType . GetFields ( ) )
87
+ using ( ns is null ? null : sb . BlockInvariant ( $ "namespace { ns } ") )
88
+ {
89
+ using ( sb . BlockInvariant ( $ "{ ( ns is null ? "" : "export " ) } class { message . type . Name } ") )
85
90
{
86
- sb . AppendLineInvariant ( $ "public { field . Name } : { GetTSFieldType ( field . Type ) } ;") ;
87
- }
91
+ sb . AppendLineInvariant ( $ "/* Pack={ packValue } */") ;
88
92
89
- if ( messageType . Name . EndsWith ( "Params" ) || messageType . Name . EndsWith ( "EventArgs" ) )
90
- {
91
- GenerateUnmarshaler ( messageType , sb , packValue ) ;
92
- }
93
+ foreach ( var field in message . type . GetFields ( ) )
94
+ {
95
+ sb . AppendLineInvariant ( $ "public { field . Name } : { GetTSFieldType ( field . Type ) } ;" ) ;
96
+ }
93
97
94
- if ( messageType . Name . EndsWith ( "Return" ) || messageType . Name . EndsWith ( "EventArgs" ) )
95
- {
96
- GenerateMarshaler ( messageType , sb , packValue ) ;
98
+ if ( message . attr . GetNamedValue < CodeGeneration > ( nameof ( TSInteropMessageAttribute . UnMarshaller ) ) switch
99
+ {
100
+ CodeGeneration . Enabled => true ,
101
+ CodeGeneration . Disabled => false ,
102
+ _ => message . type . Name . EndsWith ( "Params" ) || message . type . Name . EndsWith ( "EventArgs" ) ,
103
+ } )
104
+ {
105
+ GenerateUnmarshaler ( message . type , sb , packValue ) ;
106
+ }
107
+
108
+ if ( message . attr . GetNamedValue < CodeGeneration > ( nameof ( TSInteropMessageAttribute . Marshaller ) ) switch
109
+ {
110
+ CodeGeneration . Enabled => true ,
111
+ CodeGeneration . Disabled => false ,
112
+ _ => message . type . Name . EndsWith ( "Return" ) || message . type . Name . EndsWith ( "EventArgs" ) ,
113
+ } )
114
+ {
115
+ GenerateMarshaler ( message . type , sb , packValue ) ;
116
+ }
97
117
}
98
118
}
99
119
100
- var outputPath = Path . Combine ( _bindingsPaths , $ "{ messageType . Name } .ts") ;
120
+ var outputPath = Path . Combine ( _bindingsPaths , $ "{ ( ns is null ? "" : ns . Replace ( '.' , '_' ) + "_" ) } { message . type . Name } .ts") ;
101
121
102
122
var fileExists = File . Exists ( outputPath ) ;
103
123
var output = sb . ToString ( ) ;
104
124
105
- if (
106
- ( fileExists && File . ReadAllText ( outputPath ) != output )
107
- || ! fileExists )
125
+ if ( ! fileExists || File . ReadAllText ( outputPath ) != output )
108
126
{
109
127
File . WriteAllText ( outputPath , output ) ;
110
128
}
@@ -361,6 +379,7 @@ field.Type.SpecialType is SpecialType.System_UInt32 ||
361
379
SymbolEqualityComparer . Default . Equals ( field . Type , _intPtrSymbol ) ||
362
380
field . Type . SpecialType is SpecialType . System_Single ||
363
381
field . Type . SpecialType is SpecialType . System_Boolean ||
382
+ field . Type . SpecialType is SpecialType . System_Byte ||
364
383
field . Type is IArrayTypeSymbol
365
384
)
366
385
{
0 commit comments