Skip to content

Commit

Permalink
Merge pull request #126 from OctopusDeploy/dylan/write-fewer-newlines
Browse files Browse the repository at this point in the history
Reduce newlines when serialising OCL

+semver: minor
  • Loading branch information
dylanlerch authored Aug 16, 2022
2 parents 91f563e + 5d1e807 commit 9afc81c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
24 changes: 17 additions & 7 deletions source/Ocl/OclWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class OclWriter : IDisposable
readonly TextWriter writer;
int currentIndent;
bool isFirstLine = true;
bool isFirstChildInBlock;
bool lastWrittenWasBlock;

public OclWriter(StringBuilder sb, OclSerializerOptions? options = null)
Expand Down Expand Up @@ -60,7 +61,7 @@ public void Write(OclAttribute attribute)

public void Write(OclBlock block)
{
if (!isFirstLine && !lastWrittenWasBlock)
if (!isFirstLine && !lastWrittenWasBlock && !isFirstChildInBlock)
writer.WriteLine();
WriteNextLine();
WriteIndent();
Expand All @@ -74,14 +75,23 @@ public void Write(OclBlock block)

writer.Write(" {");

currentIndent++;
foreach (var child in block)
Write(child);
currentIndent--;
if (block.Any())
{
currentIndent++;
isFirstChildInBlock = true;

writer.WriteLine();
foreach (var child in block)
{
Write(child);
isFirstChildInBlock = false;
}

currentIndent--;

writer.WriteLine();
WriteIndent();
}

WriteIndent();
writer.Write("}");

lastWrittenWasBlock = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ connectivity_policy {
project_id = "Projects-1"

steps "Deploy Website" {

actions "Deploy Website" {
action_type = "Octopus.IIS"
properties = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ connectivity_policy {
project_id = "Projects-1"

steps "My Rolling Step" {

actions "First" {
action_type = "Octopus.Script"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ connectivity_policy {
project_id = "Projects-1"

steps "Backup the Database" {

actions "Backup the Database" {
action_type = "Octopus.Script"
properties = {
Expand Down
19 changes: 7 additions & 12 deletions source/Tests/ToString/OclWriterFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ public void MultilineStringsUseHeredocAndTheHeredocIdentifierFromOptions()
public void WriteBlockEmpty()
=> Execute(w => w.Write(new OclBlock("MyBlock")))
.Should()
.Be("MyBlock {\n}");
.Be("MyBlock {}");

[Test]
public void WriteBlockSpecialCharactersInName()
=> Execute(w => w.Write(new OclBlock("My0%&2_'\"-Block")))
.Should()
.Be("My0__2___-Block {\n}");
.Be("My0__2___-Block {}");

[Test]
public void WriteBlockSingleLabel()
Expand All @@ -172,7 +172,7 @@ public void WriteBlockSingleLabel()

Execute(w => w.Write(block))
.Should()
.Be("MyBlock \"MyLabel\" {\n}");
.Be("MyBlock \"MyLabel\" {}");
}

[Test]
Expand All @@ -185,7 +185,7 @@ public void WriteBlockMultipleLabel()

Execute(w => w.Write(block))
.Should()
.Be("MyBlock \"MyLabel\" \"OtherLabel\" \"LastLabel\" {\n}");
.Be("MyBlock \"MyLabel\" \"OtherLabel\" \"LastLabel\" {}");
}

[Test]
Expand All @@ -196,7 +196,7 @@ public void WriteBlockDoubleQuotesInLabel()

Execute(w => w.Write(block))
.Should()
.Be("MyBlock \"My\\\"Label\" {\n}");
.Be("MyBlock \"My\\\"Label\" {}");
}

[Test]
Expand All @@ -208,9 +208,7 @@ public void WriteBlockSingleChildBlock()
};

var expected = @"MyBlock {
Child {
}
Child {}
}";

Execute(w => w.Write(block))
Expand Down Expand Up @@ -281,8 +279,7 @@ public void WriteBlockMixedAttributesAndBlocks()
ThirdChild = 3
}
Fourth {
}
Fourth {}
Last = 9
}";
Expand All @@ -306,7 +303,6 @@ public void CollectionAttributeFollowedByEndOfBlock()
};

const string expected = @"OuterBlock {
InnerBlock {
MapAttribute = {
alpha = 1
Expand Down Expand Up @@ -335,7 +331,6 @@ public void CollectionAttributeFollowedByAttribute()
};

const string expected = @"OuterBlock {
InnerBlock {
MapAttribute = {
alpha = 1
Expand Down

0 comments on commit 9afc81c

Please sign in to comment.