示例#1
0
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context)
        {
            var    projectInfo = context.GetVariable <ProjectInfo>("ProjectInfo");
            var    model       = context.GetVariable <object>("Model");
            string templateDir = context.GetVariable <string>("TemplateDirectory");
            string permissionDefinitionsText = TextGenerator.GenerateByTemplateName(templateDir, "Permissions_Definitions", model);

            var builders = new List <ModificationBuilder <CSharpSyntaxNode> >();

            builders.Add(new InsertionBuilder <CSharpSyntaxNode>(
                             root => root.Descendants <MethodDeclarationSyntax>().First().GetEndLine(),
                             permissionDefinitionsText,
                             InsertPosition.Before,
                             root => root.DescendantsNotContain <ClassDeclarationSyntax>(permissionDefinitionsText)
                             ));

            if (projectInfo.TemplateType == TemplateType.Application)
            {
                // Noting special to do
            }
            else if (projectInfo.TemplateType == TemplateType.Module)
            {
                string addGroupText = TextGenerator.GenerateByTemplateName(templateDir, "Permissions_AddGroup", model);

                // Uncomment the add group statement
                builders.Add(new ReplacementBuilder <CSharpSyntaxNode>(
                                 root => root.Descendants <MethodDeclarationSyntax>().First().GetStartLine() + 2,
                                 root => root.Descendants <MethodDeclarationSyntax>().First().GetStartLine() + 2,
                                 addGroupText,
                                 modifyCondition: root => !root.DescendantsNotContain <MethodDeclarationSyntax>($"//" + addGroupText)
                                 ));
            }

            return(builders);
        }
示例#2
0
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context, CompilationUnitSyntax rootUnit)
        {
            var    model                 = context.GetVariable <object>("Model");
            string entityUsingText       = context.GetVariable <string>("EntityUsingText");
            string templateDir           = context.GetVariable <string>(VariableNames.TemplateDirectory);
            string dbContextUsingText    = TextGenerator.GenerateByTemplateName(templateDir, "DbContextInterface_Using", model);
            string dbContextPropertyText = TextGenerator.GenerateByTemplateName(templateDir, "DbContextInterface_Property", model);

            return(new List <ModificationBuilder <CSharpSyntaxNode> >
            {
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => 1,
                    dbContextUsingText,
                    InsertPosition.Before,
                    root => root.DescendantsNotContain <UsingDirectiveSyntax>(dbContextUsingText)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <UsingDirectiveSyntax>().Last().GetEndLine(),
                    entityUsingText,
                    InsertPosition.After,
                    root => root.DescendantsNotContain <UsingDirectiveSyntax>(entityUsingText)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <InterfaceDeclarationSyntax>().Single().GetEndLine(),
                    dbContextPropertyText,
                    modifyCondition: root => root.DescendantsNotContain <PropertyDeclarationSyntax>(dbContextPropertyText)
                    )
            });
        }
