Skip to content

Commit

Permalink
Remove the option to export a type to a single file, the granularity …
Browse files Browse the repository at this point in the history
…of a file is a Namespace
  • Loading branch information
Cyril Gandon committed Oct 6, 2016
1 parent c402f11 commit bb3d9a4
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 225 deletions.
3 changes: 0 additions & 3 deletions Nimrod.Console/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> Files { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Nimrod.Console/OptionsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 0 additions & 23 deletions Nimrod.Test/GeneratorTests.cs

This file was deleted.

1 change: 0 additions & 1 deletion Nimrod.Test/Nimrod.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
<Compile Include="Extensions\MethodExtensionsTests.cs" />
<Compile Include="Extensions\ObjectExtensionsTests.cs" />
<Compile Include="Extensions\StringExtensionsTests.cs" />
<Compile Include="GeneratorTests.cs" />
<Compile Include="TupleToTypeScriptTests.cs" />
<Compile Include="HttpMethodAttributeTests.cs" />
<Compile Include="JsonNetResult.cs" />
Expand Down
14 changes: 7 additions & 7 deletions Nimrod.Test/ToTypescriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ public class Generic1<T>
[Test]
public void GetTypescriptType_Generic_Test()
{
Assert.AreEqual("Nimrod.Test.Generic<string> | null", typeof(Generic<string>).ToTypeScript().ToString(true, true));
Assert.AreEqual("Nimrod.Test.Generic | null", typeof(Generic<string>).ToTypeScript().ToString(true, false));
Assert.AreEqual("Generic<string> | null", typeof(Generic<string>).ToTypeScript().ToString(false, true));
Assert.AreEqual("Generic | null", typeof(Generic<string>).ToTypeScript().ToString(false, false));
Assert.AreEqual("Nimrod_Test.Generic<string> | null", typeof(Generic<string>).ToTypeScript().ToString(p => true, true));
Assert.AreEqual("Nimrod_Test.Generic | null", typeof(Generic<string>).ToTypeScript().ToString(p => true, false));
Assert.AreEqual("Generic<string> | null", typeof(Generic<string>).ToTypeScript().ToString(p => false, true));
Assert.AreEqual("Generic | null", typeof(Generic<string>).ToTypeScript().ToString(p => false, false));
}

[Test]
public void GetTypescriptType_GenericWithNumber_Test()
{
Assert.AreEqual("Nimrod.Test.Generic1<string> | null", typeof(Generic1<string>).ToTypeScript().ToString(true, true));
Assert.AreEqual("Nimrod_Test.Generic1<string> | null", typeof(Generic1<string>).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]
Expand Down
12 changes: 6 additions & 6 deletions Nimrod.Test/TupleToTypeScriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ public class TupleToTypeScriptTests
[Test]
public void TupleToTypeScriptTests_NameSpaceInclude_Test()
{
Assert.AreEqual("{ Item1: SomeClass | null } | null", typeof(Tuple<SomeClass>).ToTypeScript().ToString(false));
Assert.AreEqual("{ Item1: Very.Specific.NameSpace.SomeClass | null } | null", typeof(Tuple<SomeClass>).ToTypeScript().ToString(true));
Assert.AreEqual("{ Item1: SomeClass | null } | null", typeof(Tuple<SomeClass>).ToTypeScript().ToString(type => false));
Assert.AreEqual("{ Item1: Very_Specific_NameSpace.SomeClass | null } | null", typeof(Tuple<SomeClass>).ToTypeScript().ToString(type => true));
}

[Test]
public void TupleToTypeScriptTests_Generic_Test()
{
Assert.AreEqual("{ Item1: SomeGenericClass<number> | null } | null", typeof(Tuple<SomeGenericClass<int>>).ToTypeScript().ToString(false, true));
Assert.AreEqual("{ Item1: Very.Specific.NameSpace.SomeGenericClass<number> | null } | null", typeof(Tuple<SomeGenericClass<int>>).ToTypeScript().ToString(true, true));
Assert.AreEqual("{ Item1: SomeGenericClass<number> | null } | null", typeof(Tuple<SomeGenericClass<int>>).ToTypeScript().ToString(type => false, true));
Assert.AreEqual("{ Item1: Very_Specific_NameSpace.SomeGenericClass<number> | null } | null", typeof(Tuple<SomeGenericClass<int>>).ToTypeScript().ToString(type => true, true));

Assert.AreEqual("{ Item1: SomeGenericClass | null } | null", typeof(Tuple<SomeGenericClass<int>>).ToTypeScript().ToString(false, false));
Assert.AreEqual("{ Item1: Very.Specific.NameSpace.SomeGenericClass | null } | null", typeof(Tuple<SomeGenericClass<int>>).ToTypeScript().ToString(true, false));
Assert.AreEqual("{ Item1: SomeGenericClass | null } | null", typeof(Tuple<SomeGenericClass<int>>).ToTypeScript().ToString(type => false, false));
Assert.AreEqual("{ Item1: Very_Specific_NameSpace.SomeGenericClass | null } | null", typeof(Tuple<SomeGenericClass<int>>).ToTypeScript().ToString(type => true, false));
}

[Test]
Expand Down
18 changes: 2 additions & 16 deletions Nimrod.Test/Writers/BaseWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,11 @@ public class GenericWrapper<T>
public GenericItem<T> 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<int>).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;"));
}
Expand All @@ -46,7 +32,7 @@ public void GetTypescriptType_GenericListContainer()
public void GetTypescriptType_GenericCustomContainer()
{
var genericTypeDefinition = typeof(Fuzz<int>).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<T> | null;"));
Expand Down
2 changes: 1 addition & 1 deletion Nimrod.Test/Writers/ControllerWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
4 changes: 2 additions & 2 deletions Nimrod.Test/Writers/EnumWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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);
}
Expand Down
19 changes: 6 additions & 13 deletions Nimrod.Test/Writers/ModelWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -48,15 +48,15 @@ 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"));
}

