Skip to content

Commit ad7cf6e

Browse files
author
TheSnoozer
committed
#18: support YML format
1 parent 1d84b3d commit ad7cf6e

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@
205205
<artifactId>javax.json</artifactId>
206206
<version>1.1.4</version>
207207
</dependency>
208+
<!-- yaml -->
209+
<dependency>
210+
<groupId>org.yaml</groupId>
211+
<artifactId>snakeyaml</artifactId>
212+
<version>2.0</version>
213+
</dependency>
208214

209215
<!-- Test stuff -->
210216
<dependency>

src/main/java/pl/project13/core/CommitIdPropertiesOutputFormat.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ public enum CommitIdPropertiesOutputFormat {
3434
* Indicator to generate a xml file.
3535
*/
3636
XML,
37+
/**
38+
* Indicator to generate a yml file.
39+
*/
40+
YML,
3741
}

src/main/java/pl/project13/core/PropertiesFileGenerator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import pl.project13.core.util.JsonManager;
2424
import pl.project13.core.util.PropertyManager;
2525
import pl.project13.core.util.XmlManager;
26+
import pl.project13.core.util.YmlManager;
2627

2728
import javax.annotation.Nonnull;
2829
import java.io.*;
2930
import java.nio.charset.Charset;
3031
import java.nio.file.Files;
31-
import java.nio.file.Paths;
3232
import java.util.Comparator;
3333
import java.util.Properties;
3434

@@ -76,6 +76,10 @@ public void maybeGeneratePropertiesFile(
7676
log.info(String.format("Reading existing xml file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
7777
persistedProperties = XmlManager.readXmlProperties(gitPropsFile, sourceCharset);
7878
break;
79+
case YML:
80+
log.info(String.format("Reading existing yml file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
81+
persistedProperties = YmlManager.readYmlProperties(gitPropsFile, sourceCharset);
82+
break;
7983
default:
8084
throw new GitCommitIdExecutionException("Not implemented:" + propertiesOutputFormat);
8185
}
@@ -115,6 +119,11 @@ public void maybeGeneratePropertiesFile(
115119
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
116120
XmlManager.dumpXml(outputStream, sortedLocalProperties, sourceCharset);
117121
break;
122+
case YML:
123+
log.info(String.format("Writing yml file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
124+
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
125+
YmlManager.dumpYml(outputStream, sortedLocalProperties, sourceCharset);
126+
break;
118127
default:
119128
throw new GitCommitIdExecutionException("Not implemented:" + propertiesOutputFormat);
120129
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin-core is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin-core is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin-core. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.core.util;
19+
20+
import nu.studer.java.util.OrderedProperties;
21+
import org.yaml.snakeyaml.DumperOptions;
22+
import org.yaml.snakeyaml.LoaderOptions;
23+
import org.yaml.snakeyaml.Yaml;
24+
import pl.project13.core.CannotReadFileException;
25+
26+
import javax.annotation.Nonnull;
27+
import java.io.File;
28+
import java.io.FileInputStream;
29+
import java.io.IOException;
30+
import java.io.InputStreamReader;
31+
import java.io.OutputStream;
32+
import java.io.OutputStreamWriter;
33+
import java.io.Writer;
34+
import java.nio.charset.Charset;
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
import java.util.Properties;
38+
39+
public class YmlManager {
40+
public static void dumpYml(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException {
41+
try (Writer outputWriter = new OutputStreamWriter(outputStream, sourceCharset)) {
42+
DumperOptions dumperOptions = new DumperOptions();
43+
dumperOptions.setAllowUnicode(true);
44+
dumperOptions.setAllowReadOnlyProperties(true);
45+
dumperOptions.setPrettyFlow(true);
46+
// dumperOptions.setCanonical(true);
47+
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO);
48+
49+
Yaml yaml = new Yaml(dumperOptions);
50+
51+
Map<String, Object> dataMap = new HashMap<>();
52+
for (Map.Entry<String, String> e: sortedLocalProperties.entrySet()) {
53+
dataMap.put(e.getKey(), e.getValue());
54+
}
55+
yaml.dump(dataMap, outputWriter);
56+
}
57+
}
58+
59+
public static Properties readYmlProperties(@Nonnull File xmlFile, Charset sourceCharset) throws CannotReadFileException {
60+
Properties retVal = new Properties();
61+
62+
try (FileInputStream fis = new FileInputStream(xmlFile)) {
63+
try (InputStreamReader reader = new InputStreamReader(fis, sourceCharset)) {
64+
LoaderOptions loaderOptions = new LoaderOptions();
65+
loaderOptions.setAllowDuplicateKeys(false);
66+
loaderOptions.setAllowRecursiveKeys(false);
67+
loaderOptions.setProcessComments(false);
68+
Yaml yaml = new Yaml(loaderOptions);
69+
Map<String, Object> data = yaml.load(reader);
70+
for (Map.Entry<String, Object> e: data.entrySet()) {
71+
retVal.put(e.getKey(), e.getValue());
72+
}
73+
}
74+
} catch (IOException e) {
75+
throw new CannotReadFileException(e);
76+
}
77+
return retVal;
78+
}
79+
}

src/test/java/pl/project13/core/GitCommitIdPluginIntegrationTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import pl.project13.core.git.GitDescribeConfig;
2828
import pl.project13.core.util.JsonManager;
2929
import pl.project13.core.util.XmlManager;
30+
import pl.project13.core.util.YmlManager;
3031

3132
import javax.annotation.Nonnull;
3233
import java.io.File;
@@ -492,6 +493,33 @@ public void shouldGenerateCustomPropertiesFileXml(boolean useNativeGit) throws E
492493
Assert.assertEquals(p, properties);
493494
}
494495

496+
@Test
497+
@Parameters(method = "useNativeGit")
498+
public void shouldGenerateCustomPropertiesFileYml(boolean useNativeGit) throws Exception {
499+
// given
500+
File dotGitDirectory = createTmpDotGitDirectory(AvailableGitTestRepo.WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS);
501+
502+
File targetFilePath = sandbox.resolve("custom-git.yml").toFile();
503+
targetFilePath.delete();
504+
505+
GitCommitIdPlugin.Callback cb =
506+
new GitCommitIdTestCallback()
507+
.setDotGitDirectory(dotGitDirectory)
508+
.setUseNativeGit(useNativeGit)
509+
.setShouldGenerateGitPropertiesFile(true)
510+
.setGenerateGitPropertiesFilename(targetFilePath)
511+
.setPropertiesOutputFormat(CommitIdPropertiesOutputFormat.YML)
512+
.build();
513+
Properties properties = new Properties();
514+
515+
// when
516+
GitCommitIdPlugin.runPlugin(cb, properties);
517+
// then
518+
assertThat(targetFilePath).exists();
519+
Properties p = YmlManager.readYmlProperties(targetFilePath, StandardCharsets.UTF_8);
520+
assertThat(p.size() > 10);
521+
Assert.assertEquals(p, properties);
522+
}
495523

496524
@Test
497525
@Parameters(method = "useNativeGit")

0 commit comments

Comments
 (0)