Skip to content

Commit

Permalink
GH-490 Support encoding in fileUpdate hook (Fix #490) (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk authored Jun 20, 2022
1 parent 9aeedbf commit cf7caf2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'
}
}
Expand All @@ -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"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -37,25 +37,28 @@ 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()
);

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -86,4 +87,5 @@ class FileUpdateSpec {

var pattern: (String, HookContext) -> String = { previousVersion, context -> "" }
var replacement: (String, HookContext) -> String = { currentVersion, context -> "" }
var encoding: String? = null
}

0 comments on commit cf7caf2

Please sign in to comment.