Veikedo

Untitled

Feb 19th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. ${
  2.     using Typewriter.Extensions.Types;
  3.     using System.IO;
  4.     using System.Text;
  5.  
  6.     Template(Settings settings)
  7.     {
  8.         settings.IncludeProject("ARMS.ReportWriter.DataModel")
  9.                 .IncludeProject("ARMS.NIBRS")
  10.                 .IncludeProject("ARMS.ReportWriter.Common")
  11.             ;
  12.  
  13.         settings.OutputFilenameFactory = file => file.Name
  14.             .Replace("Dto", "")
  15.             .Replace(".cs", ".ts");
  16.     }
  17.  
  18.     string FixedName(Class c) => FixName(c.Name);
  19.     string FixedName(Property prop) => FixName(prop.Type);
  20.     string FixedName(string name) => FixName(name);
  21.     static string FixName(string name) => name.Replace("Dto", "");
  22.  
  23.     string GetClassName(Class c)
  24.     {
  25.         var t = c.Attributes.First(a => a.Name == "GenerateTypeScript").Value;
  26.         return t == "null"? FixName(c.Name) : t;
  27.         ;
  28.     }
  29.  
  30.     string GetConstantValue(Constant c)
  31.     {
  32.         return c.Type == "string" ? $"\"{c.Value}\"" : c.Value;
  33.     }
  34.  
  35.     Type UnwrapCollections(Type t)
  36.     {
  37.         if(!t.IsGeneric) return t;
  38.         if(!t.IsEnumerable) return t;
  39.  
  40.         var itemType = t.TypeArguments.Single();
  41.         return itemType;
  42.     }    
  43.    
  44.     string TypesForImport(Property p) {
  45.         var type = UnwrapCollections(p.Type);        
  46.         var str = new[] {type}
  47.             .Where(NeedToImport)
  48.             .Select(c => FixName(c.ClassName()));
  49.      
  50.         return string.Join(Environment.NewLine, str);
  51.     }
  52.  
  53.     string FileFromImport(Property p) {
  54.         return $"./{FixName(p.Type.ClassName())}";
  55.     }
  56.  
  57.     bool NeedToImport(Type type) =>  UnwrapCollections(type).Namespace.StartsWith("ARMS");
  58.     bool NeedToImport(Property p) => NeedToImport(p.Type);
  59. }
  60.  
  61. $Classes([GenerateTypeScript])[
  62. $Properties(p => NeedToImport(p))[
  63. import {$TypesForImport} from "$FileFromImport";]
  64.  
  65. export class $GetClassName {
  66.     $Properties[
  67.     $name: $FixedName;]
  68.     $Constants[
  69.     static $name: $Type = $GetConstantValue;]
  70. }]
  71.  
  72. $Classes[
  73. $NestedClasses([GenerateTypeScript])[
  74. export class $GetClassName {
  75.     $Properties[
  76.     $name: $FixedName;]
  77.     $Constants[
  78.     static $name: $Type = $GetConstantValue;]
  79. }]]
  80.  
  81. $Enums([GenerateTypeScript])[
  82. export enum $Name {
  83.     $Values[
  84.     $Name = $Value,]
  85. }]
Advertisement
Add Comment
Please, Sign In to add comment