|
| 1 | +// `mur clean-local` — removes local NuGet packages produced by `mur pack-local`, |
| 2 | +// clears the matching entries from the NuGet global packages cache, and uninstalls |
| 3 | +// any `dotnet new` project templates that were installed from the local feed. |
| 4 | +// |
| 5 | +// This is the inverse of `mur pack-local` and addresses the "need uninstall" |
| 6 | +// half of https://github.com/microsoft/microsoft-ui-reactor/issues/238. |
| 7 | + |
| 8 | +using System.Diagnostics; |
| 9 | + |
| 10 | +namespace Microsoft.UI.Reactor.Cli.Pack; |
| 11 | + |
| 12 | +public static class CleanLocalCommand |
| 13 | +{ |
| 14 | + // Package IDs that `pack-local` produces — lowercase to match the NuGet |
| 15 | + // global-packages folder convention. |
| 16 | + internal static readonly string[] PackageIds = |
| 17 | + [ |
| 18 | + "microsoft.ui.reactor", |
| 19 | + "microsoft.ui.reactor.projecttemplates", |
| 20 | + ]; |
| 21 | + |
| 22 | + // Template package ID used by `dotnet new install`. |
| 23 | + internal const string TemplatePackageId = "Microsoft.UI.Reactor.ProjectTemplates"; |
| 24 | + |
| 25 | + public static int Run(string[] args) |
| 26 | + { |
| 27 | + var repoRoot = RepoRootFinder.FindRepoRoot(Directory.GetCurrentDirectory()) |
| 28 | + ?? RepoRootFinder.FindRepoRoot(); |
| 29 | + |
| 30 | + if (repoRoot is null) |
| 31 | + { |
| 32 | + Console.Error.WriteLine("mur clean-local: must be run from a Reactor source checkout (could not locate src/Reactor)."); |
| 33 | + return 1; |
| 34 | + } |
| 35 | + |
| 36 | + var feed = Path.Combine(repoRoot, "local-nupkgs"); |
| 37 | + var removed = 0; |
| 38 | + |
| 39 | + // 1. Delete .nupkg / .snupkg files from the local feed. |
| 40 | + if (Directory.Exists(feed)) |
| 41 | + { |
| 42 | + foreach (var file in Directory.EnumerateFiles(feed, "*.nupkg") |
| 43 | + .Concat(Directory.EnumerateFiles(feed, "*.snupkg"))) |
| 44 | + { |
| 45 | + try |
| 46 | + { |
| 47 | + File.Delete(file); |
| 48 | + Console.WriteLine($" Deleted {Path.GetFileName(file)}"); |
| 49 | + removed++; |
| 50 | + } |
| 51 | + catch (Exception ex) |
| 52 | + { |
| 53 | + Console.Error.WriteLine($" Could not delete {Path.GetFileName(file)}: {ex.Message}"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (removed == 0) |
| 59 | + Console.WriteLine(" No local packages found — nothing to remove."); |
| 60 | + |
| 61 | + // 2. Remove cached copies from the NuGet global-packages folder so that |
| 62 | + // subsequent restores don't silently resolve a stale local version. |
| 63 | + var globalPackages = GetGlobalPackagesPath(); |
| 64 | + if (globalPackages is not null) |
| 65 | + { |
| 66 | + foreach (var id in PackageIds) |
| 67 | + { |
| 68 | + var pkgDir = Path.Combine(globalPackages, id); |
| 69 | + if (!Directory.Exists(pkgDir)) continue; |
| 70 | + |
| 71 | + // Only delete versions that look like local builds. |
| 72 | + foreach (var versionDir in Directory.EnumerateDirectories(pkgDir)) |
| 73 | + { |
| 74 | + var versionName = Path.GetFileName(versionDir); |
| 75 | + if (IsLocalVersion(versionName)) |
| 76 | + { |
| 77 | + try |
| 78 | + { |
| 79 | + Directory.Delete(versionDir, recursive: true); |
| 80 | + Console.WriteLine($" Removed cached {id}/{versionName}"); |
| 81 | + } |
| 82 | + catch (Exception ex) |
| 83 | + { |
| 84 | + Console.Error.WriteLine($" Could not remove cached {id}/{versionName}: {ex.Message}"); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // 3. Clear the NuGet HTTP cache so stale metadata doesn't linger. |
| 92 | + RunDotnet(repoRoot, "nuget", "locals", "http-cache", "--clear"); |
| 93 | + |
| 94 | + // 4. Uninstall project templates (non-fatal). |
| 95 | + UninstallTemplates(repoRoot); |
| 96 | + |
| 97 | + Console.WriteLine(); |
| 98 | + Console.WriteLine("Done."); |
| 99 | + return 0; |
| 100 | + } |
| 101 | + |
| 102 | + internal static string? GetGlobalPackagesPath() |
| 103 | + { |
| 104 | + try |
| 105 | + { |
| 106 | + var psi = new ProcessStartInfo("dotnet") |
| 107 | + { |
| 108 | + UseShellExecute = false, |
| 109 | + RedirectStandardOutput = true, |
| 110 | + RedirectStandardError = true, |
| 111 | + ArgumentList = { "nuget", "locals", "global-packages", "--list" }, |
| 112 | + }; |
| 113 | + using var proc = Process.Start(psi); |
| 114 | + if (proc is null) return null; |
| 115 | + var output = proc.StandardOutput.ReadToEnd(); |
| 116 | + proc.WaitForExit(); |
| 117 | + |
| 118 | + // Output is: "global-packages: C:\Users\...\.nuget\packages\" |
| 119 | + var idx = output.IndexOf(':'); |
| 120 | + return idx >= 0 ? output[(idx + 1)..].Trim().TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) : null; |
| 121 | + } |
| 122 | + catch |
| 123 | + { |
| 124 | + return null; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + internal static bool IsLocalVersion(string version) |
| 129 | + { |
| 130 | + // Match PackLocalCommand.DefaultLocalVersion ("0.0.0-local") and any |
| 131 | + // user-supplied version containing "local" (e.g. "1.0.0-local.42"). |
| 132 | + return version.Contains("local", StringComparison.OrdinalIgnoreCase); |
| 133 | + } |
| 134 | + |
| 135 | + static void UninstallTemplates(string repoRoot) |
| 136 | + { |
| 137 | + try |
| 138 | + { |
| 139 | + var psi = new ProcessStartInfo("dotnet") |
| 140 | + { |
| 141 | + UseShellExecute = false, |
| 142 | + WorkingDirectory = repoRoot, |
| 143 | + RedirectStandardOutput = true, |
| 144 | + RedirectStandardError = true, |
| 145 | + ArgumentList = { "new", "uninstall", TemplatePackageId }, |
| 146 | + }; |
| 147 | + using var proc = Process.Start(psi); |
| 148 | + if (proc is null) return; |
| 149 | + proc.WaitForExit(); |
| 150 | + if (proc.ExitCode == 0) |
| 151 | + Console.WriteLine($" Uninstalled dotnet new template: {TemplatePackageId}"); |
| 152 | + // Exit code != 0 means the template wasn't installed — that's fine. |
| 153 | + } |
| 154 | + catch { /* non-fatal */ } |
| 155 | + } |
| 156 | + |
| 157 | + static void RunDotnet(string workingDirectory, params string[] arguments) |
| 158 | + { |
| 159 | + try |
| 160 | + { |
| 161 | + var psi = new ProcessStartInfo("dotnet") |
| 162 | + { |
| 163 | + UseShellExecute = false, |
| 164 | + WorkingDirectory = workingDirectory, |
| 165 | + RedirectStandardOutput = true, |
| 166 | + RedirectStandardError = true, |
| 167 | + }; |
| 168 | + foreach (var a in arguments) psi.ArgumentList.Add(a); |
| 169 | + using var proc = Process.Start(psi); |
| 170 | + proc?.WaitForExit(); |
| 171 | + } |
| 172 | + catch { /* non-fatal */ } |
| 173 | + } |
| 174 | +} |
0 commit comments