Skip to content

Commit

Permalink
Add new error message in case we found type referenced but not it's DLLs
Browse files Browse the repository at this point in the history
Consider adding a flag to ignore those. It's just an error for now, that block the whole process
  • Loading branch information
Cyril Gandon committed Aug 18, 2016
1 parent 0fd4b68 commit cac6d70
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 43 deletions.
8 changes: 7 additions & 1 deletion Nimrod.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ static class Program
{
static int Main(string[] args)
{

args = @"-m typescript -o C:\SoftwareGit\software\WikiProject\WebClient\src\ServerApi.Generated --files=C:\SoftwareGit\software\WikiProject\WebServer\bin\RES.WikiProject.WebServer.dll,C:\SoftwareGit\software\WikiProject\WebServer\bin\RES.Insee.Wiki.dll --verbose".Split(' ');
var tracer = new ConsoleTraceListener();
try
{
Expand Down Expand Up @@ -48,7 +50,11 @@ static void OnOptionsSuccessful(Options options, TraceListener tracer)
}
else
{
var logger = options.Verbose ? new DateTimeConsoleTracer(tracer) : null;
ILogger logger = VoidLogger.Default;
if (options.Verbose)
{
logger = new DateTimeConsoleTracer(tracer);
}
var ioOperations = new IoOperations(new FileSystem(), options.OutputPath, logger);
var generator = new Generator(ioOperations);
generator.Generate(options.Files, options.ModuleType);
Expand Down
5 changes: 4 additions & 1 deletion Nimrod.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nimrod", "Nimrod\Nimrod.csproj", "{326311F5-EB2F-4D03-B74B-F7FAEBA95FE5}"
EndProject
Expand Down Expand Up @@ -45,4 +45,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EnterpriseLibraryConfigurationToolBinariesPathV6 = packages\EnterpriseLibrary.TransientFaultHandling.6.0.1304.0\lib\portable-net45+win+wp8;packages\EnterpriseLibrary.TransientFaultHandling.Data.6.0.1304.1\lib\NET45
EndGlobalSection
EndGlobal
11 changes: 5 additions & 6 deletions Nimrod/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;

namespace Nimrod
{
Expand All @@ -12,13 +11,13 @@ public static class TypeExtensions
public static readonly Type[] NumberTypes = { typeof(short), typeof(int), typeof(long), typeof(float), typeof(double), typeof(decimal) };

public static readonly Dictionary<Type, HttpMethodAttribute> TypeToHttpMethodAttribute = new Dictionary<Type, HttpMethodAttribute> {
{ typeof(HttpGetAttribute), HttpMethodAttribute.Get },
{ typeof(System.Web.Mvc.HttpGetAttribute), HttpMethodAttribute.Get },
{ typeof(System.Web.Http.HttpGetAttribute), HttpMethodAttribute.Get },
{ typeof(HttpPostAttribute), HttpMethodAttribute.Post },
{ typeof(System.Web.Mvc.HttpPostAttribute), HttpMethodAttribute.Post },
{ typeof(System.Web.Http.HttpPostAttribute), HttpMethodAttribute.Post },
{ typeof(HttpPutAttribute), HttpMethodAttribute.Put },
{ typeof(System.Web.Mvc.HttpPutAttribute), HttpMethodAttribute.Put },
{ typeof(System.Web.Http.HttpPutAttribute), HttpMethodAttribute.Put },
{ typeof(HttpDeleteAttribute), HttpMethodAttribute.Delete },
{ typeof(System.Web.Mvc.HttpDeleteAttribute), HttpMethodAttribute.Delete },
{ typeof(System.Web.Http.HttpDeleteAttribute), HttpMethodAttribute.Delete },
};

Expand All @@ -38,7 +37,7 @@ public static class TypeExtensions

public static bool IsController(this Type type)
{
if (typeof(Controller).IsAssignableFrom(type))
if (typeof(System.Web.Mvc.Controller).IsAssignableFrom(type))
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Nimrod/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private IEnumerable<Type> GetTypesToWrite(IEnumerable<Assembly> assemblies)
{
this.IoOperations.WriteLog($"Discovering types..");
var assemblyTypes = TypeDiscovery.GetControllers(assemblies)
.SelectMany(controller => TypeDiscovery.SeekTypesFromController(controller))
.SelectMany(controller => TypeDiscovery.SeekTypesFromController(controller, this.IoOperations.Logger))
.ToList();

// Write all types except the ones in System
Expand Down
19 changes: 10 additions & 9 deletions Nimrod/IOOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@ public class IoOperations
{
public string OutputFolderPath { get; }
public IFileSystem FileSystem { get; }
private ILogger Logger { get; }
public ILogger Logger { get; }

public IoOperations(IFileSystem fileSystem, string outputFolderPath, ILogger logger)
{
this.FileSystem = fileSystem.ThrowIfNull(nameof(fileSystem));
this.OutputFolderPath = outputFolderPath;
this.Logger = logger;
this.Logger = logger.ThrowIfNull(nameof(logger)); ;

this.WriteLog("outputFolderPath = " + outputFolderPath);
}

public void WriteLog(string log)
{
if (this.Logger != null)
{
this.Logger.WriteLine(log);
}
this.Logger.WriteLine(log);
}

public void RecreateOutputFolder()
Expand Down Expand Up @@ -71,7 +68,10 @@ public IEnumerable<FileInfoBase> GetFileInfos(IEnumerable<string> filePaths)
}


// Load all assemblies in the folder
/// <summary>
/// Load all assemblies found in the same folder as the given DLL
/// </summary>
/// <param name="files"></param>
public void LoadAssemblies(IEnumerable<FileInfoBase> files)
{
var directories = files.Select(f => f.DirectoryName).Distinct();
Expand All @@ -80,8 +80,9 @@ public void LoadAssemblies(IEnumerable<FileInfoBase> files)
{
foreach (var assemblyFile in this.FileSystem.Directory.EnumerateFiles(directory, "*.dll"))
{
this.WriteLog($"Loading assembly {assemblyFile}");
Assembly.LoadFile(this.FileSystem.Path.Combine(directory, assemblyFile));
this.WriteLog($"Trying to load assembly {assemblyFile}...");
var assembly = Assembly.LoadFile(this.FileSystem.Path.Combine(directory, assemblyFile));
this.WriteLog($"Loaded {assembly.FullName}");
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions Nimrod/Nimrod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\EnterpriseLibrary.TransientFaultHandling.6.0.1304.0\lib\portable-net45+win+wp8\Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\EnterpriseLibrary.TransientFaultHandling.Data.6.0.1304.1\lib\NET45\Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.Data.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -113,6 +121,7 @@
<Compile Include="Generator.cs" />
<Compile Include="HttpMethodAttribute.cs" />
<Compile Include="ModuleType.cs" />
<Compile Include="VoidLogger.cs" />
<Compile Include="Writers\Default\ControllerToDefaultTypeScript.cs" />
<Compile Include="Writers\Default\StaticToDefaultTypeScript.cs" />
<Compile Include="Writers\Default\ToDefaultTypeScriptBuildRules.cs" />
Expand Down
52 changes: 27 additions & 25 deletions Nimrod/TypeDiscovery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

Expand All @@ -14,54 +15,54 @@ public static class TypeDiscovery
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static IEnumerable<Type> EnumerateTypes(Type type)
public static HashSet<Type> EnumerateTypes(Type type) => EnumerateTypes(type, VoidLogger.Default);
public static HashSet<Type> EnumerateTypes(Type type, ILogger logger)
{
return EnumerateTypesRecursive(type, new HashSet<Type>())
.Distinct();
var types = new HashSet<Type>();
EnumerateTypesRecursive(type, types, logger);
return types;
}

private static IEnumerable<Type> EnumerateTypesRecursive(Type type, HashSet<Type> cache)
private static void EnumerateTypesRecursive(Type type, HashSet<Type> cache, ILogger logger)
{
if (!cache.Contains(type))
{
cache.Add(type);
if (type == typeof(string))
// string is a reference type, but we don't want to generate property type Length
if (type != typeof(string))
{
// string is a reference type, but we don't want to generate property type Length
yield return type;
}
else
{
yield return type;

// generics
foreach (var genericArgumentType in type.GetGenericArguments())
{
yield return genericArgumentType;
foreach (var subType in EnumerateTypesRecursive(genericArgumentType, cache))
{
yield return subType;
}
EnumerateTypesRecursive(genericArgumentType, cache, logger);
}

// properties
foreach (var property in type.GetProperties())
{
foreach (var subType in EnumerateTypesRecursive(property.PropertyType, cache))
try
{
yield return subType;
EnumerateTypesRecursive(property.PropertyType, cache, logger);
}
catch (FileNotFoundException fileNotFoundException)
{
// during reflection, a type could be found without finding is corrsponding DLLs for reference
string message = $@"
Cannot understand the property {property.Name} of type {type.FullName}.
The following DLL has not been found in the loaded assemblies: {fileNotFoundException.FileName}
You should check that the DLLs exists in the folder, and version numbers are the sames.
";
throw new FileNotFoundException(message, fileNotFoundException.FileName, fileNotFoundException);
}
}

// inheritance
foreach (var baseType in GetBaseTypes(type))
{
yield return baseType;
foreach (var subType in EnumerateTypesRecursive(baseType, cache))
{
yield return subType;
}
EnumerateTypesRecursive(baseType, cache, logger);
}

}
}
}
Expand All @@ -72,7 +73,8 @@ private static IEnumerable<Type> EnumerateTypesRecursive(Type type, HashSet<Type
/// </summary>
/// <param name="controller"></param>
/// <returns></returns>
public static IEnumerable<Type> SeekTypesFromController(Type controller)
public static IEnumerable<Type> SeekTypesFromController(Type controller) => SeekTypesFromController(controller, VoidLogger.Default);
public static IEnumerable<Type> SeekTypesFromController(Type controller, ILogger logger)
{
if (!controller.IsController())
{
Expand All @@ -88,7 +90,7 @@ public static IEnumerable<Type> SeekTypesFromController(Type controller)
{
foreach (var actionType in GetControllerActionParameterTypes(method))
{
foreach (var referencedType in EnumerateTypes(actionType))
foreach (var referencedType in EnumerateTypes(actionType, logger))
{
yield return referencedType;
}
Expand Down
16 changes: 16 additions & 0 deletions Nimrod/VoidLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nimrod
{
public class VoidLogger : ILogger
{
public static readonly VoidLogger Default = new VoidLogger();
public void WriteLine(string log)
{
// void logger doesn't log
}
}
}
2 changes: 2 additions & 0 deletions Nimrod/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommandLineParser" version="1.9.71" targetFramework="net46" />
<package id="EnterpriseLibrary.TransientFaultHandling" version="6.0.1304.0" targetFramework="net46" />
<package id="EnterpriseLibrary.TransientFaultHandling.Data" version="6.0.1304.1" targetFramework="net46" />
<package id="Microsoft.AspNet.Mvc" version="4.0.40804.0" targetFramework="net46" />
<package id="Microsoft.AspNet.Razor" version="2.0.20710.0" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
Expand Down

0 comments on commit cac6d70

Please sign in to comment.