Skip to content

Commit b8e267b

Browse files
committed
Added option to disable console colors.
Added method "GenerateOneFromString", that accept a cs string and not a path to file. Small fixes.
1 parent fa8edc9 commit b8e267b

File tree

4 files changed

+226
-158
lines changed

4 files changed

+226
-158
lines changed

CSharpToJavaScript/CSTOJS.cs

Lines changed: 118 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
using Microsoft.CodeAnalysis.CSharp;
77
using System.Threading.Tasks;
88
using Microsoft.CodeAnalysis;
9-
using CSharpToJavaScript.Utils;
109
using System.Linq;
1110
using Microsoft.CodeAnalysis.Text;
11+
using System.Text;
12+
using System.Runtime.CompilerServices;
13+
using System;
14+
using System.Reflection.Metadata;
15+
using System.Collections.Immutable;
1216

1317
namespace CSharpToJavaScript
1418
{
@@ -17,9 +21,9 @@ namespace CSharpToJavaScript
1721
/// </summary>
1822
public class CSTOJS
1923
{
20-
public static SemanticModel Model { get; set; }
24+
public SemanticModel Model { get; set; } = null;
2125

22-
private CSTOJSOptions _Options = new();
26+
public CSTOJSOptions Options { get; set; } = new();
2327

2428
private Walker _Walker = new();
2529

@@ -28,7 +32,7 @@ public class CSTOJS
2832
/// </summary>
2933
public CSTOJS()
3034
{
31-
if (_Options.Debug)
35+
if (Options.Debug)
3236
{
3337
if (File.Exists(Directory.GetCurrentDirectory() + "/debug.txt"))
3438
File.Delete(Directory.GetCurrentDirectory() + "/debug.txt");
@@ -41,7 +45,7 @@ public CSTOJS()
4145

4246
Assembly assembly = Assembly.GetExecutingAssembly();
4347
//https://stackoverflow.com/a/73474279
44-
SM.Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
48+
Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
4549
}
4650

4751
/// <summary>
@@ -50,9 +54,9 @@ public CSTOJS()
5054
/// <param name="options">Options of <see cref="CSTOJS"/>, see <see cref="CSTOJSOptions"/>.</param>
5155
public CSTOJS(CSTOJSOptions options)
5256
{
53-
_Options = options;
57+
Options = options;
5458

55-
if (_Options.Debug)
59+
if (Options.Debug)
5660
{
5761
if (File.Exists(Directory.GetCurrentDirectory() + "/debug.txt"))
5862
File.Delete(Directory.GetCurrentDirectory() + "/debug.txt");
@@ -65,7 +69,7 @@ public CSTOJS(CSTOJSOptions options)
6569

6670
Assembly assembly = Assembly.GetExecutingAssembly();
6771
//https://stackoverflow.com/a/73474279
68-
SM.Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
72+
Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
6973
}
7074

7175
/// <summary>
@@ -80,10 +84,53 @@ public async Task GenerateOneAsync(string path, string? filename = null)
8084

8185
FileInfo file = new(path);
8286

83-
if(filename != null)
84-
await GenerateAsync(path, assembly, filename);
87+
SyntaxTree? _tree = null;
88+
89+
using (var stream = File.OpenRead(path))
90+
{
91+
_tree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: path);
92+
}
93+
94+
await GenerateAsync(_tree, assembly);
95+
96+
if (!Directory.Exists(Options.OutPutPath))
97+
{
98+
Directory.CreateDirectory(Options.OutPutPath);
99+
}
100+
101+
string pathCombined = string.Empty;
102+
103+
if (filename != null)
104+
pathCombined = Path.Combine(Options.OutPutPath, filename);
85105
else
86-
await GenerateAsync(path, assembly, file.Name.Replace(".cs", ".js"));
106+
pathCombined = Path.Combine(Options.OutPutPath, file.Name.Replace(".cs", ".js"));
107+
108+
109+
await File.WriteAllTextAsync(pathCombined, _Walker.JSSB.ToString());
110+
111+
Log($"--- Done!");
112+
Log($"--- Path: {pathCombined}");
113+
Log($"--- --- ---");
114+
}
115+
116+
public async Task<StringBuilder> GenerateOneFromStringAsync(string csstring, List<MetadataReference>? references = null)
117+
{
118+
if (csstring == null)
119+
throw new ArgumentNullException(nameof(csstring));
120+
121+
Assembly assembly = Assembly.GetEntryAssembly();
122+
123+
SyntaxTree? _tree = CSharpSyntaxTree.ParseText(csstring.Normalize());
124+
125+
if(references != null)
126+
await GenerateAsync(_tree, assembly, references);
127+
else
128+
await GenerateAsync(_tree, assembly);
129+
130+
Log($"--- Done!");
131+
Log($"--- --- ---");
132+
133+
return _Walker.JSSB;
87134
}
88135
/// <summary>
89136
/// Method for generating multiply js files.
@@ -100,42 +147,65 @@ public async Task GenerateManyAsync(string path)
100147

101148
foreach (FileInfo file in Files)
102149
{
103-
await GenerateAsync(file.FullName, assembly, file.Name.Replace(".cs", ".js"));
104-
}
105-
}
150+
SyntaxTree? _tree = null;
106151

107-
private async Task GenerateAsync(string path, Assembly assembly, string filename = "main.js")
108-
{
109-
_Walker = new(_Options);
152+
using (var stream = File.OpenRead(file.FullName))
153+
{
154+
_tree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: file.FullName);
155+
}
110156

111-
SyntaxTree? tree = null;
157+
await GenerateAsync(_tree, assembly);
112158

113-
using (var stream = File.OpenRead(path))
114-
{
115-
tree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: path);
159+
if (!Directory.Exists(Options.OutPutPath))
160+
{
161+
Directory.CreateDirectory(Options.OutPutPath);
162+
}
163+
164+
string pathCombined = Path.Combine(Options.OutPutPath, file.Name.Replace(".cs", ".js"));
165+
166+
await File.WriteAllTextAsync(pathCombined, _Walker.JSSB.ToString());
167+
168+
Log($"--- Done!");
169+
Log($"--- Path: {pathCombined}");
170+
Log($"--- --- ---");
116171
}
172+
}
173+
174+
private async Task GenerateAsync(SyntaxTree? tree, Assembly assembly, List<MetadataReference>? refs = null)
175+
{
176+
_Walker = new(this);
117177

118178
CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
119179

180+
120181
string assemblyPath = Path.GetDirectoryName(assembly.Location);
121182
List<MetadataReference> references = new() { };
122183

123184
string rtPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
124185

125-
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.Private.CoreLib.dll")));
126-
127-
AssemblyName[] a = assembly.GetReferencedAssemblies();
128-
foreach (AssemblyName item in a)
186+
if (refs == null)
129187
{
130-
if (File.Exists(Path.Combine(assemblyPath, item.Name + ".dll")))
131-
references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, item.Name + ".dll")));
132-
else
188+
if (rtPath != null && assemblyPath != null)
133189
{
134-
if (File.Exists(Path.Combine(rtPath, item.Name + ".dll")))
135-
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, item.Name + ".dll")));
190+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.Private.CoreLib.dll")));
191+
192+
AssemblyName[] a = assembly.GetReferencedAssemblies();
193+
foreach (AssemblyName item in a)
194+
{
195+
if (File.Exists(Path.Combine(assemblyPath, item.Name + ".dll")))
196+
references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, item.Name + ".dll")));
197+
else
198+
{
199+
if (File.Exists(Path.Combine(rtPath, item.Name + ".dll")))
200+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, item.Name + ".dll")));
201+
}
202+
}
136203
}
137204
}
138-
205+
else
206+
{
207+
references = refs;
208+
}
139209

