Esempio n. 1
0
        private static async Task <int> RefactorCommand(IHost host, FileInfo solutionPath, string projectName)
        {
            return(await AnsiConsole.Progress().AutoClear(false)
                   .Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn(),
                            new ElapsedTimeColumn()).StartAsync(async progressContext =>
            {
                var logger = host.Services.GetRequiredService <ILogger <Program> >();
                var progressSubscriber = new AnsiConsoleProgressSubscriber(progressContext);

                var workspace = CreateWorkspace(progressSubscriber, logger);

                var(solution, proj) = await OpenSolutionAndProject(solutionPath, projectName, workspace, progressSubscriber, logger);
                if (proj == null)
                {
                    return -1;
                }

                progressSubscriber.Report(
                    new FindInternalClassesProgress(FindInternalTypesStep.LoadSolution, 100));

                var port = host.Services.GetRequiredService <IFindInternalTypesPort>();
                var internalSymbols = await port.FindProjClassesWhichCanBeInternalAsync(solution, proj.Id,
                                                                                        progressSubscriber,
                                                                                        CancellationToken.None);
                AnsiConsole.WriteLine($"Found {internalSymbols.Count()} public types which can be made internal");

                var fix = host.Services.GetRequiredService <IPublicToInternalFixPort>();
                var newSolution = await fix.MakePublicTypesInternal(solution, internalSymbols);
                return workspace.TryApplyChanges(newSolution) ? 0 : -1;
            }));
        }
Esempio n. 2
0
        private static MSBuildWorkspace CreateWorkspace(AnsiConsoleProgressSubscriber progressSubscriber, ILogger <Program> logger)
        {
            progressSubscriber.Report(new FindInternalClassesProgress(FindInternalTypesStep.LoadSolution, 0));
            var workspace = MSBuildWorkspace.Create();

            workspace.WorkspaceFailed += HandleWorkspaceFailure(logger);
            return(workspace);
        }
Esempio n. 3
0
        private static async Task <int> AnalyzeCommand(IHost host, FileInfo solutionPath, string projectName)
        {
            try
            {
                return(await AnsiConsole.Progress().AutoClear(false)
                       .Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn(), new ElapsedTimeColumn()).
                       StartAsync(async progressContext =>
                {
                    var logger = host.Services.GetRequiredService <ILogger <Program> >();
                    var progressSubscriber = new AnsiConsoleProgressSubscriber(progressContext);

                    var workspace = CreateWorkspace(progressSubscriber, logger);

                    var(solution, proj) = await OpenSolutionAndProject(solutionPath, projectName, workspace, progressSubscriber, logger);
                    if (proj == null)
                    {
                        return -1;
                    }

                    progressSubscriber.Report(
                        new FindInternalClassesProgress(FindInternalTypesStep.LoadSolution, 100));

                    var port = host.Services.GetRequiredService <IFindInternalTypesPort>();
                    var internalSymbols = await port.FindProjClassesWhichCanBeInternalAsync(solution, proj.Id,
                                                                                            progressSubscriber,
                                                                                            CancellationToken.None);
                    AnsiConsole.WriteLine($"Found {internalSymbols.Count()} public types which can be made internal");

                    var table = new Table();
                    table.AddColumn("№").AddColumn("Type").AddColumn("Location");

                    var i = 0;
                    foreach (var symbol in internalSymbols)
                    {
                        table.AddRow($"{++i}", $"{symbol.TypeKind} {symbol.Name}", symbol.Locations.FirstOrDefault()?.GetLineSpan().ToString() ?? string.Empty);
                    }

                    AnsiConsole.Render(table);

                    return 0;
                }));
            }
            catch (Exception e)
            {
                AnsiConsole.WriteException(e);
                return(-1);
            }
        }
Esempio n. 4
0
        private static async Task <(Solution solution, Project?proj)> OpenSolutionAndProject(FileInfo solutionPath, string projectName, MSBuildWorkspace workspace,
                                                                                             AnsiConsoleProgressSubscriber progressSubscriber, ILogger <Program> logger)
        {
            var solution = await workspace.OpenSolutionAsync(solutionPath.ToString(), progressSubscriber);

            var proj = solution.Projects.FirstOrDefault(p => p.Name == projectName);

            if (proj == null)
            {
                logger.LogError($"Project not found: {projectName}");
                return(solution, proj);
            }

            return(solution, proj);
        }