Skip to content

Commit 4dceb02

Browse files
committed
remove Mono.Cecil + another test coder
1 parent fe50c00 commit 4dceb02

File tree

13 files changed

+125
-66
lines changed

13 files changed

+125
-66
lines changed
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
using System.IO;
2-
using Mono.Cecil;
1+
using System;
2+
using System.Reflection;
33

44
namespace LazyCoder.Runner
55
{
66
public class AssemblyReader
77
{
8-
public ModuleDefinition Read(string dllPath)
8+
public Type[] Read(string dllPath)
99
{
10-
var folder = Path.GetDirectoryName(dllPath);
11-
var readerParameters = new ReaderParameters
12-
{
13-
ReadingMode = ReadingMode.Deferred,
14-
AssemblyResolver = new AssemblyResolver(folder)
15-
};
16-
return ModuleDefinition.ReadModule(dllPath, readerParameters);
10+
return Assembly.LoadFile(dllPath).GetTypes();
1711
}
1812
}
1913
}

src/LazyCoder.Runner/AssemblyResolver.cs

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

src/LazyCoder.Runner/Runner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ string outputDirectory
1515
)
1616
{
1717
dll = Path.GetFullPath(dll);
18-
var moduleDefinition = new AssemblyReader().Read(dll);
18+
var loadedTypes = new AssemblyReader().Read(dll);
1919
var coderTypes = GetCoderTypes(dll);
2020
Console.Out.WriteLine("Found: " + string.Join(", ", coderTypes.Select(x => x.Name)));
2121
foreach (var coderType in coderTypes)
2222
{
2323
Console.Out.WriteLine("Start Coder " + coderType.Name);
2424
var ctor = (Func<ICoder>)Expression.Lambda(Expression.New(coderType)).Compile();
2525
var coder = ctor();
26-
var tsFiles = coder.Rewrite(moduleDefinition).ToArray();
26+
var tsFiles = coder.Rewrite(loadedTypes).ToArray();
2727
Console.Out.WriteLine($"Gonna create {tsFiles.Length} files");
2828

2929
Directory.CreateDirectory(Path.GetFullPath(outputDirectory));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export namespace SomeApi {
2+
export function SomeAction(value: string): number {
3+
// some body
4+
}
5+
6+
export function SomeOtherAction(value: number): boolean {
7+
// some body
8+
}
9+
}

src/LazyCoder.Runner/output/LazyCoder/TestDll/SomeEnum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export enum SomeEnum {
22
FirstValue = 0,
33
SecondValue = 1,
44
ThirdValue = 2,
5-
}
5+
}

src/LazyCoder/ICoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
using System;
12
using System.Collections.Generic;
23
using LazyCoder.Typescript;
3-
using Mono.Cecil;
44

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

src/LazyCoder/LazyCoder.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,4 @@
55
<LangVersion>7.3</LangVersion>
66
</PropertyGroup>
77

8-
<ItemGroup>
9-
<PackageReference Include="Mono.Cecil" Version="0.10.1" />
10-
</ItemGroup>
11-
128
</Project>

src/LazyCoder/Typescript/TsFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class TsFunction : ITsDeclaration
77
{
88
public TsExportKind ExportKind { get; set; }
99
public TsName Name { get; set; }
10-
public TsName ReturnType { get; set; }
10+
public TsType ReturnType { get; set; }
1111
public IEnumerable<TsFunctionParameter> Parameters { get; set; } = Array.Empty<TsFunctionParameter>();
1212
public string Body { get; set; }
1313
}

src/LazyCoder/Typescript/TsFunctionParameter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ namespace LazyCoder.Typescript
33
public class TsFunctionParameter
44
{
55
public string Name { get; set; }
6-
public TsName Type { get; set; }
6+
public TsType Type { get; set; }
77
}
8-
}
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace LazyCoder.TestDll
2+
{
3+
public class SomeController
4+
{
5+
public int SomeAction(string value)
6+
{
7+
return 0;
8+
}
9+
10+
public bool SomeOtherAction(decimal value)
11+
{
12+
return true;
13+
}
14+
}
15+
}

tests/LazyCoder.TestDll/TestCoder.cs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using LazyCoder.Typescript;
5-
using Mono.Cecil;
66

