Skip to content

Commit

Permalink
fixes for 1.2.2 release
Browse files Browse the repository at this point in the history
- Visual Studio-friendly warning and error messages
- Extracting types information from .exe assemblies
- Initialization expression for fields AST
- Assembly signing
  • Loading branch information
pavel-b-novikov committed Mar 7, 2016
1 parent 883e2a9 commit 0912c25
Show file tree
Hide file tree
Showing 35 changed files with 614 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,4 @@ FakesAssemblies/
*.opt
Playground/
/Reinforced.Typings.Private.sln
/Reinforced.Typings/Reinforced.Typings.pfx
46 changes: 37 additions & 9 deletions Reinforced.Typings.Cli/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using Reinforced.Typings.Exceptions;
using Reinforced.Typings.Fluent;

namespace Reinforced.Typings.Cli
Expand Down Expand Up @@ -40,14 +41,25 @@ public static void Main(string[] args)
Console.WriteLine("No valid parameters found. Exiting.");
Environment.Exit(0);
}
var settings = InstantiateExportSettings();
var settings = InstantiateExportContext();
ResolveFluentMethod(settings);
TsExporter exporter = new TsExporter(settings);
exporter.Export();
foreach (var rtWarning in settings.Warnings)
{
var msg = VisualStudioFriendlyErrorMessage.Create(rtWarning);
Console.WriteLine(msg.ToString());
}
}
catch (RtException rtException)
{
var error = VisualStudioFriendlyErrorMessage.Create(rtException);
Console.WriteLine(error.ToString());
Environment.Exit(1);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
BuildError(ex.Message);
Environment.Exit(1);
}

Expand Down Expand Up @@ -84,7 +96,7 @@ private static void ResolveFluentMethod(ExportContext context)
}
}

public static ExportContext InstantiateExportSettings()
public static ExportContext InstantiateExportContext()
{
ExportContext context = new ExportContext
{
Expand Down Expand Up @@ -114,7 +126,8 @@ public static void BuildReferencesCache()

public static string LookupAssemblyPath(string assemblyNameOrFullPath, bool storeIfFullName = true)
{
if (!assemblyNameOrFullPath.EndsWith(".dll")) assemblyNameOrFullPath = assemblyNameOrFullPath + ".dll";
if (!assemblyNameOrFullPath.EndsWith(".dll") && !assemblyNameOrFullPath.EndsWith(".exe"))
assemblyNameOrFullPath = assemblyNameOrFullPath + ".dll";
#if DEBUG
Console.WriteLine("Looking up for assembly {0}", assemblyNameOrFullPath);
#endif
Expand Down Expand Up @@ -148,7 +161,8 @@ public static string LookupAssemblyPath(string assemblyNameOrFullPath, bool stor
return p;
}

Console.WriteLine("Warning! Probably assembly {0} not found", assemblyNameOrFullPath, p);
BuildWarn("Assembly {0} may be resolved incorrectly", assemblyNameOrFullPath, p);

return assemblyNameOrFullPath;
}

Expand Down Expand Up @@ -224,6 +238,20 @@ public static void PrintHelp()
}
}

private static void BuildWarn(string message, params object[] args)
{
var warningMessage = string.Format(message, args);
VisualStudioFriendlyErrorMessage vsm = new VisualStudioFriendlyErrorMessage(99, warningMessage, VisualStudioFriendlyMessageType.Warning, "Build");
Console.WriteLine(vsm.ToString());
}

private static void BuildError(string message, params object[] args)
{
var errorMessage = string.Format(message, args);
VisualStudioFriendlyErrorMessage vsm = new VisualStudioFriendlyErrorMessage(999, errorMessage, VisualStudioFriendlyMessageType.Error, "Unexpected");
Console.WriteLine(vsm.ToString());
}

public static ExporterConsoleParameters ExtractParametersFromArgs(string[] args)
{
var t = typeof(ExporterConsoleParameters);
Expand All @@ -234,7 +262,7 @@ public static ExporterConsoleParameters ExtractParametersFromArgs(string[] args)
var kv = trimmed.Split('=');
if (kv.Length != 2)
{
Console.WriteLine("Unrecognized parameter: {0}", s);
BuildWarn("Unrecognized parameter: {0}", s);
continue;
}

Expand All @@ -244,7 +272,7 @@ public static ExporterConsoleParameters ExtractParametersFromArgs(string[] args)
var prop = t.GetProperty(key);
if (prop == null)
{
Console.WriteLine("Unrecognized parameter: {0}", key);
BuildWarn("Unrecognized parameter: {0}", key);
continue;
}

Expand All @@ -268,7 +296,7 @@ public static ExporterConsoleParameters ExtractParametersFromArgs(string[] args)
continue;
}

Console.WriteLine("Cannot parse parameter for source property {0}", key);
BuildWarn("Cannot parse parameter for source property {0}", key);
}