[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"));
}
Expand All @@ -65,7 +65,7 @@ public void WriteModel_IgnoreDataMember()
public void GetTypescriptType_Generic()
{
var genericTypeDefinition = typeof(GenericFoo<int>).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<T> {"));
Expand All @@ -76,26 +76,19 @@ public void GetTypescriptType_Generic()
[Test]
public void GetTypescriptType_GenericListContainer()
{
var writer = new ModelToTypeScript(typeof(BarWrapper<int>).GetGenericTypeDefinition().ToTypeScript(), true, true);
var writer = new ModelToTypeScript(typeof(BarWrapper<int>).GetGenericTypeDefinition().ToTypeScript(), true);
string ts = writer.GetLines().JoinNewLine();
Assert.IsTrue(ts.Contains("Bars: (T | null)[] | null;"));
}

[Test]
public void GetTypescriptType_GenericCustomContainer()
{
var writer = new ModelToTypeScript(typeof(Fuzz<int>).GetGenericTypeDefinition().ToTypeScript(), true, true);
var writer = new ModelToTypeScript(typeof(Fuzz<int>).GetGenericTypeDefinition().ToTypeScript(), true);
string ts = writer.GetLines().JoinNewLine();
Assert.IsTrue(ts.Contains("Fuzzs: GenericFoo<T> | null;"));
}

[Test]
public void ModelWriter_RequireExportWithoutGenericArgument()
{
var writer = new ModelToTypeScript(typeof(Fuzz<int>).ToTypeScript(), false, true);
string ts = writer.GetLines().JoinNewLine();
Assert.IsTrue(ts.Contains("export default Fuzz;"));
}
}
}

22 changes: 17 additions & 5 deletions Nimrod/FileToWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ namespace Nimrod
{
public class FileToWrite
{
public string Name { get; }
public string Namespace { get; }
public IEnumerable<string> Lines { get; }
public IEnumerable<string> Imports { get; }
public string Content => this.Imports.Concat(this.Lines).IndentLines().Concat("").JoinNewLine();
public FileToWrite(string name, IEnumerable<string> lines, IEnumerable<string> imports)
public IEnumerable<Type> Imports { get; }
public string Content =>
CustomImports.Concat(importLines).Concat(this.Lines).IndentLines().Concat("").JoinNewLine();

public IEnumerable<string> CustomImports => new[] {
$"import {{ RestApi, RequestConfig }} from '../Nimrod';",
};

public string FileName => $"{Namespace}.ts";

IEnumerable<string> 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<string> lines, IEnumerable<Type> imports)
{
this.Name = name;
this.Namespace = @namespace;
this.Lines = lines;
this.Imports = imports;
}
Expand Down
57 changes: 12 additions & 45 deletions Nimrod/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> dllPaths, IoOperations ioOperations)
{
Expand All @@ -25,27 +23,20 @@ public GeneratorResult Generate(IEnumerable<string> 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<FileToWrite> 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();


Expand All @@ -71,29 +62,5 @@ private List<Type> GetTypesToWrite(IEnumerable<Assembly> assemblies)
return toWrites;
}

private Tuple<FileType, FileToWrite> 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";
}
}
}
4 changes: 2 additions & 2 deletions Nimrod/IOOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public void Dump(IList<FileToWrite> 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!");
Expand Down
9 changes: 5 additions & 4 deletions Nimrod/ToTypeScriptOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ namespace Nimrod
{
public class ToTypeScriptOptions
{
public bool IncludeNamespace { get; }
public Predicate<Type> 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<Type> includeNamespace, bool includeGenericArguments, bool nullable)
{
this.IncludeNamespace = includeNamespace;
this.IncludeGenericArguments = includeGenericArguments;
this.Nullable = nullable;
}

public ToTypeScriptOptions WithIncludeNamespace(bool includeNamespace)
public ToTypeScriptOptions WithIncludeNamespace(Predicate<Type> includeNamespace)
=> new ToTypeScriptOptions(includeNamespace, this.IncludeGenericArguments, this.Nullable);

public ToTypeScriptOptions WithIncludeGenericArguments(bool includeGenericArguments)
Expand Down
Loading

0 comments on commit bb3d9a4

Please sign in to comment.