Skip to content

Commit b28b1b9

Browse files
committed
Merge branch 'pr-271' into devel
2 parents 62fce74 + 60bd8cb commit b28b1b9

File tree

7 files changed

+99
-10
lines changed

7 files changed

+99
-10
lines changed

src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import edu.sc.seis.launch4j.tasks.Launch4jLibraryTask;
44
import io.github.fvarrui.javapackager.packagers.Context;
5+
import io.github.fvarrui.javapackager.utils.Logger;
56
import org.gradle.api.Plugin;
67
import org.gradle.api.Project;
78

@@ -18,7 +19,6 @@ public class PackagePlugin implements Plugin<Project> {
1819

1920
@Override
2021
public void apply(Project project) {
21-
2222
Context.setContext(new GradleContext(project));
2323

2424
project.getPluginManager().apply("java");

src/main/java/io/github/fvarrui/javapackager/packagers/GenerateSetup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected File doApply(WindowsPackager packager) throws Exception {
5656

5757
// generates iss file from velocity template
5858
File issFile = new File(assetsFolder, name + ".iss");
59-
VelocityUtils.render("windows/iss.vtl", issFile, packager);
59+
VelocityUtils.render("windows/iss.vtl", issFile, packager, true);
6060

6161
// generates windows installer with inno setup command line compiler
6262
CommandUtils.execute("iscc", "/O" + outputDirectory.getAbsolutePath(), "/F" + name + "_" + version, issFile);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.github.fvarrui.javapackager.utils;
2+
3+
import java.nio.charset.Charset;
4+
import java.nio.charset.StandardCharsets;
5+
6+
import org.apache.commons.lang3.SystemUtils;
7+
8+
public class CharsetUtil {
9+
10+
private static Charset commandLineCharset;
11+
12+
public static Charset getCommandLineCharset(){
13+
if (commandLineCharset == null) {
14+
if (SystemUtils.IS_OS_WINDOWS) {
15+
commandLineCharset = chcp();
16+
} else {
17+
commandLineCharset = Charset.defaultCharset();
18+
}
19+
}
20+
return commandLineCharset;
21+
}
22+
23+
private static Charset chcp() {
24+
try{
25+
String result = CommandUtils.run("cmd /k chcp");
26+
String code = StringUtils.find("\\d+", result);
27+
Logger.debug("'chcp' code found: " + code);
28+
switch (code){
29+
case "37":
30+
case "037": return Charset.forName("IBM037");
31+
case "936": return Charset.forName("gb2312");
32+
case "950": return Charset.forName("big5");
33+
case "1145": return Charset.forName("IBM01145");
34+
case "1200": return StandardCharsets.UTF_16;
35+
case "51936": return Charset.forName("EUC-CN");
36+
case "65001": return StandardCharsets.UTF_8;
37+
}
38+
} catch (Exception e){
39+
// do nothing
40+
}
41+
return Charset.defaultCharset();
42+
}
43+
44+
}

src/main/java/io/github/fvarrui/javapackager/utils/CommandUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static ExecutionResult executeWithResult(File workingDirectory, String ex
5252

5353
Process process = command.execute();
5454

55-
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
55+
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream(), CharsetUtil.getCommandLineCharset()));
5656
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
5757
while (process.isAlive() || output.ready() || error.ready()) {
5858
if (output.ready()) {
@@ -76,5 +76,13 @@ public static ExecutionResult executeWithResult(File workingDirectory, String ex
7676

7777
return result;
7878
}
79+
80+
public static String run(String command) throws IOException {
81+
Process p = Runtime.getRuntime().exec(command);
82+
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
83+
String result = br.readLine();
84+
br.close();
85+
return result;
86+
}
7987

8088
}

src/main/java/io/github/fvarrui/javapackager/utils/FileUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,20 @@ public static boolean exists(File file) {
326326
public static boolean folderContainsFile(File folder, String filename) {
327327
return new File(folder, filename).exists();
328328
}
329+
330+
/**
331+
* Writes String to a file in UTF-8 including BOM
332+
* @param output Output file
333+
* @param data Input string
334+
* @throws Exception if something goes wrong
335+
*/
336+
public static void writeStringToFileWithBOM(File output, String data) throws Exception {
337+
FileOutputStream fileOutputStream = new FileOutputStream(output);
338+
// write utf-8 BOM
339+
byte[] uft8bom = { (byte) 0xef, (byte) 0xbb, (byte) 0xbf };
340+
fileOutputStream.write(uft8bom);
341+
fileOutputStream.write(data.getBytes(StandardCharsets.UTF_8));
342+
fileOutputStream.close();
343+
}
329344

330345
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.fvarrui.javapackager.utils;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class StringUtils {
7+
8+
public static String dosToUnix(String input) {
9+
return input.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
10+
}
11+
12+
public static String find(String pattern, String data) {
13+
Pattern r = Pattern.compile(pattern);
14+
Matcher matcher = r.matcher(data);
15+
matcher.find();
16+
return matcher.group();
17+
}
18+
19+
}

src/main/java/io/github/fvarrui/javapackager/utils/VelocityUtils.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.ArrayList;
88
import java.util.UUID;
99

10-
import org.apache.commons.lang3.StringUtils;
1110
import org.apache.velocity.Template;
1211
import org.apache.velocity.VelocityContext;
1312
import org.apache.velocity.app.VelocityEngine;
@@ -30,7 +29,7 @@ private static VelocityEngine getVelocityEngine() {
3029
velocityEngine = new VelocityEngine();
3130

3231
// specify resource loaders to use
33-
velocityEngine.setProperty("resource.loaders", "file,class");
32+
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADERS, "file,class");
3433

3534
// for the loader 'file', set the FileResourceLoader as the class to use and use 'assets' directory for templates
3635
velocityEngine.setProperty("resource.loader.file.class", FileResourceLoader.class.getName());
@@ -63,12 +62,16 @@ public static void setAssetsDir(File assetsDir) {
6362
}
6463

6564
public static void render(String templatePath, File output, Object info) throws Exception {
66-
try {
67-
String data = render(templatePath, info);
68-
data = data.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
65+
render(templatePath, output, info, false);
66+
}
67+
68+
public static void render(String templatePath, File output, Object info, boolean includeBom) throws Exception {
69+
String data = render(templatePath, info);
70+
data = StringUtils.dosToUnix(data);
71+
if (!includeBom) {
6972
writeStringToFile(output, data, "UTF-8");
70-
} catch (IOException e) {
71-
throw new Exception(e.getMessage(), e);
73+
} else {
74+
FileUtils.writeStringToFileWithBOM(output, data);
7275
}
7376
}
7477

0 commit comments

Comments
 (0)