Exemplo n.º 1
0
        public void ProcessControllers_Abstract()
        {
            var controllerSymbolMock = GetController(isAbstract: true);
            var controllerSymbol     = controllerSymbolMock.Object;

            Assert.False(ControllerRewriter.ControllerShouldBeProcessed(controllerSymbol));
        }
Exemplo n.º 2
0
        public ImmutableArray <ClassDeclarationSyntax> RewriteControllers(CSharpCompilation compiler, string outputFileName)
        {
            var mvcControllerNodes = new List <ClassDeclarationSyntax>();

            foreach (var tree in compiler.SyntaxTrees.Where(x => !x.FilePath.EndsWith(outputFileName)))
            {
                // if syntaxtree has errors, skip code generation
                if (tree.GetDiagnostics().Any(x => x.Severity == DiagnosticSeverity.Error))
                {
                    continue;
                }

                // this first part, finds all the controller classes, modifies them and saves the changes
                var controllerRewriter = new ControllerRewriter(compiler);
                var newNode            = controllerRewriter.Visit(tree.GetRoot());

                if (!newNode.IsEquivalentTo(tree.GetRoot()))
                {
                    // node has changed, update syntaxtree and persist to file
                    compiler = compiler.ReplaceSyntaxTree(tree, newNode.SyntaxTree);
                    _filePersistService.WriteFile(newNode, tree.FilePath);
                }

                // save the controller nodes from each visit to pass to the generator
                mvcControllerNodes.AddRange(controllerRewriter.MvcControllerClassNodes);
            }

            return(mvcControllerNodes.ToImmutableArray());
        }
Exemplo n.º 3
0
        public void ProcessControllers_RandomAttribute()
        {
            var controllerSymbolMock = GetController(attribute: GetClass("CustomAttribute"));
            var controllerSymbol     = controllerSymbolMock.Object;

            Assert.True(ControllerRewriter.ControllerShouldBeProcessed(controllerSymbol));
        }
Exemplo n.º 4
0
        public void ProcessControllers_NotPublic()
        {
            var controllerSymbolMock = GetController(isPublic: false);
            var controllerSymbol     = controllerSymbolMock.Object;

            Assert.False(ControllerRewriter.ControllerShouldBeProcessed(controllerSymbol));
        }
Exemplo n.º 5
0
        public void ProcessControllers_ControllerSubtype()
        {
            var controllerSymbolMock = GetController(baseClass: GetClass("ControllerBase", GetMvcControllerClass()));
            var controllerSymbol     = controllerSymbolMock.Object;

            Assert.True(ControllerRewriter.ControllerShouldBeProcessed(controllerSymbol));
        }
Exemplo n.º 6
0
        public void ProcessControllers()
        {
            var controllerSymbolMock = GetController();
            var controllerSymbol     = controllerSymbolMock.Object;

            Assert.True(ControllerRewriter.ControllerShouldBeProcessed(controllerSymbol));
        }
Exemplo n.º 7
0
        public void ProcessControllers_NestedAttributeExcluded()
        {
            var controllerSymbolMock = GetController(attribute: GetClass("CustomAttribute", GetExcludedAttribute()));
            var controllerSymbol     = controllerSymbolMock.Object;

            Assert.False(ControllerRewriter.ControllerShouldBeProcessed(controllerSymbol));
        }
Exemplo n.º 8
0
        public void ProcessControllers_NoBaseType()
        {
            var controllerSymbolMock = GetController(noBaseClass: true);
            var controllerSymbol     = controllerSymbolMock.Object;

            Assert.False(ControllerRewriter.ControllerShouldBeProcessed(controllerSymbol));
        }
Exemplo n.º 9
0
        public IList <ControllerDefinition> RewriteControllers(CSharpCompilation compiler)
        {
            var controllers = new Dictionary <string, ControllerDefinition>();

            foreach (var tree in compiler.SyntaxTrees.Where(x => !x.FilePath.EndsWith(".generated.cs")))
            {
                // if syntaxtree has errors, skip code generation
                if (tree.GetDiagnostics().Any(x => x.Severity == DiagnosticSeverity.Error))
                {
                    continue;
                }

                // this first part, finds all the controller classes, modifies them and saves the changes
                var        controllerRewriter = new ControllerRewriter(compiler);
                SyntaxNode newNode;
                try
                {
                    newNode = controllerRewriter.Visit(tree.GetRoot());
                }
                catch
                {
                    // If roslyn can't get the root of the tree, just continue to the next item
                    continue;
                }

                // save the controller nodes from each visit to pass to the generator
                foreach (var controllerNode in controllerRewriter.MvcControllerClassNodes)
                {
                    var cNamespace = controllerNode.FirstAncestorOrSelf <NamespaceDeclarationSyntax>().Name.ToFullString().Trim();
                    var cSymbol    = compiler.GetSemanticModel(tree).GetDeclaredSymbol(controllerNode);
                    var cFullName  = cNamespace + "." + cSymbol.Name;
                    if (controllers.ContainsKey(cFullName))
                    {
                        controllers[cFullName].FilePaths.Add(tree.FilePath);
                        continue;
                    }

                    var cAreaName = _controllerGenerator.GetControllerArea(cSymbol);
                    controllers[cFullName] = new ControllerDefinition
                    {
                        Namespace = cNamespace,
                        Name      = cSymbol.Name.TrimEnd("Controller"),
                        Area      = cAreaName,
                        Symbol    = cSymbol,
                        FilePaths = new List <string> {
                            tree.FilePath
                        },
                    };
                }

                if (!newNode.IsEquivalentTo(tree.GetRoot()))
                {
                    // node has changed, update syntaxtree and persist to file

                    // Updating the new syntax tree with the syntax options from the original tree
                    // Seems like the new syntax tree might be generated with a different language version than the original. (see #79)
                    var newTree = newNode.SyntaxTree.WithRootAndOptions(newNode.SyntaxTree.GetRoot(), tree.Options);

                    compiler = compiler.ReplaceSyntaxTree(tree, newTree);
                    _filePersistService.WriteFile(newNode, tree.FilePath);
                }
            }

            return(controllers.Values.ToList());
        }