99using System . Linq ;
1010using Microsoft . CodeAnalysis . Text ;
1111using System . Text ;
12- using System . Runtime . CompilerServices ;
1312using System ;
1413
1514namespace CSharpToJavaScript
1615{
1716 /// <summary>
1817 /// Main type for CSharpToJavaScript.
1918 /// </summary>
20- public class CSTOJS
21- {
22- public CSTOJSOptions Options { get ; set ; } = new ( ) ;
23-
19+ public class CSTOJS : ILog
20+ {
21+ private CSTOJSOptions _Options = new ( ) ;
22+ private Stopwatch _Stopwatch = new ( ) ;
2423 private Walker ? _Walker = null ;
25-
26- static CSTOJS ( )
27- {
28- ConsoleTraceListener consoleTraceListener = new ( ) ;
29- Trace . Listeners . Add ( consoleTraceListener ) ;
30- }
24+ private readonly ILog ? _Log = null ;
3125
3226 /// <summary>
3327 /// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
3428 /// </summary>
3529 public CSTOJS ( )
3630 {
31+ _Log = this ;
32+
3733 Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
38- //https://stackoverflow.com/a/73474279
39- Log ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
34+ //https://stackoverflow.com/a/73474279
35+ _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
4036 }
4137
4238 /// <summary>
@@ -45,14 +41,16 @@ public CSTOJS()
4541 /// <param name="options">Options of <see cref="CSTOJS"/>, see <see cref="CSTOJSOptions"/>.</param>
4642 public CSTOJS ( CSTOJSOptions options )
4743 {
48- Options = options ;
44+ _Options = options ;
4945
50- if ( Options . DisableConsoleOutput == false )
46+ _Log = ILog . GetILog ( this , _Options ) ;
47+
48+ if ( _Options . DisableConsoleOutput == false )
5149 {
5250 Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
53- //https://stackoverflow.com/a/73474279
54- Log ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
55- }
51+ //https://stackoverflow.com/a/73474279
52+ _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
53+ }
5654 }
5755
5856 /// <summary>
@@ -90,23 +88,23 @@ public async Task GenerateOneAsync(string path, string? filename = null)
9088
9189 Generate ( _tree , assembly ) ;
9290
93- if ( ! Directory . Exists ( Options . OutPutPath ) )
91+ if ( ! Directory . Exists ( _Options . OutPutPath ) )
9492 {
95- Directory . CreateDirectory ( Options . OutPutPath ) ;
93+ Directory . CreateDirectory ( _Options . OutPutPath ) ;
9694 }
9795
9896 string pathCombined = string . Empty ;
9997
10098 if ( filename != null )
101- pathCombined = Path . Combine ( Options . OutPutPath , filename ) ;
99+ pathCombined = Path . Combine ( _Options . OutPutPath , filename ) ;
102100 else
103- pathCombined = Path . Combine ( Options . OutPutPath , file . Name . Replace ( ".cs" , ".js" ) ) ;
101+ pathCombined = Path . Combine ( _Options . OutPutPath , file . Name . Replace ( ".cs" , ".js" ) ) ;
104102
105103 await File . WriteAllTextAsync ( pathCombined , _Walker . JSSB . ToString ( ) ) ;
106104
107- Log ( $ "--- Done!") ;
108- Log ( $ "--- Path: { pathCombined } ") ;
109- Log ( $ "--- --- ---") ;
105+ _Log . SuccessLine ( $ "--- Done!") ;
106+ _Log . SuccessLine ( $ "--- Path: { pathCombined } ") ;
107+ _Log . SuccessLine ( $ "--- --- ---") ;
110108 }
111109 }
112110
@@ -145,9 +143,9 @@ public List<StringBuilder> GenerateOne(string path)
145143
146144 jsStringBuilders . Add ( _Walker . JSSB ) ;
147145
148- Log ( $ "--- Done!") ;
149- Log ( $ "--- File name: { file . Name } ") ;
150- Log ( $ "--- --- ---") ;
146+ _Log . SuccessLine ( $ "--- Done!") ;
147+ _Log . SuccessLine ( $ "--- File name: { file . Name } ") ;
148+ _Log . SuccessLine ( $ "--- --- ---") ;
151149 }
152150
153151 return jsStringBuilders ;
@@ -175,8 +173,8 @@ public StringBuilder GenerateOneFromString(string csstring, List<MetadataReferen
175173 else
176174 Generate ( _tree , assembly ) ;
177175
178- Log ( $ "--- Done!") ;
179- Log ( $ "--- --- ---") ;
176+ _Log . SuccessLine ( $ "--- Done!") ;
177+ _Log . SuccessLine ( $ "--- --- ---") ;
180178
181179 return _Walker . JSSB ;
182180 }
@@ -204,23 +202,29 @@ public async Task GenerateOneFromStringAsync(string csstring, string? filename =
204202 Generate ( _tree , assembly ) ;
205203
206204
207- if ( ! Directory . Exists ( Options . OutPutPath ) )
205+ if ( ! Directory . Exists ( _Options . OutPutPath ) )
208206 {
209- Directory . CreateDirectory ( Options . OutPutPath ) ;
207+ Directory . CreateDirectory ( _Options . OutPutPath ) ;
210208 }
211209
212- string pathCombined = Path . Combine ( Options . OutPutPath , filename ) ;
210+ string pathCombined = Path . Combine ( _Options . OutPutPath , filename ) ;
213211
214212 await File . WriteAllTextAsync ( pathCombined , _Walker . JSSB . ToString ( ) ) ;
215213
216- Log ( $ "--- Done!") ;
217- Log ( $ "--- Path: { pathCombined } ") ;
218- Log ( $ "--- --- ---") ;
214+ _Log . SuccessLine ( $ "--- Done!") ;
215+ _Log . SuccessLine ( $ "--- Path: { pathCombined } ") ;
216+ _Log . SuccessLine ( $ "--- --- ---") ;
219217 }
220218
221219
222220 private void Generate ( SyntaxTree ? tree , Assembly assembly , List < MetadataReference > ? refs = null )
223221 {
222+ if ( _Options . Debug )
223+ {
224+ _Stopwatch . Restart ( ) ;
225+ _Log . WriteLine ( "Start stopwatch" ) ;
226+ }
227+
224228 CompilationUnitSyntax root = tree . GetCompilationUnitRoot ( ) ;
225229
226230
@@ -454,17 +458,17 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
454458 //TODO! does not work... sigh
455459 references = references . Distinct ( ) . ToList ( ) ;
456460
457- if ( Options . Debug )
461+ if ( _Options . Debug )
458462 {
459- Log ( $ "+++") ;
460- Log ( $ "Path assembly: { assemblyPath } ") ;
461- Log ( $ "Path rt: { rtPath } ") ;
462- Log ( $ "List of references:") ;
463+ _Log . SuccessLine ( $ "+++") ;
464+ _Log . WriteLine ( $ "Path assembly: { assemblyPath } ") ;
465+ _Log . WriteLine ( $ "Path rt: { rtPath } ") ;
466+ _Log . WriteLine ( $ "List of references:") ;
463467 foreach ( MetadataReference reference in references )
464468 {
465- Log ( reference . Display ) ;
469+ _Log . WriteLine ( reference . Display ) ;
466470 }
467- Log ( $ "+++") ;
471+ _Log . SuccessLine ( $ "+++") ;
468472 }
469473
470474 SyntaxTree trueST = trueRoot . SyntaxTree ;
@@ -473,37 +477,20 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
473477 . AddReferences ( references . ToArray ( ) )
474478 . AddSyntaxTrees ( trueST ) ;
475479
476- _Walker = new ( this , compilation . GetSemanticModel ( trueST ) ) ;
480+ _Walker = new ( _Options , compilation . GetSemanticModel ( trueST ) ) ;
477481
478- _Walker . JSSB . Append ( Options . AddSBInFront ) ;
482+ _Walker . JSSB . Append ( _Options . AddSBInFront ) ;
479483
480484 _Walker . Visit ( trueRoot ) ;
481485
482- _Walker . JSSB . Append ( Options . AddSBInEnd ) ;
483- }
486+ _Walker . JSSB . Append ( _Options . AddSBInEnd ) ;
484487
485- public void Log ( string message , [ CallerFilePath ] string ? file = null , [ CallerMemberName ] string ? member = null , [ CallerLineNumber ] int line = 0 )
486- {
487- if ( Options . DisableConsoleOutput == false )
488- {
489- if ( Options . DisableConsoleColors == false )
490- {
491- if ( message . StartsWith ( "---" ) )
492- Console . ForegroundColor = ConsoleColor . Green ;
493-
494- if ( message . StartsWith ( "ERROR" ) || message . StartsWith ( "as" ) )
495- Console . ForegroundColor = ConsoleColor . Red ;
496-
497- if ( message . StartsWith ( "WARNING" ) )
498- Console . ForegroundColor = ConsoleColor . Yellow ;
499- }
488+ if ( _Options . Debug )
489+ {
490+ _Stopwatch . Stop ( ) ;
491+ _Log . WriteLine ( $ "Stop stopwatch: { _Stopwatch . Elapsed } ") ;
492+ }
493+ }
500494
501-
502- Trace . WriteLine ( $ "({ line } ):{ Path . GetFileName ( file . Replace ( "\\ " , "/" ) ) } { member } : { message } ") ;
503-
504- if ( Options . DisableConsoleColors == false )
505- Console . ResetColor ( ) ;
506- }
507- }
508495 }
509496}
0 commit comments