Skip to content

Instantly share code, notes, and snippets.

@smithkl42
Forked from robfe/Hubs.tt
Created January 21, 2013 06:39
Show Gist options
  • Save smithkl42/4584032 to your computer and use it in GitHub Desktop.
Save smithkl42/4584032 to your computer and use it in GitHub Desktop.
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="$(SolutionDir)\packages\Microsoft.AspNet.SignalR.Core.1.0.0-rc1\lib\net40\Microsoft.AspNet.SignalR.Core.dll" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Web" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Threading.Tasks" #>
<#@ import namespace="Microsoft.AspNet.SignalR" #>
<#@ import namespace="Microsoft.AspNet.SignalR.Hubs" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".d.ts" #>
<#
var hubmanager = new DefaultHubManager(new DefaultDependencyResolver());
#>
// Get signalr.d.ts.ts from https://github.com/borisyankov/DefinitelyTyped (or delete the reference)
/// <reference path="signalr.d.ts" />
interface SignalR {
<#
foreach (var hub in hubmanager.GetHubs())
{
#>
<#= hub.Name #> : <#= hub.HubType.Name #>;
<#
}
#>
}
//////////
// HUBS //
//////////
<#
foreach (var hub in hubmanager.GetHubs())
{
#>
//#region <#= hub.Name#>
interface <#= hub.HubType.Name #> {
server : <#= hub.HubType.Name #>Server;
client : any;
}
interface <#= hub.HubType.Name #>Server {
<#
foreach (var method in hubmanager.GetHubMethods(hub.Name ))
{
var ps = method.Parameters.Select(x => x.Name+ " : "+GetTypeContractName(x.ParameterType));
#>
<#= FirstCharLowered(method.Name) #>(<#=string.Join(", ", ps)#>) : <#= GetTypeContractName(method.ReturnType)#>;
<#
}
#>
}
//#endregion
<#
}
#>
/////////////
// CLASSES //
/////////////
<#
while(viewTypes.Count!=0)
{
var type = viewTypes.Pop();
#>
//<#= type.FullName#>
interface <#= GenericSpecificName(type) #> {
<#
foreach (var property in type.GetProperties(BindingFlags.Instance|BindingFlags.Public|BindingFlags.DeclaredOnly))
{
#>
<#= property.Name#> : <#= GetTypeContractName(property.PropertyType)#>;
<#
}
#>
}
<#
}
#>
<#+
private Stack<Type> viewTypes = new Stack<Type>();
private HashSet<Type> doneTypes = new HashSet<Type>();
private string GetTypeContractName(Type type)
{
if (type == typeof (Task))
{
return "void /*task*/";
}
if (type.IsArray)
{
return GetTypeContractName(type.GetElementType())+"[]";
}
if (type.IsGenericType && typeof(Task<>).IsAssignableFrom(type.GetGenericTypeDefinition()))
{
return GetTypeContractName(type.GetGenericArguments()[0]);
}
if (type.IsGenericType && typeof(List<>).IsAssignableFrom(type.GetGenericTypeDefinition()))
{
return GetTypeContractName(type.GetGenericArguments()[0])+"[]";
}
switch (type.Name.ToLowerInvariant())
{
case "datetime":
return "string";
case "int16":
case "int32":
case "int64":
case "single":
case "double":
return "number";
case "void":
case "string":
case "bool":
return type.Name.ToLowerInvariant();
}
if (!doneTypes.Contains(type))
{
doneTypes.Add(type);
viewTypes.Push(type);
}
return GenericSpecificName(type);
}
private string GenericSpecificName(Type type)
{
string name = type.Name;
int index = name.IndexOf('`');
name = index == -1 ? name : name.Substring(0, index);
if (type.IsGenericType)
{
name += "Of"+string.Join("And", type.GenericTypeArguments.Select(GenericSpecificName));
}
return name;
}
private string FirstCharLowered(string s)
{
return Regex.Replace(s, "^.", x => x.Value.ToLowerInvariant());
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment