Skip to content

Commit

Permalink
Support multi-line handling in StepWith
Browse files Browse the repository at this point in the history
  • Loading branch information
damianh committed Nov 12, 2024
1 parent b372f30 commit 2a46ac1
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/WorkflowGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void GenerateWorkflowsForLibs()

buildJob.Step().LogIntoGitHubContainerRegistry();

buildJob.Step().ActionsSetupDotNet("8.0.x");
buildJob.Step().ActionsSetupDotNet(["8.0.x"]);

buildJob.Step().PrintEnvironment();

Expand Down Expand Up @@ -121,7 +121,7 @@ void GenerateCodeAnalysisWorkflow()

job.Step().ActionsCheckout();

job.Step().ActionsSetupDotNet("8.0.x");
job.Step().ActionsSetupDotNet(["8.0.x"]);

job.Step()
.Run("dotnet --info");
Expand Down
16 changes: 13 additions & 3 deletions libs/github/src/Actions.Workflow.Extensions/StepExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,22 @@ public static Job PrintEnvironment(this Step step)
/// <param name="step"></param>
/// <param name="versions">DotNet SDK versions</param>
/// <returns></returns>
public static Job ActionsSetupDotNet(this Step step, params string[] versions)
public static Job ActionsSetupDotNet(this Step step, string[] versions)
=> ActionsSetupDotNet(step, "v4", versions);

/// <summary>
/// Sets up dotnet.
/// </summary>
/// <param name="step"></param>
/// <param name="actionVersion">The setup-actions version</param>
/// <param name="versions">DotNet SDK versions</param>
/// <returns></returns>
public static Job ActionsSetupDotNet(this Step step, string actionVersion, string[] versions)
{
step
.Name("Setup Dotnet")
.Uses($"actions/setup-dotnet@v4")
.With(("dotnet-version", string.Join(" ", versions)));
.Uses($"actions/setup-dotnet@{actionVersion}")
.With(("dotnet-version", string.Join(Environment.NewLine, versions)));
return step.Job;
}
}
2 changes: 1 addition & 1 deletion libs/github/src/Actions.Workflow/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ internal void Build(YamlMappingNode node, SequenceStyle sequenceStyle)
foreach (var step in _steps)
{
var stepMappingNode = new YamlMappingNode();
step.Build(stepMappingNode, sequenceStyle);
step.Build(stepMappingNode);
sequenceNode.Add(stepMappingNode);
}
jobNode.Add("steps", sequenceNode);
Expand Down
4 changes: 2 additions & 2 deletions libs/github/src/Actions.Workflow/Step.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public Step ContinueOnError(bool continueOnError)
return this;
}

internal void Build(YamlMappingNode node, SequenceStyle sequenceStyle)
internal void Build(YamlMappingNode node)
{
if (!string.IsNullOrWhiteSpace(_id))
{
Expand Down Expand Up @@ -206,6 +206,6 @@ internal void Build(YamlMappingNode node, SequenceStyle sequenceStyle)
node.Add("uses", _uses);
}

_with?.Build(node, sequenceStyle);
_with?.Build(node);
}
}
18 changes: 15 additions & 3 deletions libs/github/src/Actions.Workflow/StepWith.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using YamlDotNet.Core.Events;
using YamlDotNet.Core;
using YamlDotNet.Core.Tokens;
using YamlDotNet.RepresentationModel;

namespace Logicality.GitHub.Actions.Workflow;
Expand All @@ -20,14 +21,25 @@ public class StepWith(Step step, IDictionary<string, string> map)
/// </summary>
public Workflow Workflow => Job.Workflow;

internal void Build(YamlMappingNode yamlMappingNode, SequenceStyle sequenceStyle)
internal void Build(YamlMappingNode yamlMappingNode)
{
if (map.Any())
{
var mappingNode = new YamlMappingNode();
foreach (var property in map)
{
mappingNode.Add(property.Key, new YamlScalarNode(property.Value));
if (property.Value.Contains(Environment.NewLine))
{
var yamlScalarNode = new YamlScalarNode(property.Value)
{
Style = ScalarStyle.Literal
};
mappingNode.Add(property.Key, yamlScalarNode);
}
else
{
mappingNode.Add(property.Key, property.Value);
}
}

yamlMappingNode.Add("with", mappingNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
<RootNamespace>Logicality.GitHub.Actions.Workflow</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Extensions\StepExtensionsTests.cs" />
</ItemGroup>

<ItemGroup>
<None Include="Extensions\StepExtensionsTests.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Actions.Workflow.Extensions\Actions.Workflow.Extensions.csproj" />
<ProjectReference Include="..\..\src\Actions.Workflow\Actions.Workflow.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logicality.GitHub.Actions.Workflow.Extensions
{
public class StepExtensions
{
[Fact]
public void ActionsDotnetSetupTests()
{
var actual = new Workflow("workflow")
.Job("build")
.Step()
.ActionsSetupDotNet(["8.0.x", "9.0.x"])
.Workflow
.GetYaml();

var expected = Workflow.Header + @"
name: workflow
jobs:
build:
steps:
- name: Setup Dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: |-
8.0.x
9.0.x
";

actual.ShouldBe(expected.ReplaceLineEndings()); ;
}
}
}
26 changes: 26 additions & 0 deletions libs/github/tests/Actions.Workflow.Tests/JobStepTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,32 @@ public void Step_With()
actual.ShouldBe(expected.ReplaceLineEndings());;
}

[Fact]
public void Step_With_Multiline()
{
var actual = new Workflow("workflow")
.Job("build")
.Step("step")
.With(("foo", $"bar{Environment.NewLine}baz"))
.Workflow
.GetYaml();

var expected = Workflow.Header + @"
name: workflow
jobs:
build:
steps:
- id: step
with:
foo: |-
bar
baz
";

actual.ShouldBe(expected.ReplaceLineEndings()); ;
}

[Fact]
public void Step_TimeoutMinutes()
{
Expand Down

0 comments on commit 2a46ac1

Please sign in to comment.