Skip to content

Commit

Permalink
Let type inference work on the return type of the methode of controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyril Gandon committed Oct 5, 2016
1 parent a26788b commit c402f11
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Nimrod.Console/OptionsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ static private void OnOptionsSuccessful(Options options, TraceListener tracer)
}
var ioOperations = new IoOperations(new FileSystem(), options.OutputPath, logger);
var generator = new Generator(options.StrictNullCheck, !options.Group);
generator.Generate(options.Files, ioOperations);
var result = generator.Generate(options.Files, ioOperations);
ioOperations.Dump(result.Files);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Nimrod/FileType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Nimrod
{
public enum FileType { Controller, Model, Struct, Enum }
}
33 changes: 22 additions & 11 deletions Nimrod/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Generator(bool strictNullCheck, bool singleFile)
this.StrictNullCheck = strictNullCheck;
this.SingleFile = singleFile;
}
public void Generate(IEnumerable<string> dllPaths, IoOperations ioOperations)
public GeneratorResult Generate(IEnumerable<string> dllPaths, IoOperations ioOperations)
{
var fileInfos = ioOperations.GetFileInfos(dllPaths).ToList();
ioOperations.LoadAssemblies(fileInfos);
Expand All @@ -25,20 +25,31 @@ public void Generate(IEnumerable<string> dllPaths, IoOperations ioOperations)
ioOperations.WriteLog($"Discovering types..");
var types = GetTypesToWrite(assemblies).ToList();

var stuff = types.AsDebugFriendlyParallel()
.Select(type => new { Type = type, File = GetFileToWrite(type) })
.ToList();
IEnumerable<FileToWrite> files;
if (this.SingleFile)
{
var files = types.AsDebugFriendlyParallel().Select(GetFileToWrite).ToList();

ioOperations.Dump(files);
files = stuff.Select(t => t.File.Item2);
}
else
{
var files = types.AsDebugFriendlyParallel().Select(type => new { Type = type, File = GetFileToWrite(type) })
.GroupBy(t => t.Type.Namespace)
.Select(a => new FileToWrite($"{a.Key}.ts", a.SelectMany(t => t.File.Lines), a.SelectMany(t => t.File.Imports).Distinct()))
.ToList();
ioOperations.Dump(files);
files = stuff
.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)
.ToList();
var models = stuff
.Where(t => t.File.Item1 != FileType.Controller)
.Select(t => t.File.Item2.Name)
.ToList();


return new GeneratorResult(controllers, models, files.ToList());

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

private FileToWrite GetFileToWrite(Type type)
private Tuple<FileType, FileToWrite> GetFileToWrite(Type type)
{
var toTypeScript = new ToTypeScriptBuildRules().GetToTypeScript(new TypeScriptType(type), this.StrictNullCheck, this.SingleFile);
return new FileToWrite(GetTypeScriptFilename(type), toTypeScript.GetLines(), toTypeScript.GetImports());
return Tuple.Create(toTypeScript.FileType, new FileToWrite(GetTypeScriptFilename(type), toTypeScript.GetLines(), toTypeScript.GetImports()));
}

static public string GetTypeScriptFilename(Type type)
Expand Down
34 changes: 34 additions & 0 deletions Nimrod/GeneratorResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;

namespace Nimrod
{
public class GeneratorResult
{
public List<string> Services { get; }
public List<string> Models { get; }
public List<FileToWrite> Files { get; }

public GeneratorResult(List<string> services, List<string> models, List<FileToWrite> files)
{
this.Services = services.ThrowIfNull(nameof(services));
this.Models = models.ThrowIfNull(nameof(models));
this.Files = files.ThrowIfNull(nameof(files));
}

public override string ToString()
{
using (var stream = new MemoryStream())
{
var serializer = new DataContractJsonSerializer(typeof(GeneratorResult));
serializer.WriteObject(stream, this);
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
}
}
2 changes: 2 additions & 0 deletions Nimrod/Nimrod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
<Compile Include="Extensions\TryGetFuncDelegates.cs" />
<Compile Include="Extensions\TypeExtensions.cs" />
<Compile Include="FileToWrite.cs" />
<Compile Include="FileType.cs" />
<Compile Include="GeneratorResult.cs" />
<Compile Include="Loggers\DateTimeLogger.cs" />
<Compile Include="Loggers\ILogger.cs" />
<Compile Include="IoOperations.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Nimrod/Writers/ControllerToTypeScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Nimrod.Writers
// To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
public class ControllerToTypeScript : ToTypeScript
{
public override FileType FileType => FileType.Controller;
public string ServiceName => this.Type.Name.Replace("Controller", "Service");
public virtual bool NeedNameSpace => false;

Expand Down Expand Up @@ -92,9 +93,8 @@ public string GetMethodSignature(MethodInfo method)
var arguments = method.GetParameters()
.Select(param => $", {param.Name}: {param.ParameterType.ToTypeScript().ToString(options)}")
.Join("");
var returnType = method.GetReturnType().ToTypeScript().ToString(NeedNameSpace, true, StrictNullCheck);

return $"{method.Name}(restApi: RestApi{arguments}, config?: RequestConfig): Promise<{returnType}>";
return $"{method.Name}(restApi: RestApi{arguments}, config?: RequestConfig)";
}
}
}
1 change: 1 addition & 0 deletions Nimrod/Writers/EnumToTypeScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Nimrod.Writers
{
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)
Expand Down
1 change: 1 addition & 0 deletions Nimrod/Writers/ModelToTypeScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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));

Expand Down
1 change: 1 addition & 0 deletions Nimrod/Writers/StructToTypeScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Nimrod.Writers
/// </summary>
public class StructToTypeScript : ToTypeScript
{
public override FileType FileType => FileType.Struct;

public StructToTypeScript(TypeScriptType type, bool strictNullCheck, bool singleFile)
: base(type, strictNullCheck, singleFile)
Expand Down
1 change: 1 addition & 0 deletions Nimrod/Writers/ToTypeScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Nimrod.Writers
{
public abstract class ToTypeScript
{
public abstract FileType FileType { get; }
public TypeScriptType Type { get; }
public bool StrictNullCheck { get; }
public bool SingleFile { get; }
Expand Down

0 comments on commit c402f11

Please sign in to comment.