Пример #1
0
        private async Task <int> Execute()
        {
            if (!TryValidateProjectPath(Project, out var projectPath))
            {
                return(1);
            }

            if (Plugins.Values.Count == 0)
            {
                ShowHelp();
                return(1);
            }

            var analysisContext = Analysis.CreateContext();
            var compilation     = await GetCompilationAsync(projectPath);

            analysisContext.SetData <CSharpCompilation>(compilation);

            var operations = new List <Operation>();

            for (var i = 0; i < Plugins.Values.Count; i++)
            {
                var name = Plugins.Values[i];

                Out.WriteLine($"Processing '{name}'...");

                if (!KnownPlugins.Plugins.TryGetValue(name, out var plugin))
                {
                    Out.WriteLine($"Unknown plugin '{name}'");
                    return(1);
                }

                operations.AddRange(await plugin.GetOperationsAsync(analysisContext));
            }

            Out.WriteLine("Resolved operations:");
            for (var i = 0; i < operations.Count; i++)
            {
                Out.WriteLine($"\t{operations[i]}");
            }
            Out.WriteLine();

            if (!DryRun.HasValue())
            {
                var editorContext = new EditorContext();
                editorContext.SetData <CSharpCompilation>(await analysisContext.GetDataAsync <CSharpCompilation>().ConfigureAwait(false));
                var editor = Editor.Create();

                Out.WriteLine("Performing operations:");
                for (var i = 0; i < operations.Count; i++)
                {
                    Out.WriteLine($"\t{operations[i]}");
                    await editor.ApplyAsync(editorContext, operations[i]);
                }
            }
            Out.WriteLine();

            return(0);
        }
Пример #2
0
        public override async Task ApplyAsync(EditorContext context, Operation operation, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            if (operation is ServiceDependencySet.AddOperation add)
            {
                var compilation = context.GetData <CSharpCompilation>();
                var syntaxTree  = FindCorrespondingSyntaxTree(compilation, add.ConfigureServicesMethod.SyntaxTree);

                var root = await syntaxTree.GetRootAsync().ConfigureAwait(false);

                var configureServicesMethod = root.GetCurrentNode(add.ConfigureServicesMethod);
                var edited = syntaxTree.WithRootAndOptions(
                    root.ReplaceNode(
                        configureServicesMethod,
                        configureServicesMethod.AddBodyStatements(
                            ExpressionStatement(
                                InvocationExpression(
                                    MemberAccessExpression(
                                        SyntaxKind.SimpleMemberAccessExpression,
                                        IdentifierName("services"),
                                        IdentifierName(add.MethodName)))))),
                    syntaxTree.Options);

                File.WriteAllText(edited.FilePath, edited.GetRoot().NormalizeWhitespace().ToString());

                compilation = compilation.ReplaceSyntaxTree(syntaxTree, edited);
                context.SetData <CSharpCompilation>(compilation);
            }
        }