Skip to content

Commit 76def38

Browse files
committed
.Net 8
Updated generated csharp code(Many missing webapi from w3c). Added method "GenerateOne" returns list of StringBuilders. Added method "GenerateOneFromStringAsync" writes a file, return empty Task. Added "System.Console" and "CSharpToJavaScript" references as default. c# const -> js const. c# static constructor -> js static constructor. private js fields for c# auto-implemented properties. c# static -> js static Better "this.". Small changes and fixes.
1 parent dee9e03 commit 76def38

File tree

14 files changed

+17559
-4861
lines changed

14 files changed

+17559
-4861
lines changed

CSharpToJavaScript/APIs/JS/CustomEvent.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using CSharpToJavaScript.Utils;
2-
using Microsoft.CodeAnalysis;
32
using System;
43
using System.Collections.Generic;
54
using System.Linq;
@@ -11,14 +10,10 @@ namespace CSharpToJavaScript.APIs.JS
1110
public partial class Document
1211
{
1312
[To(ToAttribute.FirstCharToLowerCase)]
14-
public Element CreateElement(string localName, string options = "")
13+
public Element CreateElement(string localName, string options = "")
1514
{
16-
throw new System.NotImplementedException();
17-
}
18-
[To(ToAttribute.FirstCharToLowerCase)]
19-
public Text CreateTextNode(string data)
20-
{
21-
throw new System.NotImplementedException();
15+
throw new NotImplementedException();
2216
}
17+
2318
}
2419
}

CSharpToJavaScript/APIs/JS/Ecma/RegExp.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CSharpToJavaScript.Utils;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace CSharpToJavaScript.APIs.JS
45
{
@@ -9,8 +10,8 @@ public partial class RegExp : RegExpPrototype
910
[To(ToAttribute.FirstCharToLowerCase)]
1011
public static RegExpPrototype Prototype { get; } = new();
1112

12-
public RegExp(string pattern) { }
13-
public RegExp(string pattern, string flags) { }
13+
public RegExp([StringSyntax(StringSyntaxAttribute.Regex)] string pattern) { }
14+
public RegExp([StringSyntax(StringSyntaxAttribute.Regex)] string pattern, string flags) { }
1415
}
1516

1617
//TODO! match etc.!!!! How? Extension methods?

CSharpToJavaScript/APIs/JS/Event.cs

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

