コード例 #1
0
        private void UpgradeCode(Package dependentPackage, ILogger log, ICodeUpgrader codeUpgrader)
        {
            if (dependentPackage == null)
            {
                throw new ArgumentNullException(nameof(dependentPackage));
            }
            if (codeUpgrader == null)
            {
                throw new ArgumentNullException(nameof(codeUpgrader));
            }

            var csharpWorkspaceAssemblies = new[] { Assembly.Load("Microsoft.CodeAnalysis.Workspaces"), Assembly.Load("Microsoft.CodeAnalysis.CSharp.Workspaces"), Assembly.Load("Microsoft.CodeAnalysis.Workspaces.Desktop") };
            var workspace = MSBuildWorkspace.Create(ImmutableDictionary <string, string> .Empty, MefHostServices.Create(csharpWorkspaceAssemblies));

            var tasks = dependentPackage.Profiles
                        .SelectMany(profile => profile.ProjectReferences)
                        .Select(projectReference => UPath.Combine(dependentPackage.RootDirectory, projectReference.Location))
                        .Distinct()
                        .Select(projectFullPath => Task.Run(async() =>
            {
                if (codeUpgrader.UpgradeProject(workspace, projectFullPath))
                {
                    // Upgrade source code
                    var f = new FileInfo(projectFullPath.ToWindowsPath());
                    if (f.Exists)
                    {
                        var project     = await workspace.OpenProjectAsync(f.FullName);
                        var compilation = await project.GetCompilationAsync();
                        var subTasks    = compilation.SyntaxTrees.Select(syntaxTree => Task.Run(() => codeUpgrader.UpgradeSourceFile(syntaxTree))).ToList();

                        await Task.WhenAll(subTasks);
                    }
                    else
                    {
                        log.Error("Cannot locate {0}.", f.FullName);
                    }
                }
            }))
                        .ToArray();

            Task.WaitAll(tasks);
        }
コード例 #2
0
        private void UpgradeCode(Package dependentPackage, ILogger log, ICodeUpgrader codeUpgrader)
        {
            if (dependentPackage == null)
            {
                throw new ArgumentNullException(nameof(dependentPackage));
            }
            if (codeUpgrader == null)
            {
                throw new ArgumentNullException(nameof(codeUpgrader));
            }

            var csharpWorkspaceAssemblies = new[] { Assembly.Load("Microsoft.CodeAnalysis.Workspaces"), Assembly.Load("Microsoft.CodeAnalysis.CSharp.Workspaces"), Assembly.Load("Microsoft.CodeAnalysis.Workspaces.Desktop") };
            var workspace = MSBuildWorkspace.Create(ImmutableDictionary <string, string> .Empty, MefHostServices.Create(csharpWorkspaceAssemblies));

            var projectFullPath = (dependentPackage.Container as SolutionProject)?.FullPath;

            if (projectFullPath != null)
            {
                Task.Run(async() =>
                {
                    codeUpgrader.UpgradeProject(workspace, projectFullPath);

                    // Upgrade source code
                    var f = new FileInfo(projectFullPath.ToWindowsPath());
                    if (f.Exists)
                    {
                        var project  = await workspace.OpenProjectAsync(f.FullName);
                        var subTasks = project.Documents.Concat(project.AdditionalDocuments).Select(x => codeUpgrader.UpgradeSourceFile(x.FilePath)).ToList();

                        await Task.WhenAll(subTasks);
                    }
                    else
                    {
                        log.Error($"Cannot locate project {f.FullName}.");
                    }
                }).Wait();
            }
        }
コード例 #3
0
ファイル: XenkoPackageUpgrader.cs プロジェクト: cg123/xenko
        private void UpgradeCode(Package dependentPackage, ILogger log, ICodeUpgrader codeUpgrader)
        {
            if (dependentPackage == null) throw new ArgumentNullException(nameof(dependentPackage));
            if (codeUpgrader == null) throw new ArgumentNullException(nameof(codeUpgrader));

            var csharpWorkspaceAssemblies = new[] { Assembly.Load("Microsoft.CodeAnalysis.Workspaces"), Assembly.Load("Microsoft.CodeAnalysis.CSharp.Workspaces"), Assembly.Load("Microsoft.CodeAnalysis.Workspaces.Desktop") };
            var workspace = MSBuildWorkspace.Create(ImmutableDictionary<string, string>.Empty, MefHostServices.Create(csharpWorkspaceAssemblies));

            var tasks = dependentPackage.Profiles
                .SelectMany(profile => profile.ProjectReferences)
                .Select(projectReference => UPath.Combine(dependentPackage.RootDirectory, projectReference.Location))
                .Distinct()
                .Select(projectFullPath => Task.Run(async () =>
                {
                    if (codeUpgrader.UpgradeProject(workspace, projectFullPath))
                    {
                        // Upgrade source code
                        var f = new FileInfo(projectFullPath.ToWindowsPath());
                        if (f.Exists)
                        {
                            var project = await workspace.OpenProjectAsync(f.FullName);
                            var compilation = await project.GetCompilationAsync();
                            var subTasks = compilation.SyntaxTrees.Select(syntaxTree => Task.Run(() => codeUpgrader.UpgradeSourceFile(syntaxTree))).ToList();

                            await Task.WhenAll(subTasks);
                        }
                        else
                        {
                            log.Error("Cannot locate {0}.", f.FullName);
                        }
                    }
                }))
                .ToArray();

            Task.WaitAll(tasks);
        }