@@ -18,20 +18,15 @@ public class CSTOJS
1818 {
1919 public static SemanticModel Model { get ; set ; }
2020
21- private readonly Assembly _Assembly ;
22-
2321 private CSTOJSOptions _Options = new ( ) ;
2422
2523 private Walker _Walker = new ( ) ;
2624
2725 /// <summary>
2826 /// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
2927 /// </summary>
30- /// <param name="assembly">Assembly of a project, get by <see cref="Assembly.GetExecutingAssembly" /></param>
31- public CSTOJS ( Assembly assembly )
28+ public CSTOJS ( )
3229 {
33- _Assembly = assembly ;
34-
3530 if ( _Options . Debug )
3631 {
3732 if ( File . Exists ( Directory . GetCurrentDirectory ( ) + "/debug.txt" ) )
@@ -45,13 +40,11 @@ public CSTOJS(Assembly assembly)
4540 }
4641
4742 /// <summary>
48- ///
43+ /// New instance of <see cref="CSTOJS"/>
4944 /// </summary>
50- /// <param name="assembly">Assembly of a project, get by <see cref="Assembly.GetExecutingAssembly" /></param>
5145 /// <param name="options">Options of <see cref="CSTOJS"/>, see <see cref="CSTOJSOptions"/>.</param>
52- public CSTOJS ( Assembly assembly , CSTOJSOptions options )
46+ public CSTOJS ( CSTOJSOptions options )
5347 {
54- _Assembly = assembly ;
5548 _Options = options ;
5649 _Walker = new ( _Options ) ;
5750
@@ -71,22 +64,48 @@ public CSTOJS(Assembly assembly, CSTOJSOptions options)
7164 /// Method for generating js file.
7265 /// </summary>
7366 /// <param name="path">Full path to cs file.</param>
67+ /// <param name="filename">Filename of a js file. Default: <c>main.js</c></param>
68+ /// <returns></returns>
69+ public async Task GenerateOneAsync ( string path , string filename = "main.js" )
70+ {
71+ Assembly assembly = Assembly . GetEntryAssembly ( ) ;
72+
73+ await GenerateAsync ( path , assembly , filename ) ;
74+ }
75+ /// <summary>
76+ /// Method for generating multiply js files.
77+ /// </summary>
78+ /// <param name="path">Full path to the folder/directory.</param>
7479 /// <returns></returns>
75- public async Task GenerateAsync ( string path )
80+ public async Task GenerateManyAsync ( string path )
81+ {
82+ Assembly assembly = Assembly . GetEntryAssembly ( ) ;
83+
84+ DirectoryInfo folder = new ( path ) ;
85+
86+ FileInfo [ ] Files = folder . GetFiles ( "*.cs" ) ;
87+
88+ foreach ( FileInfo file in Files )
89+ {
90+ await GenerateAsync ( file . FullName , assembly , file . Name . Replace ( ".cs" , ".js" ) ) ;
91+ }
92+ }
93+
94+ private async Task GenerateAsync ( string path , Assembly assembly , string filename = "main.js" )
7695 {
7796 string fileCS = await File . ReadAllTextAsync ( path ) ;
7897
7998 SyntaxTree tree = CSharpSyntaxTree . ParseText ( fileCS ) ;
8099 CompilationUnitSyntax root = tree . GetCompilationUnitRoot ( ) ;
81100
82- string assemblyPath = Path . GetDirectoryName ( _Assembly . Location ) ;
101+ string assemblyPath = Path . GetDirectoryName ( assembly . Location ) ;
83102 List < MetadataReference > references = new ( ) { } ;
84103
85104 string rtPath = Path . GetDirectoryName ( typeof ( object ) . Assembly . Location ) ;
86105
87106 references . Add ( MetadataReference . CreateFromFile ( Path . Combine ( rtPath , "System.Private.CoreLib.dll" ) ) ) ;
88107
89- AssemblyName [ ] a = _Assembly . GetReferencedAssemblies ( ) ;
108+ AssemblyName [ ] a = assembly . GetReferencedAssemblies ( ) ;
90109 foreach ( AssemblyName item in a )
91110 {
92111 if ( File . Exists ( Path . Combine ( assemblyPath , item . Name + ".dll" ) ) )
@@ -98,7 +117,7 @@ public async Task GenerateAsync(string path)
98117 }
99118 }
100119
101-
120+
102121 UsingDirectiveSyntax [ ] oldUsing = root . Usings . ToArray ( ) ;
103122
104123 //https://roslynquoter.azurewebsites.net/
@@ -254,16 +273,15 @@ public async Task GenerateAsync(string path)
254273 }
255274 )
256275) . AddUsings ( oldUsing ) ;
257-
258- //Should I make "NormalizeWhitespace" optoion??? TODO!
276+ //Should I make "NormalizeWhitespace" option??? TODO!
259277 //.NormalizeWhitespace().AddUsings(oldUsing);
260278
261279 SyntaxTree trueST = trueRoot . SyntaxTree ;
262280 CSharpCompilation compilation = CSharpCompilation
263281 . Create ( "HelloWorld" )
264282 . AddReferences ( references . ToArray ( ) )
265283 . AddSyntaxTrees ( trueST ) ;
266-
284+
267285 Model = compilation . GetSemanticModel ( trueST ) ;
268286
269287 _Walker . JSSB . Append ( _Options . AddSBInFront ) ;
@@ -277,11 +295,11 @@ public async Task GenerateAsync(string path)
277295 Directory . CreateDirectory ( _Options . OutPutPath ) ;
278296 }
279297
280- await File . WriteAllTextAsync ( Path . Combine ( _Options . OutPutPath , _Options . OutPutFileName ) , _Walker . JSSB . ToString ( ) ) ;
281-
298+ await File . WriteAllTextAsync ( Path . Combine ( _Options . OutPutPath , filename ) , _Walker . JSSB . ToString ( ) ) ;
299+
282300 SM . Log ( $ "--- Done!") ;
283301 SM . Log ( $ "--- Path: { _Options . OutPutPath } ") ;
284- SM . Log ( $ "--- File: { _Options . OutPutFileName } ") ;
302+ SM . Log ( $ "--- File: { filename } ") ;
285303 }
286304 }
287305}
0 commit comments