diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8f7f3c5..2270617 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,19 +13,46 @@ jobs: - uses: actions/checkout@v4 - uses: ./src/test/resources/ - id: success + id: simple with: file: src/test/resources/test.properties property: name - - run: '[[ "Darth Vader" == "${{ steps.success.outputs.value }}" ]]' + - run: ${{ 'Darth Vader' == steps.simple.outputs.value }} - uses: ./src/test/resources/ - id: default + id: multiline + with: + file: src/test/resources/test.properties + property: affiliation + - run: '[[ "Galactic\nEmpire" == $value ]]' + env: + value: ${{ toJSON(steps.multiline.outputs.value) }} + +# - name: Dump job context +# env: +# MULTILINE: ${{ toJson(steps.multiline) }} +# run: echo "$MULTILINE" +# +# - run: ${{ steps.multiline.outputs.value == 'Galactic\nEmpire' }} + + - uses: ./src/test/resources/ + id: simple-default with: file: src/test/resources/test.properties property: version default: 1.0.0 - - run: '[[ "1.0.0" == "${{ steps.default.outputs.value }}" ]]' + - run: '[[ "1.0.0" == "${{ steps.simple-default.outputs.value }}" ]]' + + - uses: ./src/test/resources/ + id: multiline-default + with: + file: src/test/resources/test.properties + property: version + default: |- + 1 + 2 + 3 + - run: '[[ "1\n2\n3" == "${{ steps.multiline-default.outputs.value }}" ]]' - uses: ./src/test/resources/ id: unexisting-file @@ -42,7 +69,11 @@ jobs: all: true - run: '[[ "Darth Vader" == "${{ steps.all.outputs.name }}" ]]' - run: '[[ "Sith Lord" == "${{ steps.all.outputs.occupation }}" ]]' + - run: '[[ "Galactic\nEmpire" == "${{ steps.all.outputs.affiliation }}" ]]' - run: '[[ "Luke Skywalker" == "${{ steps.all.outputs.son }}" ]]' + - run: '[[ "1" == "${{ steps.all.outputs.read-java-properties-delimiter }}" ]]' + - run: '[[ "2" == "${{ steps.all.outputs.read-java-properties-delimiter-x }}" ]]' + - run: '[[ "3\n4" == "${{ steps.all.outputs.read-java-properties-delimiter-x-x }}" ]]' - uses: ./src/test/resources/ id: failure diff --git a/src/main/kotlin/me/madhead/github/actions/rjp/ReadJavaProperties.kt b/src/main/kotlin/me/madhead/github/actions/rjp/ReadJavaProperties.kt index 251ab9f..fcbe589 100644 --- a/src/main/kotlin/me/madhead/github/actions/rjp/ReadJavaProperties.kt +++ b/src/main/kotlin/me/madhead/github/actions/rjp/ReadJavaProperties.kt @@ -6,6 +6,7 @@ import java.lang.System.lineSeparator import java.util.Properties import kotlin.io.path.Path import kotlin.io.path.appendText +import kotlin.io.path.useLines fun main(args: Array) { val file = args[0] @@ -21,14 +22,19 @@ fun main(args: Array) { properties.load(fis) if ("true".equals(all, ignoreCase = true)) { - properties.entries.forEach { (key, value) -> - githubOutput.appendText("$key=$value${lineSeparator()}") + properties.every { key, value -> + githubOutput.appendText(packKeyValue(key, value)) } } else { properties.getProperty(property)?.let { value -> - githubOutput.appendText("value=$value${lineSeparator()}") + githubOutput.appendText(packKeyValue("value", value)) } ?: run { - githubOutput.appendText("value=${default.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("$property (No such property)")}${lineSeparator()}") + githubOutput.appendText( + packKeyValue( + "value", + default.takeIf { it.isNotEmpty() } ?: throw IllegalArgumentException("$property (No such property)") + ) + ) } } } @@ -38,8 +44,31 @@ fun main(args: Array) { if ("true".equals(all, ignoreCase = true)) { throw e } else { - githubOutput.appendText("value=${default.takeIf { it.isNotEmpty() } ?: throw e}${lineSeparator()}") + githubOutput.appendText(packKeyValue("value", default.takeIf { it.isNotEmpty() } ?: throw e)) } } + println("DEBUG") + githubOutput.useLines { + it.forEach { println(it) } + } + println("DEBUG") +} + +private fun Properties.every(action: (String, String) -> Unit) { + for (element in this) action(element.key.toString(), element.value.toString()) +} + +private fun packKeyValue(key: String, value: String): String { + if (value.isMultiline()) { + var delimiter = "read-java-properties-delimiter" + while (value.contains(delimiter)) { + delimiter += "-x" + } + return "$key<<$delimiter${lineSeparator()}$value${lineSeparator()}$delimiter" + } else { + return "$key=$value${lineSeparator()}" + } } + +private fun String.isMultiline(): Boolean = this.split(Regex("^(.*)$", RegexOption.MULTILINE)).isNotEmpty() diff --git a/src/test/resources/test.properties b/src/test/resources/test.properties index 15dc0a2..c26ff57 100644 --- a/src/test/resources/test.properties +++ b/src/test/resources/test.properties @@ -1,4 +1,8 @@ name=Darth Vader occupation=Sith Lord +affiliation=Galactic\nEmpire son=Luke \ Skywalker +read-java-properties-delimiter=1 +read-java-properties-delimiter-x=2 +read-java-properties-delimiter-x-x=3\n4