Exemplo n.º 1
0
    public async Task ConvertDocumentAsync <TLanguageConversion>(string documentFilePath, Span selected, CancellationToken cancellationToken) where TLanguageConversion : ILanguageConversion, new()
    {
        try {
            var containingProject = await VisualStudioInteraction.GetFirstProjectContainingAsync(documentFilePath);
            await EnsureBuiltAsync(containingProject is null?Array.Empty <Project>() : new[] { containingProject });

            var conversionResult = await _joinableTaskFactory.RunAsync(async() => {
                var result = await ConvertDocumentUnhandledAsync <TLanguageConversion>(documentFilePath, selected, cancellationToken);
                await WriteConvertedFilesAndShowSummaryAsync(new[] { result }.ToAsyncEnumerable());
                return(result);
            });

            if ((await GetOptions()).CopyResultToClipboardForSingleDocument)
            {
                await SetClipboardTextOnUiThreadAsync(conversionResult.ConvertedCode ?? conversionResult.GetExceptionsAsString());

                await _outputWindow.WriteToOutputWindowAsync(Environment.NewLine + "Conversion result copied to clipboard.");

                await VisualStudioInteraction.ShowMessageBoxAsync("Conversion result copied to clipboard.", $"Conversion result copied to clipboard. {conversionResult.GetExceptionsAsString()}", false);
            }
        } catch (OperationCanceledException) {
            if (!_packageCancellation.CancelAll.IsCancellationRequested)
            {
                await _outputWindow.WriteToOutputWindowAsync(Environment.NewLine + "Previous conversion cancelled", forceShow : true);
            }
        }
    }
Exemplo n.º 2
0
    public async Task ConvertDocumentsAsync <TLanguageConversion>(IReadOnlyCollection <string> documentsFilePath, CancellationToken cancellationToken) where TLanguageConversion : ILanguageConversion, new()
    {
        try {
            var containingProject = await VisualStudioInteraction.GetFirstProjectContainingAsync(documentsFilePath.First());
            await EnsureBuiltAsync(containingProject is null?Array.Empty <Project>() : new[] { containingProject });

            await _joinableTaskFactory.RunAsync(async() => {
                var result = ConvertDocumentsUnhandledAsync <TLanguageConversion>(documentsFilePath, cancellationToken);
                await WriteConvertedFilesAndShowSummaryAsync(result);
            });
        } catch (OperationCanceledException) {
            if (!_packageCancellation.CancelAll.IsCancellationRequested)
            {
                await _outputWindow.WriteToOutputWindowAsync(Environment.NewLine + "Previous conversion cancelled", forceShow : true);
            }
        }
    }
Exemplo n.º 3
0
    private async Task <bool> UserHasConfirmedOverwriteAsync(List <string> files, List <string> errors, IReadOnlyCollection <string> pathsToOverwrite)
    {
        var maxExamples = 30; // Avoid a huge unreadable dialog going off the screen
        var exampleText = pathsToOverwrite.Count > maxExamples ? $". First {maxExamples} examples" : "";
        await _outputWindow.WriteToOutputWindowAsync(Environment.NewLine + "Awaiting user confirmation for overwrite....", forceShow : true);

        bool shouldOverwrite = await VisualStudioInteraction.ShowMessageBoxAsync("Overwrite solution and referencing projects?",
                                                                                 $@"The current solution file and any referencing projects will be overwritten to reference the new project(s){exampleText}:
* {string.Join(Environment.NewLine + "* ", pathsToOverwrite.Take(maxExamples))}

The old contents will be copied to 'currentFilename.bak'.
Please 'Reload All' when Visual Studio prompts you.", true, files.Count > errors.Count);

        await _outputWindow.WriteToOutputWindowAsync(shouldOverwrite? "confirmed" : "declined");

        return(shouldOverwrite);
    }
Exemplo n.º 4
0
    private async Task <string> GetConversionSummaryAsync(IReadOnlyCollection <string> files, IReadOnlyCollection <string> errors)
    {
        var oneLine        = "Code conversion failed";
        var successSummary = "";

        if (files.Any())
        {
            oneLine        = "Code conversion completed";
            successSummary = $"{files.Count} files have been written to disk.";
        }

        if (errors.Any())
        {
            oneLine += $" with {errors.Count} error" + (errors.Count == 1 ? "" : "s");
        }

        if (VisualStudioInteraction.GetUpdateWarningsOrNull() is { } warnings)
        {
            successSummary += Environment.NewLine + warnings;
        }
Exemplo n.º 5
0
    private async Task FinalizeConversionAsync(List <string> files, List <string> errors, string longestFilePath, List <ConversionResult> filesToOverwrite)
    {
        var options = await GetOptions();

        var pathsToOverwrite = filesToOverwrite.Select(f => PathRelativeToSolutionDir(f.SourcePathOrNull));
        var shouldOverwriteSolutionAndProjectFiles =
            filesToOverwrite.Any() &&
            (options.AlwaysOverwriteFiles || await UserHasConfirmedOverwriteAsync(files, errors, pathsToOverwrite.ToList()));

        if (shouldOverwriteSolutionAndProjectFiles)
        {
            var titleMessage = options.CreateBackups ? "Creating backups and overwriting files:" : "Overwriting files:" + "";
            await _outputWindow.WriteToOutputWindowAsync(Environment.NewLine + titleMessage);

            foreach (var fileToOverwrite in filesToOverwrite)
            {
                if (options.CreateBackups)
                {
                    File.Copy(fileToOverwrite.SourcePathOrNull, fileToOverwrite.SourcePathOrNull + ".bak", true);
                }
                fileToOverwrite.WriteToFile();

                var targetPathRelativeToSolutionDir = PathRelativeToSolutionDir(fileToOverwrite.TargetPathOrNull);
                await _outputWindow.WriteToOutputWindowAsync(Environment.NewLine + $"* {targetPathRelativeToSolutionDir}");
            }
            files = files.Concat(filesToOverwrite.Select(f => f.SourcePathOrNull)).ToList();
        }
        else if (longestFilePath != null)
        {
            await(await VisualStudioInteraction.OpenFileAsync(new FileInfo(longestFilePath))).SelectAllAsync();
        }

        var conversionSummary = await GetConversionSummaryAsync(files, errors);

        await _outputWindow.WriteToOutputWindowAsync(conversionSummary, false, true);
    }
Exemplo n.º 6
0
 /// <remarks>
 /// https://github.com/icsharpcode/CodeConverter/issues/592
 /// https://github.com/dotnet/roslyn/issues/6615
 /// </remarks>
 private async Task EnsureBuiltAsync(IReadOnlyCollection <Project> readOnlyCollection)
 {
     await VisualStudioInteraction.EnsureBuiltAsync(readOnlyCollection, m => _outputWindow.WriteToOutputWindowAsync(m));
 }