Skip to content

Commit 13fe6c3

Browse files
authored
Merge pull request #4375 from devlead/feature/gh-4374
GH4374: Fix Frosting argument parsing
2 parents 795eed0 + 5fae5cf commit 13fe6c3

File tree

5 files changed

+77
-49
lines changed

5 files changed

+77
-49
lines changed

build.cake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ Task("Frosting-Integration-Tests")
342342

343343
DotNetRun(test.Project.FullPath,
344344
new ProcessArgumentBuilder()
345-
.AppendSwitchQuoted("--verbosity", "=", "quiet"),
345+
.AppendSwitchQuoted("--verbosity", "=", "quiet")
346+
.AppendSwitchQuoted("--name", "=", "world"),
346347
new DotNetRunSettings
347348
{
348349
Configuration = parameters.Configuration,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Cake.Core;
5+
using Spectre.Console.Cli;
6+
7+
namespace Cake.Cli.Infrastructure
8+
{
9+
/// <summary>
10+
/// Spectre.Console <see cref="IRemainingArguments"/> extensions.
11+
/// </summary>
12+
public static class IRemainingArgumentsExtensions
13+
{
14+
/// <summary>
15+
/// Parses Spectre.Console <see cref="IRemainingArguments"/> to <see cref="CakeArguments"/>.
16+
/// </summary>
17+
/// <param name="remainingArguments">The remainingArguments.</param>
18+
/// <param name="targets">The optional targets, i.e. if specified by command.</param>
19+
/// <param name="preProcessArgs">The optional pre-process arguments.</param>
20+
/// <returns><see cref="CakeArguments"/>.</returns>
21+
public static CakeArguments ToCakeArguments(
22+
this IRemainingArguments remainingArguments,
23+
string[] targets = null,
24+
Action<IDictionary<string, List<string>>> preProcessArgs = null)
25+
{
26+
var arguments = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
27+
28+
// Keep the actual remaining arguments in the cake arguments
29+
foreach (var group in remainingArguments.Parsed)
30+
{
31+
string key = group.Key.TrimStart('-');
32+
arguments[key] = new List<string>();
33+
foreach (var argument in group)
34+
{
35+
arguments[key].Add(argument);
36+
}
37+
}
38+
39+
// Fixes #3291, We have to add arguments manually which are defined within the DefaultCommandSettings type. Those are not considered "as remaining" because they could be parsed
40+
const string targetArgumentName = "target";
41+
if (!arguments.ContainsKey(targetArgumentName))
42+
{
43+
arguments[targetArgumentName] = new List<string>();
44+
}
45+
46+
if (targets != null)
47+
{
48+
foreach (var target in targets)
49+
{
50+
arguments[targetArgumentName].Add(target);
51+
}
52+
}
53+
54+
preProcessArgs?.Invoke(arguments);
55+
56+
var argumentLookUp = arguments.SelectMany(a => a.Value, Tuple.Create).ToLookup(a => a.Item1.Key, a => a.Item2);
57+
return new CakeArguments(argumentLookUp);
58+
}
59+
}
60+
}

src/Cake.Frosting/Internal/Commands/DefaultCommand.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using Cake.Cli;
9+
using Cake.Cli.Infrastructure;
910
using Cake.Core;
1011
using Cake.Core.Diagnostics;
1112
using Cake.Core.IO;
@@ -81,31 +82,7 @@ public override int Execute(CommandContext context, DefaultCommandSettings setti
8182

8283
private static CakeArguments CreateCakeArguments(IRemainingArguments remainingArguments, DefaultCommandSettings settings)
8384
{
84-
var arguments = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
85-
86-
// Keep the actual remaining arguments in the cake arguments
87-
foreach (var group in remainingArguments.Parsed)
88-
{
89-
arguments[group.Key] = new List<string>();
90-
foreach (var argument in group)
91-
{
92-
arguments[group.Key].Add(argument);
93-
}
94-
}
95-
96-
// Fixes #3291, We have to add arguments manually which are defined within the DefaultCommandSettings type. Those are not considered "as remaining" because they could be parsed
97-
const string targetArgumentName = "target";
98-
if (!arguments.ContainsKey(targetArgumentName))
99-
{
100-
arguments[targetArgumentName] = new List<string>();
101-
}
102-
foreach (var target in settings.Targets)
103-
{
104-
arguments[targetArgumentName].Add(target);
105-
}
106-
107-
var argumentLookUp = arguments.SelectMany(a => a.Value, Tuple.Create).ToLookup(a => a.Item1.Key, a => a.Item2);
108-
return new CakeArguments(argumentLookUp);
85+
return remainingArguments.ToCakeArguments(settings.Targets);
10986
}
11087

11188
private void InstallTools(ServiceProvider provider)

src/Cake/Commands/DefaultCommand.cs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using Cake.Cli;
9+
using Cake.Cli.Infrastructure;
910
using Cake.Core;
1011
using Cake.Core.Diagnostics;
1112
using Cake.Features.Bootstrapping;
@@ -124,29 +125,17 @@ private int PerformBootstrapping(CommandContext context, DefaultCommandSettings
124125

125126
private static CakeArguments CreateCakeArguments(IRemainingArguments remainingArguments, DefaultCommandSettings settings)
126127
{
127-
var arguments = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
128-
129-
// Keep the actual remaining arguments in the cake arguments
130-
foreach (var group in remainingArguments.Parsed)
131-
{
132-
string key = group.Key.TrimStart('-');
133-
arguments[key] = new List<string>();
134-
foreach (var argument in group)
128+
return remainingArguments.ToCakeArguments(
129+
preProcessArgs: arguments =>
135130
{
136-
arguments[key].Add(argument);
137-
}
138-
}
139-
140-
// 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
141-
const string recompileArgumentName = Infrastructure.Constants.Cache.InvalidateScriptCache;
142-
if (settings.Recompile && !arguments.ContainsKey(recompileArgumentName))
143-
{
144-
arguments[recompileArgumentName] = new List<string>();
145-
arguments[recompileArgumentName].Add(true.ToString());
146-
}
147-
148-
var argumentLookUp = arguments.SelectMany(a => a.Value, Tuple.Create).ToLookup(a => a.Item1.Key, a => a.Item2);
149-
return new CakeArguments(argumentLookUp);
131+
// 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
132+
const string recompileArgumentName = Infrastructure.Constants.Cache.InvalidateScriptCache;
133+
if (settings.Recompile && !arguments.ContainsKey(recompileArgumentName))
134+
{
135+
arguments[recompileArgumentName] = new List<string>();
136+
arguments[recompileArgumentName].Add(true.ToString());
137+
}
138+
});
150139
}
151140
}
152141
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Cake.Common;
12
using Cake.Common.Diagnostics;
23
using Cake.Frosting;
34

@@ -6,6 +7,6 @@ public sealed class Hello : FrostingTask<Context>
67
{
78
public override void Run(Context context)
89
{
9-
context.Information("Hello World");
10+
context.Information("Hello {0}", context.Argument<string>("name"));
1011
}
1112
}

0 commit comments

Comments
 (0)