示例#3
0
        protected override IList <ModificationBuilder <IEnumerable <LineNode> > > CreateModifications(
            WorkflowExecutionContext context)
        {
            var    model          = context.GetVariable <object>("Model");
            string templateDir    = context.GetVariable <string>("TemplateDirectory");
            string importContents = TextGenerator.GenerateByTemplateName(templateDir, "RoutingModule_ImportList", model);
            string moduleContents = TextGenerator.GenerateByTemplateName(templateDir, "RoutingModule_Routes", model);

            int LineExpression(IEnumerable <LineNode> lines) => lines.Last(l => l.IsMath($"const routes")).LineNumber;

            return(new List <ModificationBuilder <IEnumerable <LineNode> > >
            {
                new InsertionBuilder <IEnumerable <LineNode> >(
                    lines => lines.Last(l => l.IsMath("^import")).LineNumber,
                    importContents,
                    InsertPosition.After,
                    lines => lines.Where(l => l.IsMath("^import")).All(l => !l.LineContent.Contains(importContents))
                    ),
                new ReplacementBuilder <IEnumerable <LineNode> >(
                    LineExpression,
                    LineExpression,
                    moduleContents
                    )
            });
        }
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context)
        {
            var    model             = context.GetVariable <object>("Model");
            string templateDir       = context.GetVariable <string>("TemplateDirectory");
            string usingTaskContents = TextGenerator.GenerateByTemplateName(templateDir, "AppService_UsingTask", model);
            string usingDtoContents  = TextGenerator.GenerateByTemplateName(templateDir, "AppService_UsingDto", model);
            string classContents     = TextGenerator.GenerateByTemplateName(templateDir, "AppServiceClass", model);

            return(new List <ModificationBuilder <CSharpSyntaxNode> >
            {
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => 1,
                    usingTaskContents,
                    modifyCondition: root => root.DescendantsNotContain <UsingDirectiveSyntax>(usingTaskContents)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => 1,
                    usingDtoContents,
                    modifyCondition: root => root.DescendantsNotContain <UsingDirectiveSyntax>(usingDtoContents)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <ClassDeclarationSyntax>().Single().GetEndLine(),
                    classContents
                    ),
            });
        }
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context)
        {
            var    model           = context.GetVariable <object>("Model");
            string entityUsingText = context.GetVariable <string>("EntityUsingText");
            string templateDir     = context.GetVariable <string>("TemplateDirectory");
            string contents        = TextGenerator.GenerateByTemplateName(templateDir, "EntityFrameworkCoreModule_AddRepository", model);

            CSharpSyntaxNode Func(CSharpSyntaxNode root) => root
            .Descendants <ExpressionStatementSyntax>()
            .Single(node => node.ToString().Contains("AddAbpDbContext"))
            ;

            return(new List <ModificationBuilder <CSharpSyntaxNode> >
            {
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => 1,
                    entityUsingText,
                    modifyCondition: root => root.DescendantsNotContain <UsingDirectiveSyntax>(entityUsingText)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => Func(root).GetEndLine(),
                    contents,
                    modifyCondition: root => Func(root).NotContains(contents)
                    ),
            });
        }
示例#6
0
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context)
        {
            var    model              = context.GetVariable <object>("Model");
            string entityUsingText    = context.GetVariable <string>("EntityUsingText");
            string entityDtoUsingText = context.GetVariable <string>("EntityDtoUsingText");
            string templateDir        = context.GetVariable <string>("TemplateDirectory");
            string contents           = TextGenerator.GenerateByTemplateName(templateDir, "ApplicationAutoMapperProfile_CreateMap", model);

            return(new List <ModificationBuilder <CSharpSyntaxNode> >
            {
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <UsingDirectiveSyntax>().Last().GetEndLine(),
                    entityUsingText,
                    modifyCondition: root => root.DescendantsNotContain <UsingDirectiveSyntax>(entityUsingText)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <UsingDirectiveSyntax>().Last().GetEndLine(),
                    entityDtoUsingText,
                    modifyCondition: root => root.DescendantsNotContain <UsingDirectiveSyntax>(entityDtoUsingText)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <ConstructorDeclarationSyntax>().Single().GetEndLine(),
                    contents,
                    modifyCondition: root => root.Descendants <ConstructorDeclarationSyntax>().Single().NotContains(contents)
                    )
            });
        }
示例#7
0
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context, CompilationUnitSyntax rootUnit)
        {
            var    serviceInterfaceInfo = context.GetVariable <TypeInfo>("InterfaceInfo");
            var    serviceClassInfo     = context.GetVariable <TypeInfo>("ClassInfo");
            var    controllerInfo       = context.GetVariable <TypeInfo>("ControllerInfo");
            string templateDir          = context.GetVariable <string>("TemplateDirectory");

            // Generate added methods
            var modifications = new List <ModificationBuilder <CSharpSyntaxNode> >();
            var addedMethods  = serviceClassInfo.Methods
                                .Except(controllerInfo.Methods)          // Except the existing controller methods
                                .Intersect(serviceInterfaceInfo.Methods) // Only methods defined in the interface need to generate
                                                                         // Why not just use the `serviceInterfaceInfo.Methods`? Because we need use attributes info
                                                                         // which only defined in the service class
            ;

            foreach (var method in addedMethods)
            {
                var    model      = new { method };
                string methodText = TextGenerator.GenerateByTemplateName(templateDir, "ControllerMethod", model);
                modifications.Add(
                    new InsertionBuilder <CSharpSyntaxNode>(
                        root => root.Descendants <ClassDeclarationSyntax>().First().GetEndLine(),
                        methodText
                        ));
            }

            return(modifications);
        }
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context)
        {
            var    model             = context.GetVariable <object>("Model");
            var    entityUsingText   = context.GetVariable <string>("EntityUsingText");
            string templateDir       = context.GetVariable <string>("TemplateDirectory");
            var    modelingUsingText = TextGenerator.GenerateByTemplateName(templateDir, "DbContextModelCreatingExtensions_Using", model);
            var    entityConfigText  = TextGenerator.GenerateByTemplateName(templateDir, "DbContextModelCreatingExtensions_EntityConfig", model);

            return(new List <ModificationBuilder <CSharpSyntaxNode> >
            {
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => 1,
                    entityUsingText,
                    modifyCondition: root => root.DescendantsNotContain <UsingDirectiveSyntax>(entityUsingText)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <UsingDirectiveSyntax>().Last().GetEndLine(),
                    modelingUsingText,
                    InsertPosition.After,
                    root => root.DescendantsNotContain <UsingDirectiveSyntax>(modelingUsingText)),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <MethodDeclarationSyntax>().First().GetEndLine(),
                    entityConfigText,
                    modifyCondition: root => root.Descendants <MethodDeclarationSyntax>().First().NotContains(entityConfigText)
                    )
            });
        }
