From bb3d9a43d30324b2e2d417c32379bde69f29c9e5 Mon Sep 17 00:00:00 2001 From: Cyril Gandon Date: Thu, 6 Oct 2016 10:11:21 +0200 Subject: [PATCH] Remove the option to export a type to a single file, the granularity of a file is a Namespace --- Nimrod.Console/Options.cs | 3 -- Nimrod.Console/OptionsHandler.cs | 2 +- Nimrod.Test/GeneratorTests.cs | 23 --------- Nimrod.Test/Nimrod.Test.csproj | 1 - Nimrod.Test/ToTypescriptTests.cs | 14 ++--- Nimrod.Test/TupleToTypeScriptTests.cs | 12 ++--- Nimrod.Test/Writers/BaseWriterTests.cs | 18 +------ Nimrod.Test/Writers/ControllerWriterTest.cs | 2 +- Nimrod.Test/Writers/EnumWriterTest.cs | 4 +- Nimrod.Test/Writers/ModelWriterTests.cs | 19 +++---- Nimrod/FileToWrite.cs | 22 ++++++-- Nimrod/Generator.cs | 57 +++++---------------- Nimrod/IOOperations.cs | 4 +- Nimrod/ToTypeScriptOptions.cs | 9 ++-- Nimrod/TypeScriptType.cs | 24 +++------ Nimrod/Writers/ControllerToTypeScript.cs | 27 ++++------ Nimrod/Writers/EnumToTypeScript.cs | 10 ++-- Nimrod/Writers/ModelToTypeScript.cs | 36 ++++--------- Nimrod/Writers/ModuleHelper.cs | 14 ----- Nimrod/Writers/StructToTypeScript.cs | 8 +-- Nimrod/Writers/ToTypeScript.cs | 6 +-- Nimrod/Writers/ToTypeScriptBuildRule.cs | 4 +- Nimrod/Writers/ToTypeScriptBuildRules.cs | 12 ++--- 23 files changed, 106 insertions(+), 225 deletions(-) delete mode 100644 Nimrod.Test/GeneratorTests.cs diff --git a/Nimrod.Console/Options.cs b/Nimrod.Console/Options.cs index a183a48..5c468b2 100644 --- a/Nimrod.Console/Options.cs +++ b/Nimrod.Console/Options.cs @@ -22,9 +22,6 @@ public class Options [Option('o', "output", Required = false, HelpText = "Directory where files will be generated.")] public string OutputPath { get; set; } - [Option('g', "group", Required = false, HelpText = "Group modules by namespace")] - public bool Group { get; set; } - // Assemblies to retrieve, -f --files [OptionList('f', "files", Required = true, Separator = ',', HelpText = "Specify files, separated by a comma. Example --files=bin\\Assembly1.dll,bin\\Assembly2.dll")] public IList Files { get; set; } diff --git a/Nimrod.Console/OptionsHandler.cs b/Nimrod.Console/OptionsHandler.cs index 5aa5ffa..6f17d71 100644 --- a/Nimrod.Console/OptionsHandler.cs +++ b/Nimrod.Console/OptionsHandler.cs @@ -55,7 +55,7 @@ static private void OnOptionsSuccessful(Options options, TraceListener tracer) logger = VoidLogger.Default; } var ioOperations = new IoOperations(new FileSystem(), options.OutputPath, logger); - var generator = new Generator(options.StrictNullCheck, !options.Group); + var generator = new Generator(options.StrictNullCheck); var result = generator.Generate(options.Files, ioOperations); ioOperations.Dump(result.Files); } diff --git a/Nimrod.Test/GeneratorTests.cs b/Nimrod.Test/GeneratorTests.cs deleted file mode 100644 index c9dd7e2..0000000 --- a/Nimrod.Test/GeneratorTests.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Nimrod.Test.ModelExamples; -using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Nimrod.Test -{ - [TestFixture] - public class GeneratorTests - { - [Test] - public void GetFileNameTest() - { - var filename = Generator.GetTypeScriptFilename(typeof(GenericClass)); - - Assert.AreEqual("Nimrod.Test.ModelExamples.GenericClass.ts", filename); - } - - } -} diff --git a/Nimrod.Test/Nimrod.Test.csproj b/Nimrod.Test/Nimrod.Test.csproj index 432c466..8ca1a85 100644 --- a/Nimrod.Test/Nimrod.Test.csproj +++ b/Nimrod.Test/Nimrod.Test.csproj @@ -117,7 +117,6 @@ - diff --git a/Nimrod.Test/ToTypescriptTests.cs b/Nimrod.Test/ToTypescriptTests.cs index 15f2cc2..038a240 100644 --- a/Nimrod.Test/ToTypescriptTests.cs +++ b/Nimrod.Test/ToTypescriptTests.cs @@ -63,23 +63,23 @@ public class Generic1 [Test] public void GetTypescriptType_Generic_Test() { - Assert.AreEqual("Nimrod.Test.Generic | null", typeof(Generic).ToTypeScript().ToString(true, true)); - Assert.AreEqual("Nimrod.Test.Generic | null", typeof(Generic).ToTypeScript().ToString(true, false)); - Assert.AreEqual("Generic | null", typeof(Generic).ToTypeScript().ToString(false, true)); - Assert.AreEqual("Generic | null", typeof(Generic).ToTypeScript().ToString(false, false)); + Assert.AreEqual("Nimrod_Test.Generic | null", typeof(Generic).ToTypeScript().ToString(p => true, true)); + Assert.AreEqual("Nimrod_Test.Generic | null", typeof(Generic).ToTypeScript().ToString(p => true, false)); + Assert.AreEqual("Generic | null", typeof(Generic).ToTypeScript().ToString(p => false, true)); + Assert.AreEqual("Generic | null", typeof(Generic).ToTypeScript().ToString(p => false, false)); } [Test] public void GetTypescriptType_GenericWithNumber_Test() { - Assert.AreEqual("Nimrod.Test.Generic1 | null", typeof(Generic1).ToTypeScript().ToString(true, true)); + Assert.AreEqual("Nimrod_Test.Generic1 | null", typeof(Generic1).ToTypeScript().ToString(p => true, true)); } [Test] public void GetTypescriptType_NonGeneric_Test() { - var actual = typeof(NonGenericClass).ToTypeScript().ToString(true); - Assert.AreEqual("Nimrod.Test.NonGenericClass | null", actual); + var actual = typeof(NonGenericClass).ToTypeScript().ToString(p => true); + Assert.AreEqual("Nimrod_Test.NonGenericClass | null", actual); } [Test] diff --git a/Nimrod.Test/TupleToTypeScriptTests.cs b/Nimrod.Test/TupleToTypeScriptTests.cs index 45e22ae..93c4407 100644 --- a/Nimrod.Test/TupleToTypeScriptTests.cs +++ b/Nimrod.Test/TupleToTypeScriptTests.cs @@ -26,18 +26,18 @@ public class TupleToTypeScriptTests [Test] public void TupleToTypeScriptTests_NameSpaceInclude_Test() { - Assert.AreEqual("{ Item1: SomeClass | null } | null", typeof(Tuple).ToTypeScript().ToString(false)); - Assert.AreEqual("{ Item1: Very.Specific.NameSpace.SomeClass | null } | null", typeof(Tuple).ToTypeScript().ToString(true)); + Assert.AreEqual("{ Item1: SomeClass | null } | null", typeof(Tuple).ToTypeScript().ToString(type => false)); + Assert.AreEqual("{ Item1: Very_Specific_NameSpace.SomeClass | null } | null", typeof(Tuple).ToTypeScript().ToString(type => true)); } [Test] public void TupleToTypeScriptTests_Generic_Test() { - Assert.AreEqual("{ Item1: SomeGenericClass | null } | null", typeof(Tuple>).ToTypeScript().ToString(false, true)); - Assert.AreEqual("{ Item1: Very.Specific.NameSpace.SomeGenericClass | null } | null", typeof(Tuple>).ToTypeScript().ToString(true, true)); + Assert.AreEqual("{ Item1: SomeGenericClass | null } | null", typeof(Tuple>).ToTypeScript().ToString(type => false, true)); + Assert.AreEqual("{ Item1: Very_Specific_NameSpace.SomeGenericClass | null } | null", typeof(Tuple>).ToTypeScript().ToString(type => true, true)); - Assert.AreEqual("{ Item1: SomeGenericClass | null } | null", typeof(Tuple>).ToTypeScript().ToString(false, false)); - Assert.AreEqual("{ Item1: Very.Specific.NameSpace.SomeGenericClass | null } | null", typeof(Tuple>).ToTypeScript().ToString(true, false)); + Assert.AreEqual("{ Item1: SomeGenericClass | null } | null", typeof(Tuple>).ToTypeScript().ToString(type => false, false)); + Assert.AreEqual("{ Item1: Very_Specific_NameSpace.SomeGenericClass | null } | null", typeof(Tuple>).ToTypeScript().ToString(type => true, false)); } [Test] diff --git a/Nimrod.Test/Writers/BaseWriterTests.cs b/Nimrod.Test/Writers/BaseWriterTests.cs index 74aceeb..7ddd87d 100644 --- a/Nimrod.Test/Writers/BaseWriterTests.cs +++ b/Nimrod.Test/Writers/BaseWriterTests.cs @@ -19,25 +19,11 @@ public class GenericWrapper public GenericItem Item { get; set; } } - [Test] - public void WriteModel_WriteImports_GenericWrapper() - { - var lines = ModuleHelper.GetImportLine(typeof(GenericWrapper<>), true); - Assert.AreEqual("import GenericWrapper from './Nimrod.Test.GenericWrapper';", lines); - } - - [Test] - public void WriteModel_WriteImports_GenericItem() - { - var lines = ModuleHelper.GetImportLine(typeof(GenericItem<>), true); - Assert.AreEqual("import GenericItem from './Nimrod.Test.GenericItem';", lines); - } - [Test] public void GetTypescriptType_GenericListContainer() { var genericTypeDefinition = typeof(BarWrapper).GetGenericTypeDefinition().ToTypeScript(); - var writer = new ModelToTypeScript(genericTypeDefinition, true, true); + var writer = new ModelToTypeScript(genericTypeDefinition, true); string ts = writer.GetLines().JoinNewLine(); Assert.IsTrue(ts.Contains("Bars: (T | null)[] | null;")); } @@ -46,7 +32,7 @@ public void GetTypescriptType_GenericListContainer() public void GetTypescriptType_GenericCustomContainer() { var genericTypeDefinition = typeof(Fuzz).GetGenericTypeDefinition().ToTypeScript(); - var writer = new ModelToTypeScript(genericTypeDefinition, true, true); + var writer = new ModelToTypeScript(genericTypeDefinition, true); string ts = writer.GetLines().JoinNewLine(); Assert.IsTrue(ts.Contains("Fuzzs: GenericFoo | null;")); diff --git a/Nimrod.Test/Writers/ControllerWriterTest.cs b/Nimrod.Test/Writers/ControllerWriterTest.cs index 882a477..2930a83 100644 --- a/Nimrod.Test/Writers/ControllerWriterTest.cs +++ b/Nimrod.Test/Writers/ControllerWriterTest.cs @@ -16,7 +16,7 @@ public class ControllerWriterTest [Test] public void Write_SimpleController() { - var writer = new ControllerToTypeScript(typeof(MovieController).ToTypeScript(), false, true); + var writer = new ControllerToTypeScript(typeof(MovieController).ToTypeScript(), false); string ts = writer.GetLines().JoinNewLine(); Assert.IsFalse(ts.Contains("Foo")); } diff --git a/Nimrod.Test/Writers/EnumWriterTest.cs b/Nimrod.Test/Writers/EnumWriterTest.cs index 2256d4f..3af38a3 100644 --- a/Nimrod.Test/Writers/EnumWriterTest.cs +++ b/Nimrod.Test/Writers/EnumWriterTest.cs @@ -23,7 +23,7 @@ public class EnumWriterTest [Test] public void GetTypescriptType_ArrayLike_Test() { - var writer = new EnumToTypeScript(typeof(Fruits).ToTypeScript(), false, true); + var writer = new EnumToTypeScript(typeof(Fruits).ToTypeScript(), false); var lines = writer.GetLines(); string ts = lines.JoinNewLine(); Assert.IsTrue(ts.Contains("enum Fruits")); @@ -36,7 +36,7 @@ public void GetTypescriptType_ArrayLike_Test() [ExpectedException(typeof(NotSupportedException))] public void EnumTypesNotIntNotSupported() { - var writer = new EnumToTypeScript(typeof(SomeEnumHexa).ToTypeScript(), false, true); + var writer = new EnumToTypeScript(typeof(SomeEnumHexa).ToTypeScript(), false); var value = writer.GetLines().ToList(); Assert.Fail("Should not reach this point", value); } diff --git a/Nimrod.Test/Writers/ModelWriterTests.cs b/Nimrod.Test/Writers/ModelWriterTests.cs index 19ed80c..b7c5e4a 100644 --- a/Nimrod.Test/Writers/ModelWriterTests.cs +++ b/Nimrod.Test/Writers/ModelWriterTests.cs @@ -39,7 +39,7 @@ private class DataMemberSpecificName [Test] public void WriteModel_UseDataMemberName() { - var writer = new ModelToTypeScript(typeof(DataMemberSpecificName).ToTypeScript(), true, true); + var writer = new ModelToTypeScript(typeof(DataMemberSpecificName).ToTypeScript(), true); string ts = writer.GetLines().JoinNewLine(); Assert.IsTrue(ts.Contains("bar")); Assert.IsFalse(ts.Contains("Foo")); @@ -48,7 +48,7 @@ public void WriteModel_UseDataMemberName() [Test] public void WriteModel_DoNotUseDataMemberName_IfEmptyOrWhitespace() { - var writer = new ModelToTypeScript(typeof(DataMemberSpecificName).ToTypeScript(), true, true); + var writer = new ModelToTypeScript(typeof(DataMemberSpecificName).ToTypeScript(), true); string ts = writer.GetLines().JoinNewLine(); Assert.IsTrue(ts.Contains("EmptyName")); } @@ -56,7 +56,7 @@ public void WriteModel_DoNotUseDataMemberName_IfEmptyOrWhitespace() [Test] public void WriteModel_IgnoreDataMember() { - var writer = new ModelToTypeScript(typeof(IgnoreDataMemberClass).ToTypeScript(), true, true); + var writer = new ModelToTypeScript(typeof(IgnoreDataMemberClass).ToTypeScript(), true); string ts = writer.GetLines().JoinNewLine(); Assert.IsFalse(ts.Contains("Foo")); } @@ -65,7 +65,7 @@ public void WriteModel_IgnoreDataMember() public void GetTypescriptType_Generic() { var genericTypeDefinition = typeof(GenericFoo).GetGenericTypeDefinition(); - var writer = new ModelToTypeScript(genericTypeDefinition.ToTypeScript(), true, true); + var writer = new ModelToTypeScript(genericTypeDefinition.ToTypeScript(), true); string ts = writer.GetLines().JoinNewLine(); Assert.IsTrue(ts.Contains("interface GenericFoo {")); @@ -76,7 +76,7 @@ public void GetTypescriptType_Generic() [Test] public void GetTypescriptType_GenericListContainer() { - var writer = new ModelToTypeScript(typeof(BarWrapper).GetGenericTypeDefinition().ToTypeScript(), true, true); + var writer = new ModelToTypeScript(typeof(BarWrapper).GetGenericTypeDefinition().ToTypeScript(), true); string ts = writer.GetLines().JoinNewLine(); Assert.IsTrue(ts.Contains("Bars: (T | null)[] | null;")); } @@ -84,18 +84,11 @@ public void GetTypescriptType_GenericListContainer() [Test] public void GetTypescriptType_GenericCustomContainer() { - var writer = new ModelToTypeScript(typeof(Fuzz).GetGenericTypeDefinition().ToTypeScript(), true, true); + var writer = new ModelToTypeScript(typeof(Fuzz).GetGenericTypeDefinition().ToTypeScript(), true); string ts = writer.GetLines().JoinNewLine(); Assert.IsTrue(ts.Contains("Fuzzs: GenericFoo | null;")); } - [Test] - public void ModelWriter_RequireExportWithoutGenericArgument() - { - var writer = new ModelToTypeScript(typeof(Fuzz).ToTypeScript(), false, true); - string ts = writer.GetLines().JoinNewLine(); - Assert.IsTrue(ts.Contains("export default Fuzz;")); - } } } diff --git a/Nimrod/FileToWrite.cs b/Nimrod/FileToWrite.cs index a5330d5..03756d8 100644 --- a/Nimrod/FileToWrite.cs +++ b/Nimrod/FileToWrite.cs @@ -8,13 +8,25 @@ namespace Nimrod { public class FileToWrite { - public string Name { get; } + public string Namespace { get; } public IEnumerable Lines { get; } - public IEnumerable Imports { get; } - public string Content => this.Imports.Concat(this.Lines).IndentLines().Concat("").JoinNewLine(); - public FileToWrite(string name, IEnumerable lines, IEnumerable imports) + public IEnumerable Imports { get; } + public string Content => + CustomImports.Concat(importLines).Concat(this.Lines).IndentLines().Concat("").JoinNewLine(); + + public IEnumerable CustomImports => new[] { + $"import {{ RestApi, RequestConfig }} from '../Nimrod';", + }; + + public string FileName => $"{Namespace}.ts"; + + IEnumerable importLines => this.Imports.GroupBy(t => t.Namespace) + .Where(t => t.Key != this.Namespace) + .Select(grp => $"import * as {grp.Key.Replace('.', '_')} from './{ grp.Key}';"); + + public FileToWrite(string @namespace, IEnumerable lines, IEnumerable imports) { - this.Name = name; + this.Namespace = @namespace; this.Lines = lines; this.Imports = imports; } diff --git a/Nimrod/Generator.cs b/Nimrod/Generator.cs index dad005d..f24eddd 100644 --- a/Nimrod/Generator.cs +++ b/Nimrod/Generator.cs @@ -10,11 +10,9 @@ namespace Nimrod public class Generator { public bool StrictNullCheck { get; } - public bool SingleFile { get; } - public Generator(bool strictNullCheck, bool singleFile) + public Generator(bool strictNullCheck) { this.StrictNullCheck = strictNullCheck; - this.SingleFile = singleFile; } public GeneratorResult Generate(IEnumerable dllPaths, IoOperations ioOperations) { @@ -25,27 +23,20 @@ public GeneratorResult Generate(IEnumerable dllPaths, IoOperations ioOpe ioOperations.WriteLog($"Discovering types.."); var types = GetTypesToWrite(assemblies).ToList(); - var stuff = types.AsDebugFriendlyParallel() - .Select(type => new { Type = type, File = GetFileToWrite(type) }) + var toTypeScritps = types.AsDebugFriendlyParallel() + .Select(type => new ToTypeScriptBuildRules().GetToTypeScript(new TypeScriptType(type), this.StrictNullCheck)) .ToList(); - IEnumerable files; - if (this.SingleFile) - { - files = stuff.Select(t => t.File.Item2); - } - else - { - files = stuff + var files = toTypeScritps .GroupBy(t => t.Type.Namespace) - .Select(a => new FileToWrite($"{a.Key}.ts", a.SelectMany(t => t.File.Item2.Lines), a.SelectMany(t => t.File.Item2.Imports).Distinct())); - } - var controllers = stuff - .Where(t => t.File.Item1 == FileType.Controller) - .Select(t => t.File.Item2.Name) + .Select(a => new FileToWrite($"{a.Key}", a.SelectMany(t => t.GetLines()), a.SelectMany(t => t.GetImports()))); + + var controllers = toTypeScritps + .Where(t => t.FileType == FileType.Controller) + .Select(t => t.Type.Namespace) .ToList(); - var models = stuff - .Where(t => t.File.Item1 != FileType.Controller) - .Select(t => t.File.Item2.Name) + var models = toTypeScritps + .Where(t => t.FileType != FileType.Controller) + .Select(t => t.Type.Namespace) .ToList(); @@ -71,29 +62,5 @@ private List GetTypesToWrite(IEnumerable assemblies) return toWrites; } - private Tuple GetFileToWrite(Type type) - { - var toTypeScript = new ToTypeScriptBuildRules().GetToTypeScript(new TypeScriptType(type), this.StrictNullCheck, this.SingleFile); - return Tuple.Create(toTypeScript.FileType, new FileToWrite(GetTypeScriptFilename(type), toTypeScript.GetLines(), toTypeScript.GetImports())); - } - - static public string GetTypeScriptFilename(Type type) - { - string name; - if (type.IsGenericType) - { - var genericType = type.GetGenericTypeDefinition(); - name = genericType.Name.Remove(genericType.Name.IndexOf('`')); - } - else if (type.IsWebController()) - { - name = $"{type.Name.Replace("Controller", "Service")}"; - } - else - { - name = type.Name; - } - return $"{type.Namespace}.{name}.ts"; - } } } diff --git a/Nimrod/IOOperations.cs b/Nimrod/IOOperations.cs index 962fb21..778fb9c 100644 --- a/Nimrod/IOOperations.cs +++ b/Nimrod/IOOperations.cs @@ -42,9 +42,9 @@ public void Dump(IList files) this.WriteLog($"Writing {files.Count} files..."); files.AsDebugFriendlyParallel().ForAll(content => { - this.WriteLog($"Writing {content.Name}..."); + this.WriteLog($"Writing {content.FileName}..."); - var filePath = this.FileSystem.Path.Combine(this.OutputFolderPath, content.Name); + var filePath = this.FileSystem.Path.Combine(this.OutputFolderPath, content.FileName); this.FileSystem.File.WriteAllText(filePath, content.Content); }); this.WriteLog($"Writing {files.Count} files...Done!"); diff --git a/Nimrod/ToTypeScriptOptions.cs b/Nimrod/ToTypeScriptOptions.cs index bbc1112..95c23de 100644 --- a/Nimrod/ToTypeScriptOptions.cs +++ b/Nimrod/ToTypeScriptOptions.cs @@ -8,24 +8,25 @@ namespace Nimrod { public class ToTypeScriptOptions { - public bool IncludeNamespace { get; } + public Predicate IncludeNamespace { get; } public bool IncludeGenericArguments { get; } public bool Nullable { get; } + public ToTypeScriptOptions() { - IncludeNamespace = false; + IncludeNamespace = (type) => false; IncludeGenericArguments = true; Nullable = true; } - public ToTypeScriptOptions(bool includeNamespace, bool includeGenericArguments, bool nullable) + public ToTypeScriptOptions(Predicate includeNamespace, bool includeGenericArguments, bool nullable) { this.IncludeNamespace = includeNamespace; this.IncludeGenericArguments = includeGenericArguments; this.Nullable = nullable; } - public ToTypeScriptOptions WithIncludeNamespace(bool includeNamespace) + public ToTypeScriptOptions WithIncludeNamespace(Predicate includeNamespace) => new ToTypeScriptOptions(includeNamespace, this.IncludeGenericArguments, this.Nullable); public ToTypeScriptOptions WithIncludeGenericArguments(bool includeGenericArguments) diff --git a/Nimrod/TypeScriptType.cs b/Nimrod/TypeScriptType.cs index f5e1aee..9262165 100644 --- a/Nimrod/TypeScriptType.cs +++ b/Nimrod/TypeScriptType.cs @@ -11,16 +11,6 @@ public class TypeScriptType public string Name => this.Type.Name; public string Namespace => this.Type.Namespace; - public string TypeScriptModuleName - { - get - { - var options = new ToTypeScriptOptions(true, false, false); - var fullTypeName = this.ToString(options); - return fullTypeName; - } - } - public Type Type { get; } public TypeScriptType(Type type) { @@ -28,13 +18,13 @@ public TypeScriptType(Type type) } - public override string ToString() => this.ToString(false); + public override string ToString() => this.ToString((p) => false); public string ToString(ToTypeScriptOptions options) => this.ToString(options.IncludeNamespace, options.IncludeGenericArguments, options.Nullable); - public string ToString(bool includeNamespace) => this.ToString(includeNamespace, true); - public string ToString(bool includeNamespace, bool includeGenericArguments) => this.ToString(includeNamespace, includeGenericArguments, true); - public string ToString(bool includeNamespace, bool includeGenericArguments, bool strictNullCheck) + public string ToString(Predicate includeNamespace) => this.ToString(includeNamespace, true); + public string ToString(Predicate includeNamespace, bool includeGenericArguments) => this.ToString(includeNamespace, includeGenericArguments, true); + public string ToString(Predicate includeNamespace, bool includeGenericArguments, bool strictNullCheck) { var options = new ToTypeScriptOptions(includeNamespace, includeGenericArguments, strictNullCheck); @@ -57,7 +47,7 @@ public string ToString(bool includeNamespace, bool includeGenericArguments, bool } else { - string ns = includeNamespace ? $"{this.Type.Namespace}." : ""; + string ns = includeNamespace(this.Type) ? $"{this.Type.Namespace.Replace('.', '_')}." : ""; result = $"{ns}{this.Type.Name}"; } } @@ -211,9 +201,9 @@ private static string ToTypeScriptForGenericClass(Type type, ToTypeScriptOptions private static string GenericTypeToTypeScript(Type type, ToTypeScriptOptions options) { var result = new StringBuilder(); - if (options.IncludeNamespace) + if (options.IncludeNamespace(type)) { - result.Append($"{type.GetGenericTypeDefinition().Namespace}."); + result.Append($"{type.GetGenericTypeDefinition().Namespace.Replace('.', '_')}."); } var genericTypeDefinitionName = type.GetGenericTypeDefinition().Name; diff --git a/Nimrod/Writers/ControllerToTypeScript.cs b/Nimrod/Writers/ControllerToTypeScript.cs index 89ff4c8..5abd59a 100644 --- a/Nimrod/Writers/ControllerToTypeScript.cs +++ b/Nimrod/Writers/ControllerToTypeScript.cs @@ -12,35 +12,25 @@ public class ControllerToTypeScript : ToTypeScript { public override FileType FileType => FileType.Controller; public string ServiceName => this.Type.Name.Replace("Controller", "Service"); - public virtual bool NeedNameSpace => false; - public ControllerToTypeScript(TypeScriptType type, bool strictNullCheck, bool singleFile) - : base(type, strictNullCheck, singleFile) + public ControllerToTypeScript(TypeScriptType type, bool strictNullCheck) + : base(type, strictNullCheck) { if (!type.Type.IsWebController()) { throw new ArgumentOutOfRangeException($"Type {type.Name} MUST extends System.Web.Mvc.Controller or System.Web.Http.IHttpControler", nameof(type)); } } - public override IEnumerable GetImports() + public override IEnumerable GetImports() { var actions = TypeDiscovery.GetWebControllerActions(this.Type.Type); var importedTypes = actions.SelectMany(action => action.GetReturnTypeAndParameterTypes()) .Distinct(); - var imports = ModuleHelper.GetTypesToImport(importedTypes) - .Where(type => this.SingleFile ? true : type.Namespace != this.Type.Namespace) - .Select(t => ModuleHelper.GetImportLine(t, this.SingleFile)); - return imports.Concat(new[] { - $"import {{ RestApi }} from '../Nimrod';", - $"import {{ Promise }} from '../Nimrod';", - $"import {{ RequestConfig }} from '../Nimrod';" - }); + var imports = ModuleHelper.GetTypesToImport(importedTypes); + return imports; } - - public override IEnumerable GetLines() => GetImplementation().Concat(this.SingleFile ? $"export default { this.Type.Name};" : ""); - - private IEnumerable GetImplementation() + public override IEnumerable GetLines() { var body = TypeDiscovery.GetWebControllerActions(this.Type.Type).SelectMany(method => { @@ -50,7 +40,8 @@ private IEnumerable GetImplementation() var signature = GetMethodSignature(method); var entityName = this.Type.Name.Substring(0, this.Type.Name.Length - "Controller".Length); - var genericArgString = method.GetReturnType().ToTypeScript().ToString(NeedNameSpace, true, false); + var genericArgString = method.GetReturnType().ToTypeScript() + .ToString(p => p.Namespace != this.Type.Namespace, true, false); var beautifulParamList = parameters .Select(p => $"{p.Name}: {p.Name}") @@ -89,7 +80,7 @@ private IEnumerable GetImplementation() /// The method public string GetMethodSignature(MethodInfo method) { - var options = new ToTypeScriptOptions(NeedNameSpace, true, this.StrictNullCheck); + var options = new ToTypeScriptOptions(type => type.Namespace != this.Type.Namespace, true, this.StrictNullCheck); var arguments = method.GetParameters() .Select(param => $", {param.Name}: {param.ParameterType.ToTypeScript().ToString(options)}") .Join(""); diff --git a/Nimrod/Writers/EnumToTypeScript.cs b/Nimrod/Writers/EnumToTypeScript.cs index fbc5af0..e2f53de 100644 --- a/Nimrod/Writers/EnumToTypeScript.cs +++ b/Nimrod/Writers/EnumToTypeScript.cs @@ -10,8 +10,8 @@ public class EnumToTypeScript : ToTypeScript { public override FileType FileType => FileType.Enum; public string TsName => this.Type.ToString(); - public EnumToTypeScript(TypeScriptType type, bool strictNullCheck, bool singleFile) - : base(type, strictNullCheck, singleFile) + public EnumToTypeScript(TypeScriptType type, bool strictNullCheck) + : base(type, strictNullCheck) { if (!this.Type.Type.IsEnum) { @@ -23,17 +23,17 @@ public EnumToTypeScript(TypeScriptType type, bool strictNullCheck, bool singleFi throw new NotSupportedException($"Unsupported underlying type for enums in typescript [{underlyingType}]. Only ints are supported."); } } - public override IEnumerable GetImports() => new List(); + public override IEnumerable GetImports() => new List(); public override IEnumerable GetLines() - => new[] { this.SingleFile ? $"enum {TsName} {{" : $"export enum {TsName} {{" } + => new[] { $"export enum {TsName} {{" } .Concat(this.GetBody()) .Concat(new[] { $"}}" }) .Concat(this.GetBodyDescription()); public IEnumerable GetBodyDescription() => new[] { $@" - export{(this.SingleFile ? " default" : "")} class {TsName}Utilities {{ + export class {TsName}Utilities {{ static getDescription(item: {this.TsName}): string {{ switch (item) {{ {this.Type.Type.GetEnumValues() diff --git a/Nimrod/Writers/ModelToTypeScript.cs b/Nimrod/Writers/ModelToTypeScript.cs index 2b15ca8..87a9005 100644 --- a/Nimrod/Writers/ModelToTypeScript.cs +++ b/Nimrod/Writers/ModelToTypeScript.cs @@ -8,47 +8,30 @@ namespace Nimrod.Writers public class ModelToTypeScript : ToTypeScript { public override FileType FileType => FileType.Model; - public virtual bool PrefixPropertyWithNamespace => false; - public string TsName => this.Type.ToString(new ToTypeScriptOptions().WithNullable(false)); - public ModelToTypeScript(TypeScriptType type, bool strictNullCheck, bool singleFile) - : base(type, strictNullCheck, singleFile) { } + public ModelToTypeScript(TypeScriptType type, bool strictNullCheck) + : base(type, strictNullCheck) { } - public override IEnumerable GetImports() + public override IEnumerable GetImports() { var genericArguments = this.Type.Type.GetGenericArguments().ToHashSet(); var propertyTypes = this.Type.Type.GetProperties() .Select(p => p.PropertyType) .Where(p => !genericArguments.Contains(p)); var imports = ModuleHelper.GetTypesToImport(propertyTypes) - .Where(t => this.SingleFile ? true : t.Namespace != this.Type.Namespace) - .Where(t => !genericArguments.Contains(t)) - .Select(t => ModuleHelper.GetImportLine(t, this.SingleFile)); + .Where(t => t.Namespace != this.Type.Namespace) + .Where(t => !genericArguments.Contains(t)); return imports; } protected string GetHeader() { - if (this.SingleFile) - { - return $"interface {TsName} {{"; - } - else - { - return $"export interface {TsName} {{"; - } + string name = this.Type.ToString(new ToTypeScriptOptions().WithNullable(false)); + return $"export interface {name} {{"; } - protected IEnumerable GetFooter() - { - var nonGenericTypescriptClass = this.Type.ToString(false, false, false); - yield return "}"; - if (this.SingleFile) - { - yield return $"export default {nonGenericTypescriptClass};"; - } - } + protected string GetFooter() => "}"; private IEnumerable GetBody() => this.Type.Type.GetProperties() .Select(property => new @@ -63,7 +46,8 @@ private IEnumerable GetBody() => this.Type.Type.GetProperties() var nullable = !a.Attributes.OfType().Any(); var attributeName = a.Attributes.OfType().FirstOrDefault()?.Name; string propertyName = string.IsNullOrWhiteSpace(attributeName) ? a.Property.Name : attributeName; - var options = new ToTypeScriptOptions().WithIncludeNamespace(PrefixPropertyWithNamespace) + bool includeNameSpace = this.Type.Namespace != a.Property.PropertyType.Namespace; + var options = new ToTypeScriptOptions().WithIncludeNamespace(type => type.Namespace != this.Type.Namespace) .WithNullable(this.StrictNullCheck && nullable); return $"{propertyName}: { a.Property.PropertyType.ToTypeScript().ToString(options)};"; }); diff --git a/Nimrod/Writers/ModuleHelper.cs b/Nimrod/Writers/ModuleHelper.cs index c1cb157..0c33a43 100644 --- a/Nimrod/Writers/ModuleHelper.cs +++ b/Nimrod/Writers/ModuleHelper.cs @@ -20,20 +20,6 @@ public static IEnumerable GetTypesToImport(IEnumerable types) return baseTypes.Union(genericTypes).Union(referencedType).Where(type => !type.IsSystem()); } - - public static string GetImportLine(Type type, bool singleFile) - { - var tsType = type.ToTypeScript(); - var typeName = tsType.ToString(false, false, false); - if (singleFile) - { - return $"import {typeName} from './{ tsType.TypeScriptModuleName}';"; - } - else - { - return $"import {{ {typeName} }} from './{ type.Namespace}';"; - } - } } } diff --git a/Nimrod/Writers/StructToTypeScript.cs b/Nimrod/Writers/StructToTypeScript.cs index b497dc8..599c806 100644 --- a/Nimrod/Writers/StructToTypeScript.cs +++ b/Nimrod/Writers/StructToTypeScript.cs @@ -13,8 +13,8 @@ public class StructToTypeScript : ToTypeScript { public override FileType FileType => FileType.Struct; - public StructToTypeScript(TypeScriptType type, bool strictNullCheck, bool singleFile) - : base(type, strictNullCheck, singleFile) + public StructToTypeScript(TypeScriptType type, bool strictNullCheck) + : base(type, strictNullCheck) { if (!this.Type.Type.IsValueType) { @@ -22,10 +22,10 @@ public StructToTypeScript(TypeScriptType type, bool strictNullCheck, bool single } } - public override IEnumerable GetImports() => new List(); + public override IEnumerable GetImports() => new List(); public override IEnumerable GetLines() => new[] { - this.SingleFile ? $"export default class {this.Type} extends String {{}}" : $"export class {this.Type} extends String {{}}" + $"export class {this.Type} extends String {{}}" }; } } diff --git a/Nimrod/Writers/ToTypeScript.cs b/Nimrod/Writers/ToTypeScript.cs index aaa3112..13e654d 100644 --- a/Nimrod/Writers/ToTypeScript.cs +++ b/Nimrod/Writers/ToTypeScript.cs @@ -10,14 +10,12 @@ public abstract class ToTypeScript public abstract FileType FileType { get; } public TypeScriptType Type { get; } public bool StrictNullCheck { get; } - public bool SingleFile { get; } - public ToTypeScript(TypeScriptType type, bool strictNullCheck, bool singleFile) + public ToTypeScript(TypeScriptType type, bool strictNullCheck) { this.Type = type.ThrowIfNull(nameof(type)); this.StrictNullCheck = strictNullCheck; - this.SingleFile = singleFile; } public abstract IEnumerable GetLines(); - public abstract IEnumerable GetImports(); + public abstract IEnumerable GetImports(); } } diff --git a/Nimrod/Writers/ToTypeScriptBuildRule.cs b/Nimrod/Writers/ToTypeScriptBuildRule.cs index 28081fb..b245783 100644 --- a/Nimrod/Writers/ToTypeScriptBuildRule.cs +++ b/Nimrod/Writers/ToTypeScriptBuildRule.cs @@ -8,9 +8,9 @@ namespace Nimrod.Writers public class ToTypeScriptBuildRule { public Func Predicate { get; } - public Func Builder { get; } + public Func Builder { get; } - public ToTypeScriptBuildRule(Func predicate, Func builder) + public ToTypeScriptBuildRule(Func predicate, Func builder) { this.Predicate = predicate; this.Builder = builder; diff --git a/Nimrod/Writers/ToTypeScriptBuildRules.cs b/Nimrod/Writers/ToTypeScriptBuildRules.cs index c17d3ab..258cc50 100644 --- a/Nimrod/Writers/ToTypeScriptBuildRules.cs +++ b/Nimrod/Writers/ToTypeScriptBuildRules.cs @@ -8,16 +8,16 @@ namespace Nimrod.Writers public class ToTypeScriptBuildRules { public IEnumerable Rules => new[] { - new ToTypeScriptBuildRule(type => type.Type.IsWebController(),(a, b, c) => new ControllerToTypeScript(a, b, c)), - new ToTypeScriptBuildRule(type => type.Type.IsEnum, (a, b, c) => new EnumToTypeScript(a, b, c)), - new ToTypeScriptBuildRule(type => type.Type.IsValueType, (a, b, c) => new StructToTypeScript(a, b, c)), - new ToTypeScriptBuildRule(type => true, (a, b, c) => new ModelToTypeScript(a, b, c)) + new ToTypeScriptBuildRule(type => type.Type.IsWebController(),(type, b) => new ControllerToTypeScript(type, b)), + new ToTypeScriptBuildRule(type => type.Type.IsEnum, (type, b) => new EnumToTypeScript(type, b)), + new ToTypeScriptBuildRule(type => type.Type.IsValueType, (type, b) => new StructToTypeScript(type, b)), + new ToTypeScriptBuildRule(type => true, (type, b) => new ModelToTypeScript(type, b)) }; - public ToTypeScript GetToTypeScript(TypeScriptType type, bool strictNullCheck, bool singleFile) + public ToTypeScript GetToTypeScript(TypeScriptType type, bool strictNullCheck) { var item = this.Rules .Where(s => s.Predicate(type)) - .Select(s => s.Builder(type, strictNullCheck, singleFile)) + .Select(s => s.Builder(type, strictNullCheck)) .FirstOrDefault(); if (item == null) {