From cf7caf26002c6ee5ed68f3c049276614bb2854b9 Mon Sep 17 00:00:00 2001 From: Dzikoysk Date: Mon, 20 Jun 2022 17:03:14 +0200 Subject: [PATCH] GH-490 Support encoding in `fileUpdate` hook (Fix #490) (#493) --- .../release/SimpleIntegrationTest.groovy | 9 +++++--- .../domain/hooks/FileUpdateHookAction.java | 21 +++++++++++-------- .../release/domain/KotlinDslExtensions.kt | 4 +++- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/integration/groovy/pl/allegro/tech/build/axion/release/SimpleIntegrationTest.groovy b/src/integration/groovy/pl/allegro/tech/build/axion/release/SimpleIntegrationTest.groovy index e9b2b918..f9a1a3d4 100644 --- a/src/integration/groovy/pl/allegro/tech/build/axion/release/SimpleIntegrationTest.groovy +++ b/src/integration/groovy/pl/allegro/tech/build/axion/release/SimpleIntegrationTest.groovy @@ -2,6 +2,9 @@ package pl.allegro.tech.build.axion.release import org.gradle.testkit.runner.TaskOutcome +import java.nio.charset.StandardCharsets +import java.nio.file.Files + import static pl.allegro.tech.build.axion.release.TagPrefixConf.fullPrefix class SimpleIntegrationTest extends BaseIntegrationTest { @@ -65,12 +68,12 @@ class SimpleIntegrationTest extends BaseIntegrationTest { def "should update file in pre release hook"() { given: File versionFile = newFile('version-file') - versionFile << "Version: 0.1.0" + Files.write(versionFile.toPath(), "🚀 Version: 0.1.0".getBytes(StandardCharsets.UTF_8)) buildFile(""" scmVersion { hooks { - pre 'fileUpdate', [files: ['version-file'], pattern: { v, p -> v }, replacement: { v, p -> v }] + pre 'fileUpdate', [files: ['version-file'], pattern: { v, p -> v }, replacement: { v, p -> v }, encoding: 'utf-8'] pre 'commit' } } @@ -80,7 +83,7 @@ class SimpleIntegrationTest extends BaseIntegrationTest { runGradle('release', '-Prelease.version=1.0.0', '-Prelease.localOnly', '-Prelease.disableChecks', '-s') then: - versionFile.text == "Version: 1.0.0" + versionFile.text == "🚀 Version: 1.0.0" } def "should fail gracefuly when failed to parse tag"() { diff --git a/src/main/java/pl/allegro/tech/build/axion/release/domain/hooks/FileUpdateHookAction.java b/src/main/java/pl/allegro/tech/build/axion/release/domain/hooks/FileUpdateHookAction.java index b1d936f5..7ba27210 100644 --- a/src/main/java/pl/allegro/tech/build/axion/release/domain/hooks/FileUpdateHookAction.java +++ b/src/main/java/pl/allegro/tech/build/axion/release/domain/hooks/FileUpdateHookAction.java @@ -3,14 +3,14 @@ import groovy.lang.Closure; import pl.allegro.tech.build.axion.release.util.FileLoader; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.function.BiFunction; +import java.util.Optional; import java.util.regex.Pattern; public class FileUpdateHookAction implements ReleaseHookAction { @@ -37,6 +37,11 @@ private void updateInFile(HookContext hookContext, Object potentialFile) { String replacement = ((Closure) arguments.get("replacement")) .call(hookContext.getReleaseVersion(), hookContext).toString(); + Charset charset = Optional.ofNullable(arguments.get("encoding")) + .map(Object::toString) + .map(Charset::forName) + .orElseGet(Charset::defaultCharset); + try { hookContext.getLogger().quiet( "Replacing pattern \"" + pattern + "\" with \"" + replacement + "\" in " + file.getCanonicalPath() @@ -44,18 +49,16 @@ private void updateInFile(HookContext hookContext, Object potentialFile) { String replacedText = Pattern.compile(pattern, Pattern.MULTILINE).matcher(text).replaceAll(replacement); - write(file, replacedText); + write(file, replacedText, charset); hookContext.addCommitPattern(file.getCanonicalPath()); } catch (IOException e) { e.printStackTrace(); } } - private void write(File file, String text) { - try (FileWriter fw = new FileWriter(file)) { - BufferedWriter writer = new BufferedWriter(fw); - writer.write(text); - writer.flush(); + private void write(File file, String text, Charset charset) { + try { + Files.write(file.toPath(), text.getBytes(charset)); } catch (IOException e) { throw new FileUpdateHookException(e); } diff --git a/src/main/kotlin/pl/allegro/tech/build/axion/release/domain/KotlinDslExtensions.kt b/src/main/kotlin/pl/allegro/tech/build/axion/release/domain/KotlinDslExtensions.kt index f15e8679..cd170765 100644 --- a/src/main/kotlin/pl/allegro/tech/build/axion/release/domain/KotlinDslExtensions.kt +++ b/src/main/kotlin/pl/allegro/tech/build/axion/release/domain/KotlinDslExtensions.kt @@ -43,7 +43,8 @@ class ReleaseHooksBuilder(private val hooksConfig: HooksConfig, private val preR val configMap = mapOf( "files" to fileUpdateSpec.filesToUpdate, "pattern" to fileUpdateSpec.pattern, - "replacement" to fileUpdateSpec.replacement + "replacement" to fileUpdateSpec.replacement, + "encoding" to fileUpdateSpec.encoding ) if (preRelease) { hooksConfig.pre("fileUpdate", configMap) @@ -86,4 +87,5 @@ class FileUpdateSpec { var pattern: (String, HookContext) -> String = { previousVersion, context -> "" } var replacement: (String, HookContext) -> String = { currentVersion, context -> "" } + var encoding: String? = null }