Skip to content

Commit 2e4c779

Browse files
author
Ari Hannula
committed
Outputs in addition to gitversion.properties for Bash, gitversion.ps1 for Powershell.
1 parent c704b65 commit 2e4c779

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,21 @@ public void WriteAllVariablesToTheTextWriter()
118118
{
119119
var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
120120
assemblyLocation.ShouldNotBeNull();
121-
var f = PathHelper.Combine(assemblyLocation, "gitversion.properties");
121+
var propertyFile = PathHelper.Combine(assemblyLocation, "gitversion.properties");
122+
var ps1File = PathHelper.Combine(assemblyLocation, "gitversion.ps1");
122123

123124
try
124125
{
125-
AssertVariablesAreWrittenToFile(f);
126+
AssertVariablesAreWrittenToFile(propertyFile, ps1File);
126127
}
127128
finally
128129
{
129-
File.Delete(f);
130+
File.Delete(propertyFile);
131+
File.Delete(ps1File);
130132
}
131133
}
132134

133-
private void AssertVariablesAreWrittenToFile(string file)
135+
private void AssertVariablesAreWrittenToFile(string propertyFile, string ps1File)
134136
{
135137
var writes = new List<string?>();
136138
var semanticVersion = new SemanticVersion
@@ -147,19 +149,29 @@ private void AssertVariablesAreWrittenToFile(string file)
147149

148150
var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, null);
149151

150-
this.buildServer.WithPropertyFile(file);
152+
this.buildServer.WithPropertyFile(propertyFile);
153+
this.buildServer.WithPowershellFile(ps1File);
151154

152155
this.buildServer.WriteIntegration(writes.Add, variables);
153156

154157
writes[1].ShouldBe("1.2.3-beta.1+5");
155158

156-
File.Exists(file).ShouldBe(true);
159+
File.Exists(propertyFile).ShouldBe(true);
157160

158-
var props = File.ReadAllText(file);
161+
var props = File.ReadAllText(propertyFile);
159162

160163
props.ShouldContain("export GITVERSION_MAJOR=1");
161164
props.ShouldContain("export GITVERSION_MINOR=2");
162165
props.ShouldContain("export GITVERSION_SHA=f28807e615e9f06aec8a33c87780374e0c1f6fb8");
163166
props.ShouldContain("export GITVERSION_COMMITDATE=2022-04-06");
167+
168+
File.Exists(ps1File).ShouldBe(true);
169+
170+
var psProps = File.ReadAllText(ps1File);
171+
172+
psProps.ShouldContain("$GITVERSION_MAJOR = \"1\"");
173+
psProps.ShouldContain("$GITVERSION_MINOR = \"2\"");
174+
psProps.ShouldContain("$GITVERSION_SHA = \"f28807e615e9f06aec8a33c87780374e0c1f6fb8\"");
175+
psProps.ShouldContain("$GITVERSION_COMMITDATE = \"2022-04-06\"");
164176
}
165177
}

src/GitVersion.BuildAgents/Agents/BitBucketPipelines.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,58 @@ internal class BitBucketPipelines : BuildAgentBase
99
public const string BranchEnvironmentVariableName = "BITBUCKET_BRANCH";
1010
public const string TagEnvironmentVariableName = "BITBUCKET_TAG";
1111
public const string PullRequestEnvironmentVariableName = "BITBUCKET_PR_ID";
12-
private string? file;
12+
private string? propertyFile;
13+
private string? ps1File;
1314

14-
public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log) => WithPropertyFile("gitversion.properties");
15+
public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log)
16+
{
17+
WithPropertyFile("gitversion.properties");
18+
WithPowershellFile("gitversion.ps1");
19+
}
1520

1621
protected override string EnvironmentVariable => EnvironmentVariableName;
1722

1823
public override string GenerateSetVersionMessage(GitVersionVariables variables) => variables.FullSemVer;
1924

20-
public void WithPropertyFile(string propertiesFileName) => this.file = propertiesFileName;
25+
public void WithPropertyFile(string propertiesFileName) => this.propertyFile = propertiesFileName;
26+
27+
public void WithPowershellFile(string powershellFileName) => this.ps1File = powershellFileName;
2128

2229
public override string[] GenerateSetParameterMessage(string name, string? value) => new[] { $"GITVERSION_{name.ToUpperInvariant()}={value}" };
2330

2431
public override void WriteIntegration(Action<string?> writer, GitVersionVariables variables, bool updateBuildNumber = true)
2532
{
26-
if (this.file is null)
33+
if (this.propertyFile is null || this.ps1File is null)
2734
return;
2835

2936
base.WriteIntegration(writer, variables, updateBuildNumber);
30-
writer($"Outputting variables to '{this.file}' ... ");
37+
writer($"Outputting variables to '{this.propertyFile}' for Bash,");
38+
writer($"and to '{this.ps1File}' for Powershell ... ");
3139
writer("To import the file into your build environment, add the following line to your build step:");
32-
writer($" - source {this.file}");
40+
writer($"Bash:");
41+
writer($" - source {this.propertyFile}");
42+
writer($"Powershell:");
43+
writer($" - . .\\{this.ps1File}");
3344
writer("");
3445
writer("To reuse the file across build steps, add the file as a build artifact:");
46+
writer($"Bash:");
47+
writer(" artifacts:");
48+
writer($" - {this.propertyFile}");
49+
writer($"Powershell:");
3550
writer(" artifacts:");
36-
writer($" - {this.file}");
51+
writer($" - {this.ps1File}");
3752

3853
var exports = variables
3954
.Select(variable => $"export GITVERSION_{variable.Key.ToUpperInvariant()}={variable.Value}")
4055
.ToList();
4156

42-
File.WriteAllLines(this.file, exports);
57+
File.WriteAllLines(this.propertyFile, exports);
58+
59+
var psExports = variables
60+
.Select(variable => $"$GITVERSION_{variable.Key.ToUpperInvariant()} = \"{variable.Value}\"")
61+
.ToList();
62+
63+
File.WriteAllLines(this.ps1File, psExports);
4364
}
4465

4566
public override string? GetCurrentBranch(bool usingDynamicRepos)

0 commit comments

Comments
 (0)