|
| 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