Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 10 additions & 2 deletions src/Cake.GitVersioning/GitVersioningCloudSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,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.
/// Default value: <see langword="false" />.
/// </summary>
public bool AllVariables { get; set; }
public bool AllVariables { get; set; } = false;

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

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

/// <summary>
/// Gets additional cloud build variables to define.
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", "-b" }, "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
43 changes: 43 additions & 0 deletions test/Nerdbank.GitVersioning.Tests/CommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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)
{
}

// TODO: This is tightly coupled to the cloud build service. Can I use a FakeCloudBuildService instead?
[Theory, CombinatorialData]
public void CloudCommand_CloudBuildNumber([CombinatorialValues("VisualStudioTeamServices", "TeamCity", "Jenkins")] string ciSystem, bool setCloudBuildNumber)
{
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.NotEmpty(outWriter.ToString());
}
else
{
Assert.Empty(outWriter.ToString());
}

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

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