66using Microsoft . CodeAnalysis . CSharp ;
77using System . Threading . Tasks ;
88using Microsoft . CodeAnalysis ;
9- using CSharpToJavaScript . Utils ;
109using System . Linq ;
1110using Microsoft . CodeAnalysis . Text ;
11+ using System . Text ;
12+ using System . Runtime . CompilerServices ;
13+ using System ;
14+ using System . Reflection . Metadata ;
15+ using System . Collections . Immutable ;
1216
1317namespace CSharpToJavaScript
1418{
@@ -17,9 +21,9 @@ namespace CSharpToJavaScript
1721 /// </summary>
1822 public class CSTOJS
1923 {
20- public static SemanticModel Model { get ; set ; }
24+ public SemanticModel Model { get ; set ; } = null ;
2125
22- private CSTOJSOptions _Options = new ( ) ;
26+ public CSTOJSOptions Options { get ; set ; } = new ( ) ;
2327
2428 private Walker _Walker = new ( ) ;
2529
@@ -28,7 +32,7 @@ public class CSTOJS
2832 /// </summary>
2933 public CSTOJS ( )
3034 {
31- if ( _Options . Debug )
35+ if ( Options . Debug )
3236 {
3337 if ( File . Exists ( Directory . GetCurrentDirectory ( ) + "/debug.txt" ) )
3438 File . Delete ( Directory . GetCurrentDirectory ( ) + "/debug.txt" ) ;
@@ -41,7 +45,7 @@ public CSTOJS()
4145
4246 Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
4347 //https://stackoverflow.com/a/73474279
44- SM . Log ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
48+ Log ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
4549 }
4650
4751 /// <summary>
@@ -50,9 +54,9 @@ public CSTOJS()
5054 /// <param name="options">Options of <see cref="CSTOJS"/>, see <see cref="CSTOJSOptions"/>.</param>
5155 public CSTOJS ( CSTOJSOptions options )
5256 {
53- _Options = options ;
57+ Options = options ;
5458
55- if ( _Options . Debug )
59+ if ( Options . Debug )
5660 {
5761 if ( File . Exists ( Directory . GetCurrentDirectory ( ) + "/debug.txt" ) )
5862 File . Delete ( Directory . GetCurrentDirectory ( ) + "/debug.txt" ) ;
@@ -65,7 +69,7 @@ public CSTOJS(CSTOJSOptions options)
6569
6670 Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
6771 //https://stackoverflow.com/a/73474279
68- SM . Log ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
72+ Log ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
6973 }
7074
7175 /// <summary>
@@ -80,10 +84,53 @@ public async Task GenerateOneAsync(string path, string? filename = null)
8084
8185 FileInfo file = new ( path ) ;
8286
83- if ( filename != null )
84- await GenerateAsync ( path , assembly , filename ) ;
87+ SyntaxTree ? _tree = null ;
88+
89+ using ( var stream = File . OpenRead ( path ) )
90+ {
91+ _tree = CSharpSyntaxTree . ParseText ( SourceText . From ( stream ) , path : path ) ;
92+ }
93+
94+ await GenerateAsync ( _tree , assembly ) ;
95+
96+ if ( ! Directory . Exists ( Options . OutPutPath ) )
97+ {
98+ Directory . CreateDirectory ( Options . OutPutPath ) ;
99+ }
100+
101+ string pathCombined = string . Empty ;
102+
103+ if ( filename != null )
104+ pathCombined = Path . Combine ( Options . OutPutPath , filename ) ;
85105 else
86- await GenerateAsync ( path , assembly , file . Name . Replace ( ".cs" , ".js" ) ) ;
106+ pathCombined = Path . Combine ( Options . OutPutPath , file . Name . Replace ( ".cs" , ".js" ) ) ;
107+
108+
109+ await File . WriteAllTextAsync ( pathCombined , _Walker . JSSB . ToString ( ) ) ;
110+
111+ Log ( $ "--- Done!") ;
112+ Log ( $ "--- Path: { pathCombined } ") ;
113+ Log ( $ "--- --- ---") ;
114+ }
115+
116+ public async Task < StringBuilder > GenerateOneFromStringAsync ( string csstring , List < MetadataReference > ? references = null )
117+ {
118+ if ( csstring == null )
119+ throw new ArgumentNullException ( nameof ( csstring ) ) ;
120+
121+ Assembly assembly = Assembly . GetEntryAssembly ( ) ;
122+
123+ SyntaxTree ? _tree = CSharpSyntaxTree . ParseText ( csstring . Normalize ( ) ) ;
124+
125+ if ( references != null )
126+ await GenerateAsync ( _tree , assembly , references ) ;
127+ else
128+ await GenerateAsync ( _tree , assembly ) ;
129+
130+ Log ( $ "--- Done!") ;
131+ Log ( $ "--- --- ---") ;
132+
133+ return _Walker . JSSB ;
87134 }
88135 /// <summary>
89136 /// Method for generating multiply js files.
@@ -100,42 +147,65 @@ public async Task GenerateManyAsync(string path)
100147
101148 foreach ( FileInfo file in Files )
102149 {
103- await GenerateAsync ( file . FullName , assembly , file . Name . Replace ( ".cs" , ".js" ) ) ;
104- }
105- }
150+ SyntaxTree ? _tree = null ;
106151
107- private async Task GenerateAsync ( string path , Assembly assembly , string filename = "main.js" )
108- {
109- _Walker = new ( _Options ) ;
152+ using ( var stream = File . OpenRead ( file . FullName ) )
153+ {
154+ _tree = CSharpSyntaxTree . ParseText ( SourceText . From ( stream ) , path : file . FullName ) ;
155+ }
110156
111- SyntaxTree ? tree = null ;
157+ await GenerateAsync ( _tree , assembly ) ;
112158
113- using ( var stream = File . OpenRead ( path ) )
114- {
115- tree = CSharpSyntaxTree . ParseText ( SourceText . From ( stream ) , path : path ) ;
159+ if ( ! Directory . Exists ( Options . OutPutPath ) )
160+ {
161+ Directory . CreateDirectory ( Options . OutPutPath ) ;
162+ }
163+
164+ string pathCombined = Path . Combine ( Options . OutPutPath , file . Name . Replace ( ".cs" , ".js" ) ) ;
165+
166+ await File . WriteAllTextAsync ( pathCombined , _Walker . JSSB . ToString ( ) ) ;
167+
168+ Log ( $ "--- Done!") ;
169+ Log ( $ "--- Path: { pathCombined } ") ;
170+ Log ( $ "--- --- ---") ;
116171 }
172+ }
173+
174+ private async Task GenerateAsync ( SyntaxTree ? tree , Assembly assembly , List < MetadataReference > ? refs = null )
175+ {
176+ _Walker = new ( this ) ;
117177
118178 CompilationUnitSyntax root = tree . GetCompilationUnitRoot ( ) ;
119179
180+
120181 string assemblyPath = Path . GetDirectoryName ( assembly . Location ) ;
121182 List < MetadataReference > references = new ( ) { } ;
122183
123184 string rtPath = Path . GetDirectoryName ( typeof ( object ) . Assembly . Location ) ;
124185
125- references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( rtPath , "System.Private.CoreLib.dll" ) ) ) ;
126-
127- AssemblyName [ ] a = assembly . GetReferencedAssemblies ( ) ;
128- foreach ( AssemblyName item in a )
186+ if ( refs == null )
129187 {
130- if ( File . Exists ( Path . Combine ( assemblyPath , item . Name + ".dll" ) ) )
131- references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( assemblyPath , item . Name + ".dll" ) ) ) ;
132- else
188+ if ( rtPath != null && assemblyPath != null )
133189 {
134- if ( File . Exists ( Path . Combine ( rtPath , item . Name + ".dll" ) ) )
135- references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( rtPath , item . Name + ".dll" ) ) ) ;
190+ references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( rtPath , "System.Private.CoreLib.dll" ) ) ) ;
191+
192+ AssemblyName [ ] a = assembly . GetReferencedAssemblies ( ) ;
193+ foreach ( AssemblyName item in a )
194+ {
195+ if ( File . Exists ( Path . Combine ( assemblyPath , item . Name + ".dll" ) ) )
196+ references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( assemblyPath , item . Name + ".dll" ) ) ) ;
197+ else
198+ {
199+ if ( File . Exists ( Path . Combine ( rtPath , item . Name + ".dll" ) ) )
200+ references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( rtPath , item . Name + ".dll" ) ) ) ;
201+ }
202+ }
136203 }
137204 }
138-
205+ else
206+ {
207+ references = refs ;
208+ }
139209
140210 UsingDirectiveSyntax [ ] oldUsing = root . Usings . ToArray ( ) ;
141211
@@ -303,24 +373,31 @@ private async Task GenerateAsync(string path, Assembly assembly, string filename
303373
304374 Model = compilation . GetSemanticModel ( trueST ) ;
305375
306- _Walker . JSSB . Append ( _Options . AddSBInFront ) ;
376+ _Walker . JSSB . Append ( Options . AddSBInFront ) ;
307377
308378 _Walker . Visit ( trueRoot ) ;
309379
310- _Walker . JSSB . Append ( _Options . AddSBInEnd ) ;
380+ _Walker . JSSB . Append ( Options . AddSBInEnd ) ;
381+ }
311382
312- if ( ! Directory . Exists ( _Options . OutPutPath ) )
383+ public void Log ( string message , [ CallerFilePath ] string ? file = null , [ CallerMemberName ] string ? member = null , [ CallerLineNumber ] int line = 0 )
384+ {
385+ if ( Options . DisableConsoleColors == false )
313386 {
314- Directory . CreateDirectory ( _Options . OutPutPath ) ;
315- }
387+ if ( message . StartsWith ( "---" ) )
388+ Console . ForegroundColor = ConsoleColor . Green ;
316389
317- string pathCombined = Path . Combine ( _Options . OutPutPath , filename ) ;
390+ if ( message . StartsWith ( "ERROR" ) || message . StartsWith ( "as" ) )
391+ Console . ForegroundColor = ConsoleColor . Red ;
318392
319- await File . WriteAllTextAsync ( pathCombined , _Walker . JSSB . ToString ( ) ) ;
393+ if ( message . StartsWith ( "WARNING" ) )
394+ Console . ForegroundColor = ConsoleColor . Yellow ;
395+ }
396+
397+ Trace . WriteLine ( $ "({ line } ):{ Path . GetFileName ( file ) } { member } : { message } ") ;
320398
321- SM . Log ( $ "--- Done!") ;
322- SM . Log ( $ "--- Path: { pathCombined } ") ;
323- SM . Log ( $ "--- --- ---") ;
399+ if ( Options . DisableConsoleColors == false )
400+ Console . ResetColor ( ) ;
324401 }
325402 }
326403}
0 commit comments