Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions src/Fallout.Migrate/Steps/RewriteBootstrapScriptsStep.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Fallout.Common.IO;
using Fallout.Migrate.Common;

namespace Fallout.Migrate.Steps;

/// <summary>
/// Rewrites <c>build.cmd</c>, <c>build.ps1</c>, and <c>build.sh</c> at the repository root, when
/// present, via <see cref="ScriptRewriter"/>.
/// Rewrites bootstrap scripts (<c>build.cmd</c>/<c>build.ps1</c>/<c>build.sh</c>): <c>dotnet nuke</c>
Comment thread
ITaluone marked this conversation as resolved.
/// invocations, <c>.nuke</c> path references, and legacy <c>NUKE_*</c> environment variables become
/// their Fallout equivalents.
/// </summary>
internal sealed class RewriteBootstrapScriptsStep : IMigrationStep
{
/// <summary>The ordered find/replace patterns applied by <see cref="Rewrite"/>.</summary>
private static readonly (Regex Pattern, string Replacement)[] patterns =
[
// Strip any telemetry opt-out line entirely — telemetry was removed from Fallout
// (ADR-0010), so there is nothing to opt out of. Handles bash `export`, PowerShell
// `$env:`, and cmd `set` spellings by matching the whole line. Runs before the
// env-var renames below so the line is gone rather than renamed to a dead variable.
(new Regex(@"^.*\b(?:NUKE|FALLOUT)_TELEMETRY_OPTOUT\b.*\r?\n?", RegexOptions.Compiled | RegexOptions.Multiline), ""),
// `dotnet nuke` invocations
(new Regex(@"\bdotnet\s+nuke\b", RegexOptions.Compiled), "dotnet fallout"),
// .nuke directory references → .fallout
(new Regex(@"(?<=[\\/.""'\s])\.nuke(?=[\\/""'\s])", RegexOptions.Compiled), ".fallout"),
// Legacy env vars (consumer-facing ones from P3.5c)
(new Regex(@"\bNUKE_GLOBAL_TOOL_VERSION\b", RegexOptions.Compiled), "FALLOUT_GLOBAL_TOOL_VERSION"),
(new Regex(@"\bNUKE_GLOBAL_TOOL_START_TIME\b", RegexOptions.Compiled), "FALLOUT_GLOBAL_TOOL_START_TIME"),
(new Regex(@"\bNUKE_INTERNAL_INTERCEPTOR\b", RegexOptions.Compiled), "FALLOUT_INTERNAL_INTERCEPTOR")
];

/// <inheritdoc />
public Task ExecuteAsync(MigrationContext context, Summary summary)
{
Expand All @@ -23,10 +43,32 @@ public Task ExecuteAsync(MigrationContext context, Summary summary)
var path = context.RootDirectory / name;
if (path.FileExists())
{
MigrationFileOperations.ApplyRewrite(context, path, ScriptRewriter.Rewrite, summary);
MigrationFileOperations.ApplyRewrite(context, path, Rewrite, summary);
}
}

return Task.CompletedTask;
}

