Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ${
- using Typewriter.Extensions.Types;
- using System.IO;
- using System.Text;
- Template(Settings settings)
- {
- settings.IncludeProject("ARMS.ReportWriter.DataModel")
- .IncludeProject("ARMS.NIBRS")
- .IncludeProject("ARMS.ReportWriter.Common")
- ;
- settings.OutputFilenameFactory = file => file.Name
- .Replace("Dto", "")
- .Replace(".cs", ".ts");
- }
- string FixedName(Class c) => FixName(c.Name);
- string FixedName(Property prop) => FixName(prop.Type);
- string FixedName(string name) => FixName(name);
- static string FixName(string name) => name.Replace("Dto", "");
- string GetClassName(Class c)
- {
- var t = c.Attributes.First(a => a.Name == "GenerateTypeScript").Value;
- return t == "null"? FixName(c.Name) : t;
- ;
- }
- string GetConstantValue(Constant c)
- {
- return c.Type == "string" ? $"\"{c.Value}\"" : c.Value;
- }
- Type UnwrapCollections(Type t)
- {
- if(!t.IsGeneric) return t;
- if(!t.IsEnumerable) return t;
- var itemType = t.TypeArguments.Single();
- return itemType;
- }
- string TypesForImport(Property p) {
- var type = UnwrapCollections(p.Type);
- var str = new[] {type}
- .Where(NeedToImport)
- .Select(c => FixName(c.ClassName()));
- return string.Join(Environment.NewLine, str);
- }
- string FileFromImport(Property p) {
- return $"./{FixName(p.Type.ClassName())}";
- }
- bool NeedToImport(Type type) => UnwrapCollections(type).Namespace.StartsWith("ARMS");
- bool NeedToImport(Property p) => NeedToImport(p.Type);
- }
- $Classes([GenerateTypeScript])[
- $Properties(p => NeedToImport(p))[
- import {$TypesForImport} from "$FileFromImport";]
- export class $GetClassName {
- $Properties[
- $name: $FixedName;]
- $Constants[
- static $name: $Type = $GetConstantValue;]
- }]
- $Classes[
- $NestedClasses([GenerateTypeScript])[
- export class $GetClassName {
- $Properties[
- $name: $FixedName;]
- $Constants[
- static $name: $Type = $GetConstantValue;]
- }]]
- $Enums([GenerateTypeScript])[
- export enum $Name {
- $Values[
- $Name = $Value,]
- }]
Advertisement
Add Comment
Please, Sign In to add comment