Skip to content

Commit 8147203

Browse files
authored
Merge pull request #77 from nunit/issue-71
Replace --where option; add target aliases
2 parents 6392343 + 76a706b commit 8147203

File tree

5 files changed

+82
-84
lines changed

5 files changed

+82
-84
lines changed

recipe/build-settings.cake

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,7 @@ public static class BuildSettings
372372
Console.WriteLine(" ExtensionInstallDirectory: " + package.ExtensionInstallDirectory);
373373
}
374374
var selected = SelectedPackages.Select(p => p.PackageId);
375-
if (CommandLineOptions.PackageSelector.Exists)
376-
Console.WriteLine(" SelectedPackages: " + string.Join(", ", selected.ToArray()));
377-
else
378-
Console.WriteLine(" SelectedPackages: NO SELECTOR SPECIFIED");
375+
Console.WriteLine(" SelectedPackages: " + string.Join(", ", selected.ToArray()));
379376

380377
Console.WriteLine("\nPUBLISHING");
381378
Console.WriteLine("ShouldPublishToMyGet: " + ShouldPublishToMyGet);

recipe/command-line-options.cake

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public static class CommandLineOptions
1414

1515
static public ValueOption<string> Configuration;
1616
static public ValueOption<string> PackageVersion;
17-
static public ValueOption<string> PackageSelector;
17+
static public ValueOption<string> PackageId;
18+
static public ValueOption<string> PackageType;
1819
static public ValueOption<int> TestLevel;
1920
static public ValueOption<string> TraceLevel;
2021
static public SimpleOption NoBuild;
@@ -26,57 +27,54 @@ public static class CommandLineOptions
2627
_context = context;
2728

2829
// The name of the TARGET task to be run, e.g. Test.
29-
Target = new ValueOption<string>("target", "Default", 1);
30+
Target = new ValueOption<string>("target|t", "Default");
3031

3132
// Multiple targets to be run
32-
Targets = new MultiValueOption<string>("target", "Default", 1);
33+
Targets = new MultiValueOption<string>("target|t", "Default");
3334

34-
Configuration = new ValueOption<String>("configuration", DEFAULT_CONFIGURATION, 1);
35+
Configuration = new ValueOption<String>("configuration|c", DEFAULT_CONFIGURATION);
3536

36-
PackageVersion = new ValueOption<string>("packageVersion", null, 4);
37+
PackageVersion = new ValueOption<string>("packageVersion", null);
3738

38-
PackageSelector = new ValueOption<string>("where", null, 1);
39+
PackageId = new ValueOption<string>("packageId|id", null);
3940

40-
TestLevel = new ValueOption<int>("level", 0, 3);
41+
PackageType = new ValueOption<string>("packageType|type", null);
4142

42-
TraceLevel = new ValueOption<string>("trace", "Off", 2);
43+
TestLevel = new ValueOption<int>("level|lev", 0);
4344

44-
NoBuild = new SimpleOption("nobuild", 3);
45+
TraceLevel = new ValueOption<string>("trace|tr", "Off");
4546

46-
NoPush = new SimpleOption("nopush", 3);
47+
NoBuild = new SimpleOption("nobuild|nob");
4748

48-
Usage = new SimpleOption("usage", 2);
49+
NoPush = new SimpleOption("nopush|nop");
50+
51+
Usage = new SimpleOption("usage");
4952
}
5053

5154
// Nested classes to represent individual options
5255

5356
// AbstractOption has a name and can tell us if it exists.
5457
public abstract class AbstractOption
5558
{
56-
public string Name { get; }
57-
58-
public int MinimumAbbreviation { get; internal set; }
59+
public List<string> Aliases { get; }
5960

6061
public bool Exists
6162
{
6263
get
6364
{
64-
for (int len = Name.Length; len >= MinimumAbbreviation; len--)
65-
if (_context.HasArgument(Name.Substring(0,len)))
66-
return true;
67-
return false;
65+
foreach (string alias in Aliases)
66+
if (_context.HasArgument(alias))
67+
return true;
68+
return false;
6869
}
6970
}
7071

7172
public string Description { get; }
7273

73-
public AbstractOption(string name, int minimumAbbreviation = 0, string description = null)
74+
public AbstractOption(string aliases, string description = null)
7475
{
75-
Name = name;
76-
MinimumAbbreviation = minimumAbbreviation > 0 && minimumAbbreviation <= name.Length
77-
? minimumAbbreviation
78-
: name.Length;
79-
Description = description;
76+
Aliases = new List<string>(aliases.Split('|'));
77+
Description = description;
8078
}
8179
}
8280

