Exemplo n.º 1
0
        public MSBuildConversionWorkspace(ImmutableArray <string> paths, bool noBackup, string tfm, bool keepCurrentTFMs, bool forceWeb)
        {
            var items = ImmutableArray.CreateBuilder <MSBuildConversionWorkspaceItem>();

            var globalProperties = ImmutableDictionary <string, string> .Empty;

            using var collection = new ProjectCollection();

            foreach (var path in paths)
            {
                var fileExtension = Path.GetExtension(path);
                if ((StringComparer.OrdinalIgnoreCase.Compare(fileExtension, ".fsproj") != 0) &&
                    (StringComparer.OrdinalIgnoreCase.Compare(fileExtension, ".csproj") != 0) &&
                    (StringComparer.OrdinalIgnoreCase.Compare(fileExtension, ".vbproj") != 0))
                {
                    Console.WriteLine($"'{path}' is not a .NET project, skipping it.");
                    continue;
                }

                // This is a hack, but the only way to handle this is to re-architect try-convert along these lines:
                // 1. Launch a .NET Framework process using the VS-deployed MSBuild to evaluate a project
                // 2. Serialize the evaluation model
                // 3. Use the .NET Core process to load MSBuild, but this time from .NET SDK
                // 4. Deserialize the evaluation model
                // 5. Do conversions
                RemoveTargetsNotLoadableByNETSDKMSBuild(path);

                var root = new MSBuildProjectRootElement(ProjectRootElement.Open(path, collection, preserveFormatting: true));
                if (IsSupportedProjectType(root, forceWeb))
                {
                    var configurations = DetermineConfigurations(root);

                    var unconfiguredProject = new UnconfiguredProject(configurations);
                    unconfiguredProject.LoadProjects(collection, globalProperties, path);


                    if (TryCreateSdkBaselineProject(path, unconfiguredProject.FirstConfiguredProject, root, configurations, tfm, keepCurrentTFMs, out var baseline))
                    {
                        if (!noBackup)
                        {
                            // Since git doesn't track the new '.old' addition in your changeset,
                            // failing to overwrite will crash the tool if you have one in your directory.
                            // This can be common if you're using the tool a few times and forget to delete the backup.
                            File.Copy(path, path + ".old", overwrite: true);
                        }

                        root.Reload(throwIfUnsavedChanges: false, preserveFormatting: true);
                        var item = new MSBuildConversionWorkspaceItem(root, unconfiguredProject, baseline.Value);
                        items.Add(item);
                    }
                }
            }

            WorkspaceItems = items.ToImmutable();
        }
        public MSBuildConversionWorkspace(ImmutableArray <string> paths, bool noBackup, string tfm, bool keepCurrentTFMs)
        {
            var items = ImmutableArray.CreateBuilder <MSBuildConversionWorkspaceItem>();

            var globalProperties = ImmutableDictionary <string, string> .Empty;

            using var collection = new ProjectCollection();

            foreach (var path in paths)
            {
                var fileExtension = Path.GetExtension(path);
                if ((StringComparer.OrdinalIgnoreCase.Compare(fileExtension, ".fsproj") != 0) &&
                    (StringComparer.OrdinalIgnoreCase.Compare(fileExtension, ".csproj") != 0) &&
                    (StringComparer.OrdinalIgnoreCase.Compare(fileExtension, ".vbproj") != 0))
                {
                    Console.WriteLine($"'{path}' is not a .NET project, skipping it.");
                    continue;
                }

                var root = new MSBuildProjectRootElement(ProjectRootElement.Open(path, collection, preserveFormatting: true));
                if (IsSupportedProjectType(root))
                {
                    var configurations = DetermineConfigurations(root);

                    var unconfiguredProject = new UnconfiguredProject(configurations);
                    unconfiguredProject.LoadProjects(collection, globalProperties, path);


                    if (TryCreateSdkBaselineProject(path, unconfiguredProject.FirstConfiguredProject, root, configurations, tfm, keepCurrentTFMs, out var baseline))
                    {
                        if (!noBackup)
                        {
                            // Since git doesn't track the new '.old' addition in your changeset,
                            // failing to overwrite will crash the tool if you have one in your directory.
                            // This can be common if you're using the tool a few times and forget to delete the backup.
                            File.Copy(path, path + ".old", overwrite: true);
                        }

                        root.Reload(throwIfUnsavedChanges: false, preserveFormatting: true);
                        var item = new MSBuildConversionWorkspaceItem(root, unconfiguredProject, baseline.Value);
                        items.Add(item);
                    }
                }
            }

            WorkspaceItems = items.ToImmutable();
        }
Exemplo n.º 3
0
        public MSBuildWorkspace(ImmutableArray <string> paths, bool noBackup)
        {
            var items = ImmutableArray.CreateBuilder <MSBuildWorkspaceItem>();

            var globalProperties = ImmutableDictionary <string, string> .Empty;

            using var collection = new ProjectCollection();

            foreach (var path in paths)
            {
                var fileExtension = Path.GetExtension(path);
                if ((StringComparer.OrdinalIgnoreCase.Compare(fileExtension, ".fsproj") != 0) &&
                    (StringComparer.OrdinalIgnoreCase.Compare(fileExtension, ".csproj") != 0) &&
                    (StringComparer.OrdinalIgnoreCase.Compare(fileExtension, ".vbproj") != 0))
                {
                    Console.WriteLine($"'{path}' is not a .NET project, skipping it.");
                    continue;
                }

                var root = new MSBuildProjectRootElement(ProjectRootElement.Open(path, collection, preserveFormatting: true));
                if (IsSupportedProjectType(root))
                {
                    if (!noBackup)
                    {
                        File.Copy(path, path + ".old");
                    }

                    var configurations = DetermineConfigurations(root);

                    var unconfiguredProject = new UnconfiguredProject(configurations);
                    unconfiguredProject.LoadProjects(collection, globalProperties, path);

                    var baseline = CreateSdkBaselineProject(path, unconfiguredProject.FirstConfiguredProject, root, configurations);
                    root.Reload(throwIfUnsavedChanges: false, preserveFormatting: true);

                    var item = new MSBuildWorkspaceItem(root, unconfiguredProject, baseline);
                    items.Add(item);
                }
            }

            WorkspaceItems = items.ToImmutable();
        }