Ejemplo n.º 1
0
            static string GetBestTFM(BaselineProject baselineProject, bool keepCurrentTfm, string?specifiedTFM, bool usePreviewSDK)
            {
                if (string.IsNullOrWhiteSpace(specifiedTFM))
                {
                    // Let's figure this out, friends
                    var tfmForApps = TargetFrameworkHelper.FindHighestInstalledTargetFramework(usePreviewSDK);

                    if (keepCurrentTfm)
                    {
                        specifiedTFM = baselineProject.GetTfm();
                    }
                    else if (baselineProject.ProjectStyle == ProjectStyle.WindowsDesktop || baselineProject.ProjectStyle == ProjectStyle.MSTest)
                    {
                        specifiedTFM = tfmForApps;
                    }
                    else if (baselineProject.OutputType == ProjectOutputType.Library)
                    {
                        specifiedTFM = MSBuildFacts.Netstandard20;
                    }
                    else if (baselineProject.OutputType == ProjectOutputType.Exe)
                    {
                        specifiedTFM = tfmForApps;
                    }
                    else
                    {
                        // Default is to just use what exists in the project
                        specifiedTFM = baselineProject.GetTfm();
                    }
                }

                return(specifiedTFM);
            }
Ejemplo n.º 2
0
        public static int Run(string?project, string?workspace, string?msbuildPath, string?tfm, bool allowPreviews, bool diffOnly, bool noBackup)
        {
            if (!string.IsNullOrWhiteSpace(project) && !string.IsNullOrWhiteSpace(workspace))
            {
                Console.WriteLine("Cannot specify both a project and a workspace.");
                return(-1);
            }

            try
            {
                msbuildPath = MSBuildHelpers.HookAssemblyResolveForMSBuild(msbuildPath);
                if (string.IsNullOrWhiteSpace(msbuildPath))
                {
                    Console.WriteLine("Could not find an MSBuild.");
                    return(-1);
                }

                if (tfm is null)
                {
                    tfm = TargetFrameworkHelper.FindHighestInstalledTargetFramework(allowPreviews);
                }
                else
                {
                    tfm = tfm.Trim();
                    if (!TargetFrameworkHelper.IsValidTargetFramework(tfm))
                    {
                        Console.WriteLine($"Invalid framework specified for --target-framework: '{tfm}'");
                        return(-1);
                    }
                }

                var workspacePath = string.Empty;
                MSBuildWorkspaceType workspaceType;

                if (!string.IsNullOrWhiteSpace(project))
                {
                    workspacePath = Path.GetFullPath(project, Environment.CurrentDirectory);
                    workspaceType = MSBuildWorkspaceType.Project;
                }
                else
                {
                    var(isSolution, workspaceFilePath) = MSBuildWorkspaceFinder.FindWorkspace(Environment.CurrentDirectory, workspace);
                    workspaceType = isSolution ? MSBuildWorkspaceType.Solution : MSBuildWorkspaceType.Project;
                    workspacePath = workspaceFilePath;
                }

                var workspaceLoader = new MSBuildWorkspaceLoader(workspacePath, workspaceType);
                // do not create backup if --diff-only specified
                noBackup = noBackup || diffOnly;
                var msbuildWorkspace = workspaceLoader.LoadWorkspace(workspacePath, noBackup);

                foreach (var item in msbuildWorkspace.WorkspaceItems)
                {
                    if (diffOnly)
                    {
                        var differ = new Differ(item.UnconfiguredProject.FirstConfiguredProject, item.SdkBaselineProject.Project.FirstConfiguredProject);
                        differ.GenerateReport(Directory.GetParent(workspacePath).FullName);
                    }
                    else
                    {
                        var converter = new Converter(item.UnconfiguredProject, item.SdkBaselineProject, item.ProjectRootElement);
                        converter.Convert(tfm, item.ProjectRootElement.FullPath);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(-1);
            }

            Console.WriteLine("Conversion complete!");
            return(0);
        }
Ejemplo n.º 3
0
        public static int Run(string?project, string?workspace, string?msbuildPath, string?tfm, bool forceWebConversion, bool preview, bool diffOnly, bool noBackup, bool keepCurrentTfms, bool update, bool mauiConversion, bool forceRemoveCustomImports)
        {
            if (update)
            {
                UpdateTryConvert.Update();
                return(0);
            }

            if (!string.IsNullOrWhiteSpace(project) && !string.IsNullOrWhiteSpace(workspace))
            {
                Console.WriteLine("Cannot specify both a project and a workspace.");
                return(-1);
            }

            if (!string.IsNullOrWhiteSpace(tfm) && keepCurrentTfms)
            {
                Console.WriteLine($"Both '{nameof(tfm)}' and '{nameof(keepCurrentTfms)}' cannot be specified. Please pick one.");
                return(-1);
            }

            try
            {
                //For Xamarin Projects, set MSBuild path to VSInstallation Dir via Environment Variable
                if (mauiConversion)
                {
                    var vsinstalldir = Environment.GetEnvironmentVariable("VSINSTALLDIR");
                    if (!string.IsNullOrEmpty(vsinstalldir))
                    {
                        msbuildPath = MSBuildHelpers.HookAssemblyResolveForMSBuild(Path.Combine(vsinstalldir, "MSBuild", "Current", "Bin"));
                    }
                    else
                    {
                        string vsPath = new VisualStudioLocator().GetLatestVisualStudioPath();
                        if (string.IsNullOrWhiteSpace(vsPath))
                        {
                            Console.WriteLine("Error locating VS Install Directory. Try setting Environment Variable VSINSTALLDIR.");
                            return(-1);
                        }
                        else
                        {
                            msbuildPath = MSBuildHelpers.HookAssemblyResolveForMSBuild(Path.Combine(vsPath, "MSBuild", "Current", "Bin"));
                        }
                    }
                }
                else
                {
                    msbuildPath = MSBuildHelpers.HookAssemblyResolveForMSBuild(msbuildPath);
                }

                if (string.IsNullOrWhiteSpace(msbuildPath))
                {
                    Console.WriteLine("Could not find an MSBuild.");
                    return(-1);
                }

                if (!string.IsNullOrWhiteSpace(tfm))
                {
                    tfm = tfm.Trim();
                    if (!TargetFrameworkHelper.IsValidTargetFramework(tfm))
                    {
                        Console.WriteLine($"Invalid framework specified for --target-framework: '{tfm}'");
                        return(-1);
                    }
                }
                else
                {
                    tfm = TargetFrameworkHelper.FindHighestInstalledTargetFramework(preview, msbuildPath);
                }

                var workspacePath = string.Empty;
                MSBuildConversionWorkspaceType workspaceType;

                if (!string.IsNullOrWhiteSpace(project))
                {
                    workspacePath = Path.GetFullPath(project, Environment.CurrentDirectory);
                    workspaceType = MSBuildConversionWorkspaceType.Project;
                }
                else
                {
                    var(isSolution, workspaceFilePath) = MSBuildConversionWorkspaceFinder.FindWorkspace(Environment.CurrentDirectory, workspace);
                    workspaceType = isSolution ? MSBuildConversionWorkspaceType.Solution : MSBuildConversionWorkspaceType.Project;
                    workspacePath = workspaceFilePath;
                }

                var workspaceLoader = new MSBuildConversionWorkspaceLoader(workspacePath, workspaceType);
                // do not create backup if --diff-only specified
                noBackup = noBackup || diffOnly;
                var msbuildWorkspace = workspaceLoader.LoadWorkspace(workspacePath, noBackup, tfm, keepCurrentTfms, forceWebConversion);

                if (msbuildWorkspace.WorkspaceItems.Length is 0)
                {
                    Console.WriteLine("No projects converted.");
                    return(0);
                }

                foreach (var item in msbuildWorkspace.WorkspaceItems)
                {
                    if (diffOnly)
                    {
                        var differ = new Differ(item.UnconfiguredProject.FirstConfiguredProject, item.SdkBaselineProject.Project.FirstConfiguredProject);
                        var parent = Directory.GetParent(workspacePath);
                        if (parent is null)
                        {
                            differ.GenerateReport(workspacePath);
                        }
                        else
                        {
                            differ.GenerateReport(parent.FullName);
                        }
                    }
                    else
                    {
                        var converter = new Converter(item.UnconfiguredProject, item.SdkBaselineProject, item.ProjectRootElement, noBackup, forceRemoveCustomImports);
                        converter.Convert(item.ProjectRootElement.FullPath);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(-1);
            }

            Console.WriteLine("Conversion complete!");
            return(0);
        }
Ejemplo n.º 4
0
        public static int Run(string?project, string?workspace, string?msbuildPath, string?tfm, bool forceWebConversion, bool preview, bool diffOnly, bool noBackup, bool keepCurrentTfms, decimal?generateDependenciesVersion)
        {
            if (!string.IsNullOrWhiteSpace(project) && !string.IsNullOrWhiteSpace(workspace))
            {
                Console.WriteLine("Cannot specify both a project and a workspace.");
                return(-1);
            }

            if (!string.IsNullOrWhiteSpace(tfm) && keepCurrentTfms)
            {
                Console.WriteLine($"Both '{nameof(tfm)}' and '{nameof(keepCurrentTfms)}' cannot be specified. Please pick one.");
                return(-1);
            }

            try
            {
                if (generateDependenciesVersion != null)
                {
                    Console.WriteLine($"Generating dependencies for {generateDependenciesVersion} version");
                    DependenciesGenerator.Generate(generateDependenciesVersion.Value);
                    return(0);
                }

                msbuildPath = MSBuildHelpers.HookAssemblyResolveForMSBuild(msbuildPath);
                if (string.IsNullOrWhiteSpace(msbuildPath))
                {
                    Console.WriteLine("Could not find an MSBuild.");
                    return(-1);
                }

                if (!string.IsNullOrWhiteSpace(tfm))
                {
                    tfm = tfm.Trim();
                    if (!TargetFrameworkHelper.IsValidTargetFramework(tfm))
                    {
                        Console.WriteLine($"Invalid framework specified for --target-framework: '{tfm}'");
                        return(-1);
                    }
                }
                else
                {
                    tfm = TargetFrameworkHelper.FindHighestInstalledTargetFramework(preview);
                }

                var workspacePath = string.Empty;
                MSBuildConversionWorkspaceType workspaceType;

                if (!string.IsNullOrWhiteSpace(project))
                {
                    workspacePath = Path.GetFullPath(project, Environment.CurrentDirectory);
                    workspaceType = MSBuildConversionWorkspaceType.Project;
                }
                else
                {
                    var(isSolution, workspaceFilePath) = MSBuildConversionWorkspaceFinder.FindWorkspace(Environment.CurrentDirectory, workspace);
                    workspaceType = isSolution ? MSBuildConversionWorkspaceType.Solution : MSBuildConversionWorkspaceType.Project;
                    workspacePath = workspaceFilePath;
                }

                var workspaceLoader = new MSBuildConversionWorkspaceLoader(workspacePath, workspaceType);
                // do not create backup if --diff-only specified
                noBackup = noBackup || diffOnly;
                var msbuildWorkspace = workspaceLoader.LoadWorkspace(workspacePath, noBackup, tfm, keepCurrentTfms, forceWebConversion);

                if (msbuildWorkspace.WorkspaceItems.Length is 0)
                {
                    Console.WriteLine("No projects converted.");
                    return(0);
                }

                foreach (var item in msbuildWorkspace.WorkspaceItems)
                {
                    if (diffOnly)
                    {
                        var differ = new Differ(item.UnconfiguredProject.FirstConfiguredProject, item.SdkBaselineProject.Project.FirstConfiguredProject);
                        var parent = Directory.GetParent(workspacePath);
                        if (parent is null)
                        {
                            differ.GenerateReport(workspacePath);
                        }
                        else
                        {
                            differ.GenerateReport(parent.FullName);
                        }
                    }
                    else
                    {
                        var converter = new Converter(item.UnconfiguredProject, item.SdkBaselineProject, item.ProjectRootElement, noBackup);
                        converter.Convert(item.ProjectRootElement.FullPath);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(-1);
            }

            Console.WriteLine("Conversion complete!");
            return(0);
        }