@@ -86,21 +84,22 @@ public static class CommandLineOptions
8684
{
8785
static public implicit operator bool(SimpleOption o) => o.Exists;
8886

89-
public SimpleOption(string name, int minimumAbbreviation = 0, string description = null)
90-
: base(name, minimumAbbreviation, description)
87+
public SimpleOption(string aliases, string description = null)
88+
: base(aliases, description)
9189
{
92-
if (_context.Argument(name, (string)null) != null)
93-
throw new Exception($"Option --{name} does not take a value.");
94-
}
90+
foreach (string alias in Aliases)
91+
if (_context.Argument(alias, (string)null) != null)
92+
throw new Exception($"Option --{alias} does not take a value.");
93+
}
9594
}
9695

9796
// Generic ValueOption adds Value as well as a default value
9897
public class ValueOption<T> : AbstractOption
9998
{
10099
public T DefaultValue { get; }
101100

102-
public ValueOption(string name, T defaultValue, int minimumAbbreviation = 0, string description = null)
103-
: base(name, minimumAbbreviation, description)
101+
public ValueOption(string aliases, T defaultValue, string description = null)
102+
: base(aliases, description)
104103
{
105104
DefaultValue = defaultValue;
106105
}
@@ -109,37 +108,31 @@ public static class CommandLineOptions
109108
{
110109
get
111110
{
112-
for (int len = Name.Length; len >= MinimumAbbreviation; len--)
113-
{
114-
string abbrev = Name.Substring(0,len);
115-
if (_context.HasArgument(abbrev))
116-
return _context.Argument<T>(abbrev);
117-
}
118-
119-
return DefaultValue;
111+
foreach (string alias in Aliases)
112+
if (_context.HasArgument(alias))
113+
return _context.Argument<T>(alias);
114+
115+
return DefaultValue;
120116
}
121117
}
122118
}
123119