140210
UsingDirectiveSyntax[] oldUsing = root.Usings.ToArray();
141211

@@ -303,24 +373,31 @@ private async Task GenerateAsync(string path, Assembly assembly, string filename
303373

304374
Model = compilation.GetSemanticModel(trueST);
305375

306-
_Walker.JSSB.Append(_Options.AddSBInFront);
376+
_Walker.JSSB.Append(Options.AddSBInFront);
307377

308378
_Walker.Visit(trueRoot);
309379

310-
_Walker.JSSB.Append(_Options.AddSBInEnd);
380+
_Walker.JSSB.Append(Options.AddSBInEnd);
381+
}
311382

312-
if (!Directory.Exists(_Options.OutPutPath))
383+
public void Log(string message, [CallerFilePath] string? file = null, [CallerMemberName] string? member = null, [CallerLineNumber] int line = 0)
384+
{
385+
if (Options.DisableConsoleColors == false)
313386
{
314-
Directory.CreateDirectory(_Options.OutPutPath);
315-
}
387+
if (message.StartsWith("---"))
388+
Console.ForegroundColor = ConsoleColor.Green;
316389

317-
string pathCombined = Path.Combine(_Options.OutPutPath, filename);
390+
if (message.StartsWith("ERROR") || message.StartsWith("as"))
391+
Console.ForegroundColor = ConsoleColor.Red;
318392

319-
await File.WriteAllTextAsync(pathCombined, _Walker.JSSB.ToString());
393+
if (message.StartsWith("WARNING"))
394+
Console.ForegroundColor = ConsoleColor.Yellow;
395+
}
396+
397+
Trace.WriteLine($"({line}):{Path.GetFileName(file)} {member}: {message}");
320398

321-
SM.Log($"--- Done!");
322-
SM.Log($"--- Path: {pathCombined}");
323-
SM.Log($"--- --- ---");
399+
if (Options.DisableConsoleColors == false)
400+
Console.ResetColor();
324401
}
325402
}
326403
}

CSharpToJavaScript/CSTOJSOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ public class CSTOJSOptions
1919
public bool Debug { get; set; } = false;
2020

2121

22+
/// <summary>
23+
/// Self-explanatory, Disable Console Colors.
24+
/// </summary>
25+
/// <value>
26+
/// Default: <c>false</c>
27+
/// </value>
28+
public bool DisableConsoleColors { get; set; } = false;
29+
2230
/// <summary>
2331
/// Output path for javascript file.
2432
/// </summary>

CSharpToJavaScript/Utils/StaticMethods.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)