示例#9
0
        protected override IList <ModificationBuilder <IEnumerable <LineNode> > > CreateModifications(
            WorkflowExecutionContext context)
        {
            var    model                = context.GetVariable <object>("Model");
            var    entityInfo           = context.GetVariable <EntityInfo>("EntityInfo");
            string templateDir          = context.GetVariable <string>(VariableNames.TemplateDirectory);
            string importContents       = TextGenerator.GenerateByTemplateName(templateDir, "Module_ImportSharedModule", model);
            string sharedModuleContents = TextGenerator.GenerateByTemplateName(templateDir, "Module_SharedModule", model);

            int LineExpression(IEnumerable <LineNode> lines) => lines.Last(l => l.IsMath($"{entityInfo.NamespaceLastPart}RoutingModule")).LineNumber;

            return(new List <ModificationBuilder <IEnumerable <LineNode> > >
            {
                new InsertionBuilder <IEnumerable <LineNode> >(
                    lines => lines.Last(l => l.IsMath("^import")).LineNumber,
                    importContents,
                    InsertPosition.After,
                    lines => lines.Where(l => l.IsMath("^import")).All(l => !l.LineContent.Contains(importContents))
                    ),
                new InsertionBuilder <IEnumerable <LineNode> >(
                    LineExpression,
                    sharedModuleContents,
                    InsertPosition.After,
                    lines => lines.All(l => !l.LineContent.Contains(sharedModuleContents))
                    )
            });
        }
示例#10
0
        protected override IList <ModificationBuilder> CreateModifications(WorkflowExecutionContext context)
        {
            var entityInfo = context.GetVariable <EntityInfo>("EntityInfo");

            return(new List <ModificationBuilder>
            {
                new InsertionBuilder(root => root.Descendants <MethodDeclarationSyntax>()
                                     .Single(n => n.Identifier.ToString() == "ConfigureMainMenuAsync")
                                     .GetEndLine(),
                                     TextGenerator.GenerateByTemplateName("MenuContributor_AddMenuItem", new { EntityInfo = entityInfo })
                                     )
            });
        }
示例#11
0
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context, CompilationUnitSyntax rootUnit)
        {
            var    model           = context.GetVariable <object>("Model");
            string templateDir     = context.GetVariable <string>("TemplateDirectory");
            string addMenuNameText = TextGenerator.GenerateByTemplateName(templateDir, "Menus_AddMenuName", model);

            return(new List <ModificationBuilder <CSharpSyntaxNode> >
            {
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <ClassDeclarationSyntax>().Single().GetEndLine(),
                    addMenuNameText,
                    modifyCondition: root => root.DescendantsNotContain <ClassDeclarationSyntax>(addMenuNameText)
                    ),
            });
        }
