Skip to content

Commit 8c88d9c

Browse files
feat: add code generator for typescript to csharp
1 parent b10dfc0 commit 8c88d9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+20279
-2
lines changed

.github/workflows/package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ jobs:
4040
- name: Package Nightly Nuget 📦
4141
run: |
4242
SUFFIX=`date "+%y%m%d%H%M%S"`
43-
dotnet build src/CssInCSharp.csproj
4443
dotnet pack src/CssInCSharp.csproj /p:PackageVersion=$Version-nightly-${SUFFIX} -c Release -o publish
44+
dotnet pack generators/CssInCSharp.CommandLine/CssInCSharp.CommandLine.csproj /p:PackageVersion=$Version-nightly-${SUFFIX} -c Release -o publish
4545
4646
- name: Publish to Nuget ✔
4747
run: |

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ jobs:
4141
run: |
4242
VERSION=`git describe --tags`
4343
echo "Publishing Version: ${VERSION}"
44-
dotnet build src/CssInCSharp.csproj
4544
dotnet pack src/CssInCSharp.csproj /p:PackageVersion=$VERSION -c Release -o publish
45+
dotnet pack generators/CssInCSharp.CommandLine/CssInCSharp.CommandLine.csproj /p:PackageVersion=$VERSION -c Release -o publish
4646
dotnet nuget push publish/*.nupkg -s https://api.nuget.org/v3/index.json -k $NUGET_API_KEY --skip-duplicate

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,11 @@ AMD Ryzen 7 5700G with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
8383
| CreateCss | .NET 7.0 | .NET 7.0 | 50.61 μs | 0.999 μs | 2.062 μs | 0.87 | 0.05 | 17.6392 | 1.0376 | 144.42 KB | 0.96 |
8484
| CreateCss | .NET 8.0 | .NET 8.0 | 37.73 μs | 0.748 μs | 1.642 μs | 0.65 | 0.03 | 17.6392 | 0.9155 | 144.3 KB | 0.96 |
8585

86+
87+
## Special Thanks
88+
89+
[cssinjs](https://github.com/ant-design/cssinjs): Component level cssinjs solution used in ant.design.
90+
[stylis](https://github.com/thysultan/stylis): A Light–weight CSS Preprocessor.
91+
[csstype](https://github.com/frenic/csstype): TypeScript and Flow definitions for CSS.
92+
[tinycolor](https://github.com/scttcper/tinycolor): A small library for color manipulation and conversion.
93+
[TypeScriptAST](https://github.com/ToCSharp/TypeScriptAST): .NET port of TypeScript parser.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Zu.TypeScript.TsTypes;
6+
7+
namespace Zu.TypeScript.Change
8+
{
9+
public class ChangeAST
10+
{
11+
private readonly ICollection<NodeChangeItem> _nodeChangeItems;
12+
13+
public ChangeAST(ICollection<NodeChangeItem> changeItems = null)
14+
{
15+
_nodeChangeItems = changeItems ?? new List<NodeChangeItem>();
16+
}
17+
18+
public static string Change(string source, IEnumerable<NodeChangeItem> changeItems)
19+
{
20+
var changes = changeItems.OrderBy(v => v.Node.Pos).ThenBy(v2 => v2.ChangeType);
21+
var sb = new StringBuilder();
22+
var pos = 0;
23+
foreach (var ch in changes)
24+
{
25+
if (ch.Node.Pos == null) throw new NullReferenceException("Node.Pos");
26+
switch (ch.ChangeType)
27+
{
28+
case NodeChangeType.InsertBefore:
29+
if (ch.Node.Pos > pos) sb.Append(source.Substring(pos, (int) ch.Node.Pos - pos));
30+
sb.Append(ch.NewValue);
31+
pos = (int) ch.Node.Pos;
32+
break;
33+
case NodeChangeType.Change:
34+
if (ch.Node.Pos > pos) sb.Append(source.Substring(pos, (int) ch.Node.Pos - pos));
35+
sb.Append(ch.NewValue);
36+
if (ch.Node.End != null) pos = (int) ch.Node.End;
37+
else throw new NullReferenceException("Node.End");
38+
break;
39+
case NodeChangeType.Delete:
40+
if (ch.Node.Pos > pos) sb.Append(source.Substring(pos, (int) ch.Node.Pos - pos));
41+
if (ch.Node.End != null) pos = (int) ch.Node.End + 1;
42+
break;
43+
case NodeChangeType.InsertAfter:
44+
if (ch.Node.End > pos) sb.Append(source.Substring(pos, (int) ch.Node.End - pos));
45+
sb.Append(ch.NewValue);
46+
if (ch.Node.End != null) pos = (int) ch.Node.End;
47+
break;
48+
default:
49+
throw new ArgumentOutOfRangeException();
50+
}
51+
}
52+
if (pos < source.Length) sb.Append(source.Substring(pos));
53+
var newSource = sb.ToString();
54+
55+
return newSource;
56+
}
57+
58+
public string GetChangedSource(string baseSource)
59+
{
60+
return Change(baseSource, _nodeChangeItems);
61+
}
62+
63+
public void ChangeNode(INode node, string newValue)
64+
{
65+
if (_nodeChangeItems.Any(v => v.Node == node &&
66+
(v.ChangeType == NodeChangeType.Change ||
67+
v.ChangeType == NodeChangeType.Delete)))
68+
throw new Exception("ChangeItems already have this node. Delete first");
69+
if (_nodeChangeItems.Any(v => v.Node.Pos < node.Pos && v.Node.End > node.Pos))
70+
throw new Exception("ChangeItems already have node that contains this node. Delete first");
71+
72+
if (newValue != node.GetTextWithComments())
73+
{
74+
var nodeCh = new NodeChangeItem {ChangeType = NodeChangeType.Change, Node = node, NewValue = newValue};
75+
_nodeChangeItems.Add(nodeCh);
76+
}
77+
else
78+
{
79+
throw new Exception("Same value");
80+
}
81+
}
82+
83+
public void InsertBefore(INode node, string newValue)
84+
{
85+
if (node != null)
86+
{
87+
//if (_nodeChangeItems.Any(v => v.Node.Pos < node.Pos && v.Node.End > node.Pos))
88+
// throw new Exception("ChangeItems already have node that contains this node. Delete first");
89+
90+
var nodeCh = new NodeChangeItem
91+
{
92+
ChangeType = NodeChangeType.InsertBefore,
93+
Node = node,
94+
NewValue = newValue
95+
};
96+
_nodeChangeItems.Add(nodeCh);
97+
}
98+
}
99+
100+
public void InsertAfter(INode node, string newValue)
101+
{
102+
if (node != null)
103+
{
104+
//if (_nodeChangeItems.Any(v => v.Node.Pos < node.Pos && v.Node.End > node.Pos))
105+
// throw new Exception("ChangeItems already have node that contains this node. Delete first");
106+
107+
var nodeCh = new NodeChangeItem
108+
{
109+
ChangeType = NodeChangeType.InsertAfter,
110+
Node = node,
111+
NewValue = newValue
112+
};
113+
_nodeChangeItems.Add(nodeCh);
114+
}
115+
}
116+
117+
public void Delete(INode node)
118+
{
119+
if (node != null)
120+
{
121+
if (_nodeChangeItems.Any(v => v.Node.Pos < node.Pos && v.Node.End > node.Pos))
122+
throw new Exception("ChangeItems already have node that contains this node. Delete first");
123+
124+
var nodeCh = new NodeChangeItem {ChangeType = NodeChangeType.Delete, Node = node};
125+
_nodeChangeItems.Add(nodeCh);
126+
}
127+
}
128+
}
129+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Zu.TypeScript.TsTypes;
2+
3+
namespace Zu.TypeScript.Change
4+
{
5+
public class NodeChangeItem
6+
{
7+
public NodeChangeType ChangeType { get; set; }
8+
9+
public INode Node { get; set; }
10+
//public int Pos { get; set; }
11+
12+
//public int End { get; set; }
13+
public string NewValue { get; set; }
14+
15+
private string NewValueSmall => NewValue == null
16+
? ""
17+
: NewValue.Length > 20
18+
? NewValue.Substring(0, 18) + $"..({NewValue.Length})"
19+
: NewValue;
20+
21+
public override string ToString()
22+
{
23+
if (ChangeType == NodeChangeType.Delete) return $"{ChangeType} {Node}.";
24+
return $"{ChangeType} {Node}. NewValue = \"{NewValueSmall}\"";
25+
}
26+
}
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Zu.TypeScript.Change
2+
{
3+
public enum NodeChangeType
4+
{
5+
InsertBefore,
6+
Change,
7+
Delete,
8+
InsertAfter
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CssInCSharp.Ast.TypeScript is based TypeScriptAST project hosted on https://github.com/ToCSharp/TypeScriptAST distributed with Apache 2.0 license.

0 commit comments

Comments
 (0)