From 545d6ddca094c8b03192edfb194cfd216acb83c1 Mon Sep 17 00:00:00 2001 From: Simon Lovely Date: Wed, 5 Nov 2014 18:54:19 +0000 Subject: [PATCH] Create TypeScriptSample.Generator project --- src/TypeScriptSample.Generator/App.config | 6 ++ src/TypeScriptSample.Generator/Program.cs | 52 +++++++++++++++ .../Properties/AssemblyInfo.cs | 36 ++++++++++ .../TypeScriptSample.Generator.csproj | 65 +++++++++++++++++++ .../packages.config | 4 ++ src/TypeScriptSample.Web.sln | 6 ++ 6 files changed, 169 insertions(+) create mode 100644 src/TypeScriptSample.Generator/App.config create mode 100644 src/TypeScriptSample.Generator/Program.cs create mode 100644 src/TypeScriptSample.Generator/Properties/AssemblyInfo.cs create mode 100644 src/TypeScriptSample.Generator/TypeScriptSample.Generator.csproj create mode 100644 src/TypeScriptSample.Generator/packages.config diff --git a/src/TypeScriptSample.Generator/App.config b/src/TypeScriptSample.Generator/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/src/TypeScriptSample.Generator/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/TypeScriptSample.Generator/Program.cs b/src/TypeScriptSample.Generator/Program.cs new file mode 100644 index 0000000..4c5136d --- /dev/null +++ b/src/TypeScriptSample.Generator/Program.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; +using System.Reflection; +using TypeLite; + +namespace TypeScriptSample.Generator +{ + class Program + { + static void Main(string[] args) + { + var assemblyFile = args[0]; + var outputPath = args[1]; + + LoadReferencedAssemblies(assemblyFile); + GenerateTypeScriptContracts(assemblyFile, outputPath); + } + + private static void LoadReferencedAssemblies(string assemblyFile) + { + var sourceAssemblyDirectory = Path.GetDirectoryName(assemblyFile); + foreach (var file in Directory.GetFiles(sourceAssemblyDirectory, "*.dll")) + { + File.Copy(file, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, new FileInfo(file).Name), true); + } + } + + private static void GenerateTypeScriptContracts(string assemblyFile, string outputPath) + { + var assembly = Assembly.LoadFrom(assemblyFile); + // If you want a subset of classes from this assembly, filter them here + var models = assembly.GetTypes(); + + var generator = new TypeScriptFluent() + .WithConvertor(c => "string"); + + foreach (var model in models) + { + generator.ModelBuilder.Add(model); + } + + //Generate enums + var tsEnumDefinitions = generator.Generate(TsGeneratorOutput.Enums); + Directory.CreateDirectory(outputPath); + File.WriteAllText(Path.Combine(outputPath, "enums.ts"), tsEnumDefinitions); + //Generate interface definitions for all classes + var tsClassDefinitions = generator.Generate(TsGeneratorOutput.Properties | TsGeneratorOutput.Fields); + File.WriteAllText(Path.Combine(outputPath, "classes.d.ts"), tsClassDefinitions); + + } + } +} diff --git a/src/TypeScriptSample.Generator/Properties/AssemblyInfo.cs b/src/TypeScriptSample.Generator/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c6e4287 --- /dev/null +++ b/src/TypeScriptSample.Generator/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TypeScriptSample.Generator")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TypeScriptSample.Generator")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("51ef5651-8ef9-4082-bc24-d9b915a8f2b2")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/TypeScriptSample.Generator/TypeScriptSample.Generator.csproj b/src/TypeScriptSample.Generator/TypeScriptSample.Generator.csproj new file mode 100644 index 0000000..258be3e --- /dev/null +++ b/src/TypeScriptSample.Generator/TypeScriptSample.Generator.csproj @@ -0,0 +1,65 @@ + + + + + Debug + AnyCPU + {37941D06-6CD1-43E3-81A2-72727D5985EE} + Exe + Properties + TypeScriptSample.Generator + TypeScriptSample.Generator + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + ..\packages\TypeLite.Lib.1.0.0.0\lib\net4\TypeLite.dll + + + ..\packages\TypeLite.Lib.1.0.0.0\lib\net4\TypeLite.Net4.dll + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/TypeScriptSample.Generator/packages.config b/src/TypeScriptSample.Generator/packages.config new file mode 100644 index 0000000..8cb588e --- /dev/null +++ b/src/TypeScriptSample.Generator/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/TypeScriptSample.Web.sln b/src/TypeScriptSample.Web.sln index 199b129..4d9093c 100644 --- a/src/TypeScriptSample.Web.sln +++ b/src/TypeScriptSample.Web.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypeScriptSample.Web", "Typ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypeScriptSample.Models", "TypeScriptSample.Models\TypeScriptSample.Models.csproj", "{F09679BE-2BD7-4DB6-A9B5-37C525472CDB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypeScriptSample.Generator", "TypeScriptSample.Generator\TypeScriptSample.Generator.csproj", "{37941D06-6CD1-43E3-81A2-72727D5985EE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {F09679BE-2BD7-4DB6-A9B5-37C525472CDB}.Debug|Any CPU.Build.0 = Debug|Any CPU {F09679BE-2BD7-4DB6-A9B5-37C525472CDB}.Release|Any CPU.ActiveCfg = Release|Any CPU {F09679BE-2BD7-4DB6-A9B5-37C525472CDB}.Release|Any CPU.Build.0 = Release|Any CPU + {37941D06-6CD1-43E3-81A2-72727D5985EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {37941D06-6CD1-43E3-81A2-72727D5985EE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {37941D06-6CD1-43E3-81A2-72727D5985EE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {37941D06-6CD1-43E3-81A2-72727D5985EE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE