Veikedo

tst angular dto

Feb 13th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.23 KB | None | 0 0
  1. ${
  2.  using System.IO;
  3.     using System.Text;
  4.     using File = Typewriter.CodeModel.File;
  5.  
  6.  string FilePath(File f)
  7.  {
  8.         if (f.Classes.Any()) {
  9.       var @class = f.Classes.Single();
  10.       var fileName = ModuleName(@class) + "_" + Path.ChangeExtension(@class.Name, "ts");
  11.  
  12.       var path = fileName;
  13.       return path;
  14.         } else {
  15.             var @enum = f.Enums.Single();
  16.       var fileName = ModuleName(@enum) + "_" + Path.ChangeExtension(@enum.Name, "ts");
  17.  
  18.       var path = fileName;
  19.       return path;
  20.         }
  21.  }
  22.  
  23.     Template(Settings settings)
  24.     {
  25.         settings.OutputFilenameFactory = FilePath;
  26.     }
  27.  
  28.  const string ApiNamespace = "OneInc.PolicyOne.HtmlFrontend.ClientApi";
  29.  
  30.  bool BelongsToClientApi(string @namespace) => @namespace.StartsWith(ApiNamespace) && !@namespace.Contains("CodeGenerationSupport");
  31.  bool BelongsToClientApi(Class @class) => BelongsToClientApi(@class.Namespace) && !@class.Attributes.Any(a => a.Name == "ClientRefIgnore");
  32.  bool BelongsToClientApi(Enum @enum) => BelongsToClientApi(@enum.Namespace) && !@enum.Attributes.Any(a => a.Name == "ClientRefIgnore");
  33.  bool BelongsToClientApi(Type t) => BelongsToClientApi(t.Namespace) && !t.Attributes.Any(a => a.Name == "ClientRefIgnore");
  34.  
  35.     bool IsRecursivelyDefined(Property p) => p.Attributes.Any(a => a.Name == "RecursivelyDefined");
  36.  
  37.  bool IsController(Class @class)
  38.  {
  39.         var parentClass = @class.BaseClass;
  40.         if (parentClass != null)
  41.         {
  42.            if (parentClass.Name == "Controller")
  43.            {
  44.               return true;
  45.            }
  46.            else
  47.            {
  48.               return IsController(parentClass);
  49.            }
  50.         }
  51.  
  52.         return false;
  53.  }
  54.  
  55.  string ModuleName(string @namespace)
  56.     {
  57.         var relativeNamespace = @namespace.Replace(ApiNamespace, "").Remove(0, 1);
  58.         return relativeNamespace.Split(new [] {"."}, StringSplitOptions.None).First();
  59.     }
  60.  
  61.  string ModuleName(Class @class) => ModuleName(@class.Namespace);
  62.  string ModuleName(Enum @enum) => ModuleName(@enum.Namespace);
  63.  string ModuleName(Type t) => ModuleName(t.Namespace);
  64.  
  65.  Type UnwrapCollections(Type t)
  66.  {
  67.   if(!t.IsGeneric) return t;
  68.   if(!t.IsEnumerable) return t;
  69.  
  70.   var itemType = t.TypeArguments.Single();
  71.   return itemType;
  72.  }
  73.  
  74.     string ProduceTargetTypeName(Type t, bool isRecursivelyDefined)
  75.     {
  76.   if(BelongsToClientApi(t))
  77.         {
  78.             return t.Name;
  79.         }
  80.         else
  81.         {
  82.             if(t == "Date")
  83.             {
  84.                 return "string";
  85.             }
  86.  
  87.             return t.Name;
  88.         }
  89.     }
  90.  
  91.     void AppendLineWithTabs (StringBuilder sb, string str, int i)
  92.     {
  93.         sb.Append(' ', i*4);
  94.         sb.AppendLine(str);
  95.     }
  96.  
  97.     string RewrittenTypeName(Type t, bool isRecursivelyDefined)
  98.     {
  99.       var unwrappedType = UnwrapCollections(t);
  100.       var hasUnwrapped = unwrappedType != t;
  101.       var targetTypeName = ProduceTargetTypeName(unwrappedType, isRecursivelyDefined);
  102.       return hasUnwrapped ? targetTypeName + "[]" : targetTypeName;
  103.     }
  104.  
  105.     string RewrittenTypeName(Property p)
  106.     {
  107.       return RewrittenTypeName(p.Type, IsRecursivelyDefined(p));
  108.     }
  109.  
  110.     string RewrittenTypeNameForDecorator(Property p) {
  111.         var unwrappedType = UnwrapCollections(p.Type);
  112.         if(unwrappedType.IsEnum)
  113.         {
  114.             return "Number";
  115.         }
  116.         var rewrittenTypeName = ProduceTargetTypeName(unwrappedType, IsRecursivelyDefined(p));
  117.  
  118.         if(p.Type.IsPrimitive) {
  119.             rewrittenTypeName = char.ToUpper(rewrittenTypeName[0]) + rewrittenTypeName.Substring(1);
  120.         }
  121.         return rewrittenTypeName;
  122.     }
  123.  
  124.     bool IsEnumerable(Property p) => p.Type.IsEnumerable;
  125.     bool HasProperties(Class c) => c.Properties.Any();
  126.     bool HasBase(Class c) => c.BaseClass != null;
  127.     string BaseClassModuleName(Class c) => ModuleName(c.BaseClass);
  128.     IEnumerable<Type> PropertyTypesToImport(Class c) => c.Properties
  129.         .Select(p => UnwrapCollections(p.Type))
  130.         .Where(t => t.FullName != c.FullName)
  131.         .GroupBy(t => t.FullName)
  132.         .Select(g => g.First());
  133.  
  134.     const int maxOrderNum = int.MaxValue;
  135.     const string orderIndexAttributeName = "OrderIndex";
  136.  
  137.     static string GenerateOrderedEnumValues(Enum e)
  138.     {
  139.         if(!e.Values.Any()) return string.Empty;
  140.         var sb = new StringBuilder();
  141.         var tabsCount = 1;
  142.  
  143.         // HACK [as] We need to use Tuple<> because typewriter doesnt support anonymous classes
  144.         var t = e.Values
  145.             .Where(x => !x.Attributes.Any(a => a.Name == "ClientRefIgnoreEnumValue"))
  146.             .Select(x => new Tuple<EnumValue, Attribute>(x, x.Attributes.SingleOrDefault(a => a.Name == orderIndexAttributeName)))
  147.             .Select(x => new Tuple<EnumValue, int>(x.Item1, x.Item2 != null ? int.Parse(x.Item2.Value) : maxOrderNum))
  148.             .OrderBy(x => x.Item2);
  149.  
  150.  
  151.         for (var i = 0; i < t.Count(); i++)
  152.         {
  153.             var value = t.ElementAt(i);
  154.             var enumValueTemplate = "{0} = {1}";
  155.  
  156.             var isCommaRequired = i != t.Count() - 1;
  157.             if(isCommaRequired)
  158.             {
  159.                 enumValueTemplate += ",";
  160.             }
  161.  
  162.             AppendLineWithTabs(sb, string.Format("/// {0}",  value.Item1.DocComment), tabsCount);
  163.             AppendLineWithTabs(sb, string.Format(enumValueTemplate, value.Item1.Name, value.Item1.Value), tabsCount);
  164.         }
  165.  
  166.         return sb.ToString();
  167.     }
  168.  
  169.     string GenerateSwitchString(Enum e)
  170.     {
  171.         var sb = new StringBuilder();
  172.         var tabsCount = 2;
  173.  
  174.         AppendLineWithTabs(sb, "switch(value)", tabsCount);
  175.         AppendLineWithTabs(sb, "{", tabsCount++);
  176.         foreach(var value in e.Values)
  177.         {
  178.             var ignoreAttribute = value.Attributes.SingleOrDefault(x=>x.Name == "ClientRefIgnoreEnumValue");
  179.             if(ignoreAttribute != null)
  180.             {
  181.                 continue;
  182.             }
  183.  
  184.             var presentationAttribute = value.Attributes.SingleOrDefault(x=>x.Name == "StringValuePresentation");
  185.             var stringValue = presentationAttribute != null ?
  186.                 presentationAttribute.Value :
  187.                 value.Name;
  188.  
  189.             AppendLineWithTabs(sb, $"case {e.Name}.{value.Name}:", tabsCount++);
  190.             AppendLineWithTabs(sb, $"return \"{stringValue}\";", tabsCount--);
  191.         }
  192.         AppendLineWithTabs(sb, "}", --tabsCount);
  193.         return sb.ToString();
  194.     }
  195.  
  196. }// ! Generated Code ! $Classes(c => !IsController(c) && BelongsToClientApi(c))[
  197. $HasProperties[import {mappable} from "../infrastructure/ObjectMapper/MapConfiguration";
  198. ]$PropertyTypesToImport[$BelongsToClientApi[import {$Name} from "./$ModuleName_$Name";
  199. ]]$HasBase[import {$BaseClass} from "./$BaseClassModuleName_$BaseClass";
  200. ]
  201. export class $Name $HasBase[extends $BaseClass ][]{$Properties[
  202.     @mappable($IsEnumerable[$RewrittenTypeNameForDecorator][])
  203.     public $name: $RewrittenTypeName;]
  204. }
  205. ]$Enums(e=> BelongsToClientApi(e))[
  206.  
  207. // ReSharper disable InconsistentNaming
  208. export enum $Name {
  209. $GenerateOrderedEnumValues
  210. }
  211.  
  212. export module $Name {
  213.     export function getStringValuePresentation(value: $Name): string {
  214. $GenerateSwitchString
  215.     }
  216. }
  217.  
  218.  
  219. // ReSharper restore InconsistentNaming
  220. ]
Add Comment
Please, Sign In to add comment