-
-
Notifications
You must be signed in to change notification settings - Fork 11
Improve fallout-migrate: NuGet-scoped version pin, TFM warning/bump, confirmation prompt, async pipeline #509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0162a77
Bump Fallout.Migrate to the 10.x version line
dennisdoomen b615bf1
Resolve Fallout.Common version from NuGet, scoped to the tool's own m…
dennisdoomen f6145f5
Warn when the build project targets an older TFM than .NET 10
dennisdoomen 8850126
Ask for confirmation before the migration writes any files
dennisdoomen 571a785
Fold CsprojRewriter into RewriteCsprojsStep
dennisdoomen 0e25d72
Bump build project to net10.0 and global.json SDK to 10.0.100
dennisdoomen a3ae50a
Make IMigrationStep and the migration pipeline asynchronous
dennisdoomen 57c10db
Deduplicate the TFM-minimum check shared by two migration steps
dennisdoomen 9d0b642
Reorder ResolveFalloutVersionStep by invocation order and tag NuGet r…
dennisdoomen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using System.Globalization; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Fallout.Migrate.Common; | ||
|
|
||
| /// <summary> | ||
| /// Shared parsing helpers for modern, dotted .NET target framework monikers (e.g. <c>net10.0</c>), | ||
| /// used by migration steps that compare a project's current TFM against a minimum. | ||
| /// </summary> | ||
| internal static class TargetFrameworkMonikers | ||
| { | ||
| /// <summary> | ||
| /// Matches a modern, dotted .NET moniker such as <c>net8.0</c> or <c>net10.0</c>. | ||
| /// </summary> | ||
| private static readonly Regex modernMonikerPattern = new( | ||
| @"^net(?<major>\d+)\.\d+$", | ||
| RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
|
|
||
| /// <summary> | ||
| /// Extracts the major version segment from a modern TFM moniker such as <c>net10.0</c>. | ||
| /// </summary> | ||
| /// <param name="moniker">A modern, dotted target framework moniker.</param> | ||
| /// <returns>The parsed major version.</returns> | ||
| public static int ExtractMajor(string moniker) | ||
| { | ||
| return int.Parse( | ||
| modernMonikerPattern.Match(moniker).Groups["major"].Value, | ||
| CultureInfo.InvariantCulture); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns <c>true</c> when <paramref name="moniker"/> targets an older .NET than | ||
| /// <paramref name="minimumMajor"/> — including non-modern monikers (.NET Framework, .NET | ||
| /// Standard, out-of-support <c>netcoreapp*</c>) which never satisfy any minimum. | ||
| /// </summary> | ||
| /// <param name="moniker">A single target framework moniker, e.g. <c>net8.0</c>.</param> | ||
| /// <param name="minimumMajor">The minimum supported major version.</param> | ||
| public static bool IsOlderThanMinimumSupported(string moniker, int minimumMajor) | ||
| { | ||
| Match match = modernMonikerPattern.Match(moniker); | ||
| if (!match.Success) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| int major = int.Parse(match.Groups["major"].Value, CultureInfo.InvariantCulture); | ||
| return major < minimumMajor; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| using System; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading.Tasks; | ||
| using Fallout.Migrate.Common; | ||
|
|
||
| namespace Fallout.Migrate.Steps; | ||
|
|
||
| /// <summary> | ||
| /// Bumps the repo's build orchestrator project (<c>_build.csproj</c>) to target | ||
| /// <see cref="TargetFramework"/> and pins <c>global.json</c>'s SDK version to | ||
| /// <see cref="SdkVersion"/> — but only when what's there is behind those minimums. Already | ||
| /// up-to-date or newer values (e.g. a build project already on <c>net11.0</c>) are left alone. | ||
| /// </summary> | ||
| internal sealed class BumpDotNetVersionStep : IMigrationStep | ||
| { | ||
| /// <summary> | ||
| /// The .NET target framework moniker to bump <c>_build.csproj</c> to when it's behind. | ||
| /// </summary> | ||
| private const string TargetFramework = "net10.0"; | ||
|
|
||
| /// <summary> | ||
| /// The .NET SDK version to pin <c>global.json</c> to when it's behind. | ||
| /// </summary> | ||
| private const string SdkVersion = "10.0.100"; | ||
|
dennisdoomen marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// The minimum .NET SDK version. Versions at or above this aren't touched. | ||
| /// </summary> | ||
| private static readonly Version minimumSupportedSdkVersion = new(10, 0, 100); | ||
|
|
||
| /// <summary> | ||
| /// The minimum .NET target framework major version, derived from <see cref="TargetFramework"/>. | ||
| /// Monikers at or above this aren't touched. Shared with | ||
| /// <see cref="VerifyBuildTargetFrameworkStep"/> so the minimum is only defined once. | ||
| /// </summary> | ||
| internal static readonly int MinimumSupportedMajor = TargetFrameworkMonikers.ExtractMajor(TargetFramework); | ||
|
|
||
| /// <summary> | ||
| /// Matches the <c>TargetFramework</c> element's raw value. Build projects don't multi-target, | ||
| /// so <c>TargetFrameworks</c> (plural) is intentionally not matched here. | ||
| /// </summary> | ||
| private static readonly Regex targetFrameworkElementPattern = new( | ||
| @"<TargetFramework>(?<value>[^<]+)</TargetFramework>", | ||
| RegexOptions.Compiled); | ||
|
|
||
| /// <summary> | ||
| /// Matches the value of <c>global.json</c>'s <c>sdk.version</c> property. | ||
| /// </summary> | ||
| private static readonly Regex sdkVersionPattern = new( | ||
| @"(?<=""sdk""\s*:\s*\{[^}]*?""version""\s*:\s*"")[^""]+", | ||
| RegexOptions.Compiled | RegexOptions.Singleline); | ||
|
|
||
| /// <inheritdoc /> | ||
| public Task ExecuteAsync(MigrationContext context, Summary summary) | ||
| { | ||
| foreach (var path in MigrationFileOperations.EnumerateFiles(context.RootDirectory, "_build.csproj")) | ||
| { | ||
| MigrationFileOperations.ApplyRewrite(context, path, BumpTargetFramework, summary); | ||
| } | ||
|
|
||
| foreach (var path in MigrationFileOperations.EnumerateFiles(context.RootDirectory, "global.json")) | ||
| { | ||
| MigrationFileOperations.ApplyRewrite(context, path, BumpSdkVersion, summary); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Rewrites a build project's <c>TargetFramework</c> element to <see cref="TargetFramework"/> | ||
| /// when its current moniker is behind <see cref="MinimumSupportedMajor"/>. | ||
| /// </summary> | ||
| /// <param name="original">The original <c>_build.csproj</c> content.</param> | ||
| /// <returns>The rewritten content and the number of edits made.</returns> | ||
| public static RewriteResult BumpTargetFramework(string original) | ||
| { | ||
| Match match = targetFrameworkElementPattern.Match(original); | ||
| if (!match.Success || | ||
| !TargetFrameworkMonikers.IsOlderThanMinimumSupported(match.Groups["value"].Value, MinimumSupportedMajor)) | ||
| { | ||
| return new RewriteResult(original, 0); | ||
| } | ||
|
|
||
| string content = targetFrameworkElementPattern.Replace( | ||
| original, | ||
| $"<TargetFramework>{TargetFramework}</TargetFramework>", | ||
| count: 1); | ||
|
|
||
| return new RewriteResult(content, 1); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Rewrites <c>global.json</c>'s <c>sdk.version</c> to <see cref="SdkVersion"/> only when the | ||
| /// current version is behind <see cref="minimumSupportedSdkVersion"/>. | ||
| /// </summary> | ||
| /// <param name="original">The original <c>global.json</c> content.</param> | ||
| /// <returns>The rewritten content and the number of edits made.</returns> | ||
| public static RewriteResult BumpSdkVersion(string original) | ||
| { | ||
| Match match = sdkVersionPattern.Match(original); | ||
| if (!match.Success || !IsOlderThanMinimumSupportedSdk(match.Value)) | ||
| { | ||
| return new RewriteResult(original, 0); | ||
| } | ||
|
|
||
| string content = sdkVersionPattern.Replace(original, SdkVersion, count: 1); | ||
| return new RewriteResult(content, 1); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns <c>true</c> when <paramref name="version"/> is behind | ||
| /// <see cref="minimumSupportedSdkVersion"/>, or can't be parsed as a version at all. | ||
| /// </summary> | ||
| /// <param name="version">The <c>sdk.version</c> value from <c>global.json</c>.</param> | ||
| private static bool IsOlderThanMinimumSupportedSdk(string version) | ||
| { | ||
| Version parsed; | ||
| if (!Version.TryParse(version, out parsed)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return parsed < minimumSupportedSdkVersion; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Fallout.Migrate.Common; | ||
| using Spectre.Console; | ||
|
|
||
| namespace Fallout.Migrate.Steps; | ||
|
|
||
| /// <summary> | ||
| /// Asks the user to confirm before any subsequent step writes to disk. Runs last among the | ||
| /// read-only/advisory steps, immediately before <see cref="RewriteCsprojsStep"/> — the first step | ||
| /// in <see cref="Migration.steps"/> that mutates files. Sets <see cref="Summary.Cancelled"/> when | ||
| /// the user declines, which <see cref="Migration.RunAsync"/> checks to stop early. | ||
| /// </summary> | ||
| internal sealed class ConfirmMigrationStep : IMigrationStep | ||
| { | ||
| /// <inheritdoc /> | ||
| public async Task ExecuteAsync(MigrationContext context, Summary summary) | ||
| { | ||
| if (context.DryRun) | ||
| { | ||
| // Nothing will be written; no point confirming. | ||
| return; | ||
| } | ||
|
|
||
| if (Console.IsInputRedirected) | ||
| { | ||
| // Non-interactive (CI, piped input, automated tests) — nothing to prompt against, so | ||
| // proceed as if confirmed. | ||
| return; | ||
| } | ||
|
|
||
| var prompt = new ConfirmationPrompt( | ||
| $"This will modify files under [blue]{context.RootDirectory}[/]. Continue?"); | ||
|
dennisdoomen marked this conversation as resolved.
|
||
|
|
||
| bool proceed = await prompt.ShowAsync(AnsiConsole.Console, CancellationToken.None); | ||
| if (!proceed) | ||
| { | ||
| summary.Cancelled = true; | ||
| AnsiConsole.MarkupLine("[yellow]Migration cancelled — no files were changed.[/]"); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.