CSharpToJavaScript/APIs/JS/EventTarget.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using CSharpToJavaScript.Utils;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Text;
@@ -9,9 +8,9 @@ namespace CSharpToJavaScript.APIs.JS
98
{
109
public partial class EventTarget
1110
{
12-
public CSharpToJavaScript.Utils.Unsupported /*undefined*/ AddEventListener(string type, Action? callback, Union1 options) { throw new System.NotImplementedException(); }
13-
public CSharpToJavaScript.Utils.Unsupported /*undefined*/ AddEventListener(string type, Action<MouseEvent>? callback, Union1 options) { throw new System.NotImplementedException(); }
14-
public CSharpToJavaScript.Utils.Unsupported /*undefined*/ AddEventListener(string type, Action<Event>? callback, Union1 options) { throw new System.NotImplementedException(); }
11+
public CSharpToJavaScript.Utils.Unsupported /*undefined*/ AddEventListener(string type, Action? callback, Union41 options) { throw new System.NotImplementedException(); }
12+
public CSharpToJavaScript.Utils.Unsupported /*undefined*/ AddEventListener(string type, Action<MouseEvent>? callback, Union41 options) { throw new System.NotImplementedException(); }
13+
public CSharpToJavaScript.Utils.Unsupported /*undefined*/ AddEventListener(string type, Action<Event>? callback, Union41 options) { throw new System.NotImplementedException(); }
1514

1615
public CSharpToJavaScript.Utils.Unsupported /*undefined*/ RemoveEventListener(string type, Action? callback, Union2 options) { throw new System.NotImplementedException(); }
1716
}

CSharpToJavaScript/APIs/JS/Generated/JS.generated.cs

Lines changed: 17305 additions & 4701 deletions
Large diffs are not rendered by default.

CSharpToJavaScript/APIs/JS/HTMLElement.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ public partial class HTMLElement
1212
[To(ToAttribute.FirstCharToLowerCase)]
1313
public CSSStyleDeclaration Style { get; }
1414
}
15-
16-
public partial interface CSSStyleDeclaration
15+
public partial class CSSStyleDeclaration
1716
{
1817
[To(ToAttribute.FirstCharToLowerCase)]
1918
public string Display { get; set; }

CSharpToJavaScript/APIs/JS/MouseEvent.cs

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

CSharpToJavaScript/APIs/JS/Text.cs

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

CSharpToJavaScript/CSTOJS.cs

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ public class CSTOJS
2323

2424
private Walker? _Walker = null;
2525

26+
static CSTOJS()
27+
{
28+
ConsoleTraceListener consoleTraceListener = new();
29+
Trace.Listeners.Add(consoleTraceListener);
30+
}
31+
2632
/// <summary>
2733
/// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
2834
/// </summary>
2935
public CSTOJS()
3036
{
31-
ConsoleTraceListener consoleTraceListener = new();
32-
if(Trace.Listeners.Contains(consoleTraceListener) == false)
33-
Trace.Listeners.Add(consoleTraceListener);
34-
3537
Assembly assembly = Assembly.GetExecutingAssembly();
3638
//https://stackoverflow.com/a/73474279
3739
Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
@@ -47,11 +49,6 @@ public CSTOJS(CSTOJSOptions options)
4749

4850
if (Options.DisableConsoleOutput == false)
4951
{
50-
ConsoleTraceListener consoleTraceListener = new();
51-
if (Trace.Listeners.Contains(consoleTraceListener) == false)
52-
Trace.Listeners.Add(consoleTraceListener);
53-
54-
5552
Assembly assembly = Assembly.GetExecutingAssembly();
5653
//https://stackoverflow.com/a/73474279
5754
Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
@@ -63,7 +60,7 @@ public CSTOJS(CSTOJSOptions options)
6360
/// </summary>
6461
/// <param name="path">Full path to cs file or to the folder with cs files.</param>
6562
/// <param name="filename">Optional! Filename of a js file if you generating one file!</param>
66-
/// <returns></returns>
63+
/// <returns>empty Task</returns>
6764
public async Task GenerateOneAsync(string path, string? filename = null)
6865
{
6966
Assembly assembly = Assembly.GetEntryAssembly();
@@ -113,6 +110,50 @@ public async Task GenerateOneAsync(string path, string? filename = null)
113110
}
114111
}
115112

113+
/// <summary>
114+
/// Method for generating js StringBuilder/StringBuilders.
115+
/// </summary>
116+
/// <param name="path">Full path to cs file or to the folder with cs files.</param>
117+
/// <returns>List of StringBuilder</returns>
118+
public List<StringBuilder> GenerateOne(string path)
119+
{
120+
Assembly assembly = Assembly.GetEntryAssembly();
121+
List<FileInfo> files = new();
122+
List<StringBuilder> jsStringBuilders = new();
123+
124+
if (File.Exists(path))
125+
{
126+
files.Add(new FileInfo(path));
127+
}
128+
else
129+
{
130+
DirectoryInfo folder = new(path);
131+
132+
files = folder.GetFiles("*.cs").ToList();
133+
}
134+
135+
foreach (FileInfo file in files)
136+
{
137+
SyntaxTree? _tree = null;
138+
139+
using (var stream = File.OpenRead(file.FullName))
140+
{
141+
_tree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: file.FullName);
142+
}
143+
144+
Generate(_tree, assembly);
145+
146+
jsStringBuilders.Add(_Walker.JSSB);
147+
148+
Log($"--- Done!");
149+
Log($"--- File name: {file.Name}");
150+
Log($"--- --- ---");
151+
}
152+
153+
return jsStringBuilders;
154+
}
155+
156+
116157
/// <summary>
117158
/// Method for generating from string.
118159
/// </summary>
@@ -140,6 +181,44 @@ public StringBuilder GenerateOneFromString(string csstring, List<MetadataReferen
140181
return _Walker.JSSB;
141182
}
142183

184+
/// <summary>
185+
/// Method for generating from string. Writes a file.
186+
/// </summary>
187+
/// <param name="csstring">CSharp string.</param>
188+
/// <param name="filename">Filename of a js file.</param>
189+
/// <param name="references">Needed if you don't have access to files. Because Assembly.location is null in Blazor WebAssembly.</param>
190+
/// <returns>empty Task</returns>
191+
/// <exception cref="ArgumentNullException"></exception>
192+
public async Task GenerateOneFromStringAsync(string csstring, string? filename = "main.js", List<MetadataReference>? references = null)
193+
{
194+
if (csstring == null)
195+
throw new ArgumentNullException(nameof(csstring));
196+
197+
Assembly assembly = Assembly.GetEntryAssembly();
198+
199+
SyntaxTree? _tree = CSharpSyntaxTree.ParseText(csstring);
200+
201+
if (references != null)
202+
Generate(_tree, assembly, references);
203+
else
204+
Generate(_tree, assembly);
205+
206+
207+
if (!Directory.Exists(Options.OutPutPath))
208+
{
209+
Directory.CreateDirectory(Options.OutPutPath);
210+
}
211+
212+
string pathCombined = Path.Combine(Options.OutPutPath, filename);
213+
214+
await File.WriteAllTextAsync(pathCombined, _Walker.JSSB.ToString());
215+
216+
Log($"--- Done!");
217+
Log($"--- Path: {pathCombined}");
218+
Log($"--- --- ---");
219+
}
220+
221+
143222
private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReference>? refs = null)
144223
{
145224
CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
@@ -354,14 +433,25 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
354433

355434
if (File.Exists(Path.Combine(rtPath, "System.Threading.Tasks.dll")))
356435
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.Threading.Tasks.dll")));
436+
437+
if (File.Exists(Path.Combine(rtPath, "System.Console.dll")))
438+
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.Console.dll")));
357439

358440
foreach (UsingDirectiveSyntax oU in oldUsing)
359441
{
442+
360443
if (File.Exists(Path.Combine(rtPath, oU.Name + ".dll")))
361444
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, oU.Name + ".dll")));
362445
}
363446
}
364447

448+
if (assemblyPath != null && assemblyPath != string.Empty)
449+
{
450+
if (File.Exists(Path.Combine(assemblyPath, "CSharpToJavaScript.dll")))
451+
references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "CSharpToJavaScript.dll")));
452+
}
453+
454+
//TODO! does not work... sigh
365455
references = references.Distinct().ToList();
366456

367457
if (Options.Debug)

0 commit comments

Comments
 (0)