Skip to content

Commit 66ae53b

Browse files
committed
Add "GitVersioningCloud" alias to Cake.GitVersioning
1 parent 09a13ca commit 66ae53b

File tree

5 files changed

+173
-10
lines changed

5 files changed

+173
-10
lines changed

src/Cake.GitVersioning/GitVersioningAliases.cs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
using System.IO;
2-
using System.Reflection;
3-
using Cake.Core;
4-
using Cake.Core.Annotations;
5-
using Nerdbank.GitVersioning;
6-
7-
namespace Cake.GitVersioning
1+
namespace Cake.GitVersioning
82
{
93
using System;
10-
using System.Linq;
11-
12-
using Validation;
4+
using System.IO;
5+
using System.Reflection;
6+
using Cake.Core;
7+
using Cake.Core.Annotations;
8+
using Nerdbank.GitVersioning;
139

1410
/// <summary>
1511
/// Contains functionality for using Nerdbank.GitVersioning.
@@ -45,5 +41,46 @@ public static VersionOracle GitVersioningGetVersion(this ICakeContext cakeContex
4541
var gitContext = GitContext.Create(fullProjectDirectory);
4642
return new VersionOracle(gitContext, cloudBuild: CloudBuild.Active);
4743
}
44+
45+
/// <summary>
46+
/// Adds versioning information to the current build environment's variables.
47+
/// </summary>
48+
/// <example>
49+
/// Task("SetBuildVersion")
50+
/// .Does(() =>
51+
/// {
52+
/// GitVersioningCloud()
53+
/// });
54+
/// </example>
55+
/// <param name="cakeContext">The context.</param>
56+
/// <param name="projectDirectory">Directory to start the search for version.json.</param>
57+
/// <param name="settings">The settings to use for updating variables.</param>
58+
[CakeMethodAlias]
59+
public static void GitVersioningCloud(this ICakeContext cakeContext, string projectDirectory = ".", GitVersioningCloudSettings settings = null)
60+
{
61+
var fullProjectDirectory = (new DirectoryInfo(projectDirectory)).FullName;
62+
63+
string directoryName = Path.GetDirectoryName(Assembly.GetAssembly(typeof(GitVersioningAliases)).Location);
64+
65+
if (string.IsNullOrWhiteSpace(directoryName))
66+
{
67+
throw new InvalidOperationException("Could not locate the Cake.GitVersioning library");
68+
}
69+
70+
settings ??= new GitVersioningCloudSettings();
71+
72+
var cloudCommand = new CloudCommand(Console.Out, Console.Error);
73+
cloudCommand.SetBuildVariables(
74+
fullProjectDirectory,
75+
settings.Metadata,
76+
settings.Version,
77+
settings.CISystem?.ToString(),
78+
settings.AllVariables,
79+
settings.CommonVariables,
80+
settings.AdditionalVariables,
81+
false
82+
);
83+
}
84+
4885
}
4986
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace Cake.GitVersioning
2+
{
3+
/// <summary>
4+
/// Defines the supported cloud build providers for the <see cref="GitVersioningAliases.GitVersioningCloud" /> alias.
5+
/// </summary>
6+
public enum GitVersioningCloudProvider
7+
{
8+
/// <summary>
9+
/// Use AppVeyor cloud build provider.
10+
/// </summary>
11+
AppVeyor,
12+
13+
/// <summary>
14+
/// Use Azure Pipeline / Visual Studio Team Services / TFS cloud build provider.
15+
/// </summary>
16+
VisualStudioTeamServices,
17+
18+
/// <summary>
19+
/// Use GitHub Actions cloud build provider.
20+
/// </summary>
21+
GitHubActions,
22+
23+
/// <summary>
24+
/// Use the TeamCity cloud build provider.
25+
/// </summary>
26+
TeamCity,
27+
28+
/// <summary>
29+
/// Use the Atlassian Bamboo cloud build provider.
30+
/// </summary>
31+
AtlassianBamboo,
32+
33+
/// <summary>
34+
/// Use the Jenkins cloud build provider.
35+
/// </summary>
36+
Jenkins,
37+
38+
/// <summary>
39+
/// Use the GitLab CI cloud build provider.
40+
/// </summary>
41+
GitLab,
42+
43+
/// <summary>
44+
/// Use the Travis CI cloud build provider.
45+
/// </summary>
46+
Travis,
47+
48+
/// <summary>
49+
/// Use the Jetbrains Space cloud build provider.
50+
/// </summary>
51+
SpaceAutomation,
52+
}
53+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Cake.GitVersioning
2+
{
3+
using System.Collections.Generic;
4+
5+
/// <summary>
6+
/// Defines settings for the <see cref="GitVersioningAliases.GitVersioningCloud"/> alias.
7+
/// </summary>
8+
public class GitVersioningCloudSettings
9+
{
10+
/// <summary>
11+
/// The string to use for the cloud build number.
12+
/// If not value os specified, the computed version will be used.
13+
/// </summary>
14+
public string Version { get; set; } = null;
15+
16+
/// <summary>
17+
/// Adds an identifier to the build metadata part of a semantic version.
18+
/// </summary>
19+
public IList<string> Metadata { get; set; } = new List<string>();
20+
21+
/// <summary>
22+
/// Force activation for a particular CI system. If not specified,
23+
/// auto-detection will be used.
24+
/// </summary>
25+
public GitVersioningCloudProvider? CISystem { get; set; } = null;
26+
27+
/// <summary>
28+
/// Defines ALL version variables as cloud build variables, with a "NBGV_" prefix.
29+
/// </summary>
30+
public bool AllVariables { get; set; } = false;
31+
32+
/// <summary>
33+
/// Defines a few common version variables as cloud build variables, with a "Git" prefix.
34+
/// </summary>
35+
public bool CommonVariables { get; set; } = false;
36+
37+
/// <summary>
38+
/// Additional cloud build variables to define.
39+
/// </summary>
40+
public IDictionary<string, string> AdditionalVariables { get; set; } = new Dictionary<string, string>();
41+
}
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Linq;
3+
using Cake.GitVersioning;
4+
using Nerdbank.GitVersioning;
5+
using Xunit;
6+
7+
/// <summary>
8+
/// Tests to verify the <see cref="GitVersioningCloudProvider"/> enum (part of the Cake integration) is up-to-date
9+
/// </summary>
10+
public class GitVersioningCloudProviderTests
11+
{
12+
[Fact]
13+
public void HasExpectedValues()
14+
{
15+
var expectedValues = CloudBuild.SupportedCloudBuilds.Select(cb => cb.GetType().Name);
16+
var actualValues = Enum.GetNames(typeof(GitVersioningCloudProvider));
17+
18+
var missingValues = expectedValues.Except(actualValues);
19+
Assert.True(
20+
!missingValues.Any(),
21+
$"Enumeration is missing the following values of supported cloud build providers: {string.Join(", ", missingValues)}"
22+
);
23+
24+
var redundantValues = actualValues.Except(expectedValues);
25+
Assert.True(
26+
!redundantValues.Any(),
27+
$"Enumeration contains values which were not found among supported cloud build providers: {string.Join(",", redundantValues)}"
28+
);
29+
}
30+
}

src/NerdBank.GitVersioning.Tests/NerdBank.GitVersioning.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<EmbeddedResource Include="repos\*" />
2929
</ItemGroup>
3030
<ItemGroup>
31+
<ProjectReference Include="..\Cake.GitVersioning\Cake.GitVersioning.csproj" />
3132
<ProjectReference Include="..\Nerdbank.GitVersioning.Tasks\Nerdbank.GitVersioning.Tasks.csproj" />
3233
<ProjectReference Include="..\NerdBank.GitVersioning\NerdBank.GitVersioning.csproj" />
3334
</ItemGroup>

0 commit comments

Comments
 (0)