Skip to content

Commit b81da4c

Browse files
committed
Added option "DisableConsoleOutput".
Added default references to the compilation.
1 parent 390c3ba commit b81da4c

File tree

2 files changed

+85
-33
lines changed

2 files changed

+85
-33
lines changed

CSharpToJavaScript/CSTOJS.cs

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@ public class CSTOJS
3030
/// </summary>
3131
public CSTOJS()
3232
{
33-
if (Options.Debug)
34-
{
35-
if (File.Exists(Directory.GetCurrentDirectory() + "/debug.txt"))
36-
File.Delete(Directory.GetCurrentDirectory() + "/debug.txt");
37-
38-
Trace.Listeners.Add(new TextWriterTraceListener("debug.txt"));
39-
Trace.AutoFlush = true;
40-
}
41-
4233
ConsoleTraceListener consoleTraceListener = new();
4334
if(Trace.Listeners.Contains(consoleTraceListener) == false)
4435
Trace.Listeners.Add(consoleTraceListener);
@@ -56,22 +47,26 @@ public CSTOJS(CSTOJSOptions options)
5647
{
5748
Options = options;
5849

59-
if (Options.Debug)
50+
if (Options.DisableConsoleOutput == false)
6051
{
61-
if (File.Exists(Directory.GetCurrentDirectory() + "/debug.txt"))
62-
File.Delete(Directory.GetCurrentDirectory() + "/debug.txt");
52+
if (Options.Debug)
53+
{
54+
if (File.Exists(Directory.GetCurrentDirectory() + "/debug.txt"))
55+
File.Delete(Directory.GetCurrentDirectory() + "/debug.txt");
6356

64-
Trace.Listeners.Add(new TextWriterTraceListener("debug.txt"));
65-
Trace.AutoFlush = true;
66-
}
57+
Trace.Listeners.Add(new TextWriterTraceListener("debug.txt"));
58+
Trace.AutoFlush = true;
59+
}
6760

68-
ConsoleTraceListener consoleTraceListener = new();
69-
if (Trace.Listeners.Contains(consoleTraceListener) == false)
70-
Trace.Listeners.Add(consoleTraceListener);
61+
ConsoleTraceListener consoleTraceListener = new();
62+
if (Trace.Listeners.Contains(consoleTraceListener) == false)
63+
Trace.Listeners.Add(consoleTraceListener);
7164

72-
Assembly assembly = Assembly.GetExecutingAssembly();
73-
//https://stackoverflow.com/a/73474279
74-
Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
65+
66+
Assembly assembly = Assembly.GetExecutingAssembly();
67+
//https://stackoverflow.com/a/73474279
68+
Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
69+
}
7570
}
7671

