Last active
March 7, 2018 11:20
-
-
Save harriyott/732666987ea89478a1bddcbcc36ef39e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template debug="false" hostspecific="false" language="C#" #> | |
<#@ assembly name="System.Core" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ assembly name="$(SolutionDir)Kittens.Web\bin\Kittens.Web.dll "#> | |
<#@ output extension=".ts" #> | |
export class KittensRoutes { | |
<# | |
// Get a collection of the constants in the class | |
var constants = typeof(Kittens.Web.KittensRoutes).GetFields(); | |
// Pull out any parameters, e.g. {kittenId} | |
var paramsRegex = new System.Text.RegularExpressions.Regex(@"(?!{)[a-zA-Z]+?(?=})"); | |
foreach(var c in constants) | |
{ | |
// The url is the value of the contant | |
var url = c.GetRawConstantValue().ToString(); | |
// The new name is a camel case version of the constant name | |
var name = c.Name.First().ToString().ToLower() + c.Name.Substring(1); | |
// Find any parameters in the url, and turn them into TS parameter syntax | |
var matches = paramsRegex.Matches(url); | |
var array = matches.Cast<System.Text.RegularExpressions.Match>().Select(x => x + ": any").ToArray(); | |
var arguments = String.Join(", ", array); | |
// If there are parameters, make a function that accepts them and uses them in a template string | |
if (matches.Count > 0) { #> | |
static <#= name #>(<#= arguments #>): string { | |
return `<#= url.Replace("{", "${") #>`; | |
} | |
<# | |
} | |
else { | |
// A property will suffice if there are no parameters | |
#> | |
static readonly <#= name #> = '<#= url #>'; | |
<# | |
} | |
} | |
#>} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment