예제 #1
0
        public void GenerateDataModelAST(TypeScriptClassModel classModel, string outputPath)
        {
            var request = new ClassGenerationRequest();

            string fileName = $"{NameCaseConverter.ToKebabCase(classModel.Name)}.model.{TypeScriptFileExtension.File}";

            var typeGenerator = new TypeGenerator();

            request.OutputPath = Path.Combine(outputPath, "models", fileName);

            request.DataModel = new ClassModel()
            {
                Name           = classModel.Name,
                BaseClass      = classModel.BaseClass,
                Decorators     = new string[] { },
                TypeParameters = classModel.TypeParameters.Select(i => i.Name).ToArray(),
                Imports        = classModel.Imports.Select(i => new ImportModel()
                {
                    Names = new string[] { i.Name },
                    Path  = i.DependencyKind == DependencyKind.Model
                        ? $"./{NameCaseConverter.ToKebabCase(i.Name)}.model"
                        : $"../enums/{NameCaseConverter.ToKebabCase(i.Name)}.enum"
                }).ToArray(),
                Properties = classModel.Properties.Select(p => new PropertyModel()
                {
                    Name         = NameCaseConverter.ToCamelCase(p.Name),
                    Type         = typeGenerator.GetEmittedType(p.Type),
                    IsPrivate    = false,
                    InitialValue = null,
                }).ToArray(),
            };

            var result = CallGenerator("/generate/class", CreateStringContent(request));
        }
예제 #2
0
 public void ToCamelCaseTest()
 {
     Assert.AreEqual("urlValue", NameCaseConverter.ToCamelCase("URLValue"));
     Assert.AreEqual("url", NameCaseConverter.ToCamelCase("URL"));
     Assert.AreEqual("id", NameCaseConverter.ToCamelCase("ID"));
     Assert.AreEqual("i", NameCaseConverter.ToCamelCase("I"));
     Assert.AreEqual("", NameCaseConverter.ToCamelCase(""));
     Assert.AreEqual(null, NameCaseConverter.ToCamelCase(null));
     Assert.AreEqual("person", NameCaseConverter.ToCamelCase("Person"));
     Assert.AreEqual("iPhone", NameCaseConverter.ToCamelCase("iPhone"));
     Assert.AreEqual("iPhone", NameCaseConverter.ToCamelCase("IPhone"));
     Assert.AreEqual("i Phone", NameCaseConverter.ToCamelCase("I Phone"));
     Assert.AreEqual("i  Phone", NameCaseConverter.ToCamelCase("I  Phone"));
     Assert.AreEqual(" IPhone", NameCaseConverter.ToCamelCase(" IPhone"));
     Assert.AreEqual(" IPhone ", NameCaseConverter.ToCamelCase(" IPhone "));
     Assert.AreEqual("isCIA", NameCaseConverter.ToCamelCase("IsCIA"));
     Assert.AreEqual("vmQ", NameCaseConverter.ToCamelCase("VmQ"));
     Assert.AreEqual("xml2Json", NameCaseConverter.ToCamelCase("Xml2Json"));
     Assert.AreEqual("snAkEcAsE", NameCaseConverter.ToCamelCase("SnAkEcAsE"));
     Assert.AreEqual("snA__kEcAsE", NameCaseConverter.ToCamelCase("SnA__kEcAsE"));
     Assert.AreEqual("snA__ kEcAsE", NameCaseConverter.ToCamelCase("SnA__ kEcAsE"));
     Assert.AreEqual("already_snake_case_ ", NameCaseConverter.ToCamelCase("already_snake_case_ "));
     Assert.AreEqual("isJSONProperty", NameCaseConverter.ToCamelCase("IsJSONProperty"));
     Assert.AreEqual("shoutinG_CASE", NameCaseConverter.ToCamelCase("SHOUTING_CASE"));
     Assert.AreEqual("9999-12-31T23:59:59.9999999Z", NameCaseConverter.ToCamelCase("9999-12-31T23:59:59.9999999Z"));
     Assert.AreEqual("hi!! This is text. Time to test.", NameCaseConverter.ToCamelCase("Hi!! This is text. Time to test."));
 }
