Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheritance support #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

210 changes: 198 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,208 @@
[![Nuget.org](https://img.shields.io/nuget/v/SignalrTypescriptGenerator.svg?style=flat)](https://www.nuget.org/packages/SignalrTypescriptGenerator)
[![Build status](https://ci.appveyor.com/api/projects/status/uwhg9nwo62kx2fif?svg=true)](https://ci.appveyor.com/project/cjbhaines/signalr-hubs-typescriptgenerator)

# SignalrTypescriptGenerator
A command line tool for generating typescript definitions for Signalr
# Signalr.Hubs.TypeScriptGenerator
Utility library for generating typescript definitions for Signalr Hubs. This is a fork of [yetanotherchris/SignalrTypeScriptGenerator](https://github.com/yetanotherchris/SignalrTypeScriptGenerator "SignalrTypeScriptGenerator") by [yetanotherchris](https://github.com/yetanotherchris "yetanotherchris"). I have split the packages up into a referenced library and a console app.

This tool is based off this gist: https://gist.github.com/robfe/4583549. It works using the same C# logic, but skips the need for a T4 template which can be fiddly to get working on build servers.
Our usage at Genius Sports is to generate the Hub proxies at build time using our [geniussportsgroup/SignalR.ProxyGenerator](https://github.com/geniussportsgroup/SignalR.ProxyGenerator "Proxy Generator") publishing them to our internal NPM feed. We then use this tool to generate TypeScript definitions our those proxies again publishing them to our internal NPM feed. This allows our UI developers to get strongly typed Hub APIs and allows us to do proper Continous Integrtaion between the back end and front end. Move quickly and break fast.

### Nuget

install-package SignalrTypescriptGenerator
## Installation - Nuget

### Usage
- [Signalr.Hubs.TypeScriptGenerator](https://www.nuget.org/packages/Signalr.Hubs.TypeScriptGenerator "Signalr.Hubs.TypeScriptGenerator")
- [Signalr.Hubs.TypeScriptGenerator.Console](https://www.nuget.org/packages/Signalr.Hubs.TypeScriptGenerator.Console "Signalr.Hubs.TypeScriptGenerator.Console")

.\SignalrTypescriptGenerator.exe -a "c:\etc\path-to-myassembly.dll"
## Usage

This will print the Typescript to the console window. You can write to a file, which automatically checks if the file has changed:
### Signalr.Hubs.TypeScriptGenerator
The utility library is simple to use, load any assemblies required and then create a HubTypeScriptGenerator and call Generate. It returns the TypeScript as a string. We can't pick a specific assembly to scan because we are using the SignalR DefaultHubManager which looks in all loaded assemblies.

.\SignalrTypescriptGenerator.exe -a "c:\etc\path-to-myassembly.dll" -o "C:\temp\.myfile.d.ts"
var generator = new HubTypeScriptGenerator();
var typeScript = generator.Generate();

If you don't specify an output file, the typescript output is written to the console window. A post-build command line in Visual Studio might look like this:

$(SolutionDir)\packages\SignalrTypescriptGenerator.1.0.11\tools\SignalrTypescriptGenerator.exe -a "$(TargetPath)" -o "$(SolutionDir)src\MyProject.Web\Scripts\typings\Hubs.d.ts"
### Signalr.Hubs.TypeScriptGenerator.Console
This will output the TypeScript to the specified file path

.\Signalr.Hubs.TypeScriptGenerator.Console.exe -a "c:\etc\path-to-myassembly.dll" -o "C:\temp\.myfile.d.ts"

If the output file is not specified the result is written to standard out.

**# ONLY COMBATIBLE WITH SIGNALR VERSIONS AT 2.2.1.0 OR EARLIER #**

We have compiled this at verison 2.2.1.0 so in order for the HubManager to recognise your hubs we are using an assembly redirect. If Microsoft release a new version we will need to update this.

<dependentAssembly>
<assemblyIdentity name="Microsoft.AspNet.SignalR.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.1.0" newVersion="2.2.1.0" />
</dependentAssembly>

### Data Contract Property Name
Sometimes the serialized name of your data contract properties are changed from the actual C# property name. This is done through the DataMember property:

[DataContract]
public class SomethingDto
{
[DataMember(Name = "iChangedTheName")]
public string Property1 { get; set; }

[DataMember]
public Guid Property2 { get; set; }
}

This library will respect the DataMember name and use this as the TypeScript property name:

declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts
{
interface SomethingDto
{
iChangedTheName : string;
Property2 : System.Guid;
}
}

## Example Output


/// Autogenerated at 16/09/2016 13:22:56 by https://github.com/geniussportsgroup/Signalr.Hubs.TypeScriptGenerator
/// <reference path="../signalr/index.d.ts" />
/// <reference path="../jquery/index.d.ts" />

// Hubs
interface SignalR
{
hubA : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs.HubA;
hubC : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs.HubC;
hubB : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs.HubB;
}

// Service contracts

declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs
{

interface HubA
{
server : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs.HubAServer;
client : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs.IHubAClient;
}

interface HubAServer
{
getSomething() : JQueryPromise<GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts.SomethingDto>;
ping() : JQueryPromise<void>;
}
}


declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs
{

interface HubC
{
server : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs.HubCServer;
// Hub does not have a Client Interface as a generic argument - it is recommend to add one
client : any;
}

interface HubCServer
{
aServerSideMethod() : JQueryPromise<void>;
}
}


declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs
{

interface HubB
{
server : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs.HubBServer;
client : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs.IHubBClient;
}

interface HubBServer
{
getOtherSomething() : JQueryPromise<GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts.OtherSomethingDto>;
doOtherSomethingElse() : JQueryPromise<void>;
}
}


// Clients

declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs
{
interface IHubAClient
{
pong : () => void;
takeThis : (somethingDto : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts.SomethingDto) => void;
}
}


declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.Hubs
{
interface IHubBClient
{
takeOtherThis : (otherSomethingDto : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts.OtherSomethingDto) => void;
}
}


// Data contracts

declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts
{
interface OtherSomethingDto
{
Property1 : string;
Property2 : Date;
Property3 : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts.InnerSomethingDto;
Property4 : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts.InnerSomethingDto;
}
}


declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts
{
interface InnerSomethingDto
{
InnerProperty1 : number;
innerProperty2 : Date;
innerProperty3WithCrazyCustomName : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts.SomethingEnum;
}
}


declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts
{
interface SomethingDto
{
iChangedTheName : string;
Property2 : System.Guid;
Property3 : GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts.InnerSomethingDto;
}
}


declare module System
{
interface Guid
{
}
}


// Enums

declare module GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts
{
enum SomethingEnum
{
One = 0,
Two = 1,
Three = 2,
}
}


48 changes: 48 additions & 0 deletions Signalr.Hubs.TypescriptGenerator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Signalr.Hubs.TypeScriptGenerator", "src\Signalr.Hubs.TypeScriptGenerator\Signalr.Hubs.TypeScriptGenerator.csproj", "{C90F8C7B-213B-4CD0-A9F0-3A39634CECDE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Signalr.Hubs.TypeScriptGenerator.Console", "src\Signalr.Hubs.TypeScriptGenerator.Console\Signalr.Hubs.TypeScriptGenerator.Console.csproj", "{B68600AA-F2AE-4EBF-9AF5-DEF252E71142}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "App", "App", "{AE98D3B3-6832-4544-A021-3C32156F971B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".sln", ".sln", "{6C40DD74-1C6E-4292-9252-252D1573C321}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sample", "Sample", "{5522BF7C-CC56-45CA-8C07-A735916CC2BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleUsage", "src\SampleUsage\SampleUsage.csproj", "{668072BF-5142-4796-BA67-8CB676C7196C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C90F8C7B-213B-4CD0-A9F0-3A39634CECDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C90F8C7B-213B-4CD0-A9F0-3A39634CECDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C90F8C7B-213B-4CD0-A9F0-3A39634CECDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C90F8C7B-213B-4CD0-A9F0-3A39634CECDE}.Release|Any CPU.Build.0 = Release|Any CPU
{B68600AA-F2AE-4EBF-9AF5-DEF252E71142}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B68600AA-F2AE-4EBF-9AF5-DEF252E71142}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B68600AA-F2AE-4EBF-9AF5-DEF252E71142}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B68600AA-F2AE-4EBF-9AF5-DEF252E71142}.Release|Any CPU.Build.0 = Release|Any CPU
{668072BF-5142-4796-BA67-8CB676C7196C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{668072BF-5142-4796-BA67-8CB676C7196C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{668072BF-5142-4796-BA67-8CB676C7196C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{668072BF-5142-4796-BA67-8CB676C7196C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C90F8C7B-213B-4CD0-A9F0-3A39634CECDE} = {AE98D3B3-6832-4544-A021-3C32156F971B}
{B68600AA-F2AE-4EBF-9AF5-DEF252E71142} = {AE98D3B3-6832-4544-A021-3C32156F971B}
{668072BF-5142-4796-BA67-8CB676C7196C} = {5522BF7C-CC56-45CA-8C07-A735916CC2BE}
EndGlobalSection
EndGlobal
22 changes: 0 additions & 22 deletions SignalrTypescriptGenerator.sln

This file was deleted.

Binary file removed icon.png
Binary file not shown.
27 changes: 27 additions & 0 deletions src/SampleUsage/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
</appSettings>

<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
11 changes: 11 additions & 0 deletions src/SampleUsage/DataContracts/InheritedSomethingDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Runtime.Serialization;

namespace GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts
{
[DataContract]
public class InheritedSomethingDto : SomethingDto
{
[DataMember]
public int InheritedSomethingDtoProperty1 { get; set; }
}
}
18 changes: 18 additions & 0 deletions src/SampleUsage/DataContracts/InnerSomethingDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Runtime.Serialization;

namespace GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts
{
[DataContract]
public class InnerSomethingDto
{
[DataMember]
public int InnerProperty1 { get; set; }

[DataMember(Name = "innerProperty2")]
public DateTime InnerProperty2 { get; set; }

[DataMember(Name = "innerProperty3WithCrazyCustomName")]
public SomethingEnum InnerProperty3 { get; set; }
}
}
21 changes: 21 additions & 0 deletions src/SampleUsage/DataContracts/OtherSomethingDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Runtime.Serialization;

namespace GeniusSports.Signalr.Hubs.TypeScriptGenerator.SampleUsage.DataContracts
{
[DataContract]
public class OtherSomethingDto
{
[DataMember]
public string Property1 { get; set; }

[DataMember]
public DateTime Property2 { get; set; }

[DataMember]
public InnerSomethingDto Property3 { get; set; }

[DataMember]
public InnerSomethingDto Property4 { get; set; }
}
}
Loading