Skip to content

Commit 162595f

Browse files
committed
clean rewrite
1 parent 2f8c742 commit 162595f

29 files changed

+729
-372
lines changed

.editorconfig

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ indent_style = space
1515
indent_size = 4
1616
insert_final_newline = true
1717
trim_trailing_whitespace = true
18+
max_line_length=100
1819

1920
#########################
2021
# File Extension Settings

src/LazyCoder/CSharp/CsClass.cs

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

44
namespace LazyCoder.CSharp
55
{
6-
public class CsClass: CsType
6+
public class CsClass: CsDeclaration
77
{
88
public IEnumerable<CsClassMember> Members { get; set; } = Array.Empty<CsClassMember>();
99
public IEnumerable<CsType> Generics { get; set; } = Array.Empty<CsType>();

src/LazyCoder/CSharp/CsDeclaration.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace LazyCoder.CSharp
5+
{
6+
public abstract class CsDeclaration
7+
{
8+
public string Name { get; set; }
9+
public string Namespace { get; set; }
10+
public IEnumerable<CsAttribute> Attributes { get; set; } = Array.Empty<CsAttribute>();
11+
public CsType CsType { get; set; }
12+
}
13+
}

src/LazyCoder/CSharp/CsEnum.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace LazyCoder.CSharp
44
{
5-
public class CsEnum: CsType
5+
public class CsEnum: CsDeclaration
66
{
77
public IEnumerable<CsEnumValue> Values { get; set; }
88
}

src/LazyCoder/CSharp/CsInterface.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33

44
namespace LazyCoder.CSharp
55
{
6-
public class CsInterface: CsType
6+
public class CsInterface: CsDeclaration
77
{
88
public IEnumerable<CsClassMember> Members { get; set; } = Array.Empty<CsClassMember>();
99
public IEnumerable<CsType> Generics { get; set; } = Array.Empty<CsType>();
1010
}
11-
}
11+
12+
public class CsStruct: CsDeclaration
13+
{
14+
public IEnumerable<CsClassMember> Members { get; set; } = Array.Empty<CsClassMember>();
15+
}
16+
}

src/LazyCoder/CSharp/CsSimpleType.cs

-6
This file was deleted.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace LazyCoder.CSharp
2+
{
3+
public class CsStructMember
4+
{
5+
public string Name { get; set; }
6+
}
7+
}

src/LazyCoder/CSharp/CsType.cs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
using System;
2-
using System.Collections.Generic;
32

43
namespace LazyCoder.CSharp
54
{
6-
public abstract class CsType
5+
public class CsType
76
{
8-
public string Name { get; set; }
9-
public string Namespace { get; set; }
10-
public IEnumerable<CsAttribute> Attributes { get; set; } = Array.Empty<CsAttribute>();
11-
public Type OriginalType { get; set; }
7+
public CsType(Type originalType)
8+
{
9+
Name = originalType.Name;
10+
Namespace = originalType.Namespace;
11+
OriginalType = originalType;
12+
}
13+
14+
public string Name { get; }
15+
public string Namespace { get; }
16+
public Type OriginalType { get; }
17+
18+
public override string ToString()
19+
{
20+
return Name;
21+
}
1222
}
1323
}

src/LazyCoder/Converter.cs

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using LazyCoder.CSharp;
6+
using LazyCoder.Typescript;
7+
using LazyCoder.Writers;
8+
9+
namespace LazyCoder
10+
{
11+
public static class Converter
12+
{
13+
private static int depth;
14+
15+
public static TsFile[] Convert(Type[] types,
16+
ICoder[] coders)
17+
{
18+
var csDeclarations = CsDeclarationFactory.Create(types).ToArray();
19+
var tsFiles = coders.SelectMany(coder => coder.Rewrite(csDeclarations))
20+
.ToArray();
21+
var resolutionContext = new ResolutionContext();
22+
var tsFilesToWrite = EnsureDependencies(tsFiles, resolutionContext);
23+
return tsFilesToWrite.Concat(resolutionContext.DependencyTsFiles).ToArray();
24+
}
25+
26+
public static void WriteFile(string outputDirectory,
27+
TsFile tsFile)
28+
{
29+
var directory = Path.Combine(Path.GetFullPath(outputDirectory), tsFile.Directory);
30+
Directory.CreateDirectory(directory);
31+
var writerContext = new WriterContext();
32+
writerContext.Write(tsFile);
33+
var content = writerContext.GetResult();
34+
File.WriteAllText(Path.Combine(directory, tsFile.Name + ".ts"), content);
35+
}
36+
37+
private static TsFile[] EnsureDependencies(TsFile[] tsFiles,
38+
ResolutionContext
39+
baseResolutionContext)
40+
{
41+
depth++;
42+
var resolutionContext = baseResolutionContext.Add(tsFiles.SelectMany(GetExports));
43+
foreach (var tsFile in tsFiles)
44+
{
45+
var dependencies = GetDependencies(tsFile);
46+
Console.Out.WriteLine(new string('\t', depth) + $"Resolve {tsFile.Name}");
47+
var imports = Resolve(dependencies, resolutionContext);
48+
tsFile.Imports = tsFile.Imports.Concat(imports);
49+
}
50+
51+
depth--;
52+
return tsFiles.ToArray();
53+
}
54+
55+
56+
private static Export[] GetExports(TsFile tsFile)
57+
{
58+
return tsFile.Declarations
59+
.SelectMany(GetExports)
60+
.ToArray();
61+
62+
IEnumerable<Export> GetExports(TsDeclaration tsDeclaration)
63+
{
64+
switch (tsDeclaration)
65+
{
66+
case TsClass _:
67+
case TsInterface _:
68+
case TsEnum _:
69+
return new[] { new Export(tsDeclaration, tsFile) };
70+
case TsFunction _:
71+
return Array.Empty<Export>();
72+
case TsNamespace tsNamespace:
73+
return tsNamespace.Declarations.SelectMany(GetExports);
74+
default:
75+
throw new ArgumentOutOfRangeException(nameof(tsDeclaration),
76+
tsDeclaration.GetType().Name,
77+
null);
78+
}
79+
}
80+
}
81+
82+
private static Dependency[] GetDependencies(TsFile tsFile)
83+
{
84+
var exports = GetExports(tsFile);
85+
return tsFile.Declarations
86+
.SelectMany(DependencyFinder.Find)
87+
.Where(x => !exports.Select(y => y.CsType.OriginalType)
88+
.Contains(x.OriginalType))
89+
.Select(x => new Dependency(x, tsFile))
90+
.DistinctBy(x => x.CsType.OriginalType)
91+
.ToArray();
92+
}
93+
94+
private static TsImport[] Resolve(Dependency[] dependencies,
95+
ResolutionContext resolutionContext)
96+
{
97+
return dependencies
98+
.Select(d => new { Dependency = d, Export = GetExportFor(d) })
99+
.Select(x => new TsImport
100+
{
101+
Named = new[] { x.Export.Name },
102+
Path = Helpers.GetPathFromAToB(x.Dependency.Path,
103+
x.Export.Path)
104+
+ "/" + x.Export.Name
105+
})
106+
.ToArray();
107+
108+
Export GetExportFor(Dependency dependency)
109+
{
110+
Console.Out.WriteLine(new string('\t', depth) + $"GetExportFor {dependency}");
111+
var export = resolutionContext.GetExportFor(dependency);
112+
if (export != null)
113+
{
114+
return export;
115+
}
116+
117+
var csDeclaration = CsDeclarationFactory.Create(dependency.CsType.OriginalType);
118+
var tsFile = DefaultCoder.Rewrite(csDeclaration);
119+
var tsFiles = EnsureDependencies(new[] { tsFile }, resolutionContext);
120+
resolutionContext.AddFiles(tsFiles);
121+
return resolutionContext.GetExportFor(dependency);
122+
}
123+
}
124+
125+
private class ResolutionContext
126+
{
127+
private readonly List<Export> exports = new List<Export>();
128+
129+
public List<TsFile> DependencyTsFiles { get; } = new List<TsFile>();
130+
131+
public Export GetExportFor(Dependency dependency)
132+
{
133+
return exports.SingleOrDefault(e => e.CsType.OriginalType ==
134+
dependency.CsType.OriginalType);
135+
}
136+
137+
public ResolutionContext Add(IEnumerable<Export> newExports)
138+
{
139+
exports.AddRange(newExports);
140+
return this;
141+
}
142+
143+
public void AddFiles(IEnumerable<TsFile> tsFiles)
144+
{
145+
DependencyTsFiles.AddRange(tsFiles);
146+
}
147+
}
148+
149+
private class Export
150+
{
151+
public Export(TsDeclaration tsDeclaration,
152+
TsFile tsFile)
153+
{
154+
Path = DirectoryToPath(tsFile.Directory);
155+
CsType = tsDeclaration.CsType;
156+
Name = tsDeclaration.Name;
157+
}
158+
159+
public string Name { get; }
160+
public CsType CsType { get; }
161+
public string[] Path { get; }
162+
163+
public override string ToString()
164+
{
165+
return Name;
166+
}
167+
}
168+
169+
private class Dependency
170+
{
171+
public Dependency(CsType csType,
172+
TsFile tsFile)
173+
{
174+
CsType = csType;
175+
Path = DirectoryToPath(tsFile.Directory);
176+
}
177+
178+
public CsType CsType { get; }
179+
public string[] Path { get; }
180+
181+
public override string ToString()
182+
{
183+
return CsType.ToString();
184+
}
185+
}
186+
187+
private static string[] DirectoryToPath(string directory)
188+
{
189+
return string.IsNullOrEmpty(directory) || directory == "."
190+
? Array.Empty<string>()
191+
: directory.Split(Path.DirectorySeparatorChar);
192+
}
193+
}
194+
}

0 commit comments

Comments
 (0)