Skip to content

Commit 50f879b

Browse files
committed
Add option to set common and all cloud build vars
1 parent cb6bd37 commit 50f879b

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

src/nbgv/Program.cs

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public static int Main(string[] args)
5252
var format = string.Empty;
5353
bool quiet = false;
5454
var cisystem = string.Empty;
55+
bool cloudBuildCommonVars = false;
56+
bool cloudBuildAllVars = false;
5557

5658
ArgumentCommand<string> install = null;
5759
ArgumentCommand<string> getVersion = null;
@@ -88,6 +90,8 @@ public static int Main(string[] args)
8890
syntax.DefineOption("p|project", ref projectPath, "The path to the project or project directory used to calculate the version. The default is the current directory. Ignored if the -v option is specified.");
8991
syntax.DefineOption("v|version", ref version, "The string to use for the cloud build number. If not specified, the computed version will be used.");
9092
syntax.DefineOption("c|ci-system", ref cisystem, "Force activation for a particular CI system. If not specified, auto-detection will be used. Supported values are: " + string.Join(", ", CloudProviderNames));
93+
syntax.DefineOption("a|all-vars", ref cloudBuildAllVars, false, "Defines ALL version variables as cloud build variables, with a \"NBGV_\" prefix.");
94+
syntax.DefineOption("g|common-vars", ref cloudBuildCommonVars, false, "Defines a few common version variables as cloud build variables, with a \"Git\" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion).");
9195
syntax.DefineOptionList("d|define", ref cloudVariables, "Additional cloud build variables to define. Each should be in the NAME=VALUE syntax.");
9296
});
9397

@@ -113,7 +117,7 @@ public static int Main(string[] args)
113117
}
114118
else if (cloud.IsActive)
115119
{
116-
exitCode = OnCloudCommand(projectPath, version, cisystem, cloudVariables);
120+
exitCode = OnCloudCommand(projectPath, version, cisystem, cloudBuildAllVars, cloudBuildCommonVars, cloudVariables);
117121
}
118122

119123
return (int)exitCode;
@@ -422,25 +426,13 @@ private static ExitCodes OnGetCommitsCommand(string projectPath, string version,
422426
return ExitCodes.OK;
423427
}
424428

425-
private static ExitCodes OnCloudCommand(string projectPath, string version, string cisystem, IReadOnlyList<string> cloudVariables)
429+
private static ExitCodes OnCloudCommand(string projectPath, string version, string cisystem, bool cloudBuildAllVars, bool cloudBuildCommonVars, IReadOnlyList<string> cloudVariables)
426430
{
427-
var variables = new Dictionary<string, string>();
428-
foreach (string def in cloudVariables)
431+
string searchPath = GetSpecifiedOrCurrentDirectoryPath(projectPath);
432+
if (!Directory.Exists(searchPath))
429433
{
430-
string[] split = def.Split(new char[] { '=' }, 2);
431-
if (split.Length < 2)
432-
{
433-
Console.Error.WriteLine($"\"{def}\" is not in the NAME=VALUE syntax required for cloud variables.");
434-
return ExitCodes.BadCloudVariable;
435-
}
436-
437-
if (variables.ContainsKey(split[0]))
438-
{
439-
Console.Error.WriteLine($"Cloud build variable \"{split[0]}\" specified more than once.");
440-
return ExitCodes.DuplicateCloudVariable;
441-
}
442-
443-
variables.Add(split[0], split[1]);
434+
Console.Error.WriteLine("\"{0}\" is not an existing directory.", searchPath);
435+
return ExitCodes.NoGitRepo;
444436
}
445437

446438
ICloudBuild activeCloudBuild = CloudBuild.Active;
@@ -456,22 +448,51 @@ private static ExitCodes OnCloudCommand(string projectPath, string version, stri
456448
activeCloudBuild = CloudBuild.SupportedCloudBuilds[matchingIndex];
457449
}
458450

459-
string searchPath = GetSpecifiedOrCurrentDirectoryPath(projectPath);
460-
if (!Directory.Exists(searchPath))
451+
var oracle = VersionOracle.Create(searchPath, cloudBuild: activeCloudBuild);
452+
var variables = new Dictionary<string, string>();
453+
if (cloudBuildAllVars)
461454
{
462-
Console.Error.WriteLine("\"{0}\" is not an existing directory.", searchPath);
463-
return ExitCodes.NoGitRepo;
455+
foreach (var pair in oracle.CloudBuildAllVars)
456+
{
457+
variables.Add(pair.Key, pair.Value);
458+
}
459+
}
460+
461+
if (cloudBuildCommonVars)
462+
{
463+
foreach (var pair in oracle.CloudBuildVersionVars)
464+
{
465+
variables.Add(pair.Key, pair.Value);
466+
}
467+
}
468+
469+
foreach (string def in cloudVariables)
470+
{
471+
string[] split = def.Split(new char[] { '=' }, 2);
472+
if (split.Length < 2)
473+
{
474+
Console.Error.WriteLine($"\"{def}\" is not in the NAME=VALUE syntax required for cloud variables.");
475+
return ExitCodes.BadCloudVariable;
476+
}
477+
478+
if (variables.ContainsKey(split[0]))
479+
{
480+
Console.Error.WriteLine($"Cloud build variable \"{split[0]}\" specified more than once.");
481+
return ExitCodes.DuplicateCloudVariable;
482+
}
483+
484+
variables[split[0]] = split[1];
464485
}
465486

466487
if (activeCloudBuild != null)
467488
{
468489
if (string.IsNullOrEmpty(version))
469490
{
470-
var oracle = VersionOracle.Create(searchPath, cloudBuild: activeCloudBuild);
471491
version = oracle.CloudBuildNumber;
472492
}
473493

474494
activeCloudBuild.SetCloudBuildNumber(version, Console.Out, Console.Error);
495+
475496
foreach (var pair in variables)
476497
{
477498
activeCloudBuild.SetCloudBuildVariable(pair.Key, pair.Value, Console.Out, Console.Error);

0 commit comments

Comments
 (0)