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()); }
public void CompleteAndWriteFile(CompilationUnitSyntax contents, string filePath) { contents = contents .NormalizeWhitespace() // NOTE reenable pragma codes after normalizing whitespace or it doesn't place on newline .WithPragmaCodes(true, pragmaCodes); _filePersistService.WriteFile(contents, filePath); }
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()); }