Skip to content

Commit 69d1bd1

Browse files
committed
add default class
1 parent 8a01856 commit 69d1bd1

File tree

5 files changed

+117
-40
lines changed

5 files changed

+117
-40
lines changed

generators/CssInCSharp.CommandLine/Configuration.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using CssInCSharp.Generator;
1+
using System.Text.RegularExpressions;
2+
using CssInCSharp.Generator;
3+
using CssInCSharp.Generator.Extensions;
24

35
namespace CssInCSharp.CommandLine
46
{
@@ -25,5 +27,41 @@ internal class IncludeItem
2527
public string? Src { get; set; }
2628
public string? Dest { get; set; }
2729
public CSharpOptions? CsOptions { get; set; }
30+
31+
public IncludeItem Update()
32+
{
33+
if (CsOptions is not { DefaultClassName: not null }) return this;
34+
var match = Regex.Match(CsOptions.DefaultClassName, @"(\{(dir|file):?(\S*)\})\w*");
35+
if (!match.Success) return this;
36+
if (match.Groups.Count < 3) return this;
37+
if (Dest == null) return this;
38+
39+
var replace = match.Groups[1].Value;
40+
var type = match.Groups[2].Value;
41+
var param = match.Groups[3].Value;
42+
43+
string name;
44+
if (type == "dir")
45+
{
46+
var dir = Path.GetDirectoryName(Dest)!;
47+
if (!string.IsNullOrEmpty(param))
48+
{
49+
dir = Path.GetDirectoryName(Path.GetFullPath(Path.Join(Dest, param)));
50+
}
51+
52+
name = Path.GetFileName(dir);
53+
}
54+
else
55+
{
56+
name = Path.GetFileNameWithoutExtension(Dest);
57+
}
58+
59+
if (!string.IsNullOrEmpty(name))
60+
{
61+
CsOptions.DefaultClassName = CsOptions.DefaultClassName.Replace(replace, name);
62+
}
63+
64+
return this;
65+
}
2866
}
2967
}

generators/CssInCSharp.CommandLine/ConvertCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private async Task ExecAsync(string configFile, ConverterType type, string src,
5656
Src = file.FullPath,
5757
Dest = Util.GetDest(file.Dir, file.FullPath, inc.Dest, ".cs"),
5858
CsOptions = inc.CsOptions ?? config.CsOptions
59-
});
59+
}.Update());
6060

6161
foreach (var item in items)
6262
{

generators/CssInCSharp.Generator/Extensions/NodeExtensions.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,17 @@ public static bool IsNodeType<T>(this SyntaxNode node)
2525

2626
public static bool IsMember(this Ts.TsTypes.INode node)
2727
{
28-
/*
29-
* depth 0 is source file
30-
* depth 1 is member declaration
31-
*/
32-
if (node.Depth <= 1) return true;
33-
return false;
34-
35-
// switch (node.Kind)
36-
// {
37-
// case Ts.TsTypes.SyntaxKind.SourceFile:
38-
// case Ts.TsTypes.SyntaxKind.InterfaceDeclaration:
39-
// case Ts.TsTypes.SyntaxKind.VariableDeclaration:
40-
// case Ts.TsTypes.SyntaxKind.VariableStatement:
41-
// case Ts.TsTypes.SyntaxKind.FunctionDeclaration:
42-
// case Ts.TsTypes.SyntaxKind.PropertySignature:
43-
// return true;
44-
// default: return false;
45-
// }
28+
switch (node.Kind)
29+
{
30+
case Ts.TsTypes.SyntaxKind.SourceFile:
31+
case Ts.TsTypes.SyntaxKind.InterfaceDeclaration:
32+
case Ts.TsTypes.SyntaxKind.VariableDeclaration:
33+
case Ts.TsTypes.SyntaxKind.VariableStatement:
34+
case Ts.TsTypes.SyntaxKind.FunctionDeclaration:
35+
case Ts.TsTypes.SyntaxKind.PropertySignature:
36+
return true;
37+
default: return false;
38+
}
4639
}
4740

4841
public static bool IsIndexerProperty(this Ts.TsTypes.INode node)

generators/CssInCSharp.Generator/Extensions/StringExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,13 @@ public static string Purify(this string str)
1212
{
1313
return str.Trim('\'');
1414
}
15+
16+
public static string ToPascalCase(this string input) =>
17+
input switch
18+
{
19+
null => throw new ArgumentNullException(nameof(input)),
20+
"" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
21+
_ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1))
22+
};
1523
}
1624
}

