Skip to content

Commit 9afc81c

Browse files
authored
Merge pull request #126 from OctopusDeploy/dylan/write-fewer-newlines
Reduce newlines when serialising OCL +semver: minor
2 parents 91f563e + 5d1e807 commit 9afc81c

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

source/Ocl/OclWriter.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class OclWriter : IDisposable
1515
readonly TextWriter writer;
1616
int currentIndent;
1717
bool isFirstLine = true;
18+
bool isFirstChildInBlock;
1819
bool lastWrittenWasBlock;
1920

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

6162
public void Write(OclBlock block)
6263
{
63-
if (!isFirstLine && !lastWrittenWasBlock)
64+
if (!isFirstLine && !lastWrittenWasBlock && !isFirstChildInBlock)
6465
writer.WriteLine();
6566
WriteNextLine();
6667
WriteIndent();
@@ -74,14 +75,23 @@ public void Write(OclBlock block)
7475

7576
writer.Write(" {");
7677

77-
currentIndent++;
78-
foreach (var child in block)
79-
Write(child);
80-
currentIndent--;
78+
if (block.Any())
79+
{
80+
currentIndent++;
81+
isFirstChildInBlock = true;
8182

82-
writer.WriteLine();
83+
foreach (var child in block)
84+
{
85+
Write(child);
86+
isFirstChildInBlock = false;
87+
}
88+
89+
currentIndent--;
90+
91+
writer.WriteLine();
92+
WriteIndent();
93+
}
8394

84-
WriteIndent();
8595
writer.Write("}");
8696

8797
lastWrittenWasBlock = true;

source/Tests/RealLifeScenario/RealLifeScenarioFixture.IisAction.approved.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ connectivity_policy {
88
project_id = "Projects-1"
99

1010
steps "Deploy Website" {
11-
1211
actions "Deploy Website" {
1312
action_type = "Octopus.IIS"
1413
properties = {

source/Tests/RealLifeScenario/RealLifeScenarioFixture.Rolling.approved.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ connectivity_policy {
88
project_id = "Projects-1"
99

1010
steps "My Rolling Step" {
11-
1211
actions "First" {
1312
action_type = "Octopus.Script"
1413
}

source/Tests/RealLifeScenario/RealLifeScenarioFixture.ScriptAction.approved.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ connectivity_policy {
88
project_id = "Projects-1"
99

1010
steps "Backup the Database" {
11-
1211
actions "Backup the Database" {
1312
action_type = "Octopus.Script"
1413
properties = {

source/Tests/ToString/OclWriterFixture.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ public void MultilineStringsUseHeredocAndTheHeredocIdentifierFromOptions()
156156
public void WriteBlockEmpty()
157157
=> Execute(w => w.Write(new OclBlock("MyBlock")))
158158
.Should()
159-
.Be("MyBlock {\n}");
159+
.Be("MyBlock {}");
160160

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

167167
[Test]
168168
public void WriteBlockSingleLabel()
@@ -172,7 +172,7 @@ public void WriteBlockSingleLabel()
172172

173173
Execute(w => w.Write(block))
174174
.Should()
175-
.Be("MyBlock \"MyLabel\" {\n}");
175+
.Be("MyBlock \"MyLabel\" {}");
176176
}
177177

178178
[Test]
@@ -185,7 +185,7 @@ public void WriteBlockMultipleLabel()
185185

186186
Execute(w => w.Write(block))
187187
.Should()
188-
.Be("MyBlock \"MyLabel\" \"OtherLabel\" \"LastLabel\" {\n}");
188+
.Be("MyBlock \"MyLabel\" \"OtherLabel\" \"LastLabel\" {}");
189189
}
190190

191191
[Test]
@@ -196,7 +196,7 @@ public void WriteBlockDoubleQuotesInLabel()
196196

197197
Execute(w => w.Write(block))
198198
.Should()
199-
.Be("MyBlock \"My\\\"Label\" {\n}");
199+
.Be("MyBlock \"My\\\"Label\" {}");
200200
}
201201

202202
[Test]
@@ -208,9 +208,7 @@ public void WriteBlockSingleChildBlock()
208208
};
209209

210210
var expected = @"MyBlock {
211-
212-
Child {
213-
}
211+
Child {}
214212
}";
215213

216214
Execute(w => w.Write(block))
@@ -281,8 +279,7 @@ public void WriteBlockMixedAttributesAndBlocks()
281279
ThirdChild = 3
282280
}
283281
284-
Fourth {
285-
}
282+
Fourth {}
286283
287284
Last = 9
288285
}";
@@ -306,7 +303,6 @@ public void CollectionAttributeFollowedByEndOfBlock()
306303
};
307304

308305
const string expected = @"OuterBlock {
309-
310306
InnerBlock {
311307
MapAttribute = {
312308
alpha = 1
@@ -335,7 +331,6 @@ public void CollectionAttributeFollowedByAttribute()
335331
};
336332

337333
const string expected = @"OuterBlock {
338-
339334
InnerBlock {
340335
MapAttribute = {
341336
alpha = 1

0 commit comments

Comments
 (0)