diff --git a/src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs b/src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs index 7978ce31ad..79418b60ee 100644 --- a/src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs +++ b/src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs @@ -118,19 +118,21 @@ public void WriteAllVariablesToTheTextWriter() { var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); assemblyLocation.ShouldNotBeNull(); - var f = PathHelper.Combine(assemblyLocation, "gitversion.properties"); + var propertyFile = PathHelper.Combine(assemblyLocation, "gitversion.properties"); + var ps1File = PathHelper.Combine(assemblyLocation, "gitversion.ps1"); try { - AssertVariablesAreWrittenToFile(f); + AssertVariablesAreWrittenToFile(propertyFile, ps1File); } finally { - File.Delete(f); + File.Delete(propertyFile); + File.Delete(ps1File); } } - private void AssertVariablesAreWrittenToFile(string file) + private void AssertVariablesAreWrittenToFile(string propertyFile, string ps1File) { var writes = new List(); var semanticVersion = new SemanticVersion @@ -147,19 +149,29 @@ private void AssertVariablesAreWrittenToFile(string file) var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, null); - this.buildServer.WithPropertyFile(file); + this.buildServer.WithPropertyFile(propertyFile); + this.buildServer.WithPowershellFile(ps1File); this.buildServer.WriteIntegration(writes.Add, variables); writes[1].ShouldBe("1.2.3-beta.1+5"); - File.Exists(file).ShouldBe(true); + File.Exists(propertyFile).ShouldBe(true); - var props = File.ReadAllText(file); + var props = File.ReadAllText(propertyFile); props.ShouldContain("export GITVERSION_MAJOR=1"); props.ShouldContain("export GITVERSION_MINOR=2"); props.ShouldContain("export GITVERSION_SHA=f28807e615e9f06aec8a33c87780374e0c1f6fb8"); props.ShouldContain("export GITVERSION_COMMITDATE=2022-04-06"); + + File.Exists(ps1File).ShouldBe(true); + + var psProps = File.ReadAllText(ps1File); + + psProps.ShouldContain("$GITVERSION_MAJOR = \"1\""); + psProps.ShouldContain("$GITVERSION_MINOR = \"2\""); + psProps.ShouldContain("$GITVERSION_SHA = \"f28807e615e9f06aec8a33c87780374e0c1f6fb8\""); + psProps.ShouldContain("$GITVERSION_COMMITDATE = \"2022-04-06\""); } } diff --git a/src/GitVersion.BuildAgents/Agents/BitBucketPipelines.cs b/src/GitVersion.BuildAgents/Agents/BitBucketPipelines.cs index 3b1a23a778..1ef01a929c 100644 --- a/src/GitVersion.BuildAgents/Agents/BitBucketPipelines.cs +++ b/src/GitVersion.BuildAgents/Agents/BitBucketPipelines.cs @@ -9,37 +9,58 @@ internal class BitBucketPipelines : BuildAgentBase public const string BranchEnvironmentVariableName = "BITBUCKET_BRANCH"; public const string TagEnvironmentVariableName = "BITBUCKET_TAG"; public const string PullRequestEnvironmentVariableName = "BITBUCKET_PR_ID"; - private string? file; + private string? propertyFile; + private string? ps1File; - public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log) => WithPropertyFile("gitversion.properties"); + public BitBucketPipelines(IEnvironment environment, ILog log) : base(environment, log) + { + WithPropertyFile("gitversion.properties"); + WithPowershellFile("gitversion.ps1"); + } protected override string EnvironmentVariable => EnvironmentVariableName; public override string GenerateSetVersionMessage(GitVersionVariables variables) => variables.FullSemVer; - public void WithPropertyFile(string propertiesFileName) => this.file = propertiesFileName; + public void WithPropertyFile(string propertiesFileName) => this.propertyFile = propertiesFileName; + + public void WithPowershellFile(string powershellFileName) => this.ps1File = powershellFileName; public override string[] GenerateSetParameterMessage(string name, string? value) => new[] { $"GITVERSION_{name.ToUpperInvariant()}={value}" }; public override void WriteIntegration(Action writer, GitVersionVariables variables, bool updateBuildNumber = true) { - if (this.file is null) + if (this.propertyFile is null || this.ps1File is null) return; base.WriteIntegration(writer, variables, updateBuildNumber); - writer($"Outputting variables to '{this.file}' ... "); + writer($"Outputting variables to '{this.propertyFile}' for Bash,"); + writer($"and to '{this.ps1File}' for Powershell ... "); writer("To import the file into your build environment, add the following line to your build step:"); - writer($" - source {this.file}"); + writer($"Bash:"); + writer($" - source {this.propertyFile}"); + writer($"Powershell:"); + writer($" - . .\\{this.ps1File}"); writer(""); writer("To reuse the file across build steps, add the file as a build artifact:"); + writer($"Bash:"); + writer(" artifacts:"); + writer($" - {this.propertyFile}"); + writer($"Powershell:"); writer(" artifacts:"); - writer($" - {this.file}"); + writer($" - {this.ps1File}"); var exports = variables .Select(variable => $"export GITVERSION_{variable.Key.ToUpperInvariant()}={variable.Value}") .ToList(); - File.WriteAllLines(this.file, exports); + File.WriteAllLines(this.propertyFile, exports); + + var psExports = variables + .Select(variable => $"$GITVERSION_{variable.Key.ToUpperInvariant()} = \"{variable.Value}\"") + .ToList(); + + File.WriteAllLines(this.ps1File, psExports); } public override string? GetCurrentBranch(bool usingDynamicRepos)