1+ using System . Collections . Generic ;
2+ using System . IO ;
3+ using System . Linq ;
4+ using System . Reflection ;
5+ using System . Threading . Tasks ;
6+ using Microsoft . CodeAnalysis ;
7+ using Microsoft . CodeAnalysis . CSharp ;
8+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
9+
10+ namespace Workshop . Manager
11+ {
12+ public static class CompilationUtilitys
13+ {
14+ #region Public Method
15+
16+ public static MemoryStream CompileClientProxy ( IEnumerable < SyntaxTree > trees , IEnumerable < MetadataReference > references )
17+ {
18+ #if ! NET
19+ var assemblys = new [ ]
20+ {
21+ "System.Runtime" ,
22+ "mscorlib" ,
23+ "System.Threading.Tasks" ,
24+ "System.Collections"
25+ } ;
26+ references = assemblys . Select ( i => MetadataReference . CreateFromFile ( Assembly . Load ( new AssemblyName ( i ) ) . Location ) ) . Concat ( references ) . Distinct ( ) ;
27+ #endif
28+ references = new [ ]
29+ {
30+ MetadataReference . CreateFromFile ( typeof ( SyntaxTree ) . GetTypeInfo ( ) . Assembly . Location ) ,
31+ MetadataReference . CreateFromFile ( typeof ( StatementSyntax ) . GetTypeInfo ( ) . Assembly . Location ) ,
32+ MetadataReference . CreateFromFile ( typeof ( SyntaxFactory ) . GetTypeInfo ( ) . Assembly . Location )
33+ } . Concat ( references ) . Distinct ( ) ;
34+ return Compile ( AssemblyInfo . Create ( "RoslynSyntaxTool.Proxy.ConvertToCSharpProxy" ) , trees , references ) ;
35+ }
36+
37+ public static MemoryStream Compile ( AssemblyInfo assemblyInfo , IEnumerable < SyntaxTree > trees , IEnumerable < MetadataReference > references )
38+ {
39+ return Compile ( assemblyInfo . Title , assemblyInfo , trees , references ) ;
40+ }
41+
42+ public static MemoryStream Compile ( string assemblyName , AssemblyInfo assemblyInfo , IEnumerable < SyntaxTree > trees , IEnumerable < MetadataReference > references )
43+ {
44+ trees = trees . Concat ( new [ ] { GetAssemblyInfo ( assemblyInfo ) } ) ;
45+ var compilation = CSharpCompilation . Create ( assemblyName , trees , references , new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) ) ;
46+ var stream = new MemoryStream ( ) ;
47+ var result = compilation . Emit ( stream ) ;
48+ if ( ! result . Success )
49+ {
50+ foreach ( var message in result . Diagnostics . Select ( i => i . ToString ( ) ) )
51+ {
52+ System . Console . WriteLine ( message ) ;
53+ }
54+ return null ;
55+ }
56+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
57+ return stream ;
58+ }
59+
60+ #endregion Public Method
61+
62+ #region Private Method
63+
64+ private static SyntaxTree GetAssemblyInfo ( AssemblyInfo info )
65+ {
66+ return SyntaxFactory . CompilationUnit ( )
67+ . WithUsings (
68+ SyntaxFactory . List (
69+ new [ ]
70+ {
71+ SyntaxFactory . UsingDirective (
72+ SyntaxFactory . QualifiedName (
73+ SyntaxFactory . IdentifierName ( "System" ) ,
74+ SyntaxFactory . IdentifierName ( "Reflection" ) ) ) ,
75+ SyntaxFactory . UsingDirective (
76+ SyntaxFactory . QualifiedName (
77+ SyntaxFactory . QualifiedName (
78+ SyntaxFactory . IdentifierName ( "System" ) ,
79+ SyntaxFactory . IdentifierName ( "Runtime" ) ) ,
80+ SyntaxFactory . IdentifierName ( "InteropServices" ) ) ) ,
81+ SyntaxFactory . UsingDirective (
82+ SyntaxFactory . QualifiedName (
83+ SyntaxFactory . QualifiedName (
84+ SyntaxFactory . IdentifierName ( "System" ) ,
85+ SyntaxFactory . IdentifierName ( "Runtime" ) ) ,
86+ SyntaxFactory . IdentifierName ( "Versioning" ) ) )
87+ } ) )
88+ . WithAttributeLists (
89+ SyntaxFactory . List (
90+ new [ ]
91+ {
92+ SyntaxFactory . AttributeList (
93+ SyntaxFactory . SingletonSeparatedList (
94+ SyntaxFactory . Attribute (
95+ SyntaxFactory . IdentifierName ( "TargetFramework" ) )
96+ . WithArgumentList (
97+ SyntaxFactory . AttributeArgumentList (
98+ SyntaxFactory . SeparatedList < AttributeArgumentSyntax > (
99+ new SyntaxNodeOrToken [ ] {
100+ SyntaxFactory . AttributeArgument (
101+ SyntaxFactory . LiteralExpression (
102+ SyntaxKind . StringLiteralExpression ,
103+ SyntaxFactory . Literal ( "net6.0" ) ) ) ,
104+ SyntaxFactory . Token ( SyntaxKind . CommaToken ) ,
105+ SyntaxFactory . AttributeArgument (
106+ SyntaxFactory . LiteralExpression (
107+ SyntaxKind . StringLiteralExpression ,
108+ SyntaxFactory . Literal ( "net6.0" ) ) )
109+ . WithNameEquals (
110+ SyntaxFactory . NameEquals (
111+ SyntaxFactory . IdentifierName ( "FrameworkDisplayName" ) ) ) } ) ) ) ) )
112+ . WithTarget (
113+ SyntaxFactory . AttributeTargetSpecifier (
114+ SyntaxFactory . Token ( SyntaxKind . AssemblyKeyword ) ) ) ,
115+ SyntaxFactory . AttributeList (
116+ SyntaxFactory . SingletonSeparatedList (
117+ SyntaxFactory . Attribute (
118+ SyntaxFactory . IdentifierName ( "AssemblyTitle" ) )
119+ . WithArgumentList (
120+ SyntaxFactory . AttributeArgumentList (
121+ SyntaxFactory . SingletonSeparatedList (
122+ SyntaxFactory . AttributeArgument (
123+ SyntaxFactory . LiteralExpression (
124+ SyntaxKind . StringLiteralExpression ,
125+ SyntaxFactory . Literal ( info . Title ) ) ) ) ) ) ) )
126+ . WithTarget (
127+ SyntaxFactory . AttributeTargetSpecifier (
128+ SyntaxFactory . Token ( SyntaxKind . AssemblyKeyword ) ) ) ,
129+ SyntaxFactory . AttributeList (
130+ SyntaxFactory . SingletonSeparatedList (
131+ SyntaxFactory . Attribute (
132+ SyntaxFactory . IdentifierName ( "AssemblyProduct" ) )
133+ . WithArgumentList (
134+ SyntaxFactory . AttributeArgumentList (
135+ SyntaxFactory . SingletonSeparatedList (
136+ SyntaxFactory . AttributeArgument (
137+ SyntaxFactory . LiteralExpression (
138+ SyntaxKind . StringLiteralExpression ,
139+ SyntaxFactory . Literal ( info . Product ) ) ) ) ) ) ) )
140+ . WithTarget (
141+ SyntaxFactory . AttributeTargetSpecifier (
142+ SyntaxFactory . Token ( SyntaxKind . AssemblyKeyword ) ) ) ,
143+ SyntaxFactory . AttributeList (
144+ SyntaxFactory . SingletonSeparatedList (
145+ SyntaxFactory . Attribute (
146+ SyntaxFactory . IdentifierName ( "AssemblyCopyright" ) )
147+ . WithArgumentList (
148+ SyntaxFactory . AttributeArgumentList (
149+ SyntaxFactory . SingletonSeparatedList (
150+ SyntaxFactory . AttributeArgument (
151+ SyntaxFactory . LiteralExpression (
152+ SyntaxKind . StringLiteralExpression ,
153+ SyntaxFactory . Literal ( info . Copyright ) ) ) ) ) ) ) )
154+ . WithTarget (
155+ SyntaxFactory . AttributeTargetSpecifier (
156+ SyntaxFactory . Token ( SyntaxKind . AssemblyKeyword ) ) ) ,
157+ SyntaxFactory . AttributeList (
158+ SyntaxFactory . SingletonSeparatedList (
159+ SyntaxFactory . Attribute (
160+ SyntaxFactory . IdentifierName ( "ComVisible" ) )
161+ . WithArgumentList (
162+ SyntaxFactory . AttributeArgumentList (
163+ SyntaxFactory . SingletonSeparatedList (
164+ SyntaxFactory . AttributeArgument (
165+ SyntaxFactory . LiteralExpression ( info . ComVisible
166+ ? SyntaxKind . TrueLiteralExpression
167+ : SyntaxKind . FalseLiteralExpression ) ) ) ) ) ) )
168+ . WithTarget (
169+ SyntaxFactory . AttributeTargetSpecifier (
170+ SyntaxFactory . Token ( SyntaxKind . AssemblyKeyword ) ) ) ,
171+ SyntaxFactory . AttributeList (
172+ SyntaxFactory . SingletonSeparatedList (
173+ SyntaxFactory . Attribute (
174+ SyntaxFactory . IdentifierName ( "Guid" ) )
175+ . WithArgumentList (
176+ SyntaxFactory . AttributeArgumentList (
177+ SyntaxFactory . SingletonSeparatedList (
178+ SyntaxFactory . AttributeArgument (
179+ SyntaxFactory . LiteralExpression (
180+ SyntaxKind . StringLiteralExpression ,
181+ SyntaxFactory . Literal ( info . Guid ) ) ) ) ) ) ) )
182+ . WithTarget (
183+ SyntaxFactory . AttributeTargetSpecifier (
184+ SyntaxFactory . Token ( SyntaxKind . AssemblyKeyword ) ) ) ,
185+ SyntaxFactory . AttributeList (
186+ SyntaxFactory . SingletonSeparatedList (
187+ SyntaxFactory . Attribute (
188+ SyntaxFactory . IdentifierName ( "AssemblyVersion" ) )
189+ . WithArgumentList (
190+ SyntaxFactory . AttributeArgumentList (
191+ SyntaxFactory . SingletonSeparatedList (
192+ SyntaxFactory . AttributeArgument (
193+ SyntaxFactory . LiteralExpression (
194+ SyntaxKind . StringLiteralExpression ,
195+ SyntaxFactory . Literal ( info . Version ) ) ) ) ) ) ) )
196+ . WithTarget (
197+ SyntaxFactory . AttributeTargetSpecifier (
198+ SyntaxFactory . Token ( SyntaxKind . AssemblyKeyword ) ) ) ,
199+ SyntaxFactory . AttributeList (
200+ SyntaxFactory . SingletonSeparatedList (
201+ SyntaxFactory . Attribute (
202+ SyntaxFactory . IdentifierName ( "AssemblyFileVersion" ) )
203+ . WithArgumentList (
204+ SyntaxFactory . AttributeArgumentList (
205+ SyntaxFactory . SingletonSeparatedList (
206+ SyntaxFactory . AttributeArgument (
207+ SyntaxFactory . LiteralExpression (
208+ SyntaxKind . StringLiteralExpression ,
209+ SyntaxFactory . Literal ( info . FileVersion ) ) ) ) ) ) ) )
210+ . WithTarget (
211+ SyntaxFactory . AttributeTargetSpecifier (
212+ SyntaxFactory . Token ( SyntaxKind . AssemblyKeyword ) ) )
213+ } ) )
214+ . NormalizeWhitespace ( )
215+ . SyntaxTree ;
216+ }
217+
218+ #endregion Private Method
219+
220+ #region Help Class
221+
222+ public class AssemblyInfo
223+ {
224+ public string Title { get ; set ; }
225+ public string Product { get ; set ; }
226+ public string Copyright { get ; set ; }
227+ public string Guid { get ; set ; }
228+ public string Version { get ; set ; }
229+ public string FileVersion { get ; set ; }
230+ public bool ComVisible { get ; set ; }
231+
232+ public static AssemblyInfo Create ( string name , string copyright = "Copyright © MatoApp" , string version = "0.0.0.1" )
233+ {
234+ return new AssemblyInfo
235+ {
236+ Title = name ,
237+ Product = name ,
238+ Copyright = copyright ,
239+ Guid = System . Guid . NewGuid ( ) . ToString ( "D" ) ,
240+ ComVisible = false ,
241+ Version = version ,
242+ FileVersion = version
243+ } ;
244+ }
245+ }
246+
247+ #endregion Help Class
248+ }
249+ }
0 commit comments