Skip to content

Commit 8c3c1d3

Browse files
committed
csharp ast
1 parent ec67d16 commit 8c3c1d3

30 files changed

+798
-598
lines changed

.editorconfig

+282-282
Large diffs are not rendered by default.

LazyCoder.sln.DotSettings

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
6+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/LazyCoder.Runner/Runner.cs

+59-58
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,59 @@
1-
using System;
2-
using System.IO;
3-
using System.Linq;
4-
using System.Linq.Expressions;
5-
using System.Reflection;
6-
using LazyCoder.Runner.Writer;
7-
using LazyCoder.Typescript;
8-
9-
namespace LazyCoder.Runner
10-
{
11-
public class Runner
12-
{
13-
public static int Run(string dll,
14-
string outputDirectory
15-
)
16-
{
17-
dll = Path.GetFullPath(dll);
18-
var loadedTypes = new AssemblyReader().Read(dll);
19-
var coderTypes = GetCoderTypes(dll);
20-
Console.Out.WriteLine("Found: " + string.Join(", ", coderTypes.Select(x => x.Name)));
21-
foreach (var coderType in coderTypes)
22-
{
23-
Console.Out.WriteLine("Start Coder " + coderType.Name);
24-
var ctor = (Func<ICoder>)Expression.Lambda(Expression.New(coderType)).Compile();
25-
var coder = ctor();
26-
var tsFiles = coder.Rewrite(loadedTypes).ToArray();
27-
Console.Out.WriteLine($"Gonna create {tsFiles.Length} files");
28-
29-
Directory.CreateDirectory(Path.GetFullPath(outputDirectory));
30-
foreach (var tsFile in tsFiles) WriteFile(outputDirectory, tsFile);
31-
32-
Console.Out.WriteLine($"Coder {coderType.Name} finished");
33-
}
34-
35-
return 0;
36-
}
37-
38-
private static void WriteFile(string outputDirectory,
39-
TsFile tsFile)
40-
{
41-
var directory = Path.Combine(Path.GetFullPath(outputDirectory), tsFile.Directory);
42-
Directory.CreateDirectory(directory);
43-
var writerContext = new WriterContext();
44-
writerContext.Write(tsFile);
45-
var content = writerContext.GetResult();
46-
File.WriteAllText(Path.Combine(directory, tsFile.Name + ".ts"), content);
47-
}
48-
49-
private static Type[] GetCoderTypes(string dll)
50-
{
51-
return Assembly.LoadFile(dll)
52-
.GetTypes()
53-
.Where(x => x.GetInterfaces()
54-
.Any(y => y == typeof(ICoder)))
55-
.ToArray();
56-
}
57-
}
58-
}
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Reflection;
6+
using LazyCoder.Runner.Writer;
7+
using LazyCoder.Typescript;
8+
9+
namespace LazyCoder.Runner
10+
{
11+
public class Runner
12+
{
13+
public static int Run(string dll,
14+
string outputDirectory
15+
)
16+
{
17+
dll = Path.GetFullPath(dll);
18+
var loadedTypes = new AssemblyReader().Read(dll);
19+
var csAstTypes = CsAstFactory.Create(loadedTypes);
20+
var coderTypes = GetCoderTypes(dll);
21+
Console.Out.WriteLine("Found: " + string.Join(", ", coderTypes.Select(x => x.Name)));
22+
foreach (var coderType in coderTypes)
23+
{
24+
Console.Out.WriteLine("Start Coder " + coderType.Name);
25+
var ctor = (Func<ICoder>)Expression.Lambda(Expression.New(coderType)).Compile();
26+
var coder = ctor();
27+
var tsFiles = coder.Rewrite(csAstTypes).ToArray();
28+
Console.Out.WriteLine($"Gonna create {tsFiles.Length} files");
29+
30+
Directory.CreateDirectory(Path.GetFullPath(outputDirectory));
31+
foreach (var tsFile in tsFiles) WriteFile(outputDirectory, tsFile);
32+
33+
Console.Out.WriteLine($"Coder {coderType.Name} finished");
34+
}
35+
36+
return 0;
37+
}
38+
39+
private static void WriteFile(string outputDirectory,
40+
TsFile tsFile)
41+
{
42+
var directory = Path.Combine(Path.GetFullPath(outputDirectory), tsFile.Directory);
43+
Directory.CreateDirectory(directory);
44+
var writerContext = new WriterContext();
45+
writerContext.Write(tsFile);
46+
var content = writerContext.GetResult();
47+
File.WriteAllText(Path.Combine(directory, tsFile.Name + ".ts"), content);
48+
}
49+
50+
private static Type[] GetCoderTypes(string dll)
51+
{
52+
return Assembly.LoadFile(dll)
53+
.GetTypes()
54+
.Where(x => x.GetInterfaces()
55+
.Any(y => y == typeof(ICoder)))
56+
.ToArray();
57+
}
58+
}
59+
}

