Skip to content

Commit 545d6dd

Browse files
committed
Create TypeScriptSample.Generator project
1 parent 41ff4e8 commit 545d6dd

6 files changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using TypeLite;
5+
6+
namespace TypeScriptSample.Generator
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
var assemblyFile = args[0];
13+
var outputPath = args[1];
14+
15+
LoadReferencedAssemblies(assemblyFile);
16+
GenerateTypeScriptContracts(assemblyFile, outputPath);
17+
}
18+
19+
private static void LoadReferencedAssemblies(string assemblyFile)
20+
{
21+
var sourceAssemblyDirectory = Path.GetDirectoryName(assemblyFile);
22+
foreach (var file in Directory.GetFiles(sourceAssemblyDirectory, "*.dll"))
23+
{
24+
File.Copy(file, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, new FileInfo(file).Name), true);
25+
}
26+
}
27+
28+
private static void GenerateTypeScriptContracts(string assemblyFile, string outputPath)
29+
{
30+
var assembly = Assembly.LoadFrom(assemblyFile);
31+
// If you want a subset of classes from this assembly, filter them here
32+
var models = assembly.GetTypes();
33+
34+
var generator = new TypeScriptFluent()
35+
.WithConvertor<Guid>(c => "string");
36+
37+
foreach (var model in models)
38+
{
39+
generator.ModelBuilder.Add(model);
40+
}
41+
42+
//Generate enums
43+
var tsEnumDefinitions = generator.Generate(TsGeneratorOutput.Enums);
44+
Directory.CreateDirectory(outputPath);
45+
File.WriteAllText(Path.Combine(outputPath, "enums.ts"), tsEnumDefinitions);
46+
//Generate interface definitions for all classes
47+
var tsClassDefinitions = generator.Generate(TsGeneratorOutput.Properties | TsGeneratorOutput.Fields);
48+
File.WriteAllText(Path.Combine(outputPath, "classes.d.ts"), tsClassDefinitions);
49+
50+
}
51+
}
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("TypeScriptSample.Generator")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("TypeScriptSample.Generator")]
13+
[assembly: AssemblyCopyright("Copyright © 2014")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("51ef5651-8ef9-4082-bc24-d9b915a8f2b2")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{37941D06-6CD1-43E3-81A2-72727D5985EE}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>TypeScriptSample.Generator</RootNamespace>
11+
<AssemblyName>TypeScriptSample.Generator</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Xml" />
42+
<Reference Include="TypeLite">
43+
<HintPath>..\packages\TypeLite.Lib.1.0.0.0\lib\net4\TypeLite.dll</HintPath>
44+
</Reference>
45+
<Reference Include="TypeLite.Net4">
46+
<HintPath>..\packages\TypeLite.Lib.1.0.0.0\lib\net4\TypeLite.Net4.dll</HintPath>
47+
</Reference>
48+
</ItemGroup>
49+
<ItemGroup>
50+
<Compile Include="Program.cs" />
51+
<Compile Include="Properties\AssemblyInfo.cs" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<None Include="App.config" />
55+
<None Include="packages.config" />
56+
</ItemGroup>
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
59+
Other similar extension points exist, see Microsoft.Common.targets.
60+
<Target Name="BeforeBuild">
61+
</Target>
62+
<Target Name="AfterBuild">
63+
</Target>
64+
-->
65+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="TypeLite.Lib" version="1.0.0.0" targetFramework="net45" />
4+
</packages>

src/TypeScriptSample.Web.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypeScriptSample.Web", "Typ
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypeScriptSample.Models", "TypeScriptSample.Models\TypeScriptSample.Models.csproj", "{F09679BE-2BD7-4DB6-A9B5-37C525472CDB}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypeScriptSample.Generator", "TypeScriptSample.Generator\TypeScriptSample.Generator.csproj", "{37941D06-6CD1-43E3-81A2-72727D5985EE}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{F09679BE-2BD7-4DB6-A9B5-37C525472CDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{F09679BE-2BD7-4DB6-A9B5-37C525472CDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{F09679BE-2BD7-4DB6-A9B5-37C525472CDB}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{37941D06-6CD1-43E3-81A2-72727D5985EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{37941D06-6CD1-43E3-81A2-72727D5985EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{37941D06-6CD1-43E3-81A2-72727D5985EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{37941D06-6CD1-43E3-81A2-72727D5985EE}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)