Exemplo n.º 1
0
        public static IEnumerable <ConversionResult> ConvertProjects(IEnumerable <Project> projects)
        {
            var solutionDir = Path.GetDirectoryName(projects.First().Solution.FilePath);

            foreach (var project in projects)
            {
                var compilation       = project.GetCompilationAsync().GetAwaiter().GetResult();
                var projectConversion = new ProjectConversion <TLanguageConversion>(compilation, solutionDir);
                foreach (var pathNodePair in projectConversion.Convert())
                {
                    yield return(new ConversionResult(pathNodePair.Value.ToFullString())
                    {
                        SourcePathOrNull = pathNodePair.Key
                    });
                }

                foreach (var error in projectConversion._errors)
                {
                    yield return(new ConversionResult(error.Value)
                    {
                        SourcePathOrNull = error.Key
                    });
                }
            }
        }
Exemplo n.º 2
0
        public static async Task <ConversionResult> ConvertSingle <TLanguageConversion>(Document document, SingleConversionOptions conversionOptions, IProgress <ConversionProgress> progress = null, CancellationToken cancellationToken = default) where TLanguageConversion : ILanguageConversion, new()
        {
            progress = progress ?? new Progress <ConversionProgress>();
            using var roslynEntryPoint = await RoslynEntryPoint(progress);

            var languageConversion = new TLanguageConversion {
                ConversionOptions = conversionOptions
            };

            bool returnSelectedNode = conversionOptions.SelectedTextSpan.Length > 0;

            if (returnSelectedNode)
            {
                document = await WithAnnotatedSelection(document, conversionOptions.SelectedTextSpan);
            }

            var projectContentsConverter = await languageConversion.CreateProjectContentsConverter(document.Project, progress, cancellationToken);

            document = projectContentsConverter.Project.GetDocument(document.Id);

            var conversion        = new ProjectConversion(projectContentsConverter, new[] { document }, Enumerable.Empty <TextDocument>(), languageConversion, cancellationToken, conversionOptions.ShowCompilationErrors, returnSelectedNode);
            var conversionResults = await conversion.Convert(progress).ToArrayAsync();

            return(GetSingleResultForDocument(conversionResults, document, progress));
        }
Exemplo n.º 3
0
        public static async Task <ConversionResult> ConvertSingle(Compilation compilation, SyntaxTree syntaxTree, TextSpan selected)
        {
            if (selected.Length > 0)
            {
                var annotatedSyntaxTree = await GetSyntaxTreeWithAnnotatedSelection(syntaxTree, selected);

                compilation = compilation.ReplaceSyntaxTree(syntaxTree, annotatedSyntaxTree);
                syntaxTree  = annotatedSyntaxTree;
            }

            var conversion = new ProjectConversion <TLanguageConversion>(compilation, new [] { syntaxTree });
            var converted  = conversion.Convert();

            if (!converted.Any())
            {
                var conversionError = conversion._errors.Single();
                return(new ConversionResult(conversionError.Value)
                {
                    SourcePathOrNull = conversionError.Key
                });
            }
            var resultPair = converted.Single();
            var resultNode = GetSelectedNode(resultPair.Value);

            return(new ConversionResult(resultNode.ToFullString())
            {
                SourcePathOrNull = resultPair.Key
            });
        }
Exemplo n.º 4
0
        public static async Task <ConversionResult> ConvertSingle <TLanguageConversion>(Document document, SingleConversionOptions conversionOptions, IProgress <ConversionProgress> progress = null, CancellationToken cancellationToken = default) where TLanguageConversion : ILanguageConversion, new()
        {
            progress = progress ?? new Progress <ConversionProgress>();
            using var roslynEntryPoint = await RoslynEntryPoint(progress);

            var languageConversion = new TLanguageConversion {
                ConversionOptions = conversionOptions
            };

            bool returnSelectedNode = conversionOptions.SelectedTextSpan.Length > 0;

            if (returnSelectedNode)
            {
                document = await WithAnnotatedSelection(document, conversionOptions.SelectedTextSpan);
            }

            var projectContentsConverter = await languageConversion.CreateProjectContentsConverter(document.Project, progress, cancellationToken);

            document = projectContentsConverter.Project.GetDocument(document.Id);

            var conversion        = new ProjectConversion(projectContentsConverter, new[] { document }, languageConversion, cancellationToken, conversionOptions.ShowCompilationErrors, returnSelectedNode);
            var conversionResults = await conversion.Convert(progress).ToArrayAsync();

            var codeResult = conversionResults.SingleOrDefault(x => !string.IsNullOrWhiteSpace(x.ConvertedCode))
                             ?? conversionResults.First();

            codeResult.Exceptions = conversionResults.SelectMany(x => x.Exceptions).ToArray();
            return(codeResult);
        }