src/LazyCoder.Runner/Writer/TsNamespaceWriter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public void Write(IKeyboard keyboard,
99
TsNamespace tsNamespace)
1010
{
1111
keyboard.Write(tsNamespace.ExportKind)
12-
.Type("namespace ", tsNamespace.Name, " ");
12+
.Type("namespace ", tsNamespace.Name.Value, " ");
1313
using (keyboard.Block())
1414
{
1515
var declarations = tsNamespace.Declarations.ToArray();
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace LazyCoder.CSharp
2+
{
3+
public enum CsAccessModifier
4+
{
5+
Public,
6+
Private,
7+
Protected,
8+
Internal
9+
}
10+
}

src/LazyCoder/CSharp/CsClass.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace LazyCoder.CSharp
5+
{
6+
public class CsClass: CsType
7+
{
8+
public IEnumerable<CsClassMember> Members { get; set; } = Array.Empty<CsClassMember>();
9+
public IEnumerable<CsType> Generics { get; set; } = Array.Empty<CsType>();
10+
}
11+
}

src/LazyCoder/CSharp/CsClassMember.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace LazyCoder.CSharp
2+
{
3+
public abstract class CsClassMember
4+
{
5+
public string Name { get; set; }
6+
public bool IsStatic { get; set; }
7+
public CsAccessModifier AccessModifier { get; set; }
8+
}
9+
}

src/LazyCoder/CSharp/CsEnum.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
3+
namespace LazyCoder.CSharp
4+
{
5+
public class CsEnum : CsType
6+
{
7+
public IEnumerable<CsEnumValue> Values { get; set; }
8+
}
9+
}

src/LazyCoder/CSharp/CsEnumValue.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LazyCoder.CSharp
2+
{
3+
public class CsEnumValue
4+
{
5+
public string Name { get; set; }
6+
public int Value { get; set; }
7+
}
8+
}

src/LazyCoder/CSharp/CsMethod.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace LazyCoder.CSharp
4+
{
5+
public class CsMethod: CsClassMember
6+
{
7+
public CsType ReturnType { get; set; }
8+
public IEnumerable<CsMethodParameter> Parameters { get; set; }
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LazyCoder.CSharp
2+
{
3+
public class CsMethodParameter
4+
{
5+
public string Name { get; set; }
6+
public CsType Type { get; set; }
7+
}
8+
}

src/LazyCoder/CSharp/CsSimpleType.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace LazyCoder.CSharp
2+
{
3+
public class CsSimpleType: CsType
4+
{
5+
}
6+
}

src/LazyCoder/CSharp/CsType.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace LazyCoder.CSharp
4+
{
5+
public abstract class CsType
6+
{
7+
public string Name { get; set; }
8+
public string Namespace { get; set; }
9+
public Type OriginalType { get; set; }
10+
}
11+
}

src/LazyCoder/CsAstFactory.cs

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using LazyCoder.CSharp;
6+
7+
namespace LazyCoder
8+
{
9+
//todo cyclic ast
10+
public static class CsAstFactory
11+
{
12+
public static IEnumerable<CsType> Create(IEnumerable<Type> types)
13+
{
14+
return types.Select(Create);
15+
}
16+
17+
public static CsType Create(Type type)
18+
{
19+
if (type.IsEnum)
20+
{
21+
return new CsEnum
22+
{
23+
Name = type.Name,
24+
Namespace = type.Namespace,
25+
OriginalType = type,
26+
Values = type.GetFields()
27+
.Where(f => f.Name != "value__")
28+
.Select(y => new CsEnumValue
29+
{
30+
Name = y.Name,
31+
Value = (int)y.GetRawConstantValue()
32+
})
33+
};
34+
}
35+
36+
if (type.IsClass)
37+
{
38+
return new CsClass
39+
{
40+
Name = type.Name,
41+
Namespace = type.Namespace,
42+
OriginalType = type,
43+
Members = type.GetMembers()
44+
.Where(m => !typeof(object)
45+
.GetMembers()
46+
.Select(me => me.Name)
47+
.Contains(m.Name))
48+
.Select(Create)
49+
};
50+
}
51+
52+
if (type.IsValueType)
53+
{
54+
return new CsSimpleType
55+
{
56+
OriginalType = type
57+
};
58+
}
59+
60+
throw new Exception($"Type {type.Name} is unsupported");
61+
}
62+
63+
private static CsClassMember Create(MemberInfo memberInfo)
64+
{
65+
switch (memberInfo)
66+
{
67+
case MethodInfo methodInfo:
68+
return Create(methodInfo);
69+
default:
70+
throw new ArgumentOutOfRangeException(nameof(memberInfo), memberInfo, null);
71+
}
72+
}
73+
74+
private static CsMethod Create(MethodInfo methodInfo)
75+
{
76+
return new CsMethod
77+
{
78+
Name = methodInfo.Name,
79+
IsStatic = methodInfo.IsStatic,
80+
AccessModifier = GetAccessModifier(methodInfo.IsPrivate,
81+
methodInfo.IsFamily,
82+
methodInfo.IsPublic,
83+
methodInfo.IsAssembly),
84+
ReturnType = Create(methodInfo.ReturnType),
85+
Parameters = methodInfo.GetParameters()
86+
.Select(Create)
87+
};
88+
}
89+
90+
private static CsMethodParameter Create(ParameterInfo x)
91+
{
92+
return new CsMethodParameter
93+
{
94+
Name = x.Name,
95+
Type = Create(x.ParameterType)
96+
};
97+
}
98+
99+
private static CsAccessModifier GetAccessModifier(bool isPrivate, bool isFamily, bool isPublic, bool isAssembly)
100+
{
101+
if (isPrivate)
102+
return CsAccessModifier.Private;
103+
if (isFamily)
104+
return CsAccessModifier.Protected;
105+
if (isAssembly)
106+
return CsAccessModifier.Internal;
107+
if (isPublic)
108+
return CsAccessModifier.Public;
109+
throw new ArgumentException("Unsupported access modifier");
110+
}
111+
}
112+
}

src/LazyCoder/ICoder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System;
21
using System.Collections.Generic;
2+
using LazyCoder.CSharp;
33
using LazyCoder.Typescript;
44

55
namespace LazyCoder
66
{
77
public interface ICoder
88
{
9-
IEnumerable<TsFile> Rewrite(Type[] types);
9+
IEnumerable<TsFile> Rewrite(IEnumerable<CsType> types);
1010
}
1111
}

src/LazyCoder/Typescript/ITsDeclaration.cs

-6
This file was deleted.

src/LazyCoder/Typescript/TsClass.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33

44
namespace LazyCoder.Typescript
55
{
6-
public class TsClass : ITsDeclaration
6+
public class TsClass : TsDeclaration
77
{
8-
public TsName Name { get; set; }
9-
public TsExportKind ExportKind { get; set; }
108
public IEnumerable<TsName> Base { get; set; } = Array.Empty<TsName>();
119
}
1210
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LazyCoder.Typescript
2+
{
3+
public abstract class TsDeclaration
4+
{
5+
public TsName Name { get; set; }
6+
public TsExportKind ExportKind { get; set; }
7+
}
8+
}

src/LazyCoder/Typescript/TsEnum.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33

44
namespace LazyCoder.Typescript
55
{
6-
public class TsEnum : ITsDeclaration
6+
public class TsEnum : TsDeclaration
77
{
8-
public TsExportKind ExportKind { get; set; }
9-
public TsName Name { get; set; }
108
public IEnumerable<TsEnumValue> Values { get; set; } = Array.Empty<TsEnumValue>();
119
}
1210
}

0 commit comments

Comments
 (0)