diff --git a/src/Fallout.Cli/Commands/GetConfigurationCommand.cs b/src/Fallout.Cli/Commands/GetConfigurationCommand.cs new file mode 100644 index 00000000..4af696eb --- /dev/null +++ b/src/Fallout.Cli/Commands/GetConfigurationCommand.cs @@ -0,0 +1,27 @@ +using System; +using Fallout.Common; +using Fallout.Common.IO; +using Fallout.Common.Utilities; +using Fallout.Common.Utilities.Collections; + +namespace Fallout.Cli.Commands; + +/// +/// fallout :get-configuration: prints the build configuration parsed from the build script. +/// +public sealed class GetConfigurationCommand : IFalloutCommand +{ + public string Name => "get-configuration"; + + public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript) + { + // Program.GetConfiguration(buildScript, evaluate) is shared with add-package/update/cake; + // it moves into a configuration service in the final #392 collapse PR. + var configuration = Program.GetConfiguration(buildScript.NotNull(), evaluate: false); + + Host.Information($"Configuration from {buildScript}:"); + configuration.ForEach(x => Console.WriteLine($"{x.Key} = {x.Value}")); + + return 0; + } +} diff --git a/src/Fallout.Cli/Program.GetConfiguration.cs b/src/Fallout.Cli/Program.GetConfiguration.cs index 9a9875d9..6cda748b 100644 --- a/src/Fallout.Cli/Program.GetConfiguration.cs +++ b/src/Fallout.Cli/Program.GetConfiguration.cs @@ -19,16 +19,8 @@ partial class Program private const string DOTNET_INSTALL_URL = nameof(DOTNET_INSTALL_URL); private const string DOTNET_CHANNEL = nameof(DOTNET_CHANNEL); - public static int GetConfiguration(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript) - { - var configuration = GetConfiguration(buildScript.NotNull(), evaluate: false); - - Host.Information($"Configuration from {buildScript}:"); - configuration.ForEach(x => Console.WriteLine($"{x.Key} = {x.Value}")); - - return 0; - } - + // Residual after the :get-configuration command moved to GetConfigurationCommand: this helper is + // shared with add-package/update/cake and moves into a configuration service in the #392 collapse PR. internal static Dictionary GetConfiguration(AbsolutePath buildScript, bool evaluate) { string ReplaceScriptDirectory(string value) diff --git a/src/Fallout.Cli/Program.cs b/src/Fallout.Cli/Program.cs index b713e615..43ac5823 100644 --- a/src/Fallout.Cli/Program.cs +++ b/src/Fallout.Cli/Program.cs @@ -55,6 +55,7 @@ private static void RegisterCommands(IServiceCollection services) services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); // Legacy handlers still living on Program, adapted until they are extracted into command // types. Each conversion deletes one line here plus its Program.X.cs partial. @@ -63,7 +64,6 @@ private static void RegisterCommands(IServiceCollection services) services.AddSingleton(new DelegateCommand("add-package", AddPackage)); services.AddSingleton(new DelegateCommand("cake-convert", CakeConvert)); services.AddSingleton(new DelegateCommand("cake-clean", CakeClean)); - services.AddSingleton(new DelegateCommand("get-configuration", GetConfiguration)); services.AddSingleton(new DelegateCommand("secrets", Secrets)); services.AddSingleton(new DelegateCommand("GetNextDirectory", GetNextDirectory)); services.AddSingleton(new DelegateCommand("PopDirectory", PopDirectory)); diff --git a/tests/Fallout.Cli.Tests/Commands/GetConfigurationCommandTests.cs b/tests/Fallout.Cli.Tests/Commands/GetConfigurationCommandTests.cs new file mode 100644 index 00000000..23adf0ba --- /dev/null +++ b/tests/Fallout.Cli.Tests/Commands/GetConfigurationCommandTests.cs @@ -0,0 +1,41 @@ +using System; +using System.IO; +using Fallout.Cli.Commands; +using Fallout.Common.IO; +using FluentAssertions; +using Xunit; + +namespace Fallout.Cli.Tests.Commands; + +public class GetConfigurationCommandTests +{ + [Fact] + public void Name_IsGetConfiguration() + => new GetConfigurationCommand().Name.Should().Be("get-configuration"); + + [Fact] + public void Execute_ParsesConfigurationBlock_ReturnsZero() + { + var dir = (AbsolutePath)Path.Combine(Path.GetTempPath(), "fallout-getcfg-" + Guid.NewGuid().ToString("N")); + dir.CreateDirectory(); + var buildScript = dir / "build.sh"; + try + { + File.WriteAllText(buildScript, string.Join("\n", + "# CONFIGURATION", + "##############", + "", + "BUILD_PROJECT_FILE=\"build/_build.csproj\"", + "TEMP_DIRECTORY=\"$SCRIPT_DIR/.fallout/temp\"", + "", + "# EXECUTION", + "dotnet run")); + + new GetConfigurationCommand().Execute([], dir, buildScript).Should().Be(0); + } + finally + { + Directory.Delete(dir, recursive: true); + } + } +}