Ejemplo n.º 1
0
        protected override IList <ModificationBuilder <CSharpSyntaxNode> > CreateModifications(WorkflowExecutionContext context, CompilationUnitSyntax rootUnit)
        {
            var    projectInfo = context.GetVariable <ProjectInfo>("ProjectInfo");
            string templateDir = context.GetVariable <string>("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);
        }