/// <summary>
/// Rewrites <paramref name="original"/> script content, applying every pattern in
/// <see cref="patterns"/> in order.
/// </summary>
/// <param name="original">The original script file content.</param>
/// <returns>The rewritten content and the number of edits made.</returns>
private static RewriteResult Rewrite(string original)
{
var edits = 0;
var content = original;
foreach (var (pattern, replacement) in patterns)
{
content = pattern.Replace(content, _ =>
{
edits++;
return replacement;
});
}

return new RewriteResult(content, edits);
}
}
2 changes: 1 addition & 1 deletion src/Fallout.Migrate/Steps/RewriteCsFilesStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Task ExecuteAsync(MigrationContext context, Summary summary)
/// </summary>
/// <param name="original">The original <c>.cs</c> file content.</param>
/// <returns>The rewritten content and the number of edits made.</returns>
public static RewriteResult Rewrite(string original)
private static RewriteResult Rewrite(string original)
{
var edits = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/Fallout.Migrate/Steps/RewriteCsprojsStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Task ExecuteAsync(MigrationContext context, Summary summary)
/// <param name="original">The original <c>.csproj</c> file content.</param>
/// <param name="falloutVersion">The Fallout version to pin into rewritten inline-versioned references.</param>
/// <returns>The rewritten content and the number of edits made.</returns>
public static RewriteResult Rewrite(string original, string falloutVersion)
private static RewriteResult Rewrite(string original, string falloutVersion)
{
var edits = 0;
var content = original;
Expand Down
52 changes: 0 additions & 52 deletions src/Fallout.Migrate/Steps/ScriptRewriter.cs

This file was deleted.

6 changes: 6 additions & 0 deletions tests/Fallout.Migrate.Specs/MigrationIntegrationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class Build : NukeBuild
#!/usr/bin/env bash
export NUKE_TELEMETRY_OPTOUT=1
TEMP_DIRECTORY="$SCRIPT_DIR/.nuke/temp"

if [[ ! -z ${NUKE_ENTERPRISE_TOKEN+x} && "$NUKE_ENTERPRISE_TOKEN" != "" ]]; then
"$DOTNET_EXE" nuget remove source "nuke-enterprise" &>/dev/null || true
"$DOTNET_EXE" nuget add source "https://f.feedz.io/nuke/enterprise/nuget" --name "nuke-enterprise" --username "PAT" --password "$NUKE_ENTERPRISE_TOKEN" --store-password-in-clear-text &>/dev/null || true
fi

dotnet nuke "$@"
""");

Expand Down
198 changes: 198 additions & 0 deletions tests/Fallout.Migrate.Specs/RewriteBootstrapScriptsStepSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Fallout.Common.IO;
using Fallout.Migrate.Common;
using Fallout.Migrate.Steps;
using FluentAssertions;
using Xunit;

namespace Fallout.Migrate.Specs;

public class RewriteBootstrapScriptsStepSpecs : IDisposable
{
private readonly AbsolutePath tempDirectory;
private readonly MigrationContext context;
private readonly Summary summary = new();

public RewriteBootstrapScriptsStepSpecs()
{
tempDirectory = AbsolutePath.Temp("fallout-migrate-test");
context = new MigrationContext(tempDirectory, dryRun: false, TextWriter.Null);
}

public void Dispose()
{
tempDirectory.DeleteDirectory();
}

[Fact]
public async Task Dotnet_nuke_invocations_become_dotnet_fallout()
{
(tempDirectory / "build.sh").WriteAllText("dotnet nuke Compile", eofLineBreak: false);

await new RewriteBootstrapScriptsStep().ExecuteAsync(context, summary);

summary.EditCount.Should().Be(1);
var buildSh = (tempDirectory / "build.sh").ReadAllText();
buildSh.Should().Be("dotnet fallout Compile");
}

[Fact]
public async Task Dot_nuke_directory_references_become_dot_fallout()
{
(tempDirectory / "build.sh").WriteAllText("""TEMP_DIRECTORY="$SCRIPT_DIR/.nuke/temp" """, eofLineBreak: false);

await new RewriteBootstrapScriptsStep().ExecuteAsync(context, summary);

summary.EditCount.Should().Be(1);
var buildSh = (tempDirectory / "build.sh").ReadAllText();
buildSh.Should().Contain(".fallout/temp");
buildSh.Should().NotContain(".nuke/");
}

[Fact]
public async Task Legacy_nuke_env_vars_are_renamed_to_their_fallout_equivalents()
{
(tempDirectory / "build.ps1").WriteAllText("""
$env:NUKE_GLOBAL_TOOL_VERSION = "10.0"
""", eofLineBreak: false);

await new RewriteBootstrapScriptsStep().ExecuteAsync(context, summary);

summary.EditCount.Should().Be(1);
var buildPs1 = (tempDirectory / "build.ps1").ReadAllText();
buildPs1.Should().Contain("FALLOUT_GLOBAL_TOOL_VERSION");
}

[Theory]
[InlineData("build.sh", "export NUKE_TELEMETRY_OPTOUT=1")] // build.sh
[InlineData("build.ps1", """$env:NUKE_TELEMETRY_OPTOUT = "1" """)] // build.ps1
[InlineData("build.cmd", "set NUKE_TELEMETRY_OPTOUT=1")] // build.cmd
public async Task Telemetry_opt_out_line_is_stripped_entirely(string fileName, string optOutLine)
{
// Telemetry was removed from Fallout (ADR-0010) — the opt-out is dropped, not renamed
// to a dead FALLOUT_TELEMETRY_OPTOUT. Whichever bootstrap script spelled it, the whole
// line goes and the surrounding ones are untouched.
var input = $"""
export DOTNET_ROLL_FORWARD="Major"
{optOutLine}
dotnet nuke "$@"
""";
(tempDirectory / fileName).WriteAllText(input, eofLineBreak: false);

await new RewriteBootstrapScriptsStep().ExecuteAsync(context, summary);

var content = (tempDirectory / fileName).ReadAllText();
content.Should().NotContain("TELEMETRY_OPTOUT");
content.Should().Contain("DOTNET_ROLL_FORWARD");
content.Should().Contain("dotnet fallout");
}

[Fact]
public async Task Plain_word_nuke_in_prose_is_left_alone()
{
// The word "nuke" in a comment or string isn't a command invocation.
(tempDirectory / "build.sh").WriteAllText("# This was previously a NUKE-based build.", eofLineBreak: false);

await new RewriteBootstrapScriptsStep().ExecuteAsync(context, summary);

summary.EditCount.Should().Be(0);
}

[Fact]
public async Task Removes_Nuke_enterprise_unix_bootstrapper_leftovers()
{
(tempDirectory / "build.sh").WriteAllText("""
echo "Microsoft (R) .NET SDK version $("$DOTNET_EXE" --version)"

if [[ ! -z ${NUKE_ENTERPRISE_TOKEN+x} && "$NUKE_ENTERPRISE_TOKEN" != "" ]]; then
"$DOTNET_EXE" nuget remove source "nuke-enterprise" &>/dev/null || true
"$DOTNET_EXE" nuget add source "https://f.feedz.io/nuke/enterprise/nuget" --name "nuke-enterprise" --username "PAT" --password "$NUKE_ENTERPRISE_TOKEN" --store-password-in-clear-text &>/dev/null || true
fi

"$DOTNET_EXE" build "$BUILD_PROJECT_FILE" /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet
"$DOTNET_EXE" run --project "$BUILD_PROJECT_FILE" --no-build -- "$@"
""", eofLineBreak: false);

await new CleanupBootstrapScriptsStep().ExecuteAsync(context, summary);

var buildSh = (tempDirectory / "build.sh").ReadAllText();
summary.EditCount.Should().Be(1);
buildSh.Should().Be(
"""
echo "Microsoft (R) .NET SDK version $("$DOTNET_EXE" --version)"

"$DOTNET_EXE" build "$BUILD_PROJECT_FILE" /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet
"$DOTNET_EXE" run --project "$BUILD_PROJECT_FILE" --no-build -- "$@"
""");
}

[Fact]
public async Task Does_not_throw_when_Nuke_enterprise_check_is_last_in_file()
{
(tempDirectory / "build.sh").WriteAllText("""
if [[ ! -z ${NUKE_ENTERPRISE_TOKEN+x} && "$NUKE_ENTERPRISE_TOKEN" != "" ]]; then
"$DOTNET_EXE" nuget remove source "nuke-enterprise" &>/dev/null || true
"$DOTNET_EXE" nuget add source "https://f.feedz.io/nuke/enterprise/nuget" --name "nuke-enterprise" --username "PAT" --password "$NUKE_ENTERPRISE_TOKEN" --store-password-in-clear-text &>/dev/null || true
fi
""");

await new CleanupBootstrapScriptsStep().ExecuteAsync(context, summary);

var buildSh = (tempDirectory / "build.sh").ReadAllText();
summary.EditCount.Should().Be(1);
buildSh.Should().Be("");
}

[Fact]
public async Task Removes_Nuke_enterprise_windows_bootstrapper_leftovers()
{
(tempDirectory / "build.ps1").WriteAllText("""
Write-Output "Microsoft (R) .NET SDK version $(& $env:DOTNET_EXE --version)"

if (Test-Path env:NUKE_ENTERPRISE_TOKEN) {
& $env:DOTNET_EXE nuget remove source "nuke-enterprise" > $null
& $env:DOTNET_EXE nuget add source "https://f.feedz.io/nuke/enterprise/nuget" --name "nuke-enterprise" --username "PAT" --password $env:NUKE_ENTERPRISE_TOKEN > $null
}

ExecSafe { & $env:DOTNET_EXE build $BuildProjectFile /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet }
ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile --no-build -- $BuildArguments }
""", eofLineBreak: false);

await new CleanupBootstrapScriptsStep().ExecuteAsync(context, summary);

var buildPs1 = (tempDirectory / "build.ps1").ReadAllText();
summary.EditCount.Should().Be(1);
buildPs1.Should().Be(
"""
Write-Output "Microsoft (R) .NET SDK version $(& $env:DOTNET_EXE --version)"

ExecSafe { & $env:DOTNET_EXE build $BuildProjectFile /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet }
ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile --no-build -- $BuildArguments }
""");
}

[Fact]
public async Task Leaves_bootstrapper_scripts_without_leftovers_alone()
{
(tempDirectory / "build.sh").WriteAllText("""
echo "Microsoft (R) .NET SDK version $("$DOTNET_EXE" --version)"

"$DOTNET_EXE" build "$BUILD_PROJECT_FILE" /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet
"$DOTNET_EXE" run --project "$BUILD_PROJECT_FILE" --no-build -- "$@"
""", eofLineBreak: false);

await new CleanupBootstrapScriptsStep().ExecuteAsync(context, summary);

var buildSh = (tempDirectory / "build.sh").ReadAllText();
summary.EditCount.Should().Be(0);
buildSh.Should().Be(
"""
echo "Microsoft (R) .NET SDK version $("$DOTNET_EXE" --version)"

"$DOTNET_EXE" build "$BUILD_PROJECT_FILE" /nodeReuse:false /p:UseSharedCompilation=false -nologo -clp:NoSummary --verbosity quiet
"$DOTNET_EXE" run --project "$BUILD_PROJECT_FILE" --no-build -- "$@"
""");
}
}
Loading
Loading