7772
/// <summary>
@@ -132,7 +127,7 @@ public async Task GenerateOneAsync(string path, string? filename = null)
132127
/// Method for generating from string.
133128
/// </summary>
134129
/// <param name="csstring">CSharp string.</param>
135-
/// <param name="references">Needed if you don't have access to files. Like if Blazor WebAssembly, because Assembly.location is empty.</param>
130+
/// <param name="references">Needed if you don't have access to files. Because Assembly.location is null in Blazor WebAssembly.</param>
136131
/// <returns>JS <see cref="StringBuilder"/></returns>
137132
/// <exception cref="ArgumentNullException"></exception>
138133
public async Task<StringBuilder> GenerateOneFromStringAsync(string csstring, List<MetadataReference>? references = null)
@@ -349,6 +344,51 @@ private async Task GenerateAsync(SyntaxTree? tree, Assembly assembly, List<Metad
349344
//Should I make "NormalizeWhitespace" option??? TODO!
350345
//.NormalizeWhitespace().AddUsings(oldUsing);
351346

347+
if (rtPath != null || rtPath != string.Empty)
348+
{
349+
if (File.Exists(Path.Combine(rtPath, "System.dll")))
350+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.dll")));
351+
352+
if (File.Exists(Path.Combine(rtPath, "System.Collections.Generics.dll")))
353+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.Collections.Generics.dll")));
354+
355+
if (File.Exists(Path.Combine(rtPath, "System.IO.dll")))
356+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.IO.dll")));
357+
358+
if (File.Exists(Path.Combine(rtPath, "System.Linq.dll")))
359+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.Linq.dll")));
360+
361+
if (File.Exists(Path.Combine(rtPath, "System.Net.Http.dll")))
362+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.Net.Http.dll")));
363+
364+
if (File.Exists(Path.Combine(rtPath, "System.Threading.dll")))
365+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.Threading.dll")));
366+
367+
if (File.Exists(Path.Combine(rtPath, "System.Threading.Tasks.dll")))
368+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.Threading.Tasks.dll")));
369+
370+
foreach (UsingDirectiveSyntax oU in oldUsing)
371+
{
372+
if (File.Exists(Path.Combine(rtPath, oU.Name + ".dll")))
373+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, oU.Name + ".dll")));
374+
}
375+
}
376+
377+
references = references.Distinct().ToList();
378+
379+
if (Options.Debug)
380+
{
381+
Log($"+++");
382+
Log($"assemblyPath: {assemblyPath}");
383+
Log($"rtPath: {rtPath}");
384+
Log($"List of references:");
385+
foreach (MetadataReference reference in references)
386+
{
387+
Log(reference.Display);
388+
}
389+
Log($"+++");
390+
}
391+
352392
SyntaxTree trueST = trueRoot.SyntaxTree;
353393
CSharpCompilation compilation = CSharpCompilation
354394
.Create("HelloWorld")
@@ -366,22 +406,26 @@ private async Task GenerateAsync(SyntaxTree? tree, Assembly assembly, List<Metad
366406

367407
public void Log(string message, [CallerFilePath] string? file = null, [CallerMemberName] string? member = null, [CallerLineNumber] int line = 0)
368408
{
369-
if (Options.DisableConsoleColors == false)
409+
if (Options.DisableConsoleOutput == false)
370410
{
371-
if (message.StartsWith("---"))
372-
Console.ForegroundColor = ConsoleColor.Green;
411+
if (Options.DisableConsoleColors == false)
412+
{
413+
if (message.StartsWith("---"))
414+
Console.ForegroundColor = ConsoleColor.Green;
373415

374-
if (message.StartsWith("ERROR") || message.StartsWith("as"))
375-
Console.ForegroundColor = ConsoleColor.Red;
416+
if (message.StartsWith("ERROR") || message.StartsWith("as"))
417+
Console.ForegroundColor = ConsoleColor.Red;
376418

377-
if (message.StartsWith("WARNING"))
378-
Console.ForegroundColor = ConsoleColor.Yellow;
379-
}
419+
if (message.StartsWith("WARNING"))
420+
Console.ForegroundColor = ConsoleColor.Yellow;
421+
}
380422

381-
Trace.WriteLine($"({line}):{Path.GetFileName(file)} {member}: {message}");
382423

383-
if (Options.DisableConsoleColors == false)
384-
Console.ResetColor();
424+
Trace.WriteLine($"({line}):{Path.GetFileName(file)} {member}: {message}");
425+
426+
if (Options.DisableConsoleColors == false)
427+
Console.ResetColor();
428+
}
385429
}
386430
}
387431
}

CSharpToJavaScript/CSTOJSOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public class CSTOJSOptions
2727
/// </value>
2828
public bool DisableConsoleColors { get; set; } = false;
2929

30+
/// <summary>
31+
/// Self-explanatory, Disable Console Output.
32+
/// </summary>
33+
/// <value>
34+
/// Default: <c>false</c>
35+
/// </value>
36+
public bool DisableConsoleOutput { get; set; } = false;
37+
3038
/// <summary>
3139
/// Output path for javascript file.
3240
/// </summary>

0 commit comments

Comments
 (0)