@@ -20,45 +20,41 @@ namespace CSharpToJavaScript;
2020/// </summary>
2121public class CSTOJS
2222{
23- private readonly CSTOJSOptions _Options = new ( ) ;
2423 private readonly Stopwatch _Stopwatch = new ( ) ;
2524
25+ private CSTOJSOptions _DefaultOptions = new ( ) ;
2626 private Walker ? _Walker = null ;
2727 private FileSystemWatcher ? _FSWatcher = null ;
2828
2929 private bool _IsRunning = false ;
3030
3131 /// <summary>
32- /// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/> .
32+ /// New instance of <see cref="CSTOJS"/>.
3333 /// </summary>
34- public CSTOJS ( )
35- {
36- PrintVersion ( ) ;
37- }
34+ /// <param name="printVersion">Print version to the console.</param>
3835
39- /// <summary>
40- /// New instance of <see cref="CSTOJS"/>
41- /// </summary>
42- /// <param name="options">Options of <see cref="CSTOJS"/>, see <see cref="CSTOJSOptions"/>.</param>
43- public CSTOJS ( CSTOJSOptions options )
36+ public CSTOJS ( bool printVersion = true )
4437 {
45- _Options = options ;
46-
47- if ( _Options . DisableConsoleOutput == false )
38+ if ( printVersion )
4839 {
49- PrintVersion ( ) ;
40+ Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
41+ //https://stackoverflow.com/a/73474279
42+ Log . WriteLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ", _DefaultOptions ) ;
5043 }
5144 }
5245
5346 /// <summary>
5447 /// Method for generating js file/files.
5548 /// </summary>
5649 /// <param name="path">Full path to cs file or to the folder with cs files.</param>
57- /// <param name="filename ">Optional! Filename of a js file if you generating one file! </param>
50+ /// <param name="options ">Optional! Options of the CSTOJS, see <see cref="CSTOJSOptions"/> for default options. </param>
5851 /// <returns>empty Task</returns>
5952 /// <exception cref="DirectoryNotFoundException"></exception>
60- public async Task GenerateOneAsync ( string path , string ? filename = null )
53+ public async Task GenerateOneAsync ( string path , CSTOJSOptions ? options = null )
6154 {
55+ if ( options == null )
56+ options = _DefaultOptions ;
57+
6258 Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
6359 List < FileInfo > files = new ( ) ;
6460
@@ -75,7 +71,7 @@ public async Task GenerateOneAsync(string path, string? filename = null)
7571
7672 files = folder . GetFiles ( "*.cs" ) . ToList ( ) ;
7773
78- filename = null ;
74+ options . OutputFileName = null ;
7975 }
8076
8177 foreach ( FileInfo file in files )
@@ -87,37 +83,42 @@ public async Task GenerateOneAsync(string path, string? filename = null)
8783 _tree = CSharpSyntaxTree . ParseText ( SourceText . From ( stream ) , path : file . FullName ) ;
8884 }
8985
90- Generate ( _tree , assembly ) ;
86+ Generate ( _tree , assembly , options ) ;
9187
92- if ( ! Directory . Exists ( _Options . OutPutPath ) )
88+ if ( ! Directory . Exists ( options . OutputPath ) )
9389 {
94- Directory . CreateDirectory ( _Options . OutPutPath ) ;
90+ Directory . CreateDirectory ( options . OutputPath ) ;
9591 }
9692
9793 string pathCombined = string . Empty ;
9894
99- if ( filename != null )
100- pathCombined = Path . Combine ( _Options . OutPutPath , filename ) ;
95+ if ( options . OutputFileName != null )
96+ pathCombined = Path . Combine ( options . OutputPath , options . OutputFileName ) ;
10197 else
102- pathCombined = Path . Combine ( _Options . OutPutPath , file . Name . Replace ( ".cs" , ".js" ) ) ;
98+ pathCombined = Path . Combine ( options . OutputPath , file . Name . Replace ( ".cs" , ".js" ) ) ;
10399
104100 await File . WriteAllTextAsync ( pathCombined , _Walker ? . JSSB . ToString ( ) ) ;
105101
106- Log . SuccessLine ( $ "--- Done!", _Options ) ;
107- Log . SuccessLine ( $ "--- Path: { pathCombined } ", _Options ) ;
108- Log . SuccessLine ( $ "--- --- ---", _Options ) ;
102+ Log . SuccessLine ( $ "--- Done!", options ) ;
103+ Log . SuccessLine ( $ "--- Path: { pathCombined } ", options ) ;
104+ Log . SuccessLine ( $ "--- --- ---", options ) ;
109105 }
110106 }
111107
112108 /// <summary>
113109 /// Method for generating js StringBuilder/StringBuilders.
114110 /// </summary>
115111 /// <param name="path">Full path to cs file or to the folder with cs files.</param>
112+ /// <param name="options">Optional! Options of the CSTOJS, see <see cref="CSTOJSOptions"/> for default options.</param>
116113 /// <returns>List of StringBuilder</returns>
117114 /// <exception cref="DirectoryNotFoundException"></exception>
118- public List < StringBuilder > GenerateOne ( string path )
115+ public List < StringBuilder > GenerateOne ( string path , CSTOJSOptions ? options = null )
119116 {
117+ if ( options == null )
118+ options = _DefaultOptions ;
119+
120120 Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
121+
121122 List < FileInfo > files = new ( ) ;
122123 List < StringBuilder > jsStringBuilders = new ( ) ;
123124
@@ -144,13 +145,13 @@ public List<StringBuilder> GenerateOne(string path)
144145 _tree = CSharpSyntaxTree . ParseText ( SourceText . From ( stream ) , path : file . FullName ) ;
145146 }
146147
147- Generate ( _tree , assembly ) ;
148+ Generate ( _tree , assembly , options ) ;
148149
149150 jsStringBuilders . Add ( _Walker . JSSB ) ;
150151
151- Log . SuccessLine ( $ "--- Done!", _Options ) ;
152- Log . SuccessLine ( $ "--- File name: { file . Name } ", _Options ) ;
153- Log . SuccessLine ( $ "--- --- ---", _Options ) ;
152+ Log . SuccessLine ( $ "--- Done!", options ) ;
153+ Log . SuccessLine ( $ "--- File name: { file . Name } ", options ) ;
154+ Log . SuccessLine ( $ "--- --- ---", options ) ;
154155 }
155156
156157 return jsStringBuilders ;
@@ -161,24 +162,28 @@ public List<StringBuilder> GenerateOne(string path)
161162 /// Method for generating from string.
162163 /// </summary>
163164 /// <param name="csstring">CSharp string.</param>
165+ /// <param name="options">Optional! Options of the CSTOJS, see <see cref="CSTOJSOptions"/> for default options.</param>
164166 /// <param name="references">Needed if you don't have access to files. Because Assembly.location is null in Blazor WebAssembly.</param>
165167 /// <returns>JS <see cref="StringBuilder"/></returns>
166168 /// <exception cref="ArgumentNullException"></exception>
167- public StringBuilder GenerateOneFromString ( string csstring , List < MetadataReference > ? references = null )
169+ public StringBuilder GenerateOneFromString ( string csstring , CSTOJSOptions ? options = null , List < MetadataReference > ? references = null )
168170 {
171+ if ( options == null )
172+ options = _DefaultOptions ;
173+
169174 ArgumentNullException . ThrowIfNull ( csstring ) ;
170175
171176 Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
172177
173178 SyntaxTree _tree = CSharpSyntaxTree . ParseText ( csstring ) ;
174179
175180 if ( references != null )
176- Generate ( _tree , assembly , references ) ;
181+ Generate ( _tree , assembly , options , references ) ;
177182 else
178- Generate ( _tree , assembly ) ;
183+ Generate ( _tree , assembly , options ) ;
179184
180- Log . SuccessLine ( $ "--- Done!", _Options ) ;
181- Log . SuccessLine ( $ "--- --- ---", _Options ) ;
185+ Log . SuccessLine ( $ "--- Done!", options ) ;
186+ Log . SuccessLine ( $ "--- --- ---", options ) ;
182187
183188 return _Walker . JSSB ;
184189 }
@@ -187,36 +192,39 @@ public StringBuilder GenerateOneFromString(string csstring, List<MetadataReferen
187192 /// Method for generating from string. Writes a file.
188193 /// </summary>
189194 /// <param name="csstring">CSharp string.</param>
190- /// <param name="filename">Filename of a js file .</param>
195+ /// <param name="options">Optional! Options of the CSTOJS, see <see cref="CSTOJSOptions"/> for default options .</param>
191196 /// <param name="references">Needed if you don't have access to files. Because Assembly.location is null in Blazor WebAssembly.</param>
192197 /// <returns>empty Task</returns>
193198 /// <exception cref="ArgumentNullException"></exception>
194- public async Task GenerateOneFromStringAsync ( string csstring , string ? filename = "main.js" , List < MetadataReference > ? references = null )
199+ public async Task GenerateOneFromStringAsync ( string csstring , CSTOJSOptions ? options = null , List < MetadataReference > ? references = null )
195200 {
201+ if ( options == null )
202+ options = _DefaultOptions ;
203+
196204 ArgumentNullException . ThrowIfNull ( csstring ) ;
197205
198206 Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
199207
200208 SyntaxTree _tree = CSharpSyntaxTree . ParseText ( csstring ) ;
201209
202210 if ( references != null )
203- Generate ( _tree , assembly , references ) ;
211+ Generate ( _tree , assembly , options , references ) ;
204212 else
205- Generate ( _tree , assembly ) ;
213+ Generate ( _tree , assembly , options ) ;
206214
207215
208- if ( ! Directory . Exists ( _Options . OutPutPath ) )
216+ if ( ! Directory . Exists ( options . OutputPath ) )
209217 {
210- Directory . CreateDirectory ( _Options . OutPutPath ) ;
218+ Directory . CreateDirectory ( options . OutputPath ) ;
211219 }
212220
213- string pathCombined = Path . Combine ( _Options . OutPutPath , filename ) ;
221+ string pathCombined = Path . Combine ( options . OutputPath , options . OutputFileName ?? "main.js" ) ;
214222
215223 await File . WriteAllTextAsync ( pathCombined , _Walker . JSSB . ToString ( ) ) ;
216224
217- Log . SuccessLine ( $ "--- Done!", _Options ) ;
218- Log . SuccessLine ( $ "--- Path: { pathCombined } ", _Options ) ;
219- Log . SuccessLine ( $ "--- --- ---", _Options ) ;
225+ Log . SuccessLine ( $ "--- Done!", options ) ;
226+ Log . SuccessLine ( $ "--- Path: { pathCombined } ", options ) ;
227+ Log . SuccessLine ( $ "--- --- ---", options ) ;
220228 }
221229
222230 /// <summary>
@@ -226,11 +234,15 @@ public async Task GenerateOneFromStringAsync(string csstring, string? filename =
226234 /// <blockquote class="NOTE"><h5>NOTE</h5><para>Note: You must call <see cref="CSTOJS.StopWatching" /> before completing a program.</para></blockquote>
227235 /// </remarks>
228236 /// <param name="path">Full path to cs file.</param>
237+ /// <param name="options">Optional! Options of the CSTOJS, see <see cref="CSTOJSOptions"/> for default options.</param>
229238 /// <returns>void</returns>
230239 /// <exception cref="DirectoryNotFoundException"></exception>
231240 /// <exception cref="FileNotFoundException"></exception>
232- public void GenerateOneContinuously ( string path )
241+ public void GenerateOneContinuously ( string path , CSTOJSOptions ? options = null )
233242 {
243+ if ( options != null )
244+ _DefaultOptions = options ;
245+
234246 if ( File . Exists ( path ) )
235247 {
236248 FileInfo file = new ( path ) ;
@@ -252,7 +264,7 @@ public void GenerateOneContinuously(string path)
252264 _FSWatcher . IncludeSubdirectories = true ;
253265 _FSWatcher . EnableRaisingEvents = true ;
254266
255- Log . WriteLine ( $ "Watching to: { path } ", _Options ) ;
267+ Log . WriteLine ( $ "Watching to: { path } ", options ) ;
256268 }
257269 else
258270 {
@@ -264,7 +276,7 @@ private async void OnChanged(object sender, FileSystemEventArgs e)
264276 if ( e . ChangeType != WatcherChangeTypes . Changed )
265277 return ;
266278
267- Log . WriteLine ( $ "Changed: { e . FullPath } ", _Options ) ;
279+ Log . WriteLine ( $ "Changed: { e . FullPath } ", _DefaultOptions ) ;
268280
269281 if ( ! _IsRunning )
270282 await GenerateOneAsync ( e . FullPath ) ;
@@ -273,19 +285,19 @@ private async void OnChanged(object sender, FileSystemEventArgs e)
273285 private void OnCreated ( object sender , FileSystemEventArgs e )
274286 {
275287 string value = $ "Created: { e . FullPath } ";
276- Log . WriteLine ( value , _Options ) ;
288+ Log . WriteLine ( value , _DefaultOptions ) ;
277289 }
278290
279291 private void OnDeleted ( object sender , FileSystemEventArgs e )
280292 {
281- Log . WriteLine ( $ "Deleted: { e . FullPath } ", _Options ) ;
293+ Log . WriteLine ( $ "Deleted: { e . FullPath } ", _DefaultOptions ) ;
282294 }
283295
284296 private void OnRenamed ( object sender , RenamedEventArgs e )
285297 {
286- Log . WriteLine ( $ "Renamed:", _Options ) ;
287- Log . WriteLine ( $ " Old: { e . OldFullPath } ", _Options ) ;
288- Log . WriteLine ( $ " New: { e . FullPath } ", _Options ) ;
298+ Log . WriteLine ( $ "Renamed:", _DefaultOptions ) ;
299+ Log . WriteLine ( $ " Old: { e . OldFullPath } ", _DefaultOptions ) ;
300+ Log . WriteLine ( $ " New: { e . FullPath } ", _DefaultOptions ) ;
289301 }
290302
291303 private void OnError ( object sender , ErrorEventArgs e )
@@ -298,21 +310,23 @@ private void OnError(object sender, ErrorEventArgs e)
298310 /// </summary>
299311 public void StopWatching ( )
300312 {
313+ _DefaultOptions = new ( ) ;
314+
301315 if ( _FSWatcher != null )
302316 {
303317 _FSWatcher . Dispose ( ) ;
304318 _FSWatcher = null ;
305319 }
306320 }
307321
308- private void Generate ( SyntaxTree tree , Assembly ? assembly , List < MetadataReference > ? refs = null )
322+ private void Generate ( SyntaxTree tree , Assembly ? assembly , CSTOJSOptions options , List < MetadataReference > ? refs = null )
309323 {
310324 _IsRunning = true ;
311325
312- if ( _Options . Debug )
326+ if ( options . Debug )
313327 {
314328 _Stopwatch . Restart ( ) ;
315- Log . WriteLine ( "Start stopwatch" , _Options ) ;
329+ Log . WriteLine ( "Start stopwatch" , options ) ;
316330 }
317331
318332 CompilationUnitSyntax root = tree . GetCompilationUnitRoot ( ) ;
@@ -503,10 +517,10 @@ private void Generate(SyntaxTree tree, Assembly? assembly, List<MetadataReferenc
503517 )
504518 ) . AddUsings ( oldUsing ) ;
505519
506- if ( _Options . NormalizeWhitespace )
520+ if ( options . NormalizeWhitespace )
507521 trueRoot = trueRoot . NormalizeWhitespace ( ) ;
508522
509- if ( _Options . KeepBraceOnTheSameLine )
523+ if ( options . KeepBraceOnTheSameLine )
510524 {
511525 //Mostly deleted whitespaces, still TODO?
512526 SyntaxToken [ ] allBraces = trueRoot . DescendantTokens ( ) . Where ( ( e ) => e . IsKind ( SyntaxKind . OpenBraceToken ) ) . ToArray ( ) ;
@@ -614,22 +628,22 @@ private void Generate(SyntaxTree tree, Assembly? assembly, List<MetadataReferenc
614628 trueReferences = references ;
615629 }
616630
617- if ( _Options . Debug )
631+ if ( options . Debug )
618632 {
619- Log . SuccessLine ( $ "+++", _Options ) ;
620- Log . WriteLine ( $ "Path assembly: { assemblyPath } ", _Options ) ;
621- Log . WriteLine ( $ "Path rt: { rtPath } ", _Options ) ;
622- Log . WriteLine ( $ "List of references({ references . Count } ):", _Options ) ;
633+ Log . SuccessLine ( $ "+++", options ) ;
634+ Log . WriteLine ( $ "Path assembly: { assemblyPath } ", options ) ;
635+ Log . WriteLine ( $ "Path rt: { rtPath } ", options ) ;
636+ Log . WriteLine ( $ "List of references({ references . Count } ):", options ) ;
623637 foreach ( MetadataReference reference in references )
624638 {
625- Log . WriteLine ( reference . Display ?? "null display string" , _Options ) ;
639+ Log . WriteLine ( reference . Display ?? "null display string" , options ) ;
626640 }
627- Log . WriteLine ( $ "List of trueReferences({ trueReferences . Count } ):", _Options ) ;
641+ Log . WriteLine ( $ "List of trueReferences({ trueReferences . Count } ):", options ) ;
628642 foreach ( MetadataReference reference in trueReferences )
629643 {
630- Log . WriteLine ( reference . Display ?? "null display string" , _Options ) ;
644+ Log . WriteLine ( reference . Display ?? "null display string" , options ) ;
631645 }
632- Log . SuccessLine ( $ "+++", _Options ) ;
646+ Log . SuccessLine ( $ "+++", options ) ;
633647 }
634648
635649 SyntaxTree trueST = trueRoot . SyntaxTree ;
@@ -639,26 +653,20 @@ private void Generate(SyntaxTree tree, Assembly? assembly, List<MetadataReferenc
639653 . AddSyntaxTrees ( trueST ) ;
640654
641655
642- _Walker = new ( _Options , compilation . GetSemanticModel ( trueST ) ) ;
656+ _Walker = new ( options , compilation . GetSemanticModel ( trueST ) ) ;
643657
644- _Walker . JSSB . Append ( _Options . AddSBAtTheTop ) ;
658+ _Walker . JSSB . Append ( options . AddSBAtTheTop ) ;
645659
646660 _Walker . Visit ( trueRoot ) ;
647661
648- _Walker . JSSB . Append ( _Options . AddSBAtTheBottom ) ;
662+ _Walker . JSSB . Append ( options . AddSBAtTheBottom ) ;
649663
650- if ( _Options . Debug )
664+ if ( options . Debug )
651665 {
652666 _Stopwatch . Stop ( ) ;
653- Log . WriteLine ( $ "Stop stopwatch: { _Stopwatch . Elapsed } ", _Options ) ;
667+ Log . WriteLine ( $ "Stop stopwatch: { _Stopwatch . Elapsed } ", options ) ;
654668 }
655669
656670 _IsRunning = false ;
657671 }
658- private void PrintVersion ( )
659- {
660- Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
661- //https://stackoverflow.com/a/73474279
662- Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ", _Options ) ;
663- }
664672}
0 commit comments