public ApiAppClass(string ns, Func <string, Stream> createStream, AppApiTemplate template, AppVersionKey versionKey)
 {
     this.ns           = ns;
     this.createStream = createStream;
     this.template     = template;
     this.versionKey   = versionKey;
 }
Beispiel #2
0
    public async Task Output(AppApiTemplate appTemplate)
    {
        await generateApp(appTemplate);

        var formTemplates         = appTemplate.FormTemplates(ApiCodeGenerators.TypeScript);
        var complexFieldTemplates = formTemplates.SelectMany(ft => ft.Form.ComplexFieldTemplates).Distinct();

        foreach (var complexFieldTemplate in complexFieldTemplates)
        {
            await generateComplexField(complexFieldTemplate, false);
            await generateComplexFieldView(complexFieldTemplate, false);
        }
        foreach (var formTemplate in formTemplates)
        {
            await generateComplexField(formTemplate.Form, true);
            await generateComplexFieldView(formTemplate.Form, true);
        }
        foreach (var group in appTemplate.GroupTemplates)
        {
            await generateGroup(group);
        }
        await generateEntities(appTemplate);

        foreach (var numericValueTemplate in appTemplate.NumericValueTemplates(ApiCodeGenerators.TypeScript))
        {
            await generateNumericValue(numericValueTemplate);
        }
    }
Beispiel #3
0
    private Task generateEntities(AppApiTemplate appTemplate)
    {
        var tsFile = new TypeScriptFile($"{appTemplate.Name}Entities.d.ts", createStream);

        tsFile.AddLine();
        foreach (var objectTemplate in appTemplate.ObjectTemplates(ApiCodeGenerators.TypeScript))
        {
            tsFile.AddLine($"interface I{objectTemplate.DataType.Name} {{");
            tsFile.Indent();
            foreach (var property in objectTemplate.PropertyTemplates)
            {
                var tsType = getTsType(property.ValueTemplate);
                tsFile.AddLine($"{property.Name}: {tsType};");
            }
            tsFile.Outdent();
            tsFile.AddLine("}");
        }
        foreach (var numericValueTemplate in appTemplate.NumericValueTemplates(ApiCodeGenerators.TypeScript))
        {
            tsFile.AddLine($"interface I{numericValueTemplate.DataType.Name} {{");
            tsFile.Indent();
            tsFile.AddLine($"Value: number;");
            tsFile.AddLine($"DisplayText: string;");
            tsFile.Outdent();
            tsFile.AddLine("}");
        }
        return(tsFile.Output());
    }
    public async Task Output(AppApiTemplate appTemplate)
    {
        foreach (var group in appTemplate.GroupTemplates)
        {
            var controller          = createController(appTemplate, group);
            var controllerClassName = getControllerClassName(group);
            var cSharpFile          = new CSharpFile(controller, createStream, controllerClassName);
            await cSharpFile.Output();
        }
        var namespaces = appTemplate.ObjectTemplates(ApiCodeGenerators.Dotnet)
                         .Select(o => o.DataType.Namespace ?? "")
                         .Union
                         (
            new[]
        {
            "Microsoft.AspNetCore.Authorization",
            "Microsoft.AspNetCore.Mvc",
            "XTI_App.Api",
            "XTI_WebApp.Api"
        }
                         )
                         .Distinct()
                         .OrderBy(str => str)
                         .ToArray();

        await new GlobalUsingsClass(createStream, namespaces).Output();
    }
Beispiel #5
0
 public async Task Execute(AppApiTemplate api)
 {
     if (options.TsClient?.Disable == false)
     {
         if (string.IsNullOrWhiteSpace(options.TsClient?.OutputFolder))
         {
             throw new ArgumentException("TsOutputFolder is required");
         }
         if (!Directory.Exists(options.TsClient.OutputFolder))
         {
             throw new ArgumentException($"TS Output Folder {options.TsClient.OutputFolder} does not exist");
         }
         var tsClientToDisk = new CodeToDisk(createStream => new TsClient(versionKey, createStream), options.TsClient.OutputFolder);
         await tsClientToDisk.Output(api);
     }
     if (options.CsController?.Disable == false)
     {
         if (string.IsNullOrWhiteSpace(options.CsController?.OutputFolder))
         {
             throw new ArgumentException("CsController OutputFolder is required");
         }
         if (!Directory.Exists(options.CsController.OutputFolder))
         {
             throw new ArgumentException($"CS Controller Output Folder {options.CsController.OutputFolder} does not exist");
         }
         var controllerGenerator = new CodeToDisk
                                   (
             createStream =>
             new CsControllers
             (
                 options.CsController.OutputFolder,
                 createStream
             ),
             options.CsController.OutputFolder
                                   );
         await controllerGenerator.Output(api);
     }
     if (options.CsClient?.Disable == false)
     {
         if (string.IsNullOrWhiteSpace(options.CsClient?.OutputFolder))
         {
             throw new ArgumentException("CsClient OutputFolder is required");
         }
         if (!Directory.Exists(options.CsClient.OutputFolder))
         {
             throw new ArgumentException($"CS Client Output Folder {options.CsClient.OutputFolder} does not exist");
         }
         var csClientToDisk = new CodeToDisk
                              (
             createStream => new CsClient(versionKey, options.CsClient.OutputFolder, createStream),
             options.CsClient.OutputFolder
                              );
         await csClientToDisk.Output(api);
     }
 }
    public Task Output(AppApiTemplate appTemplate)
    {
        if (!Directory.Exists(folderPath))
        {
            throw new ArgumentException($"Output folder '{folderPath}' does not exist");
        }
        var filePaths = Directory.GetFiles(folderPath)
                        .Where(f => f.EndsWith(".ts", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".cs", StringComparison.OrdinalIgnoreCase));

        foreach (var filePath in filePaths)
        {
            string firstLine;
            using (var reader = new StreamReader(filePath))
            {
                firstLine = reader.ReadLine() ?? "";
            }
            if (generatedCodeRegex.IsMatch(firstLine))
            {
                try
                {
                    File.Delete(filePath);
                    if (filePath.EndsWith(".ts", StringComparison.OrdinalIgnoreCase))
                    {
                        var jsFilePath = filePath.Remove(filePath.Length - 3) + ".js";
                        if (File.Exists(jsFilePath))
                        {
                            File.Delete(jsFilePath);
                        }
                        var jsMapFilePath = filePath.Remove(filePath.Length - 3) + ".js.map";
                        if (File.Exists(jsMapFilePath))
                        {
                            File.Delete(jsMapFilePath);
                        }
                    }
                }
                catch
                {
                }
            }
        }
        var codeGenerator = createCodeGenerator(createStream);

        return(codeGenerator.Output(appTemplate));
    }
    private CompilationUnitSyntax createController(AppApiTemplate app, AppApiGroupTemplate group)
    {
        var apiClassName        = $"{app.Name}AppApi";
        var controllerClassName = getControllerClassName(group);
        var classToken          = ClassDeclaration(controllerClassName)
                                  .AddModifiers
                                  (
            Token(SyntaxKind.PublicKeyword),
            Token(SyntaxKind.PartialKeyword)
                                  );
        var actionDeclarations = group.ActionTemplates.Select(a => actionDeclaration(group, a));
        var code = CompilationUnit()
                   .WithMembers
                   (
            SingletonList <MemberDeclarationSyntax>
            (
                namespaceDeclaration()
                .WithMembers
                (
                    SingletonList <MemberDeclarationSyntax>
                    (
                        classDeclaration(controllerClassName, group.Access.IsAnonymousAllowed)
                        .WithMembers
                        (
                            List
                            (
                                new MemberDeclarationSyntax[]
        {
            apiFieldDeclaration(apiClassName),
            constructorDeclaration(apiClassName, getControllerClassName(group))
        }
                                .Union(actionDeclarations)
                            )
                        )
                    )
                )
            )
                   );

        return(code);
    }
