public bool Uninstall(DotNetCoreSdk sdk) { Dictionary <SdkVersion, string> map; if (sdk.Is64Bit) { map = x64UninstallComannds; } else { map = x86UninstallCommands; } string uninstallCommand; if (!map.TryGetValue(sdk.Version, out uninstallCommand)) { return(false); } var psi = new ProcessStartInfo(Environment.GetEnvironmentVariable("ComSpec"), "/c " + uninstallCommand) { CreateNoWindow = true, UseShellExecute = false, }; var p = Process.Start(psi); p.WaitForExit(); return(true); }
void CalculateKeep(bool keepOnlyLastVersionPerRuntime, IEnumerable <DotNetCoreSdk> installedSdksEnumerable, HashSet <SdkVersion> visualStudioVersions, HashSet <SdkVersion> visualStudioBands) { DotNetCoreSdk previous = null; foreach (var sdk in installedSdksEnumerable.OrderByDescending(s => s.Version)) { if (previous == null) { //Always keep the latest version. SdksToKeep.Add(sdk); } else if (keepOnlyLastVersionPerRuntime && !previous.Version.IncludedRuntimeBand.Equals(sdk.Version.IncludedRuntimeBand)) { SdksToKeep.Add(sdk); } else if (!keepOnlyLastVersionPerRuntime && !previous.Version.SdkVersionBand.Equals(sdk.Version.SdkVersionBand)) { SdksToKeep.Add(sdk); } else //By the normal rules we do not want to keep this SDK, but Viusal Studio might want it. { if (visualStudioVersions.Contains(sdk.Version)) { //Keep exact matching VS version in case Visual studio does not like it getting pulled out from under it. SdksToKeep.Add(sdk); SdksPinnedByVisualStudio.Add(sdk); } else if (visualStudioBands.Contains(sdk.Version.SdkVersionBand) && !previous.Version.SdkVersionBand.Equals(sdk.Version.SdkVersionBand)) { //Keep the newest version in the version band that Visual Studio desires. SdksToKeep.Add(sdk); SdksPinnedByVisualStudio.Add(sdk); } else { SdksToDelete.Add(sdk); } } previous = sdk; } }
private void DoCleanSdks() { var installedSdk = DotNetCoreSdk.GetInstalledSdks(); HashSet <SdkVersion> vsVersions; if (IgnoreVisualStudio) { vsVersions = new HashSet <SdkVersion>(); } else { vsVersions = VSCatalog.GetVsUsedVersions(); if (vsVersions.Count == 0) { //VS 2019 16.3 stops depending on .NET SDKs less than version 3.0. Instead it installs the //shared .NET Core runtime and ASP.NET core runtimes without the associated SDKs. //I think this means that while we can't pin SDKs from Visual Studio, there are no SDKs to pin, //so we won't uninstall anything that is needed. WriteWithColor(ConsoleColor.Yellow, "WARNING: Could not find any installed Visual Studio version."); } } var delPlan = new DeletionPlan(KeepOnlyLastVersionPerRuntime, installedSdk, vsVersions); Console.WriteLine("SDKs to keep:"); foreach (var sdk in delPlan.SdksToKeep) { Console.WriteLine("\t" + sdk); } Console.WriteLine(); if (delPlan.SdksPinnedByVisualStudio.Count != 0) { Console.WriteLine("Extra SDKs kept by Visual Studio:"); foreach (var sdk in delPlan.SdksPinnedByVisualStudio) { Console.WriteLine("\t" + sdk); } Console.WriteLine(); } if (delPlan.SdksToDelete.Count == 0) { Console.WriteLine("No SDKs to delete."); return; } else { Console.WriteLine("SDKs to delete:"); foreach (var sdk in delPlan.SdksToDelete) { Console.WriteLine("\t" + sdk); } Console.WriteLine(); } if (DryRun) { return; } if (!Security.IsUserAdmin()) { throw new ExitException("User is not an administrator, exiting."); } if (!Force) { Console.Write("Type 'yes' to delete these SDKs: "); } if (Force || Console.ReadLine() == "yes") { Console.WriteLine("Preparing to delete SDKs, please wait..."); var uninstaller = new Uninstaller(); foreach (var sdk in delPlan.SdksToDelete) { Console.WriteLine("\tUninstalling: " + sdk.ToString()); if (!uninstaller.Uninstall(sdk)) { WriteWithColor(ConsoleColor.Yellow, "\tCould not find uninstall command."); } } } else { Console.WriteLine("Not cleaning SDKs per user request."); } }