generators/CssInCSharp.Generator/TypeScriptConverter.cs

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ namespace CssInCSharp.Generator
88
{
99
public class CSharpOptions
1010
{
11-
public List<string> Usings { get; set; } = ["System"];
11+
public List<string> Usings { get; set; } = ["System", "CssInCSharp"];
1212
public string Namespace { get; set; } = "CssInCSharp";
1313
public string DefaultReturnType { get; set; } = "object";
1414
public string DefaultParameterType { get; set; } = "object";
1515
public string DefaultFieldType { get; set; } = "object";
16-
public string? DefaultClassName { get; set; }
16+
public string DefaultClassName { get; set; } = "GeneratedStyle";
17+
public bool UsePartialClass { get; set; } = false;
18+
public bool UseStaticMethod { get; set; } = false;
19+
public bool UsePascalCase { get; set; } = false;
1720
}
1821

1922
public class TypeScriptConverter : IConverter
@@ -27,21 +30,27 @@ public TypeScriptConverter(CSharpOptions options = null)
2730

2831
public string Convert(string content, string fileName)
2932
{
30-
var usings = GenerateUsings();
31-
var ast = new Ts.TypeScriptAST(content, fileName);
32-
var member = GenerateMemberDeclaration(ast.RootNode);
33-
return SyntaxFactory.CompilationUnit()
34-
.WithUsings(usings)
35-
.AddMembers(member)
36-
.NormalizeWhitespace()
37-
.ToFullString();
33+
var tsAst = new Ts.TypeScriptAST(content, fileName);
34+
var csAst = Generate(tsAst.RootNode);
35+
return csAst.NormalizeWhitespace().ToFullString();
3836
}
3937

40-
private SyntaxList<UsingDirectiveSyntax> GenerateUsings()
38+
private CompilationUnitSyntax Generate(Ts.TsTypes.INode node)
4139
{
42-
var usings = _options.Usings.Select(x => SyntaxFactory.UsingDirective(
43-
SyntaxFactory.ParseName(x)));
44-
return SyntaxFactory.List<UsingDirectiveSyntax>(usings);
40+
// usings
41+
var usings = SyntaxFactory.List<UsingDirectiveSyntax>(_options.Usings.Select(x => SyntaxFactory.UsingDirective(
42+
SyntaxFactory.ParseName(x))));
43+
44+
// namespace
45+
var @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(_options.Namespace));
46+
47+
// members
48+
var members = GenerateMemberDeclaration(node);
49+
@namespace = @namespace.AddMembers(members);
50+
51+
return SyntaxFactory.CompilationUnit()
52+
.WithUsings(usings)
53+
.AddMembers(@namespace);
4554
}
4655

4756
private MemberDeclarationSyntax? GenerateMemberDeclaration(Ts.TsTypes.INode node)
@@ -154,9 +163,12 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c
154163
}
155164
else
156165
{
157-
var methodDeclaration = SyntaxFactory.MethodDeclaration(
158-
SyntaxFactory.ParseTypeName(returnType), funcName)
159-
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
166+
SyntaxToken[] tokens = _options.UseStaticMethod
167+
? [SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)]
168+
: [SyntaxFactory.Token(SyntaxKind.PublicKeyword)];
169+
var methodDeclaration = SyntaxFactory
170+
.MethodDeclaration(SyntaxFactory.ParseTypeName(returnType), Format(funcName))
171+
.AddModifiers(tokens);
160172
methodDeclaration = methodDeclaration.AddParameterListParameters(parameters);
161173
return methodDeclaration.WithBody(SyntaxFactory.Block(statements));
162174
}
@@ -420,7 +432,7 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c
420432
{
421433
var n = node.AsType<Ts.TsTypes.PropertySignature>();
422434
return SyntaxFactory
423-
.PropertyDeclaration(SyntaxFactory.ParseTypeName("string"), n.IdentifierStr)
435+
.PropertyDeclaration(GetType(n.Type), Format(n.IdentifierStr))
424436
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
425437
.AddAccessorListAccessors
426438
(
@@ -462,7 +474,10 @@ private SyntaxNodeOrList GenerateCSharpAst(Ts.TsTypes.INode node, NodeContext? c
462474
}
463475
case Ts.TsTypes.SyntaxKind.SourceFile:
464476
{
465-
return SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(_options.Namespace));
477+
SyntaxToken[] tokens = _options.UsePartialClass
478+
? [SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.PartialKeyword)]
479+
: [SyntaxFactory.Token(SyntaxKind.PublicKeyword)];
480+
return SyntaxFactory.ClassDeclaration(Format(_options.DefaultClassName)).AddModifiers(tokens);
466481
}
467482
case Ts.TsTypes.SyntaxKind.StringLiteral:
468483
{
@@ -580,7 +595,30 @@ private SyntaxKind GenerateOperatorToken(Ts.TsTypes.INode node)
580595
}
581596
}
582597

583-
static int GetLineNumber(string text, int index)
598+
private TypeSyntax GetType(Ts.TsTypes.INode node)
599+
{
600+
switch (node.Kind)
601+
{
602+
case Ts.TsTypes.SyntaxKind.NumberKeyword:
603+
return SyntaxFactory.ParseTypeName("double");
604+
case Ts.TsTypes.SyntaxKind.StringKeyword:
605+
return SyntaxFactory.ParseTypeName("double");
606+
default:
607+
return SyntaxFactory.ParseTypeName("object");
608+
}
609+
}
610+
611+
private string Format(string text)
612+
{
613+
if (_options.UsePascalCase)
614+
{
615+
return text.ToPascalCase();
616+
}
617+
618+
return text;
619+
}
620+
621+
private static int GetLineNumber(string text, int index)
584622
{
585623
if (index < 0 || index > text.Length)
586624
throw new ArgumentOutOfRangeException(nameof(index), "Index is out of the range of the text.");

0 commit comments

Comments
 (0)