Beispiel #8
0
    private async Task generateApp(AppApiTemplate appTemplate)
    {
        var appClassName = $"{appTemplate.Name}AppApi";
        var tsFile       = new TypeScriptFile(appClassName, createStream);

        tsFile.AddLine();
        tsFile.AddLine("import { AppApi } from \"@jasonbenfield/sharedwebapp/Api/AppApi\";");
        tsFile.AddLine("import { AppApiEvents } from \"@jasonbenfield/sharedwebapp/Api/AppApiEvents\";");
        foreach (var groupTemplate in appTemplate.GroupTemplates)
        {
            var groupClassName = getGroupClassName(groupTemplate);
            tsFile.AddLine($"import {{ {groupClassName} }} from \"./{groupClassName}\";");
        }
        tsFile.AddLine();
        tsFile.AddLine($"\r\nexport class {appClassName} extends AppApi {{");
        tsFile.Indent();
        //tsFile.AddLine($"public static readonly DefaultVersion = '{versionKey.Value}';");
        //tsFile.AddLine();
        tsFile.AddLine($"constructor(events: AppApiEvents) {{");
        tsFile.Indent();
        tsFile.AddLine($"super(events, '{appTemplate.Name}');");
        foreach (var groupTemplate in appTemplate.GroupTemplates)
        {
            tsFile.AddLine($"this.{groupTemplate.Name} = this.addGroup((evts, resourceUrl) => new {groupTemplate.Name}Group(evts, resourceUrl));");
        }
        tsFile.Outdent();
        tsFile.AddLine("}");
        tsFile.AddLine();
        foreach (var group in appTemplate.GroupTemplates)
        {
            tsFile.AddLine($"readonly {group.Name}: {group.Name}Group;");
        }
        tsFile.Outdent();
        tsFile.AddLine("}");
        await tsFile.Output();
    }
Beispiel #9
0
    public async Task Output(AppApiTemplate appTemplate)
    {
        var objTemplates = appTemplate.ObjectTemplates(ApiCodeGenerators.Dotnet);

        foreach (var objTemplate in objTemplates)
        {
            await new ApiObjectClass(ns, createStream, objTemplate).Output();
        }
        var formTemplates         = appTemplate.FormTemplates(ApiCodeGenerators.Dotnet);
        var complexFieldTemplates = formTemplates.SelectMany(ft => ft.Form.ComplexFieldTemplates).Distinct();

        foreach (var complexFieldTemplate in complexFieldTemplates)
        {
            await new ComplexFieldClass(ns, createStream, complexFieldTemplate, "ComplexField").Output();
        }
        foreach (var formTemplate in formTemplates)
        {
            await new ComplexFieldClass(ns, createStream, formTemplate.Form, "Form").Output();
        }
        foreach (var groupTemplate in appTemplate.GroupTemplates)
        {
            await new ApiGroupClass(ns, createStream, groupTemplate).Output();
        }
        foreach (var numericValueTemplate in appTemplate.NumericValueTemplates(ApiCodeGenerators.Dotnet))
        {
            await new NumericValueClass(ns, createStream, numericValueTemplate).Output();
        }
        await new ApiAppClass(ns, createStream, appTemplate, versionKey).Output();
        var namespaces = new[]
        {
            "XTI_WebAppClient",
            "XTI_WebAppClient.Forms"
        };

        await new GlobalUsingsClass(createStream, namespaces).Output();
    }
 protected override void ConfigureTemplate(AppApiTemplate template)
 {
     base.ConfigureTemplate(template);
     template.ExcludeValueTemplates(IsValueTemplateExcluded);
 }