@@ -23,15 +23,17 @@ public class CSTOJS
2323
2424 private Walker ? _Walker = null ;
2525
26+ static CSTOJS ( )
27+ {
28+ ConsoleTraceListener consoleTraceListener = new ( ) ;
29+ Trace . Listeners . Add ( consoleTraceListener ) ;
30+ }
31+
2632 /// <summary>
2733 /// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
2834 /// </summary>
2935 public CSTOJS ( )
3036 {
31- ConsoleTraceListener consoleTraceListener = new ( ) ;
32- if ( Trace . Listeners . Contains ( consoleTraceListener ) == false )
33- Trace . Listeners . Add ( consoleTraceListener ) ;
34-
3537 Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
3638 //https://stackoverflow.com/a/73474279
3739 Log ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
@@ -47,11 +49,6 @@ public CSTOJS(CSTOJSOptions options)
4749
4850 if ( Options . DisableConsoleOutput == false )
4951 {
50- ConsoleTraceListener consoleTraceListener = new ( ) ;
51- if ( Trace . Listeners . Contains ( consoleTraceListener ) == false )
52- Trace . Listeners . Add ( consoleTraceListener ) ;
53-
54-
5552 Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
5653 //https://stackoverflow.com/a/73474279
5754 Log ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
@@ -63,7 +60,7 @@ public CSTOJS(CSTOJSOptions options)
6360 /// </summary>
6461 /// <param name="path">Full path to cs file or to the folder with cs files.</param>
6562 /// <param name="filename">Optional! Filename of a js file if you generating one file!</param>
66- /// <returns></returns>
63+ /// <returns>empty Task </returns>
6764 public async Task GenerateOneAsync ( string path , string ? filename = null )
6865 {
6966 Assembly assembly = Assembly . GetEntryAssembly ( ) ;
@@ -113,6 +110,50 @@ public async Task GenerateOneAsync(string path, string? filename = null)
113110 }
114111 }
115112
113+ /// <summary>
114+ /// Method for generating js StringBuilder/StringBuilders.
115+ /// </summary>
116+ /// <param name="path">Full path to cs file or to the folder with cs files.</param>
117+ /// <returns>List of StringBuilder</returns>
118+ public List < StringBuilder > GenerateOne ( string path )
119+ {
120+ Assembly assembly = Assembly . GetEntryAssembly ( ) ;
121+ List < FileInfo > files = new ( ) ;
122+ List < StringBuilder > jsStringBuilders = new ( ) ;
123+
124+ if ( File . Exists ( path ) )
125+ {
126+ files . Add ( new FileInfo ( path ) ) ;
127+ }
128+ else
129+ {
130+ DirectoryInfo folder = new ( path ) ;
131+
132+ files = folder . GetFiles ( "*.cs" ) . ToList ( ) ;
133+ }
134+
135+ foreach ( FileInfo file in files )
136+ {
137+ SyntaxTree ? _tree = null ;
138+
139+ using ( var stream = File . OpenRead ( file . FullName ) )
140+ {
141+ _tree = CSharpSyntaxTree . ParseText ( SourceText . From ( stream ) , path : file . FullName ) ;
142+ }
143+
144+ Generate ( _tree , assembly ) ;
145+
146+ jsStringBuilders . Add ( _Walker . JSSB ) ;
147+
148+ Log ( $ "--- Done!") ;
149+ Log ( $ "--- File name: { file . Name } ") ;
150+ Log ( $ "--- --- ---") ;
151+ }
152+
153+ return jsStringBuilders ;
154+ }
155+
156+
116157 /// <summary>
117158 /// Method for generating from string.
118159 /// </summary>
@@ -140,6 +181,44 @@ public StringBuilder GenerateOneFromString(string csstring, List<MetadataReferen
140181 return _Walker . JSSB ;
141182 }
142183
184+ /// <summary>
185+ /// Method for generating from string. Writes a file.
186+ /// </summary>
187+ /// <param name="csstring">CSharp string.</param>
188+ /// <param name="filename">Filename of a js file.</param>
189+ /// <param name="references">Needed if you don't have access to files. Because Assembly.location is null in Blazor WebAssembly.</param>
190+ /// <returns>empty Task</returns>
191+ /// <exception cref="ArgumentNullException"></exception>
192+ public async Task GenerateOneFromStringAsync ( string csstring , string ? filename = "main.js" , List < MetadataReference > ? references = null )
193+ {
194+ if ( csstring == null )
195+ throw new ArgumentNullException ( nameof ( csstring ) ) ;
196+
197+ Assembly assembly = Assembly . GetEntryAssembly ( ) ;
198+
199+ SyntaxTree ? _tree = CSharpSyntaxTree . ParseText ( csstring ) ;
200+
201+ if ( references != null )
202+ Generate ( _tree , assembly , references ) ;
203+ else
204+ Generate ( _tree , assembly ) ;
205+
206+
207+ if ( ! Directory . Exists ( Options . OutPutPath ) )
208+ {
209+ Directory . CreateDirectory ( Options . OutPutPath ) ;
210+ }
211+
212+ string pathCombined = Path . Combine ( Options . OutPutPath , filename ) ;
213+
214+ await File . WriteAllTextAsync ( pathCombined , _Walker . JSSB . ToString ( ) ) ;
215+
216+ Log ( $ "--- Done!") ;
217+ Log ( $ "--- Path: { pathCombined } ") ;
218+ Log ( $ "--- --- ---") ;
219+ }
220+
221+
143222 private void Generate ( SyntaxTree ? tree , Assembly assembly , List < MetadataReference > ? refs = null )
144223 {
145224 CompilationUnitSyntax root = tree . GetCompilationUnitRoot ( ) ;
@@ -354,14 +433,25 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
354433
355434 if ( File . Exists ( Path . Combine ( rtPath , "System.Threading.Tasks.dll" ) ) )
356435 references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( rtPath , "System.Threading.Tasks.dll" ) ) ) ;
436+
437+ if ( File . Exists ( Path . Combine ( rtPath , "System.Console.dll" ) ) )
438+ references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( rtPath , "System.Console.dll" ) ) ) ;
357439
358440 foreach ( UsingDirectiveSyntax oU in oldUsing )
359441 {
442+
360443 if ( File . Exists ( Path . Combine ( rtPath , oU . Name + ".dll" ) ) )
361444 references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( rtPath , oU . Name + ".dll" ) ) ) ;
362445 }
363446 }
364447
448+ if ( assemblyPath != null && assemblyPath != string . Empty )
449+ {
450+ if ( File . Exists ( Path . Combine ( assemblyPath , "CSharpToJavaScript.dll" ) ) )
451+ references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( assemblyPath , "CSharpToJavaScript.dll" ) ) ) ;
452+ }
453+
454+ //TODO! does not work... sigh
365455 references = references . Distinct ( ) . ToList ( ) ;
366456
367457 if ( Options . Debug )
0 commit comments