|
1 | 1 | # CSharpToJavaScript |
2 | | -[Nuget package](https://www.nuget.org/packages/CSharpToJavaScript/) | [Website](https://tilied.github.io/CSTOJS_Pages/) | [Try it online!](https://tilied.github.io/CSTOJS_Pages/BWA/) | [CLI](https://github.com/TiLied/CSTOJS_CLI) | [VS Code Extension](https://marketplace.visualstudio.com/items?itemName=tilied.cstojs-vscode-ext) | [VS Extension](https://marketplace.visualstudio.com/items?itemName=tilied.cstojs-vs-ext) |
| 2 | +[Nuget package](https://www.nuget.org/packages/CSharpToJavaScript/) | [CLI](https://github.com/TiLied/CSTOJS_CLI) | [Website](https://tilied.github.io/CSTOJS_Pages/) | [Try it online!](https://tilied.github.io/CSTOJS_Pages/BWA/) |
3 | 3 |
|
4 | | -C# conversion to JavaScript as is. |
| 4 | +This library is a "core" where all the "magic" happens for translating/converting cs into js, using Roslyn. You should use the [CLI](https://github.com/TiLied/CSTOJS_CLI)/dotnet tool, it behaves more or less like dotnet cli, tsc cli, meson. Look for the implementation in [CLI](https://github.com/TiLied/CSTOJS_CLI), [BWA app](https://github.com/TiLied/CSTOJS_BWA) or simple "Hello world" below. |
5 | 5 |
|
6 | | -*This is a personal project. Updates will be irregular.* |
7 | | - |
8 | | -### C# input |
| 6 | +## Hello world |
| 7 | +- Install [nuget package](https://www.nuget.org/packages/CSharpToJavaScript/) or download a [specific version](https://github.com/TiLied/CSharpToJavaScript/releases). |
| 8 | +- Skip this if using a Nuget package. Follow [the instructions on how to add a project reference](https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio?pivots=dotnet-7-0#add-a-project-reference). |
| 9 | +- In the Main method, add: |
9 | 10 | ```csharp |
10 | | -namespace ConsoleAppTest.CSharp; |
11 | | - |
12 | | -public class Test |
13 | | -{ |
14 | | - public Test() |
15 | | - { |
16 | | - Console.WriteLine("Hello World!"); |
17 | | - } |
18 | | -} |
19 | | -``` |
20 | | -### Javascript output |
21 | | -```javascript |
22 | | -class Test |
| 11 | +FileData file = new() |
23 | 12 | { |
24 | | - constructor() |
25 | | - { |
26 | | - console.log("Hello World!"); |
27 | | - } |
28 | | -} |
29 | | -``` |
30 | | - |
31 | | - |
32 | | - |
33 | | -## How to use |
34 | | -- 1 Create c# project or use existed one. |
35 | | -- 2 Install [nuget package](https://www.nuget.org/packages/CSharpToJavaScript/) or Download a [specific version](https://github.com/TiLied/CSharpToJavaScript/releases) or Download a master(Code-Local-Download ZIP). |
36 | | -- 3 Skip this if using Nuget package. Follow [how to add a project reference](https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio?pivots=dotnet-7-0#add-a-project-reference). |
37 | | -- 4 In the Main method add: |
38 | | -```csharp |
39 | | -CSTOJS cstojs = new(); |
40 | | -await cstojs.GenerateOneAsync("Path(full path) to c# file(folder with c# files)"); |
41 | | -``` |
42 | | -*Other methods "GenerateOne" can be found [on the website](https://tilied.github.io/CSTOJS_Pages/api/CSharpToJavaScript.CSTOJS.html#methods).* |
43 | | -- 5 Run program and file will be generated in output path(default is "Directory.GetCurrentDirectory()") with name "|C# file name|.js"(default) |
44 | | -- 6 See below for an example "Model" and "HelloWorld". |
45 | | - |
46 | | -## Example "Model" |
47 | | -Program.cs |
48 | | -```csharp |
49 | | -using CSharpToJavaScript; |
50 | | -namespace ConsoleAppTest; |
51 | | - |
52 | | -public class Program |
53 | | -{ |
54 | | - public static async Task Main() |
55 | | - { |
56 | | - CSTOJS cstojs = new(new CSTOJSOptions() |
57 | | - { |
58 | | - KeepBraceOnTheSameLine = true, |
59 | | - NormalizeWhitespace = true |
60 | | - }); |
61 | | - await cstojs.GenerateOneAsync("C:\\GitReps\\ConsoleAppTest\\CSharp\\Person.cs"); |
62 | | - |
63 | | - Console.ReadKey(); |
64 | | - } |
65 | | -} |
66 | | -``` |
67 | | -*List of [options can be found on the website](https://tilied.github.io/CSTOJS_Pages/api/CSharpToJavaScript.CSTOJSOptions.html) or [in the code directly](https://github.com/TiLied/CSharpToJavaScript/blob/master/CSharpToJavaScript/CSTOJSOptions.cs).* |
68 | | - |
69 | | -CSharp/Person.cs |
70 | | -```csharp |
71 | | -using static CSharpToJavaScript.APIs.JS.GlobalObject; |
72 | | -namespace ConsoleAppTest.CSharp; |
| 13 | + SourceStr = @"Console.WriteLine(""Hello world."");" |
| 14 | +}; |
| 15 | +FileData[] files = CSTOJS.Translate([ file ]); |
73 | 16 |
|
74 | | -public partial class Person |
75 | | -{ |
76 | | - private int _Id; |
77 | | - public string FullName { get; set; } |
78 | | - public int Age { get; set; } |
79 | | -} |
| 17 | +Console.WriteLine(files[0].TranslatedStr); |
80 | 18 | ``` |
81 | | -Above code will generate "Person.js" file that contains: |
| 19 | +- Run a program |
| 20 | +- Console output should be: |
82 | 21 | ```javascript |
83 | | -class Person { |
84 | | - _Id; |
85 | | - #_FullName_; |
86 | | - get FullName() { return this.#_FullName_; } |
87 | | - set FullName(value) { this.#_FullName_ = value; } |
88 | | - #_Age_; |
89 | | - get Age() { return this.#_Age_; } |
90 | | - set Age(value) { this.#_Age_ = value; } |
91 | | -} |
| 22 | +console.log("Hello world."); |
92 | 23 | ``` |
93 | | - |
94 | | -## Example "HelloWorld" |
95 | | -Program.cs |
| 24 | +- For options look at the [code](https://github.com/TiLied/CSharpToJavaScript/blob/master/CSharpToJavaScript/CSTOJSOptions.cs) or on a [website](https://tilied.github.io/CSTOJS_Pages/api/CSharpToJavaScript.CSTOJSOptions.html). |
| 25 | +- To specify options (options applying per file): |
96 | 26 | ```csharp |
97 | | -using CSharpToJavaScript; |
98 | | -namespace ConsoleAppTest; |
99 | | - |
100 | | -public class Program |
| 27 | +FileData file = new() |
101 | 28 | { |
102 | | - public static async Task Main() |
103 | | - { |
104 | | - CSTOJS cstojs = new(); |
105 | | - await cstojs.GenerateOneAsync("C:\\GitReps\\ConsoleAppTest\\CSharp\\Test.cs"); |
106 | | - |
107 | | - Console.ReadKey(); |
108 | | - } |
109 | | -} |
110 | | -``` |
111 | | -CSharp/Test.cs |
112 | | -```csharp |
113 | | -using static CSharpToJavaScript.APIs.JS.GlobalObject; |
114 | | -namespace ConsoleAppTest.CSharp; |
115 | | - |
116 | | -public class Test |
117 | | -{ |
118 | | - public Test() |
119 | | - { |
120 | | - GlobalThis.Console.Log("HelloWorld!"); |
121 | | - } |
122 | | -} |
123 | | -``` |
124 | | -Above code will generate "Test.js" file that contains: |
125 | | -```javascript |
126 | | -class Test |
127 | | -{ |
128 | | - constructor() |
129 | | - { |
130 | | - globalThis.console.log("HelloWorld!"); |
131 | | - } |
132 | | -} |
| 29 | + OptionsForFile = new(){ Debug = true }, |
| 30 | + ... |
| 31 | +}; |
| 32 | +... |
133 | 33 | ``` |
134 | 34 |
|
135 | 35 | ## Related Repository |
136 | | -- Library for generating docs: https://github.com/TiLied/GenDocsLib |
137 | | -- Library for generating csharp: https://github.com/TiLied/GenCSharpLib |
138 | | - |
139 | | -CLI for library: https://github.com/TiLied/CSTOJS_CLI |
140 | | - |
141 | | -VS Code Extension using CLI: https://github.com/TiLied/CSTOJS_VSCode_Ext |
142 | | - |
143 | | -VS Extension using CLI: https://github.com/TiLied/CSTOJS_VS_Ext |
144 | | - |
145 | | -Website/documentation: https://github.com/TiLied/CSTOJS_Pages |
146 | | -- Blazor WebAssembly: https://github.com/TiLied/CSTOJS_BWA |
147 | | - |
148 | | -## Project includes |
149 | | -[Microsoft CodeAnalysis CSharp](https://www.nuget.org/packages/Microsoft.CodeAnalysis.CSharp/) Core nuget package. |
150 | | - |
151 | | -[MDN-content](https://github.com/mdn/content) for JS api. |
152 | | - |
153 | | - |
154 | | -<VersionPrefix>0.0.09</VersionPrefix> |
| 36 | +- Dotnet tool/CLI for a library: https://github.com/TiLied/CSTOJS_CLI |
| 37 | +- Library for generating various stuff: https://github.com/TiLied/CSTOJS_GenLib |
| 38 | +- Website/documentation: https://github.com/TiLied/CSTOJS_Pages |
| 39 | +- Blazor WebAssembly app: https://github.com/TiLied/CSTOJS_BWA |
0 commit comments