Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Cake.GitVersioning/GitVersioningAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static void GitVersioningCloud(this ICakeContext cakeContext, string proj
settings.CISystem?.ToString(),
settings.AllVariables,
settings.CommonVariables,
settings.CloudBuildNumber,
settings.AdditionalVariables,
false);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Cake.GitVersioning/GitVersioningCloudSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ public class GitVersioningCloudSettings
/// <summary>
/// Gets or sets a value indicating whether to define ALL version variables as cloud build variables, with a "NBGV_" prefix.
/// </summary>
/// <value>The default value is <see langword="false" />.</value>
public bool AllVariables { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to define a few common version variables as cloud build variables, with a "Git" prefix.
/// </summary>
/// <value>The default value is <see langword="false" />.</value>
public bool CommonVariables { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to set the cloud build number.
/// </summary>
/// <value>The default value is <see langword="true" />.</value>
public bool CloudBuildNumber { get; set; } = true;

/// <summary>
/// Gets additional cloud build variables to define.
/// </summary>
Expand Down
10 changes: 8 additions & 2 deletions src/NerdBank.GitVersioning/Commands/CloudCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ public enum CloudCommandError
/// <param name="commonVars">
/// Controls whether to define common version variables as cloud build variables.
/// </param>
/// <param name="cloudBuildNumber">
/// Controls whether to emit the cloud build variable to set the build number.
/// </param>
/// <param name="additionalVariables">
/// Additional cloud build variables to define.
/// </param>
/// <param name="alwaysUseLibGit2">
/// Force usage of LibGit2 for accessing the git repository.
/// </param>
public void SetBuildVariables(string projectDirectory, IEnumerable<string> metadata, string version, string ciSystem, bool allVars, bool commonVars, IEnumerable<KeyValuePair<string, string>> additionalVariables, bool alwaysUseLibGit2)
public void SetBuildVariables(string projectDirectory, IEnumerable<string> metadata, string version, string ciSystem, bool allVars, bool commonVars, bool cloudBuildNumber, IEnumerable<KeyValuePair<string, string>> additionalVariables, bool alwaysUseLibGit2)
{
Requires.NotNull(projectDirectory, nameof(projectDirectory));
Requires.NotNull(additionalVariables, nameof(additionalVariables));
Expand Down Expand Up @@ -137,7 +140,10 @@ public void SetBuildVariables(string projectDirectory, IEnumerable<string> metad
version = oracle.CloudBuildNumber;
}

activeCloudBuild.SetCloudBuildNumber(version, this.stdout, this.stderr);
if (cloudBuildNumber)
{
activeCloudBuild.SetCloudBuildNumber(version, this.stdout, this.stderr);
}

foreach (KeyValuePair<string, string> pair in variables)
{
Expand Down
8 changes: 5 additions & 3 deletions src/nbgv/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private static Parser BuildCommandLine()
var ciSystem = new Option<string>(new[] { "--ci-system", "-s" }, "Force activation for a particular CI system. If not specified, auto-detection will be used. Supported values are: " + string.Join(", ", CloudProviderNames)).FromAmong(CloudProviderNames);
var allVars = new Option<bool>(new[] { "--all-vars", "-a" }, "Defines ALL version variables as cloud build variables, with a \"NBGV_\" prefix.");
var commonVars = new Option<bool>(new[] { "--common-vars", "-c" }, "Defines a few common version variables as cloud build variables, with a \"Git\" prefix (e.g. GitBuildVersion, GitBuildVersionSimple, GitAssemblyInformationalVersion).");
var skipCloudBuildNumber = new Option<bool>(new[] { "--skip-cloud-build-number" }, "Do not emit the cloud build variable to set the build number. This is useful when you want to set other cloud build variables but not the build number.");
var define = new Option<string[]>(new[] { "--define", "-d" }, () => Array.Empty<string>(), "Additional cloud build variables to define. Each should be in the NAME=VALUE syntax.")
{
Arity = ArgumentArity.OneOrMore,
Expand All @@ -207,10 +208,11 @@ private static Parser BuildCommandLine()
ciSystem,
allVars,
commonVars,
skipCloudBuildNumber,
define,
};

cloud.SetHandler(OnCloudCommand, project, metadata, version, ciSystem, allVars, commonVars, define);
cloud.SetHandler(OnCloudCommand, project, metadata, version, ciSystem, allVars, commonVars, skipCloudBuildNumber, define);
}

Command prepareRelease;
Expand Down Expand Up @@ -656,7 +658,7 @@ private static Task<int> OnGetCommitsCommand(string project, bool quiet, string
return Task.FromResult((int)ExitCodes.OK);
}

private static Task<int> OnCloudCommand(string project, string[] metadata, string version, string ciSystem, bool allVars, bool commonVars, string[] define)
private static Task<int> OnCloudCommand(string project, string[] metadata, string version, string ciSystem, bool allVars, bool commonVars, bool skipCloudBuildNumber, string[] define)
{
string searchPath = GetSpecifiedOrCurrentDirectoryPath(project);
if (!Directory.Exists(searchPath))
Expand Down Expand Up @@ -690,7 +692,7 @@ private static Task<int> OnCloudCommand(string project, string[] metadata, strin
try
{
var cloudCommand = new CloudCommand(Console.Out, Console.Error);
cloudCommand.SetBuildVariables(searchPath, metadata, version, ciSystem, allVars, commonVars, additionalVariables, AlwaysUseLibGit2);
cloudCommand.SetBuildVariables(searchPath, metadata, version, ciSystem, allVars, commonVars, !skipCloudBuildNumber, additionalVariables, AlwaysUseLibGit2);
}
catch (CloudCommand.CloudCommandException ex)
{
Expand Down
45 changes: 45 additions & 0 deletions test/Nerdbank.GitVersioning.Tests/CommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Nerdbank.GitVersioning;
using Nerdbank.GitVersioning.Commands;
using Xunit;

public class CommandTests : RepoTestBase
{
public CommandTests(ITestOutputHelper logger)
: base(logger)
{
}

[Theory, CombinatorialData]
public void CloudCommand_CloudBuildNumber(bool setCloudBuildNumber)
{
const string ciSystem = "VisualStudioTeamServices";
const string buildNumberSyntax = "##vso[build.updatebuildnumber]";

var outWriter = new StringWriter();
var errWriter = new StringWriter();

var command = new CloudCommand(outWriter, errWriter);

command.SetBuildVariables(this.RepoPath, metadata: [], version: "1.2.3.4", ciSystem, allVars: false, commonVars: false, setCloudBuildNumber, additionalVariables: [], alwaysUseLibGit2: false);

outWriter.Flush();
errWriter.Flush();

if (setCloudBuildNumber)
{
Assert.Contains(buildNumberSyntax, outWriter.ToString());
}
else
{
Assert.DoesNotContain(buildNumberSyntax, outWriter.ToString());
}

Assert.Empty(errWriter.ToString());
}

protected override GitContext CreateGitContext(string path, string committish = null)
=> GitContext.Create(path, committish, engine: GitContext.Engine.ReadWrite);
}
Loading