Skip to content

Commit

Permalink
#18: support YML format
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSnoozer committed May 4, 2023
1 parent 1d84b3d commit ad7cf6e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
<!-- yaml -->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.0</version>
</dependency>

<!-- Test stuff -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public enum CommitIdPropertiesOutputFormat {
* Indicator to generate a xml file.
*/
XML,
/**
* Indicator to generate a yml file.
*/
YML,
}
11 changes: 10 additions & 1 deletion src/main/java/pl/project13/core/PropertiesFileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import pl.project13.core.util.JsonManager;
import pl.project13.core.util.PropertyManager;
import pl.project13.core.util.XmlManager;
import pl.project13.core.util.YmlManager;

import javax.annotation.Nonnull;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.Properties;

Expand Down Expand Up @@ -76,6 +76,10 @@ public void maybeGeneratePropertiesFile(
log.info(String.format("Reading existing xml file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
persistedProperties = XmlManager.readXmlProperties(gitPropsFile, sourceCharset);
break;
case YML:
log.info(String.format("Reading existing yml file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
persistedProperties = YmlManager.readYmlProperties(gitPropsFile, sourceCharset);
break;
default:
throw new GitCommitIdExecutionException("Not implemented:" + propertiesOutputFormat);
}
Expand Down Expand Up @@ -115,6 +119,11 @@ public void maybeGeneratePropertiesFile(
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
XmlManager.dumpXml(outputStream, sortedLocalProperties, sourceCharset);
break;
case YML:
log.info(String.format("Writing yml file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
YmlManager.dumpYml(outputStream, sortedLocalProperties, sourceCharset);
break;
default:
throw new GitCommitIdExecutionException("Not implemented:" + propertiesOutputFormat);
}
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/pl/project13/core/util/YmlManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski <[email protected]>
*
* git-commit-id-plugin-core is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-plugin-core is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-plugin-core. If not, see <http://www.gnu.org/licenses/>.
*/

package pl.project13.core.util;

import nu.studer.java.util.OrderedProperties;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import pl.project13.core.CannotReadFileException;

import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class YmlManager {
public static void dumpYml(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException {
try (Writer outputWriter = new OutputStreamWriter(outputStream, sourceCharset)) {
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setAllowUnicode(true);
dumperOptions.setAllowReadOnlyProperties(true);
dumperOptions.setPrettyFlow(true);
// dumperOptions.setCanonical(true);
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO);

Yaml yaml = new Yaml(dumperOptions);

Map<String, Object> dataMap = new HashMap<>();
for (Map.Entry<String, String> e: sortedLocalProperties.entrySet()) {
dataMap.put(e.getKey(), e.getValue());
}
yaml.dump(dataMap, outputWriter);
}
}

public static Properties readYmlProperties(@Nonnull File xmlFile, Charset sourceCharset) throws CannotReadFileException {
Properties retVal = new Properties();

try (FileInputStream fis = new FileInputStream(xmlFile)) {
try (InputStreamReader reader = new InputStreamReader(fis, sourceCharset)) {
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setAllowDuplicateKeys(false);
loaderOptions.setAllowRecursiveKeys(false);
loaderOptions.setProcessComments(false);
Yaml yaml = new Yaml(loaderOptions);
Map<String, Object> data = yaml.load(reader);
for (Map.Entry<String, Object> e: data.entrySet()) {
retVal.put(e.getKey(), e.getValue());
}
}
} catch (IOException e) {
throw new CannotReadFileException(e);
}
return retVal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pl.project13.core.git.GitDescribeConfig;
import pl.project13.core.util.JsonManager;
import pl.project13.core.util.XmlManager;
import pl.project13.core.util.YmlManager;

import javax.annotation.Nonnull;
import java.io.File;
Expand Down Expand Up @@ -492,6 +493,33 @@ public void shouldGenerateCustomPropertiesFileXml(boolean useNativeGit) throws E
Assert.assertEquals(p, properties);
}

@Test
@Parameters(method = "useNativeGit")
public void shouldGenerateCustomPropertiesFileYml(boolean useNativeGit) throws Exception {
// given
File dotGitDirectory = createTmpDotGitDirectory(AvailableGitTestRepo.WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS);

File targetFilePath = sandbox.resolve("custom-git.yml").toFile();
targetFilePath.delete();

GitCommitIdPlugin.Callback cb =
new GitCommitIdTestCallback()
.setDotGitDirectory(dotGitDirectory)
.setUseNativeGit(useNativeGit)
.setShouldGenerateGitPropertiesFile(true)
.setGenerateGitPropertiesFilename(targetFilePath)
.setPropertiesOutputFormat(CommitIdPropertiesOutputFormat.YML)
.build();
Properties properties = new Properties();

// when
GitCommitIdPlugin.runPlugin(cb, properties);
// then
assertThat(targetFilePath).exists();
Properties p = YmlManager.readYmlProperties(targetFilePath, StandardCharsets.UTF_8);
assertThat(p.size() > 10);
Assert.assertEquals(p, properties);
}

@Test
@Parameters(method = "useNativeGit")
Expand Down

0 comments on commit ad7cf6e

Please sign in to comment.