77
namespace LazyCoder.TestDll
88
{
99
public class TestCoder : ICoder
1010
{
11-
public IEnumerable<TsFile> Rewrite(ModuleDefinition assembly)
11+
public IEnumerable<TsFile> Rewrite(Type[] types)
1212
{
13-
return assembly.Types
14-
.Where(x => x.IsEnum)
15-
.Select(x => new TsFile
16-
{
17-
Name = x.Name,
18-
Directory = x.Namespace.Replace('.', Path.DirectorySeparatorChar),
19-
Declarations = new[]
20-
{
21-
new TsEnum
22-
{
23-
Name = new TsName
24-
{
25-
Value = x.Name
26-
},
27-
ExportKind = TsExportKind.Named,
28-
Values = x.Fields
29-
.Where(f => f.Name != "value__")
30-
.Select(y => new TsEnumNumberValue
31-
{
32-
Name = y.Name,
33-
Value = (int)y
34-
.Constant
35-
})
36-
}
37-
}
38-
});
13+
return types.Where(x => x.IsEnum)
14+
.Select(x => new TsFile
15+
{
16+
Name = x.Name,
17+
Directory = x.Namespace.Replace('.', Path.DirectorySeparatorChar),
18+
Declarations = new[]
19+
{
20+
new TsEnum
21+
{
22+
Name = new TsName
23+
{
24+
Value = x.Name
25+
},
26+
ExportKind = TsExportKind.Named,
27+
Values = x.GetFields()
28+
.Where(f => f.Name != "value__")
29+
.Select(y => new TsEnumNumberValue
30+
{
31+
Name = y.Name,
32+
Value = (int)y.GetRawConstantValue()
33+
})
34+
}
35+
}
36+
});
3937
}
4038
}
4139
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
using LazyCoder.Typescript;
7+
8+
namespace LazyCoder.TestDll
9+
{
10+
public class TestControllerCoder : ICoder
11+
{
12+
public IEnumerable<TsFile> Rewrite(Type[] types)
13+
{
14+
var controllers = types.Where(x => x.Name.EndsWith("Controller"));
15+
return controllers.Select(RewriteController);
16+
}
17+
18+
private static TsFile RewriteController(Type controllerType)
19+
{
20+
var name = controllerType.Name.Replace("Controller", "Api");
21+
return new TsFile
22+
{
23+
Name = name,
24+
Directory = controllerType.Namespace.Replace('.', Path.DirectorySeparatorChar),
25+
Declarations = new[]
26+
{
27+
new TsNamespace
28+
{
29+
Name = name,
30+
ExportKind = TsExportKind.Named,
31+
Declarations = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
32+
.Where(m => !typeof(object)
33+
.GetMethods()
34+
.Select(me => me.Name)
35+
.Contains(m.Name))
36+
.Select(RewriteMethod)
37+
}
38+
}
39+
};
40+
}
41+
42+
private static TsFunction RewriteMethod(MethodInfo method)
43+
{
44+
return new TsFunction
45+
{
46+
Name = new TsName { Value = method.Name },
47+
ExportKind = TsExportKind.Named,
48+
ReturnType = TsType.From(method.ReturnType),
49+
Parameters = method.GetParameters()
50+
.Select(x => new TsFunctionParameter
51+
{
52+
Name = x.Name,
53+
Type = TsType.From(x.ParameterType)
54+
}),
55+
Body = "// some body"
56+
};
57+
}
58+
}
59+
}

tests/LazyCoder.Tests/TsFunctionWriterTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ public void Simple()
1111
{
1212
var tsName = new TsFunction
1313
{
14-
Name = new TsName {Value = "SomeFunction"},
14+
Name = new TsName { Value = "SomeFunction" },
1515
Parameters = new[]
1616
{
1717
new TsFunctionParameter
1818
{
1919
Name = "firstParameter",
20-
Type = new TsName {Value = "FirstType"}
20+
Type = new TsType { Name = "FirstType" }
2121
},
2222
new TsFunctionParameter
2323
{
2424
Name = "secondParameter",
25-
Type = new TsName {Value = "SecondType"}
25+
Type = new TsType { Name = "SecondType" }
2626
}
2727
},
28-
ReturnType = new TsName {Value = "ReturnType"},
28+
ReturnType = new TsType { Name = "ReturnType" },
2929
Body = $"// not implemented{Environment.NewLine}//todo do"
3030
};
3131
tsName.ShouldBeTranslatedTo("function SomeFunction(firstParameter: FirstType, secondParameter: SecondType): ReturnType {",

0 commit comments

Comments
 (0)