Skip to content

Commit 85bf2a3

Browse files
committed
Fix #64: Support reading values containing line separators
1 parent 4d7faa6 commit 85bf2a3

File tree

4 files changed

+136
-13
lines changed

4 files changed

+136
-13
lines changed

.github/actions/compare/action.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: compare
2+
description: Compare two strings
3+
inputs:
4+
a:
5+
required: true
6+
description: A
7+
b:
8+
required: true
9+
description: B
10+
runs:
11+
using: composite
12+
steps:
13+
- run: 'echo $a > /tmp/a'
14+
shell: bash
15+
env:
16+
a: ${{ toJSON(inputs.a) }}
17+
- run: 'echo $b > /tmp/b'
18+
shell: bash
19+
env:
20+
b: ${{ toJSON(inputs.b) }}
21+
- run: diff /tmp/a /tmp/b
22+
shell: bash

.github/workflows/test.yml

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,103 @@ jobs:
1313
- uses: actions/checkout@v4
1414

1515
- uses: ./src/test/resources/
16-
id: success
16+
id: simple
1717
with:
1818
file: src/test/resources/test.properties
1919
property: name
20-
- run: '[[ "Darth Vader" == "${{ steps.success.outputs.value }}" ]]'
20+
- uses: ./.github/actions/compare
21+
with:
22+
a: Darth Vader
23+
b: ${{ steps.simple.outputs.value }}
24+
25+
- uses: ./src/test/resources/
26+
id: multiline
27+
with:
28+
file: src/test/resources/test.properties
29+
property: affiliation
30+
- uses: ./.github/actions/compare
31+
with:
32+
a: |-
33+
Galactic
34+
Empire
35+
b: ${{ steps.multiline.outputs.value }}
2136
2237
- uses: ./src/test/resources/
23-
id: default
38+
id: simple-default
2439
with:
2540
file: src/test/resources/test.properties
2641
property: version
2742
default: 1.0.0
28-
- run: '[[ "1.0.0" == "${{ steps.default.outputs.value }}" ]]'
43+
- uses: ./.github/actions/compare
44+
with:
45+
a: 1.0.0
46+
b: ${{ steps.simple-default.outputs.value }}
47+
48+
- uses: ./src/test/resources/
49+
id: multiline-default
50+
with:
51+
file: src/test/resources/test.properties
52+
property: version
53+
default: |-
54+
1
55+
2
56+
3
57+
- uses: ./.github/actions/compare
58+
with:
59+
a: |-
60+
1
61+
2
62+
3
63+
b: ${{ steps.multiline-default.outputs.value }}
2964
3065
- uses: ./src/test/resources/
3166
id: unexisting-file
3267
with:
3368
file: src/test/resources/unexisting.properties
3469
property: name
3570
default: Anakin Skywalker
36-
- run: '[[ "Anakin Skywalker" == "${{ steps.unexisting-file.outputs.value }}" ]]'
71+
- uses: ./.github/actions/compare
72+
with:
73+
a: Anakin Skywalker
74+
b: ${{ steps.unexisting-file.outputs.value }}
3775