예제 #3
0
        public string GenerateService(TypeScriptServiceModel serviceModel)
        {
            var sb = new StringBuilder();

            var typeGenerator = new TypeGenerator();

            sb.Append(AutogeneratedHeader_Compact());

            // angular imports
            sb.AppendLine("import { Injectable } from '@angular/core';");
            sb.AppendLine("import { Headers, Http, Response } from '@angular/http';");
            sb.AppendLine();

            // other imports
            sb.AppendLine("import 'rxjs/add/operator/toPromise';");
            sb.AppendLine();

            // dependencies
            GenerateImportDeclarations(serviceModel.Imports, sb);
            sb.AppendLine();

            // mark for dependency injection
            sb.AppendLine("@Injectable()");

            // class declaration
            sb.AppendLine("export class " + serviceModel.Name + "Service {");
            sb.AppendLine();

            // route prefix
            sb.AppendLine("\t" + $"private baseUrl = '{serviceModel.RoutePrefix}';");
            sb.AppendLine();

            // constructor
            sb.AppendLine("\t" + "constructor(private http: Http) { }");
            sb.AppendLine();

            foreach (var method in serviceModel.Methods)
            {
                sb.AppendLine("\t" + NameCaseConverter.ToCamelCase(method.Name) + $"(): Promise<{typeGenerator.GetEmittedType(method.ReturnType)}> {{");
                sb.AppendLine("\t\t" + $"return this.http.{MapHttpMethod(method.HttpMethod)}(this.baseUrl)");
                sb.AppendLine("\t\t\t" + ".toPromise()");
                sb.AppendLine("\t\t\t" + ".then(response => response.json())");
                sb.AppendLine("\t\t\t" + ".catch(this.handleError);");
                sb.AppendLine("\t" + "}");

                sb.AppendLine();
            }


            // error handler
            sb.AppendLine("\t" + "private handleError(error: any): Promise<any> {");
            sb.AppendLine("\t\t" + "return Promise.reject(error.message || error);");
            sb.AppendLine("\t" + "}");

            sb.AppendLine("}");

            return(sb.ToString());
        }
예제 #4
0
        public string GenerateClass(TypeScriptClassModel classModel, bool includeHeader = true)
        {
            var sb = new StringBuilder();

            if (includeHeader)
            {
                sb.Append(AutogeneratedHeader_Compact());
            }

            var typeGenerator = new TypeGenerator();

            // imports
            GenerateImportDeclarations(classModel.Imports, sb);

            // class declaration
            sb.AppendLine("export class "
                          + classModel.Name
                          + (classModel.IsGeneric ? $"<{GenerateTypeParameters(classModel.TypeParameters)}>" : "")
                          + (string.IsNullOrEmpty(classModel.BaseClass) ? "" : " extends " + classModel.BaseClass)
                          + " {");

            // properties
            foreach (var property in classModel.Properties)
            {
                var emittedType = typeGenerator.GetEmittedType(property.Type);

                sb.AppendLine("\t"                                           // indentation
                              + NameCaseConverter.ToCamelCase(property.Name) // property name
                              + (property.IsOptional ? "?" : "")             // optional?
                              + ": "
                              + emittedType                                  // property type
                              + ";");
            }

            sb.AppendLine("}");
            // sb.AppendLine();

            return(sb.ToString());
        }
예제 #5
0
        public void GenerateServiceAST(TypeScriptServiceModel serviceModel, string outputPath)
        {
            var request = new ClassGenerationRequest();

            string fileName = $"{NameCaseConverter.ToKebabCase(serviceModel.Name)}.service.{TypeScriptFileExtension.File}";

            var typeGenerator = new TypeGenerator();

            request.OutputPath = Path.Combine(outputPath, "services", fileName);

            var imports = new List <ImportModel>()
            {
                new ImportModel()
                {
                    Names = new string[] { "Injectable" }, Path = "@angular/core"
                },
                new ImportModel()
                {
                    Names = new string[] { "Http", "Headers", "Response" }, Path = "@angular/http"
                },
                new ImportModel()
                {
                    Names = new string[] { }, Path = "rxjs/add/operator/toPromise"
                }
            };

            var specificImports = serviceModel.Imports.Select(i => new ImportModel()
            {
                Names = new string[] { i.Name },
                Path  = i.DependencyKind == DependencyKind.Model
                    ? $"../models/{NameCaseConverter.ToKebabCase(i.Name)}.model"
                    : $"../enums/{NameCaseConverter.ToKebabCase(i.Name)}.enum"
            }).ToList();

            imports.AddRange(specificImports);

            request.DataModel = new ClassModel()
            {
                Name           = serviceModel.Name + "Service",
                BaseClass      = null,
                Decorators     = new string[] { "Injectable()" },
                TypeParameters = new string[] { },
                Imports        = imports.ToArray(),
                Properties     = new PropertyModel[]
                {
                    new PropertyModel()
                    {
                        Name = "baseUrl", Type = null, IsPrivate = true, InitialValue = serviceModel.RoutePrefix
                    }
                },
                ConstructorDef = new ConstructorModel()
                {
                    Parameters = new ParameterModel[]
                    {
                        new ParameterModel()
                        {
                            Name = "http", Type = "Http", IsPrivate = true
                        }
                    }
                },
                Methods = serviceModel.Methods.Select(m => new HttpMethodModel()
                {
                    Name       = NameCaseConverter.ToCamelCase(m.Name),
                    ReturnType = typeGenerator.GetEmittedType(m.ReturnType),
                    HttpMethod = m.HttpMethod,
                    Route      = m.Route,
                    Parameters = m.Parameters.Select(p => new ParameterModel()
                    {
                        Name      = p.Item2,
                        Type      = typeGenerator.GetEmittedType(p.Item1),
                        IsPrivate = false
                    }).ToArray()
                }).ToArray()
            };

            var result = CallGenerator("/generate/class", CreateStringContent(request));
        }