124120
// Generic MultiValueOption adds Values, which returns a collection of values
125121
public class MultiValueOption<T> : ValueOption<T>
126122
{
127-
public MultiValueOption(string name, T defaultValue, int minimumAbbreviation = 0, string description = null)
128-
: base(name, defaultValue, minimumAbbreviation, description) { }
123+
public MultiValueOption(string aliases, T defaultValue, string description = null)
124+
: base(aliases, defaultValue, description) { }
129125

130126
public ICollection<T> Values
131127
{
132128
get
133129
{
134130
var result = new List<T>();
135131

136-
for (int len = Name.Length; len >= MinimumAbbreviation; len--)
137-
{
138-
string abbrev = Name.Substring(0,len);
139-
if (_context.HasArgument(abbrev))
140-
result.AddRange(_context.Arguments<T>(abbrev));
141-
}
142-
132+
foreach (string alias in Aliases)
133+
if (_context.HasArgument(alias))
134+
result.AddRange(_context.Arguments<T>(alias));
135+
143136
if (result.Count == 0) result.Add(DefaultValue);
144137

145138
return result;

recipe/help-messages.cake

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,54 @@
66
This script builds the {BuildSettings.Title} project. It makes use of
77
NUnit.Cake.Recipe, which provides a number of built-in options and
88
tasks. You may define additional options and tasks in build.cake or
9-
in additional cake files you load from build.cake. Recipe options may
10-
be specified in abbreviated form, down to the minimum length shown in
11-
square braces. Single character abbreviations use a single dash.
9+
in additional cake files you load from build.cake.
1210
1311
Usage: build [options]
1412
1513
Options:
1614
17-
--target=TARGET [-t]
18-
The TARGET task to be run, e.g. Test. Default is Build. For a list
19-
of supported targets, use the Cake `--description` option.
15+
--target, -t=TARGET
16+
The TARGET task to be run, e.g. Test. Default is Build. This option
17+
may be repeated to run multiple targets. For a list of supported
18+
targets, use the Cake `--description` option.
2019
21-
--configuration=CONFIG [-c]
20+
--configuration, -t=CONFIG
2221
The name of the configuration to build. Default is Release.
2322
24-
--packageVersion=VERSION [--pack]
23+
--packageVersion=VERSION
2524
Specifies the full package version, including any pre-release
2625
suffix. If provided, this version is used directly in place of
2726
the default version calculated by the script.
2827
29-
--where=SELECTION [-w]
30-
Specifies a selction expression used to choose which packages
31-
to build and test, for use in debugging. Consists of one or
32-
more specifications, separated by '|' and '&'. Each specification
33-
is of the form "prop=value", where prop may be either id or type.
34-
Examples:
35-
--where type=nuget
36-
--where id=NUnit.Engine.Api
37-
--where "type=nuget|type=choco"
38-
39-
--level=LEVEL [--lev]
28+
--packageId, --id=ID
29+
Specifies the id of a package for which packaging is to be performed.
30+
If not specified, all ids are processed.
31+
32+
--packageType, --type=TYPE
33+
Specifies the type package for which packaging is to be performed.
34+
Valid values for TYPE are 'nuget', 'choco' and 'zip'.
35+
If not specified, all types are processed.
36+
37+
--level, --lev=LEVEL
4038
Specifies the level of package testing, 1, 2 or 3. Defaults are
4139
1 = for normal CI tests run every time you build a package
4240
2 = for PRs and Dev builds uploaded to MyGet
4341
3 = prior to publishing a release
4442
45-
--trace=LEVEL [--tr]
43+
--trace, --tr=LEVEL
4644
Specifies the default trace level for this run. Values are Off,
4745
Error, Warning, Info or Debug. Default is Off.
4846
49-
--nobuild [--nob]
47+
--nobuild, --nob
5048
Indicates that the Build task should not be run even if other
5149
tasks depend on it. The existing build is used instead.
5250
53-
--nopush [--nop]
51+
--nopush, --nop
5452
Indicates that no publishing or releasing should be done. If
5553
publish or release targets are run, a message is displayed.
5654
57-
--usage [--us]
55+
--usage
56+
5857
Displays this help message. No targets are run.
5958
6059
Selected Cake Options:

recipe/setup.cake

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,26 @@ Setup((context) =>
3838
}
3939

4040
// SelectedPackages
41-
bool hasSelector = CommandLineOptions.PackageSelector.Exists;
42-
string selector = CommandLineOptions.PackageSelector.Value;
41+
string id = CommandLineOptions.PackageId.Value;
42+
string type = CommandLineOptions.PackageType.Value?.ToLower();
4343

4444
foreach (var package in BuildSettings.Packages)
45-
if (!hasSelector || package.IsSelectedBy(selector))
45+
if ((id == null || id == package.PackageId.ToString()) && (type == null || type == package.PackageType.ToString().ToLower()))
4646
BuildSettings.SelectedPackages.Add(package);
4747

48-
if (hasSelector && BuildSettings.SelectedPackages.Count == 0)
49-
DisplayErrorAndThrow("Specified --where option does not match any packages");
48+
if (BuildSettings.SelectedPackages.Count == 0)
49+
{
50+
if (id == null && type == null)
51+
DisplayErrorAndThrow("No packages have been defined");
52+
53+
string msg = "No package found ";
54+
if (type != null)
55+
msg += $" of type {type}";
56+
if (id != null)
57+
msg += $" with id ${id}";
58+
59+
DisplayErrorAndThrow(msg);
60+
}
5061

5162
// Add settings to BuildSettings
5263
BuildSettings.Target = target;

recipe/task-definitions.cake

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,8 @@ BuildTasks.PackageTask = Task("Package")
8787
.IsDependentOn("Build")
8888
.Description("Build, Install, Verify and Test all packages")
8989
.Does(() => {
90-
var selector = CommandLineOptions.PackageSelector;
91-
foreach(var package in BuildSettings.Packages)
92-
if (!selector.Exists || package.IsSelectedBy(selector.Value))
93-
package.BuildVerifyAndTest();
90+
foreach(var package in BuildSettings.SelectedPackages)
91+
package.BuildVerifyAndTest();
9492
});
9593

9694
BuildTasks.BuildTestAndPackageTask = Task("BuildTestAndPackage")

0 commit comments

Comments
 (0)