try
Expand All @@ -277,7 +305,7 @@ public static ExporterConsoleParameters ExtractParametersFromArgs(string[] args)
}
catch (Exception ex)
{
Console.WriteLine("Parameters validation error: {0}", ex.Message);
BuildError("Parameters validation error: {0}", ex.Message);
PrintHelp();
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions Reinforced.Typings.Cli/Reinforced.Typings.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
<ItemGroup>
<Compile Include="Bootstrapper.cs" />
<Compile Include="ConsoleHelpAttribute.cs" />
<Compile Include="VisualStudioFriendlyMessageType.cs" />
<Compile Include="ExporterConsoleParameters.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VisualStudioFriendlyErrorMessage.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
49 changes: 49 additions & 0 deletions Reinforced.Typings.Cli/VisualStudioFriendlyErrorMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Reinforced.Typings.Exceptions;

namespace Reinforced.Typings.Cli
{
/// <summary>
/// Class that formats messages in appropriate way to be shown at
/// VisualStudio's errors list
///
/// Explanation is taken from here:
/// http://blogs.msdn.com/b/msbuild/archive/2006/11/03/msbuild-visual-studio-aware-error-messages-and-message-formats.aspx
/// </summary>
class VisualStudioFriendlyErrorMessage
{
public string Origin { get { return "Reinforced.Typings"; } }

public string Subcategory { get; set; }

public VisualStudioFriendlyMessageType Type { get; set; }

public int Code { get; set; }

public string CodeName { get { return string.Format("RT{0:0000}", Code); } }

public string ErrorText { get; set; }

public VisualStudioFriendlyErrorMessage(int code, string errorText, VisualStudioFriendlyMessageType type, string subcategory = "")
{
Code = code;
ErrorText = errorText;
Subcategory = subcategory;
Type = type;
}

public override string ToString()
{
return string.Format("{0} : {1} {2} {3}: {4}", Origin, Subcategory, Type == VisualStudioFriendlyMessageType.Error ? "error" : "warning", CodeName, ErrorText);
}

public static VisualStudioFriendlyErrorMessage Create(RtWarning warning)
{
return new VisualStudioFriendlyErrorMessage(warning.Code,warning.Text,VisualStudioFriendlyMessageType.Warning,warning.Subcategory);
}

public static VisualStudioFriendlyErrorMessage Create(RtException error)
{
return new VisualStudioFriendlyErrorMessage(error.Code, error.Message, VisualStudioFriendlyMessageType.Error, error.Subcategory);
}
}
}
14 changes: 14 additions & 0 deletions Reinforced.Typings.Cli/VisualStudioFriendlyMessageType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reinforced.Typings.Cli
{
public enum VisualStudioFriendlyMessageType
{
Error,
Warning
}
}
5 changes: 5 additions & 0 deletions Reinforced.Typings/Ast/RtField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public class RtField : RtMember
/// </summary>
public RtTypeName Type { get; set; }

/// <summary>
/// TypeScript expression to initialize field
/// </summary>
public string InitializationExpression { get; set; }

/// <summary>
/// Child nodes
/// </summary>
Expand Down
34 changes: 34 additions & 0 deletions Reinforced.Typings/Exceptions/ErrorMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reinforced.Typings.Exceptions
{
class ErrorMessage
{
public int Code { get; private set; }

public string MessageText { get; private set; }

public string Subcategory { get; private set; }

public ErrorMessage(int code, string messageText, string subcategory = "")
{
Code = code;
MessageText = messageText;
Subcategory = subcategory;
}

public void Throw(params object[] formatParameters)
{
throw new RtException(string.Format(MessageText, formatParameters), Code, Subcategory);
}

public RtWarning Warn(params object[] formatParameters)
{
return new RtWarning(Code, text: string.Format(MessageText, formatParameters), subcategory: Subcategory);
}
}
}
107 changes: 107 additions & 0 deletions Reinforced.Typings/Exceptions/ErrorMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reinforced.Typings.Exceptions
{
/// <summary>
/// This class contains all RT's error and siagnostic messages.
/// Why didnt I use resources? I dont want to add one more .dll to RT's NuGet package.
/// if localization will be required through issues then I will add one
/// </summary>
class ErrorMessages
{
#region Errors
/// <summary>
/// Could not acuire temorary file {0}: {1}
/// </summary>
public static readonly ErrorMessage RTE0001_TempFileError = new ErrorMessage(0001,"Could not acuire temorary file {0}: {1}","IO");

/// <summary>
/// Could not replace source file {0}: {1}
/// </summary>
public static readonly ErrorMessage RTE0002_DeployingFilesError = new ErrorMessage(0002,"Could not replace source file {0}: {1}","IO");

/// <summary>
/// Could not instantiate code generator {0}: {1}
/// </summary>
public static readonly ErrorMessage RTE0003_GeneratorInstantiate = new ErrorMessage(0003,"Could not instantiate code generator {0}: {1}","Code generation");

/// <summary>
/// Code generator {0} has thrown an error: {1}
/// </summary>
public static readonly ErrorMessage RTE0004_GeneratorError = new ErrorMessage(0004,"Code generator {0} has thrown an error: {1}","Code generation");

/// <summary>
/// Could not resolve type for {0}. An error occured: {1}
/// </summary>
public static readonly ErrorMessage RTE0005_TypeResolvationError = new ErrorMessage(0005, "Could not resolve type for {0}. An error occured: {1}", "Type resolvation");

/// <summary>
/// Exception thrown when applying fluent configuration method for {1} '{2}': {0}
/// </summary>
public static readonly ErrorMessage RTE0006_FluentSingleError = new ErrorMessage(0006, "Exception thrown when applying fluent configuration method for {1} '{2}': {0}", "Fluent configuration");

/// <summary>
/// Exception thrown when applying fluent configuration method for collection of {1}: {0}
/// </summary>
public static readonly ErrorMessage RTE0007_FluentBatchError = new ErrorMessage(0007, "Exception thrown when applying fluent configuration method for collection of {1}: {0}", "Fluent configuration");

/// <summary>
/// MethodCallExpression should be provided for .WithMethod call. Please use only lamba expressions in this place.
/// </summary>
public static readonly ErrorMessage RTE0008_FluentWithMethodError = new ErrorMessage(0008, "MethodCallExpression should be provided for .WithMethod call. Please use only lamba expressions in this place.", "Fluent configuration");


/// <summary>
/// Sorry, but {0} is not very good idea for parameter configuration. Try using simplier lambda expression.
/// </summary>
public static readonly ErrorMessage RTE0009_FluentWithMethodCouldNotParse = new ErrorMessage(0009, "Sorry, but {0} is not very good idea for parameter configuration. Try using simplier lambda expression.", "Fluent configuration");

/// <summary>
/// Property lambda expression expected in {0}
/// </summary>
public static readonly ErrorMessage RTE0010_PropertyLambdaExpected = new ErrorMessage(0010, "Property lambda expression expected in {0}", "Fluent configuration");

/// <summary>
/// Field lambda expression expected in {0}
/// </summary>
public static readonly ErrorMessage RTE0011_FieldLambdaExpected = new ErrorMessage(0011, "Field lambda expression expected in {0}", "Fluent configuration");

/// <summary>
/// NewExpression should be provided for .WithConstructor call. Please use only lamba expressions in this place.
/// </summary>
public static readonly ErrorMessage RTE0012_NewExpressionLambdaExpected = new ErrorMessage(0012, "NewExpression should be provided for .WithConstructor call. Please use only 'new ...' lamba expressions in this place.", "Fluent configuration");
#endregion

#region Warnings
/// <summary>
/// XMLDOC file not supplied
/// </summary>
public static readonly ErrorMessage RTW0001_DocumentationNotSupplied = new ErrorMessage(0001, "XMLDOC file not supplied", "JSDOC");

/// <summary>
/// Could not find XMLDOC file {0}
/// </summary>
public static readonly ErrorMessage RTW0002_DocumentationNotFound = new ErrorMessage(0002, "Could not find XMLDOC file {0}", "JSDOC");

/// <summary>
/// Could not find suitable TypeScript type for {0}. 'any' assumed.
/// </summary>
public static readonly ErrorMessage RTW0003_TypeUnknown = new ErrorMessage(0003, "Could not find suitable TypeScript type for {0}. 'any' assumed.", "Type resolvation");

/// <summary>
/// No suitable base constructor found for {0}. Generating 'super' call with all nulls.
/// </summary>
public static readonly ErrorMessage RTW0004_DefaultSuperCall = new ErrorMessage(0004, "No suitable base constructor found for {0}. Generating 'super' call with all nulls.", "Class code generation");

/// <summary>
/// Class {0} (base for {1}) is exported as interface. It is potentially unsafe facility.
/// </summary>
public static readonly ErrorMessage RTW0005_BaseClassExportingAsInterface = new ErrorMessage(0005, "Class {0} (base for {1}) is exported as interface. It is potentially unsafe facility.", "Class code generation");

#endregion
}
}
37 changes: 37 additions & 0 deletions Reinforced.Typings/Exceptions/RtException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reinforced.Typings.Exceptions
{
/// <summary>
/// Base class for RT exception.
/// All the RT exceptions will be provided to VisualStudio's errors tab
/// </summary>
public class RtException : Exception
{
/// <summary>
/// Internal error code
/// </summary>
public int Code { get; private set; }

/// <summary>
/// Error subcategory
/// </summary>
public string Subcategory { get; private set; }

/// <summary>
/// Constructs new RT exception
/// </summary>
/// <param name="message">Error message</param>
/// <param name="code">Error code</param>
/// <param name="subcategory">Error subcategory (optional)</param>
public RtException(string message, int code, string subcategory = "") : base(message)
{
Code = code;
Subcategory = subcategory;
}
}
}
Loading

0 comments on commit 0912c25

Please sign in to comment.