示例#12
0
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context, CompilationUnitSyntax rootUnit)
        {
            var    model               = context.GetVariable <object>("Model");
            string templateDir         = context.GetVariable <string>("TemplateDirectory");
            string permissionNamesText = TextGenerator.GenerateByTemplateName(templateDir, "Permissions_Names", model);

            return(new List <ModificationBuilder <CSharpSyntaxNode> >
            {
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <ClassDeclarationSyntax>().First().GetEndLine(),
                    permissionNamesText,
                    InsertPosition.Before,
                    root => root.DescendantsNotContain <ClassDeclarationSyntax>(permissionNamesText)
                    ),
            });
        }
示例#13
0
        protected override IList <ModificationBuilder> CreateModifications(WorkflowExecutionContext context)
        {
            var entityInfo         = context.GetVariable <EntityInfo>("EntityInfo");
            var entityDtoUsingText = context.GetVariable <string>("EntityDtoUsingText");

            return(new List <ModificationBuilder>
            {
                new InsertionBuilder(
                    root => root.Descendants <UsingDirectiveSyntax>().Last().GetEndLine(),
                    entityDtoUsingText,
                    modifyCondition: root => root.DescendantsNotContain <UsingDirectiveSyntax>(entityDtoUsingText)
                    ),
                new InsertionBuilder(
                    root => root.Descendants <ConstructorDeclarationSyntax>().Single().GetEndLine(),
                    TextGenerator.GenerateByTemplateName("WebAutoMapperProfile_CreateMap", new { EntityInfo = entityInfo })
                    )
            });
        }
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context, CompilationUnitSyntax rootUnit)
        {
            var    projectInfo = context.GetVariable <ProjectInfo>("ProjectInfo");
            string templateDir = context.GetVariable <string>(VariableNames.TemplateDirectory);
            string groupName   = rootUnit.Descendants <LocalDeclarationStatementSyntax>()
                                 .Single(stat => stat.ToFullString().Contains("context.AddGroup"))
                                 .Descendants <VariableDeclarationSyntax>().Single()
                                 .Variables[0].Identifier.Text;
            var model = context.GetVariable <dynamic>("Model");

            model.Bag.GroupName = groupName;
            string permissionDefinitionsText = TextGenerator.GenerateByTemplateName(templateDir, "Permissions_Definitions", model);

            var builders = new List <ModificationBuilder <CSharpSyntaxNode> >();

            builders.Add(new InsertionBuilder <CSharpSyntaxNode>(
                             root => root.Descendants <MethodDeclarationSyntax>().First().GetEndLine(),
                             permissionDefinitionsText,
                             InsertPosition.Before,
                             root => root.DescendantsNotContain <ClassDeclarationSyntax>(permissionDefinitionsText)
                             ));

            if (projectInfo.TemplateType == TemplateType.Application)
            {
                // Noting special to do
            }
            else if (projectInfo.TemplateType == TemplateType.Module)
            {
                string addGroupText = TextGenerator.GenerateByTemplateName(templateDir, "Permissions_AddGroup", model);

                // Uncomment the add group statement
                builders.Add(new ReplacementBuilder <CSharpSyntaxNode>(
                                 root => root.Descendants <MethodDeclarationSyntax>().First().GetStartLine() + 2,
                                 root => root.Descendants <MethodDeclarationSyntax>().First().GetStartLine() + 2,
                                 addGroupText,
                                 modifyCondition: root => !root.DescendantsNotContain <MethodDeclarationSyntax>($"//" + addGroupText)
                                 ));
            }

            return(builders);
        }
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context)
        {
            var    model                 = context.GetVariable <object>("Model");
            string entityUsingText       = context.GetVariable <string>("EntityUsingText");
            string templateDir           = context.GetVariable <string>("TemplateDirectory");
            string dbContextPropertyText = TextGenerator.GenerateByTemplateName(templateDir, "DbContextClass_Property", model);

            return(new List <ModificationBuilder <CSharpSyntaxNode> >
            {
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <UsingDirectiveSyntax>().Last().GetEndLine(),
                    entityUsingText,
                    InsertPosition.After,
                    root => root.DescendantsNotContain <UsingDirectiveSyntax>(entityUsingText)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <ConstructorDeclarationSyntax>().Single().Identifier.GetStartLine() - 1,
                    dbContextPropertyText,
                    modifyCondition: root => root.DescendantsNotContain <PropertyDeclarationSyntax>(dbContextPropertyText)
                    )
            });
        }
