public async Task InjectSampleAsync(string projFilePath) { //using (GitSampleProject sample = new GitSampleProject(@"https://github.com/docusign/eg-01-csharp-jwt-core/archive/master.zip")) { try { var templateMsWorkspace = MSBuildWorkspace.Create(); Project roslynProject = await templateMsWorkspace.OpenProjectAsync(projFilePath); string sourceCode = "namespace Foo{public class Foo{ }}"; Compilation compilation = roslynProject.GetCompilationAsync().Result; SyntaxTree stree = CSharpSyntaxTree.ParseText(sourceCode); SyntaxNode root = stree.GetCompilationUnitRoot(); UsingDirectiveSyntax[] usings = (root as CompilationUnitSyntax).Usings.ToArray(); NamespaceDeclarationSyntax namespaceNode = root.DescendantNodes().OfType <NamespaceDeclarationSyntax>().FirstOrDefault(); root = namespaceNode.WithName(SyntaxFactory.ParseName(compilation.GetEntryPoint(CancellationToken.None).ContainingNamespace.ToString())); root = (root as NamespaceDeclarationSyntax).AddUsings(usings); string fileName = GetFileName(root); var newDoc = roslynProject.AddDocument(fileName, root); templateMsWorkspace.TryApplyChanges(newDoc.Project.Solution); } catch (SampleInitException sampleException) { Debug.Fail(sampleException.Message, sampleException.InnerException.ToString()); } catch (Exception exception) { Debug.Fail(exception.Message, exception.ToString()); } } }
public void Exec(string projectFilePath) { //string projectFilePath = @"C:\Users\USER\source\repos\ClassLibrary1\ClassLibrary1\ClassLibrary1.csproj"; string projectDirectory = Path.GetDirectoryName(projectFilePath); var manager = new AnalyzerManager(); ProjectAnalyzer analyzer = manager.GetProject(projectFilePath); using (var workspace = new AdhocWorkspace()) { Project roslynProject = analyzer.AddToWorkspace(workspace); if (roslynProject.Language != LanguageNames.CSharp) { throw new NotSupportedException($"The language: {roslynProject.Language} is not supported."); } var sourceCode = "namespace Foo{public class Foo{ }}"; Compilation compilation = roslynProject.GetCompilationAsync().Result; SyntaxTree stree = CSharpSyntaxTree.ParseText(sourceCode); SyntaxNode root = stree.GetCompilationUnitRoot(); UsingDirectiveSyntax[] usings = (root as CompilationUnitSyntax).Usings.ToArray(); NamespaceDeclarationSyntax namespaceNode = root.DescendantNodes().OfType <NamespaceDeclarationSyntax>().FirstOrDefault(); root = namespaceNode.WithName(SyntaxFactory.ParseName(compilation.GetEntryPoint(System.Threading.CancellationToken.None).ContainingNamespace.ToString())); root = (root as NamespaceDeclarationSyntax).AddUsings(usings); string fileName = GetFileName(root); fileName = Path.ChangeExtension(fileName, "cs"); string filePath = Path.Combine(projectDirectory, fileName); File.WriteAllText(filePath, root.ToFullString()); var documentId = DocumentId.CreateNewId(roslynProject.Id); var docInfo = DocumentInfo.Create(documentId, fileName, filePath: filePath); //await _addDocumentToProjectService.AddDocumentToProject(docInfo, projectFilePath).ConfigureAwait(false); } }
private static Task <Document> RenameAsync(Document document, SyntaxNode root, NamespaceDeclarationSyntax namespaceDesclaration, string newName, CancellationToken cancellationToken) { var newNamespace = namespaceDesclaration.WithName(newName.ConstructNameSyntax()); var newRoot = root.ReplaceNode(namespaceDesclaration, newNamespace); return(Task.FromResult(document.WithSyntaxRoot(newRoot))); }
public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node) { //return base.VisitNamespaceDeclaration(node); var n = (SimpleNameSyntax)SyntaxFactory.ParseName("Codegen_" + this.compilation.AssemblyName); var namespaceName = SyntaxFactory.QualifiedName(node.Name, n); return(node.WithName(namespaceName)); }
public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node) { var newNode = node; var identifier = node.Name.DescendantNodes().OfType <IdentifierNameSyntax>().First().Identifier; var newIdentifier = CreateAopIdentifier(identifier, _config.NameSpaceSuffix); NameSpaces[identifier.ValueText] = newIdentifier.ValueText; newNode = node.WithName(node.Name.ReplaceToken(identifier, newIdentifier)); return(base.VisitNamespaceDeclaration(newNode)); }
private static async Task <Document> GenerateCodeFixAsync(Document document, NamespaceDeclarationSyntax node, CancellationToken cancellationToken) { var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken); if (syntaxTree is null) { throw new NullReferenceException(nameof(syntaxTree)); } var root = await syntaxTree.GetRootAsync(cancellationToken); var updatedSyntaxTree = root.ReplaceNode(node, node.WithName(SF.ParseName("AncientMysteries.Items"))); return(document.WithSyntaxRoot(updatedSyntaxTree)); }
public static NamespaceDeclarationSyntax Format(this NamespaceDeclarationSyntax nds) { var newName = nds.Name.WithTrailingTrivia(LineFeed); return(nds .WithName(newName) .WithNamespaceKeyword( Token( TriviaList(LineFeed), SyntaxKind.NamespaceKeyword, TriviaList(Space))) .WithOpenBraceToken( Token( TriviaList(), SyntaxKind.OpenBraceToken, TriviaList(LineFeed))) .WithCloseBraceToken( Token( TriviaList(LineFeed), SyntaxKind.CloseBraceToken, TriviaList()))); }
public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node) { var @namespace = Regex.Replace(node.Name.ToFullString(), @"\s+", string.Empty); NamespaceDeclarationSyntax updated; if (@namespace.StartsWith(this.baseNamespace, StringComparison.Ordinal)) { var name = SyntaxFactory.ParseName($@" #if {this.conditionalCompilationSymbolBaseName}_USE_LOCAL_NAMESPACE $rootnamespace${@namespace.Substring(this.baseNamespace.Length)} #else {node.Name.ToFullString()} "); name = name.WithTrailingTrivia(name.GetTrailingTrivia().AddRange(SyntaxFactory.ParseTrailingTrivia("#endif\r\n"))); updated = node.WithName(name); } else { updated = node; } return(base.VisitNamespaceDeclaration(updated)); }