Created
March 25, 2018 11:18
-
-
Save MNie/ba0cdc9ab7423dcd29881c2da04aeb11 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
${ | |
using Typewriter.Extensions.WebApi; | |
using System.Text.RegularExpressions; | |
const string ContractPrefix = "App.Contract"; | |
static string[] NotIncluedeControllers = new []{"Excel", "Diagnostics", "Download"}; | |
string FileNameFromType(string fullName) { | |
var fullNameWithoutGenericParameter = Regex.Replace(fullName, @" ?\<.*?\>", string.Empty); | |
return fullNameWithoutGenericParameter | |
.Split(new string[]{ContractPrefix}, StringSplitOptions.None) | |
.Last() | |
.Replace(".","") | |
.TrimEnd('?'); | |
} | |
IEnumerable<Type> GetTypes(Type t) { | |
if(t.IsGeneric){ | |
yield return t.TypeArguments.First(); | |
} | |
else { | |
yield return t; | |
} | |
} | |
string ImportStatements(Class @class) { | |
var otherTypesInFile = new List<Type>() | |
.Concat(@class.Methods.Select(x => x.Type)) | |
.Concat(@class.Methods.SelectMany(x => x.Type.TypeArguments)) | |
.Concat(@class.Methods.SelectMany(x => x.Parameters).Select(x => x.Type)) | |
.Concat(@class.Methods.SelectMany(x => x.Parameters).SelectMany(x => x.Type.TypeArguments)) | |
.SelectMany(GetTypes) | |
.ToList(); | |
if (@class.BaseClass != null){ | |
otherTypesInFile.Add(@class.BaseClass); | |
} | |
var imports = otherTypesInFile | |
.Where(t => t.FullName.StartsWith(ContractPrefix)) | |
.Select(t => { | |
return $"import {{ {t.Name} }} from \"../contract/{FileNameFromType(t.FullName)}\";"; | |
}) | |
.Distinct(); | |
return String.Join("\r\n", imports); | |
} | |
string MethodType(Method m) { | |
var containsVerbs = m.HttpMethod().Contains("verbs"); | |
return containsVerbs | |
? "post" | |
: m.HttpMethod(); | |
} | |
string NameFromRoute(Method m) { | |
var lastRoute = m.Route() | |
.Split(new string[]{"/"}, StringSplitOptions.RemoveEmptyEntries) | |
.Last(); | |
return lastRoute.Contains("{") | |
? m.Name | |
: lastRoute; | |
} | |
bool ShouldScanController(Class c) { | |
return c.FullName.StartsWith("App.Web.Controllers.Public", StringComparison.OrdinalIgnoreCase) | |
&& !NotIncluedeControllers.Any(x => c.Name.StartsWith(x, StringComparison.OrdinalIgnoreCase)); | |
} | |
} | |
//*************************DO NOT MODIFY************************** | |
// | |
//THESE FILES ARE AUTOGENERATED WITH TYPEWRITER AND ANY MODIFICATIONS MADE HERE WILL BE LOST | |
//PLEASE VISIT http://frhagn.github.io/Typewriter/ TO LEARN MORE ABOUT THIS VISUAL STUDIO EXTENSION | |
// | |
//*************************DO NOT MODIFY************************** | |
import { Injectable } from "@angular/core"; | |
import { Observable } from "rxjs"; | |
import { CallService } from "../core/core.module"; | |
$Classes($ShouldScanController)[$ImportStatements | |
@Injectable() | |
export class $Name { | |
constructor(private callService: CallService) { | |
} $Methods[ | |
public $NameFromRoute = ($Parameters[$name: $Type][, ]): Observable<$Type> => { | |
return this.callService.$MethodType(`$Url`, $RequestData); | |
}] | |
}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment