10
10
using Microsoft . CodeAnalysis . Text ;
11
11
using System . Text ;
12
12
using System ;
13
+ using CSharpToJavaScript . Utils ;
13
14
14
15
namespace CSharpToJavaScript
15
16
{
16
17
/// <summary>
17
18
/// Main type for CSharpToJavaScript.
18
19
/// </summary>
19
20
public class CSTOJS : ILog
20
- {
21
- private CSTOJSOptions _Options = new ( ) ;
22
- private Stopwatch _Stopwatch = new ( ) ;
21
+ {
22
+ private readonly CSTOJSOptions _Options = new ( ) ;
23
+ private readonly Stopwatch _Stopwatch = new ( ) ;
24
+ private readonly ILog ? _Log = null ;
25
+
23
26
private Walker ? _Walker = null ;
24
- private readonly ILog ? _Log = null ;
25
27
26
28
/// <summary>
27
29
/// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
28
30
/// </summary>
29
31
public CSTOJS ( )
30
32
{
31
- _Log = this ;
33
+ _Log = this ;
32
34
33
35
Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
34
- //https://stackoverflow.com/a/73474279
35
- _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
36
+ //https://stackoverflow.com/a/73474279
37
+ _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
36
38
}
37
39
38
40
/// <summary>
@@ -41,16 +43,16 @@ public CSTOJS()
41
43
/// <param name="options">Options of <see cref="CSTOJS"/>, see <see cref="CSTOJSOptions"/>.</param>
42
44
public CSTOJS ( CSTOJSOptions options )
43
45
{
44
- _Options = options ;
46
+ _Options = options ;
45
47
46
- _Log = ILog . GetILog ( this , _Options ) ;
48
+ _Log = ILog . GetILog ( this , _Options ) ;
47
49
48
- if ( _Options . DisableConsoleOutput == false )
50
+ if ( _Options . DisableConsoleOutput == false )
49
51
{
50
52
Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
51
- //https://stackoverflow.com/a/73474279
52
- _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
53
- }
53
+ //https://stackoverflow.com/a/73474279
54
+ _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
55
+ }
54
56
}
55
57
56
58
/// <summary>
@@ -61,7 +63,8 @@ public CSTOJS(CSTOJSOptions options)
61
63
/// <returns>empty Task</returns>
62
64
public async Task GenerateOneAsync ( string path , string ? filename = null )
63
65
{
64
- Assembly assembly = Assembly . GetEntryAssembly ( ) ;
66
+
67
+ Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
65
68
List < FileInfo > files = new ( ) ;
66
69
67
70
if ( File . Exists ( path ) )
@@ -100,7 +103,7 @@ public async Task GenerateOneAsync(string path, string? filename = null)
100
103
else
101
104
pathCombined = Path . Combine ( _Options . OutPutPath , file . Name . Replace ( ".cs" , ".js" ) ) ;
102
105
103
- await File . WriteAllTextAsync ( pathCombined , _Walker . JSSB . ToString ( ) ) ;
106
+ await File . WriteAllTextAsync ( pathCombined , _Walker ? . JSSB . ToString ( ) ) ;
104
107
105
108
_Log . SuccessLine ( $ "--- Done!") ;
106
109
_Log . SuccessLine ( $ "--- Path: { pathCombined } ") ;
@@ -115,7 +118,7 @@ public async Task GenerateOneAsync(string path, string? filename = null)
115
118
/// <returns>List of StringBuilder</returns>
116
119
public List < StringBuilder > GenerateOne ( string path )
117
120
{
118
- Assembly assembly = Assembly . GetEntryAssembly ( ) ;
121
+ Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
119
122
List < FileInfo > files = new ( ) ;
120
123
List < StringBuilder > jsStringBuilders = new ( ) ;
121
124
@@ -161,10 +164,9 @@ public List<StringBuilder> GenerateOne(string path)
161
164
/// <exception cref="ArgumentNullException"></exception>
162
165
public StringBuilder GenerateOneFromString ( string csstring , List < MetadataReference > ? references = null )
163
166
{
164
- if ( csstring == null )
165
- throw new ArgumentNullException ( nameof ( csstring ) ) ;
167
+ ArgumentNullException . ThrowIfNull ( csstring ) ;
166
168
167
- Assembly assembly = Assembly . GetEntryAssembly ( ) ;
169
+ Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
168
170
169
171
SyntaxTree ? _tree = CSharpSyntaxTree . ParseText ( csstring ) ;
170
172
@@ -189,10 +191,9 @@ public StringBuilder GenerateOneFromString(string csstring, List<MetadataReferen
189
191
/// <exception cref="ArgumentNullException"></exception>
190
192
public async Task GenerateOneFromStringAsync ( string csstring , string ? filename = "main.js" , List < MetadataReference > ? references = null )
191
193
{
192
- if ( csstring == null )
193
- throw new ArgumentNullException ( nameof ( csstring ) ) ;
194
+ ArgumentNullException . ThrowIfNull ( csstring ) ;
194
195
195
- Assembly assembly = Assembly . GetEntryAssembly ( ) ;
196
+ Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
196
197
197
198
SyntaxTree ? _tree = CSharpSyntaxTree . ParseText ( csstring ) ;
198
199
@@ -217,21 +218,21 @@ public async Task GenerateOneFromStringAsync(string csstring, string? filename =
217
218
}
218
219
219
220
220
- private void Generate ( SyntaxTree ? tree , Assembly assembly , List < MetadataReference > ? refs = null )
221
+ private void Generate ( SyntaxTree ? tree , Assembly ? assembly , List < MetadataReference > ? refs = null )
221
222
{
222
- if ( _Options . Debug )
223
- {
224
- _Stopwatch . Restart ( ) ;
223
+ if ( _Options . Debug )
224
+ {
225
+ _Stopwatch . Restart ( ) ;
225
226
_Log . WriteLine ( "Start stopwatch" ) ;
226
- }
227
+ }
227
228
228
229
CompilationUnitSyntax root = tree . GetCompilationUnitRoot ( ) ;
229
230
230
231
231
- string assemblyPath = Path . GetDirectoryName ( assembly . Location ) ;
232
+ string ? assemblyPath = Path . GetDirectoryName ( assembly ? . Location ) ;
232
233
List < MetadataReference > references = new ( ) { } ;
233
234
234
- string rtPath = Path . GetDirectoryName ( typeof ( object ) . Assembly . Location ) ;
235
+ string ? rtPath = Path . GetDirectoryName ( typeof ( object ) . Assembly . Location ) ;
235
236
236
237
if ( refs == null )
237
238
{
@@ -485,12 +486,12 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
485
486
486
487
_Walker . JSSB . Append ( _Options . AddSBInEnd ) ;
487
488
488
- if ( _Options . Debug )
489
- {
490
- _Stopwatch . Stop ( ) ;
489
+ if ( _Options . Debug )
490
+ {
491
+ _Stopwatch . Stop ( ) ;
491
492
_Log . WriteLine ( $ "Stop stopwatch: { _Stopwatch . Elapsed } ") ;
492
- }
493
- }
493
+ }
494
+ }
494
495
495
496
}
496
497
}
0 commit comments