3876
- uses: ./src/test/resources/
3977
id: all
4078
with:
4179
file: src/test/resources/test.properties
4280
all: true
43-
- run: '[[ "Darth Vader" == "${{ steps.all.outputs.name }}" ]]'
44-
- run: '[[ "Sith Lord" == "${{ steps.all.outputs.occupation }}" ]]'
45-
- run: '[[ "Luke Skywalker" == "${{ steps.all.outputs.son }}" ]]'
81+
- uses: ./.github/actions/compare
82+
with:
83+
a: Darth Vader
84+
b: ${{ steps.all.outputs.name }}
85+
- uses: ./.github/actions/compare
86+
with:
87+
a: Sith Lord
88+
b: ${{ steps.all.outputs.occupation }}
89+
- uses: ./.github/actions/compare
90+
with:
91+
a: |-
92+
Galactic
93+
Empire
94+
b: ${{ steps.all.outputs.affiliation }}
95+
- uses: ./.github/actions/compare
96+
with:
97+
a: Luke Skywalker
98+
b: ${{ steps.all.outputs.son }}
99+
- uses: ./.github/actions/compare
100+
with:
101+
a: 1
102+
b: ${{ steps.all.outputs.read-java-properties-delimiter }}
103+
- uses: ./.github/actions/compare
104+
with:
105+
a: 2
106+
b: ${{ steps.all.outputs.read-java-properties-delimiter-x }}
107+
- uses: ./.github/actions/compare
108+
with:
109+
a: |-
110+
3
111+
4
112+
b: ${{ steps.all.outputs.read-java-properties-delimiter-x-x }}
46113
47114
- uses: ./src/test/resources/
48115
id: failure
@@ -51,6 +118,7 @@ jobs:
51118
property: name
52119
continue-on-error: true
53120
- run: '[[ "failure" == "${{ steps.failure.outcome }}" ]]'
121+
54122
actions-typing:
55123
name: Validate actions typing
56124
runs-on: ubuntu-latest

src/main/kotlin/me/madhead/github/actions/rjp/ReadJavaProperties.kt

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.lang.System.lineSeparator
66
import java.util.Properties
77
import kotlin.io.path.Path
88
import kotlin.io.path.appendText
9+
import kotlin.io.path.useLines
910

1011
fun main(args: Array<String>) {
1112
val file = args[0]
@@ -21,14 +22,19 @@ fun main(args: Array<String>) {
2122
properties.load(fis)
2223

2324
if ("true".equals(all, ignoreCase = true)) {
24-
properties.entries.forEach { (key, value) ->
25-
githubOutput.appendText("$key=$value${lineSeparator()}")
25+
properties.every { key, value ->
26+
githubOutput.appendText(packKeyValue(key, value))
2627
}
2728
} else {
2829
properties.getProperty(property)?.let { value ->
29-
githubOutput.appendText("value=$value${lineSeparator()}")
30+
githubOutput.appendText(packKeyValue("value", value))
3031
} ?: run {
31-
githubOutput.appendText("value=${default.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("$property (No such property)")}${lineSeparator()}")
32+
githubOutput.appendText(
33+
packKeyValue(
34+
"value",
35+
default.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("$property (No such property)")
36+
)
37+
)
3238
}
3339
}
3440
}
@@ -38,8 +44,31 @@ fun main(args: Array<String>) {
3844
if ("true".equals(all, ignoreCase = true)) {
3945
throw e
4046
} else {
41-
githubOutput.appendText("value=${default.takeIf { it.isNotEmpty() } ?: throw e}${lineSeparator()}")
47+
githubOutput.appendText(packKeyValue("value", default.takeIf { it.isNotEmpty() } ?: throw e))
4248
}
4349
}
4450

51+
println("DEBUG")
52+
githubOutput.useLines {
53+
it.forEach { println(it) }
54+
}
55+
println("DEBUG")
56+
}
57+
58+
private fun Properties.every(action: (String, String) -> Unit) {
59+
for (element in this) action(element.key.toString(), element.value.toString())
60+
}
61+
62+
private fun packKeyValue(key: String, value: String): String {
63+
if (value.isMultiline()) {
64+
var delimiter = "read-java-properties-delimiter"
65+
while (value.contains(delimiter)) {
66+
delimiter += "-x"
67+
}
68+
return "$key<<$delimiter${lineSeparator()}$value${lineSeparator()}$delimiter"
69+
} else {
70+
return "$key=$value${lineSeparator()}"
71+
}
4572
}
73+
74+
private fun String.isMultiline(): Boolean = this.split(Regex("^(.*)$", RegexOption.MULTILINE)).isNotEmpty()

src/test/resources/test.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
name=Darth Vader
22
occupation=Sith Lord
3+
affiliation=Galactic\nEmpire
34
son=Luke \
45
Skywalker
6+
read-java-properties-delimiter=1
7+
read-java-properties-delimiter-x=2
8+
read-java-properties-delimiter-x-x=3\n4

0 commit comments

Comments
 (0)