Exemplo n.º 5
0
        public static IEnumerable <ConversionResult> ConvertProjectContents(Project project,
                                                                            ILanguageConversion languageConversion)
        {
            var solutionFilePath  = project.Solution.FilePath;
            var solutionDir       = Path.GetDirectoryName(solutionFilePath);
            var compilation       = project.GetCompilationAsync().GetAwaiter().GetResult();
            var projectConversion = new ProjectConversion(compilation, solutionDir, languageConversion);

            foreach (var conversionResult in ConvertProjectContents(projectConversion))
            {
                yield return(conversionResult);
            }
        }
Exemplo n.º 6
0
        public static IEnumerable <ConversionResult> ConvertProjectContents(Project project,
                                                                            ILanguageConversion languageConversion)
        {
            var solutionFilePath  = project.Solution.FilePath;
            var solutionDir       = Path.GetDirectoryName(solutionFilePath);
            var compilation       = project.GetCompilationAsync().GetAwaiter().GetResult();
            var projectConversion = new ProjectConversion(compilation, compilation.SyntaxTrees.Where(t => t.FilePath.StartsWith(solutionDir)), languageConversion, GetConvertedCompilationWithProjectReferences(project, languageConversion));

            foreach (var conversionResult in ConvertProjectContents(projectConversion))
            {
                yield return(conversionResult);
            }
        }
Exemplo n.º 7
0
        public static IEnumerable <ConversionResult> ConvertProjects(IReadOnlyCollection <Project> projects)
        {
            var solutionDir = Path.GetDirectoryName(projects.First().Solution.FilePath);

            foreach (var project in projects)
            {
                var compilation       = project.GetCompilationAsync().GetAwaiter().GetResult();
                var projectConversion = new ProjectConversion <TLanguageConversion>(compilation, solutionDir);
                foreach (var conversionResult in ConvertProject(projectConversion))
                {
                    yield return(conversionResult);
                }
            }
        }
Exemplo n.º 8
0
        public static async Task <ConversionResult> ConvertSingle(Compilation compilation, SyntaxTree syntaxTree, TextSpan selected, ILanguageConversion languageConversion)
        {
            if (selected.Length > 0)
            {
                var annotatedSyntaxTree = await GetSyntaxTreeWithAnnotatedSelection(syntaxTree, selected);

                compilation = compilation.ReplaceSyntaxTree(syntaxTree, annotatedSyntaxTree);
                syntaxTree  = annotatedSyntaxTree;
            }

            var conversion        = new ProjectConversion(compilation, new [] { syntaxTree }, languageConversion);
            var conversionResults = ConvertProjectContents(conversion).ToList();
            var codeResult        = conversionResults.SingleOrDefault(x => !string.IsNullOrWhiteSpace(x.ConvertedCode))
                                    ?? conversionResults.First();

            codeResult.Exceptions = conversionResults.SelectMany(x => x.Exceptions).ToArray();
            return(codeResult);
        }
Exemplo n.º 9
0
        private static IEnumerable <ConversionResult> ConvertProjectContents(ProjectConversion projectConversion)
        {
            foreach (var pathNodePair in projectConversion.Convert())
            {
                var errors = projectConversion._errors.TryRemove(pathNodePair.Key, out var nonFatalException)
                    ? new[] { nonFatalException }
                    : new string[0];
                yield return(new ConversionResult(pathNodePair.Value.ToFullString())
                {
                    SourcePathOrNull = pathNodePair.Key, Exceptions = errors
                });
            }

            foreach (var error in projectConversion._errors)
            {
                yield return(new ConversionResult {
                    SourcePathOrNull = error.Key, Exceptions = new [] { error.Value }
                });
            }
        }
Exemplo n.º 10
0
        public static async IAsyncEnumerable <ConversionResult> ConvertDocumentsAsync <TLanguageConversion>(
            IReadOnlyCollection <Document> documents,
            ConversionOptions conversionOptions,
            IProgress <ConversionProgress> progress = null,
            [EnumeratorCancellation] CancellationToken cancellationToken = default) where TLanguageConversion : ILanguageConversion, new()
        {
            using var roslynEntryPoint = await RoslynEntryPointAsync(progress ??= new Progress <ConversionProgress>());

            var languageConversion = new TLanguageConversion {
                ConversionOptions = conversionOptions
            };

            var project = documents.First().Project;
            var projectContentsConverter = await languageConversion.CreateProjectContentsConverterAsync(project, progress, cancellationToken);

            documents = documents.Select(doc => projectContentsConverter.SourceProject.GetDocument(doc.Id)).ToList();

            var conversion = new ProjectConversion(projectContentsConverter, documents, Enumerable.Empty <TextDocument>(), languageConversion, cancellationToken);

            await foreach (var result in conversion.Convert(progress).WithCancellation(cancellationToken))
            {
                yield return(result);
            }
        }