示例#16
0
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context, CompilationUnitSyntax rootUnit)
        {
            var    model             = context.GetVariable <object>("Model");
            string templateDir       = context.GetVariable <string>(VariableNames.TemplateDirectory);
            string protectedCtorText = TextGenerator.GenerateByTemplateName(templateDir, "Entity_ProtectedConstructor", model);
            string publicCtorText    = TextGenerator.GenerateByTemplateName(templateDir, "Entity_PublicConstructor", model);

            return(new List <ModificationBuilder <CSharpSyntaxNode> >
            {
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <ClassDeclarationSyntax>().Single().GetEndLine(),
                    protectedCtorText,
                    InsertPosition.Before,
                    root => root.DescendantsNotContain <ClassDeclarationSyntax>(protectedCtorText)
                    ),
                new InsertionBuilder <CSharpSyntaxNode>(
                    root => root.Descendants <ClassDeclarationSyntax>().Single().GetEndLine(),
                    publicCtorText,
                    InsertPosition.Before,
                    modifyCondition: root => root.DescendantsNotContain <ClassDeclarationSyntax>(publicCtorText)
                    )
            });
        }
示例#17
0
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context, CompilationUnitSyntax rootUnit)
        {
            var    projectInfo     = context.GetVariable <ProjectInfo>("ProjectInfo");
            var    model           = context.GetVariable <object>("Model");
            string templateDir     = context.GetVariable <string>(VariableNames.TemplateDirectory);
            string addMenuItemText = TextGenerator.GenerateByTemplateName(templateDir, "MenuContributor_AddMenuItem", model);

            CSharpSyntaxNode MainMenu(CSharpSyntaxNode root) => root.Descendants <MethodDeclarationSyntax>()
            .Single(n => n.Identifier.ToString().Contains("ConfigureMainMenu"));

            var builders = new List <ModificationBuilder <CSharpSyntaxNode> >();

            if (projectInfo.TemplateType == TemplateType.Application)
            {
                string usingForAppText = TextGenerator.GenerateByTemplateName(templateDir, "MenuContributor_UsingForApp", model);

                builders.Add(
                    new InsertionBuilder <CSharpSyntaxNode>(
                        root => 2,
                        usingForAppText,
                        modifyCondition: root => root.NotContains(usingForAppText)
                        ));
                builders.Add(
                    new InsertionBuilder <CSharpSyntaxNode>(
                        root => MainMenu(root).GetEndLine(),
                        addMenuItemText,
                        modifyCondition: root => MainMenu(root).NotContains(addMenuItemText)
                        )
                    );
            }
            else if (projectInfo.TemplateType == TemplateType.Module)
            {
                string configureMainMenuText = TextGenerator.GenerateByTemplateName(templateDir, "MenuContributor_ConfigureMainMenu", model);
                string usingForModuleText    = TextGenerator.GenerateByTemplateName(templateDir, "MenuContributor_UsingForModule", model);
                string localizerText         = TextGenerator.GenerateByTemplateName(templateDir, "MenuContributor_Localizer", model);
                builders.Add(
                    new ReplacementBuilder <CSharpSyntaxNode>(
                        root => MainMenu(root).GetStartLine(),
                        root => MainMenu(root).GetStartLine(),
                        configureMainMenuText,
                        modifyCondition: root => root.NotContains(configureMainMenuText)
                        ));
                builders.Add(
                    new InsertionBuilder <CSharpSyntaxNode>(
                        root => 2,
                        usingForModuleText,
                        modifyCondition: root => root.NotContains(usingForModuleText)
                        ));
                builders.Add(new InsertionBuilder <CSharpSyntaxNode>(
                                 root => MainMenu(root).GetStartLine() + 2,
                                 localizerText,
                                 modifyCondition: root => MainMenu(root).NotContains(localizerText)
                                 ));
                builders.Add(new DeletionBuilder <CSharpSyntaxNode>(
                                 root => MainMenu(root).GetEndLine() - 1,
                                 root => MainMenu(root).GetEndLine() - 1,
                                 modifyCondition: root => !root.NotContains("return Task.CompletedTask;")
                                 ));
                builders.Add(new InsertionBuilder <CSharpSyntaxNode>(
                                 root => MainMenu(root).GetEndLine(),
                                 addMenuItemText,
                                 modifyCondition: root => MainMenu(root).NotContains(addMenuItemText)
                                 ));
            }

            return(builders);
        }