Skip to content

Commit

Permalink
Within Cake.Commands.DefaultCommand.Execute(...), explicitly create t…
Browse files Browse the repository at this point in the history
…he CakeArguments from CommandContext.Remaining, adding in the DefaultCommandSettings.Recompile flag, if necessary. Follows the same pattern already established in Cake.Frosting.Internal.DefaultCommand
  • Loading branch information
FrankRay78 authored and devlead committed Oct 30, 2023
1 parent 9915abf commit 0da7e64
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Cake.Cli/Cake.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<ItemGroup>
<PackageReference Include="Autofac" Version="7.1.0" />
<PackageReference Include="Spectre.Console" Version="0.46.0" />
<PackageReference Include="Spectre.Console.Cli" Version="0.46.0" />
<PackageReference Include="Spectre.Console" Version="0.47.0" />
<PackageReference Include="Spectre.Console.Cli" Version="0.47.0" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Cake.Frosting/Internal/Commands/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public override int Execute(CommandContext context, DefaultCommandSettings setti
private static CakeArguments CreateCakeArguments(IRemainingArguments remainingArguments, DefaultCommandSettings settings)
{
var arguments = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);

// Keep the actual remaining arguments in the cake arguments
foreach (var group in remainingArguments.Parsed)
{
Expand All @@ -98,7 +99,6 @@ private static CakeArguments CreateCakeArguments(IRemainingArguments remainingAr
{
arguments[targetArgumentName] = new List<string>();
}

foreach (var target in settings.Targets)
{
arguments[targetArgumentName].Add(target);
Expand Down
11 changes: 8 additions & 3 deletions src/Cake.Tests/Fixtures/BuildFeatureFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cake.Core;
using Cake.Core.Composition;
Expand Down Expand Up @@ -63,7 +65,10 @@ public BuildFeatureFixture(

public BuildFeatureFixtureResult Run(BuildFeatureSettings settings, IDictionary<string, string> arguments = null)
{
var remaining = new FakeRemainingArguments(arguments);
var parsed = (arguments ?? new Dictionary<string, string>())
.ToLookup(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase);

var remaining = new CakeArguments(parsed);
var exitCode = ((IBuildFeature)this).Run(remaining, settings);

return new BuildFeatureFixtureResult
Expand All @@ -88,7 +93,7 @@ private string GetDefaultScriptContent()
return builder.ToString();
}

int IBuildFeature.Run(IRemainingArguments arguments, BuildFeatureSettings settings)
int IBuildFeature.Run(ICakeArguments arguments, BuildFeatureSettings settings)
{
var feature = new BuildFeature(FileSystem, Environment, Bootstrapper, ModuleSearcher, Log);
return feature.Run(arguments, settings);
Expand Down
8 changes: 4 additions & 4 deletions src/Cake.Tests/Unit/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task Should_Use_Default_Parameters_By_Default()

// Then
feature.Received(1).Run(
Arg.Any<IRemainingArguments>(),
Arg.Any<ICakeArguments>(),
Arg.Is<BuildFeatureSettings>(settings =>
settings.BuildHostKind == BuildHostKind.Build &&
settings.Debug == false &&
Expand All @@ -53,7 +53,7 @@ public async Task The_DryRun_Option_Should_Perform_A_Dry_Run_Of_Script(params st

// Then
feature.Received(1).Run(
Arg.Any<IRemainingArguments>(),
Arg.Any<ICakeArguments>(),
Arg.Is<BuildFeatureSettings>(settings =>
settings.BuildHostKind == BuildHostKind.DryRun));
}
Expand All @@ -73,7 +73,7 @@ public async Task The_Tree_Option_Should_Show_The_Script_Tree(params string[] ar

// Then
feature.Received(1).Run(
Arg.Any<IRemainingArguments>(),
Arg.Any<ICakeArguments>(),
Arg.Is<BuildFeatureSettings>(settings =>
settings.BuildHostKind == BuildHostKind.Tree));
}
Expand All @@ -93,7 +93,7 @@ public async Task The_Description_Option_Should_Show_Script_Descriptions(params

// Then
feature.Received(1).Run(
Arg.Any<IRemainingArguments>(),
Arg.Any<ICakeArguments>(),
Arg.Is<BuildFeatureSettings>(settings =>
settings.BuildHostKind == BuildHostKind.Description));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cake.Tests/Utilities/TestContainerConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using Cake.Core;
using Cake.Core.Composition;
using Cake.Core.Configuration;
using Cake.Infrastructure;
using Spectre.Console.Cli;

namespace Cake.Tests.Fakes
{
Expand All @@ -21,7 +21,7 @@ public TestContainerConfigurator()
public void Configure(
ICakeContainerRegistrar registrar,
ICakeConfiguration configuration,
IRemainingArguments arguments)
ICakeArguments arguments)
{
_decorated.Configure(registrar, configuration, arguments);

Expand Down
35 changes: 33 additions & 2 deletions src/Cake/Commands/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using Cake.Cli;
using Cake.Core;
Expand Down Expand Up @@ -69,8 +70,10 @@ public override int Execute(CommandContext context, DefaultCommandSettings setti
}
}

var arguments = CreateCakeArguments(context.Remaining, settings);

// Run the build feature.
return _builder.Run(context.Remaining, new BuildFeatureSettings(host)
return _builder.Run(arguments, new BuildFeatureSettings(host)
{
Script = settings.Script,
Verbosity = settings.Verbosity,
Expand Down Expand Up @@ -110,11 +113,39 @@ private int PerformBootstrapping(CommandContext context, DefaultCommandSettings
return 0;
}

return _bootstrapper.Run(context.Remaining, new BootstrapFeatureSettings
var arguments = CreateCakeArguments(context.Remaining, settings);

return _bootstrapper.Run(arguments, new BootstrapFeatureSettings
{
Script = settings.Script,
Verbosity = settings.Verbosity
});
}

private static CakeArguments CreateCakeArguments(IRemainingArguments remainingArguments, DefaultCommandSettings settings)
{
var arguments = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);

// Keep the actual remaining arguments in the cake arguments
foreach (var group in remainingArguments.Parsed)
{
arguments[group.Key] = new List<string>();
foreach (var argument in group)
{
arguments[group.Key].Add(argument);
}
}

// Fixes #4157, We have to add arguments manually which are defined within the DefaultCommandSettings type. Those are not considered "as remaining" because they could be parsed
const string recompileArgumentName = Infrastructure.Constants.Cache.InvalidateScriptCache;
if (settings.Recompile && !arguments.ContainsKey(recompileArgumentName))
{
arguments[recompileArgumentName] = new List<string>();
arguments[recompileArgumentName].Add(true.ToString());
}

var argumentLookUp = arguments.SelectMany(a => a.Value, Tuple.Create).ToLookup(a => a.Item1.Key, a => a.Item2);
return new CakeArguments(argumentLookUp);
}
}
}
4 changes: 2 additions & 2 deletions src/Cake/Features/Bootstrapping/BootstrapFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Cake.Features.Bootstrapping
{
public interface IBootstrapFeature
{
int Run(IRemainingArguments arguments, BootstrapFeatureSettings settings);
int Run(ICakeArguments arguments, BootstrapFeatureSettings settings);
}

public sealed class BootstrapFeature : Feature, IBootstrapFeature
Expand All @@ -32,7 +32,7 @@ public BootstrapFeature(
_environment = environment;
}

public int Run(IRemainingArguments arguments, BootstrapFeatureSettings settings)
public int Run(ICakeArguments arguments, BootstrapFeatureSettings settings)
{
// Fix the script path.
settings.Script = settings.Script ?? new FilePath("build.cake");
Expand Down
6 changes: 3 additions & 3 deletions src/Cake/Features/Building/BuildFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Cake.Features.Building
{
public interface IBuildFeature
{
int Run(IRemainingArguments arguments, BuildFeatureSettings settings);
int Run(ICakeArguments arguments, BuildFeatureSettings settings);
}

public sealed class BuildFeature : Feature, IBuildFeature
Expand All @@ -42,15 +42,15 @@ public BuildFeature(
_log = log;
}

public int Run(IRemainingArguments arguments, BuildFeatureSettings settings)
public int Run(ICakeArguments arguments, BuildFeatureSettings settings)
{
using (new ScriptAssemblyResolver(_environment, _log))
{
return RunCore(arguments, settings);
}
}

private int RunCore(IRemainingArguments arguments, BuildFeatureSettings settings)
private int RunCore(ICakeArguments arguments, BuildFeatureSettings settings)
{
// Fix the script path.
settings.Script = settings.Script ?? new FilePath("build.cake");
Expand Down
6 changes: 3 additions & 3 deletions src/Cake/Features/CakeFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Feature(

protected IContainer CreateScope(
ICakeConfiguration configuration,
IRemainingArguments arguments,
ICakeArguments arguments,
Action<ICakeContainerRegistrar> action = null)
{
var registrar = new AutofacTypeRegistrar(new ContainerBuilder());
Expand All @@ -45,10 +45,10 @@ protected IContainer CreateScope(
}

protected ICakeConfiguration ReadConfiguration(
IRemainingArguments remaining, DirectoryPath root)
ICakeArguments arguments, DirectoryPath root)
{
var provider = new CakeConfigurationProvider(_fileSystem, _environment);
var args = remaining.Parsed.ToDictionary(x => x.Key, x => x.FirstOrDefault() ?? string.Empty);
var args = arguments.GetArguments().ToDictionary(x => x.Key, x => x.Value?.FirstOrDefault() ?? string.Empty);

return provider.CreateConfiguration(root, args);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cake/Infrastructure/ContainerConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public sealed class ContainerConfigurator : IContainerConfigurator
public void Configure(
ICakeContainerRegistrar registrar,
ICakeConfiguration configuration,
IRemainingArguments arguments)
ICakeArguments arguments)
{
// Arguments
registrar.RegisterInstance(new CakeArguments(arguments.Parsed)).AsSelf().As<ICakeArguments>();
registrar.RegisterInstance(arguments).AsSelf().As<ICakeArguments>();

// Scripting
registrar.RegisterType<RoslynScriptEngine>().As<IScriptEngine>().Singleton();
Expand Down
4 changes: 2 additions & 2 deletions src/Cake/Infrastructure/IContainerConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Core;
using Cake.Core.Composition;
using Cake.Core.Configuration;
using Spectre.Console.Cli;

namespace Cake.Infrastructure
{
Expand All @@ -17,6 +17,6 @@ public interface IContainerConfigurator
void Configure(
ICakeContainerRegistrar registrar,
ICakeConfiguration configuration,
IRemainingArguments arguments);
ICakeArguments arguments);
}
}

0 comments on commit 0da7e64

Please sign in to comment.