|
| 1 | +////////////////////////////////////////////////////////////////////// |
| 2 | +// COMMAND-LINE OPTIONS |
| 3 | +////////////////////////////////////////////////////////////////////// |
| 4 | + |
| 5 | +CommandLineOptions.Initialize(Context); |
| 6 | + |
| 7 | +public static class CommandLineOptions |
| 8 | +{ |
| 9 | + private const string DEFAULT_CONFIGURATION = "Release"; |
| 10 | + |
| 11 | + static private ICakeContext _context; |
| 12 | + |
| 13 | + static public ValueOption<string> Target; |
| 14 | + static public MultiValueOption<string> Targets; |
| 15 | + |
| 16 | + static public ValueOption<string> Configuration; |
| 17 | + static public ValueOption<string> PackageVersion; |
| 18 | + static public ValueOption<string> PackageId; |
| 19 | + static public ValueOption<string> PackageType; |
| 20 | + static public ValueOption<int> TestLevel; |
| 21 | + static public ValueOption<string> TraceLevel; |
| 22 | + static public SimpleOption NoBuild; |
| 23 | + static public SimpleOption NoPush; |
| 24 | + static public SimpleOption Usage; |
| 25 | + |
| 26 | + public static void Initialize(ICakeContext context) |
| 27 | + { |
| 28 | + _context = context; |
| 29 | + |
| 30 | + // The name of the TARGET task to be run, e.g. Test. |
| 31 | + Target = new ValueOption<string>("target|t", "Default"); |
| 32 | + |
| 33 | + // Multiple targets to be run |
| 34 | + Targets = new MultiValueOption<string>("target|t", "Default"); |
| 35 | + |
| 36 | + Configuration = new ValueOption<String>("configuration|c", DEFAULT_CONFIGURATION); |
| 37 | + |
| 38 | + PackageVersion = new ValueOption<string>("packageVersion", null); |
| 39 | + |
| 40 | + PackageId = new ValueOption<string>("packageId|id", null); |
| 41 | + |
| 42 | + PackageType = new ValueOption<string>("packageType|type", null); |
| 43 | + |
| 44 | + TestLevel = new ValueOption<int>("level|lev", 0); |
| 45 | + |
| 46 | + TraceLevel = new ValueOption<string>("trace|tr", "Off"); |
| 47 | + |
| 48 | + NoBuild = new SimpleOption("nobuild|nob"); |
| 49 | + |
| 50 | + NoPush = new SimpleOption("nopush|nop"); |
| 51 | + |
| 52 | + Usage = new SimpleOption("usage"); |
| 53 | + } |
| 54 | + |
| 55 | + // Nested classes to represent individual options |
| 56 | + |
| 57 | + // AbstractOption has a name and can tell us if it exists. |
| 58 | + public abstract class AbstractOption |
| 59 | + { |
| 60 | + public List<string> Aliases { get; } |
| 61 | + |
| 62 | + public bool Exists |
| 63 | + { |
| 64 | + get |
| 65 | + { |
| 66 | + foreach (string alias in Aliases) |
| 67 | + if (_context.HasArgument(alias)) |
| 68 | + return true; |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public string Description { get; } |
| 74 | + |
| 75 | + public AbstractOption(string aliases, string description = null) |
| 76 | + { |
| 77 | + Aliases = new List<string>(aliases.Split('|')); |
| 78 | + Description = description; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Simple Option adds an implicit boolean conversion operator. |
| 83 | + // It throws an exception if you gave it a value on the command-line. |
| 84 | + public class SimpleOption : AbstractOption |
| 85 | + { |
| 86 | + static public implicit operator bool(SimpleOption o) => o.Exists; |
| 87 | + |
| 88 | + public SimpleOption(string aliases, string description = null) |
| 89 | + : base(aliases, description) |
| 90 | + { |
| 91 | + foreach (string alias in Aliases) |
| 92 | + if (_context.Argument(alias, (string)null) != null) |
| 93 | + throw new Exception($"Option --{alias} does not take a value."); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Generic ValueOption adds Value as well as a default value |
| 98 | + public class ValueOption<T> : AbstractOption |
| 99 | + { |
| 100 | + public T DefaultValue { get; } |
| 101 | + |
| 102 | + public ValueOption(string aliases, T defaultValue, string description = null) |
| 103 | + : base(aliases, description) |
| 104 | + { |
| 105 | + DefaultValue = defaultValue; |
| 106 | + } |
| 107 | + |
| 108 | + public T Value |
| 109 | + { |
| 110 | + get |
| 111 | + { |
| 112 | + foreach (string alias in Aliases) |
| 113 | + if (_context.HasArgument(alias)) |
| 114 | + return _context.Argument<T>(alias); |
| 115 | + |
| 116 | + return DefaultValue; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Generic MultiValueOption adds Values, which returns a collection of values |
| 122 | + public class MultiValueOption<T> : ValueOption<T> |
| 123 | + { |
| 124 | + public MultiValueOption(string aliases, T defaultValue, string description = null) |
| 125 | + : base(aliases, defaultValue, description) { } |
| 126 | + |
| 127 | + public ICollection<T> Values |
| 128 | + { |
| 129 | + get |
| 130 | + { |
| 131 | + var result = new List<T>(); |
| 132 | + |
| 133 | + foreach (string alias in Aliases) |
| 134 | + if (_context.HasArgument(alias)) |
| 135 | + result.AddRange(_context.Arguments<T>(alias)); |
| 136 | + |
| 137 | + if (result.Count == 0) result.Add(DefaultValue); |
| 138 | + |
| 139 | + return result; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +static public class HelpMessages |
| 146 | +{ |
| 147 | + static public string Usage => $""" |
| 148 | + BUILD.CAKE |
| 149 | +
|
| 150 | + This script builds the {BuildSettings.Title} project. It makes use of |
| 151 | + NUnit.Cake.Recipe, which provides a number of built-in options and |
| 152 | + tasks. You may define additional options and tasks in build.cake or |
| 153 | + in additional cake files you load from build.cake. |
| 154 | +
|
| 155 | + Usage: build [options] |
| 156 | +
|
| 157 | + Options: |
| 158 | +
|
| 159 | + --target, -t=TARGET |
| 160 | + The TARGET task to be run, e.g. Test. Default is Build. This option |
| 161 | + may be repeated to run multiple targets. For a list of supported |
| 162 | + targets, use the Cake `--description` option. |
| 163 | +
|
| 164 | + --configuration, -c=CONFIG |
| 165 | + The name of the configuration to build. Default is Release. |
| 166 | +
|
| 167 | + --packageVersion=VERSION |
| 168 | + Specifies the full package version, including any pre-release |
| 169 | + suffix. If provided, this version is used directly in place of |
| 170 | + the default version calculated by the script. |
| 171 | +
|
| 172 | + --packageId, --id=ID |
| 173 | + Specifies the id of a package for which packaging is to be performed. |
| 174 | + If not specified, all ids are processed. |
| 175 | +
|
| 176 | + --packageType, --type=TYPE |
| 177 | + Specifies the type package for which packaging is to be performed. |
| 178 | + Valid values for TYPE are 'nuget' and 'choco'. |
| 179 | + If not specified, both types are processed. |
| 180 | +
|
| 181 | + --level, --lev=LEVEL |
| 182 | + Specifies the level of package testing, 1, 2 or 3. Defaults are |
| 183 | + 1 = for normal CI tests run every time you build a package |
| 184 | + 2 = for PRs and Dev builds uploaded to MyGet |
| 185 | + 3 = prior to publishing a release |
| 186 | +
|
| 187 | + --trace, --tr=LEVEL |
| 188 | + Specifies the default trace level for this run. Values are Off, |
| 189 | + Error, Warning, Info or Debug. Default is Off. If used, this option |
| 190 | + affects the trace level for both unit and package tests. |
| 191 | +
|
| 192 | + --nobuild, --nob |
| 193 | + Indicates that the Build task should not be run even if other |
| 194 | + tasks depend on it. The existing build is used instead. |
| 195 | +
|
| 196 | + --nopush, --nop |
| 197 | + Indicates that no publishing or releasing should be done. If |
| 198 | + publish or release targets are run, a message is displayed. |
| 199 | +
|
| 200 | + --usage |
| 201 | +
|
| 202 | + Displays this help message. No targets are run. |
| 203 | +
|
| 204 | + Selected Cake Options: |
| 205 | + |
| 206 | + --version |
| 207 | + Displays the cake version in use. |
| 208 | +
|
| 209 | + --description |
| 210 | + Displays a list of the available tasks (targets). |
| 211 | +
|
| 212 | + --tree |
| 213 | + Displays the task dependency tree |
| 214 | +
|
| 215 | + --help |
| 216 | + Displays help information for cake itself. |
| 217 | +
|
| 218 | + NOTE: The above Cake options bypass execution of the script. |
| 219 | + """; |
| 220 | +} |
0 commit comments