Skip to content

Commit

Permalink
Merge pull request #16 from nunit/issue-14
Browse files Browse the repository at this point in the history
Add support for multiple targets on command-line
  • Loading branch information
CharliePoole authored Aug 12, 2024
2 parents 58f2f38 + 317d7de commit 4582664
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
26 changes: 25 additions & 1 deletion recipe/builder.cake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,31 @@

public Builder Build => CommandLineOptions.Usage
? new Builder(() => Information(HelpMessages.Usage))
: new Builder(() => RunTarget(CommandLineOptions.Target.Value));
: new Builder(() => RunTargets(CommandLineOptions.Targets.Values));

CakeReport RunTargets(ICollection<string> targets)
=> RunTarget(GetOrAddTargetsTask(targets).Name);

Task<CakeReport> RunTargetsAsync(ICollection<string> targets)
=> RunTargetAsync(GetOrAddTargetsTask(targets).Name);

private ICakeTaskInfo GetOrAddTargetsTask(ICollection<string> targets)
{
var targetsTaskName = string.Join('+', targets);
var targetsTask = Tasks.FirstOrDefault(task => task.Name == targetsTaskName);

if (targetsTask == null)
{
var task = Task(targetsTaskName);

foreach(var target in targets)
task.IsDependentOn(target);

targetsTask = task.Task;
}

return targetsTask;
}

public class Builder
{
Expand Down
31 changes: 31 additions & 0 deletions recipe/command-line-options.cake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public static class CommandLineOptions
static private ICakeContext _context;

static public ValueOption<string> Target;
static public MultiValueOption<string> Targets;

static public ValueOption<string> Configuration;
static public ValueOption<string> PackageVersion;
static public ValueOption<string> PackageSelector;
Expand All @@ -26,6 +28,9 @@ public static class CommandLineOptions
// The name of the TARGET task to be run, e.g. Test.
Target = new ValueOption<string>("target", "Default", 1);

// Multiple targets to be run
Targets = new MultiValueOption<string>("target", "Default", 1);

Configuration = new ValueOption<String>("configuration", DEFAULT_CONFIGURATION, 1);

PackageVersion = new ValueOption<string>("packageVersion", null, 4);
Expand Down Expand Up @@ -115,4 +120,30 @@ public static class CommandLineOptions
}
}
}

// Generic MultiValueOption adds Values, which returns a collection of values
public class MultiValueOption<T> : ValueOption<T>
{
public MultiValueOption(string name, T defaultValue, int minimumAbbreviation = 0, string description = null)
: base(name, defaultValue, minimumAbbreviation, description) { }

public ICollection<T> Values
{
get
{
var result = new List<T>();

for (int len = Name.Length; len >= MinimumAbbreviation; len--)
{
string abbrev = Name.Substring(0,len);
if (_context.HasArgument(abbrev))
result.AddRange(_context.Arguments<T>(abbrev));
}

if (result.Count == 0) result.Add(DefaultValue);

return result;
}
}
}
}

0 comments on commit 4582664

Please sign in to comment.