Skip to content

Commit 68ebc4e

Browse files
author
TheSnoozer
committed
introduce a generic file manager that allows to read / writer properties
1 parent 3e7019c commit 68ebc4e

File tree

3 files changed

+146
-101
lines changed

3 files changed

+146
-101
lines changed

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

Lines changed: 29 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,11 @@
1919

2020
import nu.studer.java.util.OrderedProperties;
2121
import pl.project13.core.log.LogInterface;
22-
import pl.project13.core.util.BuildFileChangeListener;
23-
import pl.project13.core.util.JsonManager;
24-
import pl.project13.core.util.PropertyManager;
25-
import pl.project13.core.util.XmlManager;
26-
import pl.project13.core.util.YmlManager;
22+
import pl.project13.core.util.*;
2723

2824
import javax.annotation.Nonnull;
2925
import java.io.*;
3026
import java.nio.charset.Charset;
31-
import java.nio.file.Files;
3227
import java.util.Comparator;
3328
import java.util.Properties;
3429

@@ -55,91 +50,37 @@ public void maybeGeneratePropertiesFile(
5550
Charset sourceCharset,
5651
boolean escapeUnicode
5752
) throws GitCommitIdExecutionException {
58-
try {
59-
final File gitPropsFile = craftPropertiesOutputFile(projectDir, propsFile);
60-
boolean shouldGenerate = true;
61-
62-
if (gitPropsFile.exists()) {
63-
final Properties persistedProperties;
64-
65-
try {
66-
switch (propertiesOutputFormat) {
67-
case JSON:
68-
log.info(String.format("Reading existing json file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
69-
persistedProperties = JsonManager.readJsonProperties(gitPropsFile, sourceCharset);
70-
break;
71-
case PROPERTIES:
72-
log.info(String.format("Reading existing properties file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
73-
persistedProperties = PropertyManager.readProperties(gitPropsFile);
74-
break;
75-
case XML:
76-
log.info(String.format("Reading existing xml file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
77-
persistedProperties = XmlManager.readXmlProperties(gitPropsFile, sourceCharset);
78-
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;
83-
default:
84-
throw new GitCommitIdExecutionException("Not implemented:" + propertiesOutputFormat);
85-
}
86-
87-
final Properties propertiesCopy = (Properties) localProperties.clone();
88-
89-
final String buildTimeProperty = prefixDot + GitCommitPropertyConstant.BUILD_TIME;
90-
91-
propertiesCopy.setProperty(buildTimeProperty, "");
92-
persistedProperties.setProperty(buildTimeProperty, "");
93-
94-
shouldGenerate = !propertiesCopy.equals(persistedProperties);
95-
} catch (CannotReadFileException ex) {
96-
// Read has failed, regenerate file
97-
log.info(String.format("Cannot read properties file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
98-
shouldGenerate = true;
99-
}
53+
final File gitPropsFile = craftPropertiesOutputFile(projectDir, propsFile);
54+
boolean shouldGenerate = true;
55+
56+
if (gitPropsFile.exists()) {
57+
final Properties persistedProperties;
58+
try {
59+
persistedProperties = GenericFileManager.readProperties(
60+
log, propertiesOutputFormat, gitPropsFile, sourceCharset, projectName);
61+
final Properties propertiesCopy = (Properties) localProperties.clone();
62+
63+
final String buildTimeProperty = prefixDot + GitCommitPropertyConstant.BUILD_TIME;
64+
65+
propertiesCopy.setProperty(buildTimeProperty, "");
66+
persistedProperties.setProperty(buildTimeProperty, "");
67+
68+
shouldGenerate = !propertiesCopy.equals(persistedProperties);
69+
} catch (GitCommitIdExecutionException e) {
70+
log.info(e.getMessage());
71+
shouldGenerate = true;
10072
}
73+
}
74+
75+
if (shouldGenerate) {
76+
GenericFileManager.dumpProperties(
77+
log, propertiesOutputFormat, gitPropsFile, sourceCharset, escapeUnicode, projectName, localProperties);
10178

102-
if (shouldGenerate) {
103-
Files.createDirectories(gitPropsFile.getParentFile().toPath());
104-
try (OutputStream outputStream = new FileOutputStream(gitPropsFile)) {
105-
OrderedProperties sortedLocalProperties = PropertiesFileGenerator.createOrderedProperties();
106-
localProperties.forEach((key, value) -> sortedLocalProperties.setProperty((String) key, (String) value));
107-
switch (propertiesOutputFormat) {
108-
case JSON:
109-
log.info(String.format("Writing json file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
110-
JsonManager.dumpJson(outputStream, sortedLocalProperties, sourceCharset);
111-
break;
112-
case PROPERTIES:
113-
log.info(String.format("Writing properties file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
114-
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
115-
PropertyManager.dumpProperties(outputStream, sortedLocalProperties, escapeUnicode);
116-
break;
117-
case XML:
118-
log.info(String.format("Writing xml file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
119-
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
120-
XmlManager.dumpXml(outputStream, sortedLocalProperties, sourceCharset);
121-
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;
127-
default:
128-
throw new GitCommitIdExecutionException("Not implemented:" + propertiesOutputFormat);
129-
}
130-
} catch (final IOException ex) {
131-
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
132-
}
133-
134-
if (buildFileChangeListener != null) {
135-
buildFileChangeListener.changed(gitPropsFile);
136-
}
137-
138-
} else {
139-
log.info(String.format("Properties file [%s] is up-to-date (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
79+
if (buildFileChangeListener != null) {
80+
buildFileChangeListener.changed(gitPropsFile);
14081
}
141-
} catch (IOException e) {
142-
throw new GitCommitIdExecutionException(e);
82+
} else {
83+
log.info(String.format("Properties file [%s] is up-to-date (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
14384
}
14485
}
14586

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 pl.project13.core.CannotReadFileException;
22+
import pl.project13.core.CommitIdPropertiesOutputFormat;
23+
import pl.project13.core.GitCommitIdExecutionException;
24+
import pl.project13.core.PropertiesFileGenerator;
25+
import pl.project13.core.log.LogInterface;
26+
27+
import java.io.File;
28+
import java.io.FileOutputStream;
29+
import java.io.IOException;
30+
import java.io.OutputStream;
31+
import java.nio.charset.Charset;
32+
import java.nio.file.Files;
33+
import java.util.Properties;
34+
35+
public class GenericFileManager {
36+
public static Properties readProperties(
37+
LogInterface log,
38+
CommitIdPropertiesOutputFormat propertiesOutputFormat,
39+
File gitPropsFile,
40+
Charset sourceCharset,
41+
String projectName
42+
) throws GitCommitIdExecutionException {
43+
final Properties persistedProperties;
44+
45+
try {
46+
switch (propertiesOutputFormat) {
47+
case JSON:
48+
log.info(String.format("Reading existing json file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
49+
persistedProperties = JsonManager.readJsonProperties(gitPropsFile, sourceCharset);
50+
break;
51+
case PROPERTIES:
52+
log.info(String.format("Reading existing properties file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
53+
persistedProperties = PropertyManager.readProperties(gitPropsFile);
54+
break;
55+
case XML:
56+
log.info(String.format("Reading existing xml file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
57+
persistedProperties = XmlManager.readXmlProperties(gitPropsFile, sourceCharset);
58+
break;
59+
case YML:
60+
log.info(String.format("Reading existing yml file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
61+
persistedProperties = YmlManager.readYmlProperties(gitPropsFile, sourceCharset);
62+
break;
63+
default:
64+
throw new GitCommitIdExecutionException("Not implemented:" + propertiesOutputFormat);
65+
}
66+
} catch (final CannotReadFileException ex) {
67+
// Read has failed, regenerate file
68+
throw new GitCommitIdExecutionException(
69+
String.format("Cannot read properties file [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
70+
}
71+
return persistedProperties;
72+
}
73+
74+
public static void dumpProperties(
75+
LogInterface log,
76+
CommitIdPropertiesOutputFormat propertiesOutputFormat,
77+
File gitPropsFile,
78+
Charset sourceCharset,
79+
boolean escapeUnicode,
80+
String projectName,
81+
Properties propertiesToDump
82+
) throws GitCommitIdExecutionException {
83+
try {
84+
Files.createDirectories(gitPropsFile.getParentFile().toPath());
85+
try (final OutputStream outputStream = new FileOutputStream(gitPropsFile)) {
86+
OrderedProperties sortedLocalProperties = PropertiesFileGenerator.createOrderedProperties();
87+
propertiesToDump.forEach((key, value) -> sortedLocalProperties.setProperty((String) key, (String) value));
88+
switch (propertiesOutputFormat) {
89+
case JSON:
90+
log.info(String.format("Writing json file to [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
91+
JsonManager.dumpJson(outputStream, sortedLocalProperties, sourceCharset);
92+
break;
93+
case PROPERTIES:
94+
log.info(String.format("Writing properties file to [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
95+
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
96+
PropertyManager.dumpProperties(outputStream, sortedLocalProperties, escapeUnicode);
97+
break;
98+
case XML:
99+
log.info(String.format("Writing xml file to [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
100+
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
101+
XmlManager.dumpXml(outputStream, sortedLocalProperties, sourceCharset);
102+
break;
103+
case YML:
104+
log.info(String.format("Writing yml file to [%s] (for project %s)...", gitPropsFile.getAbsolutePath(), projectName));
105+
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
106+
YmlManager.dumpYml(outputStream, sortedLocalProperties, sourceCharset);
107+
break;
108+
default:
109+
throw new GitCommitIdExecutionException("Not implemented:" + propertiesOutputFormat);
110+
}
111+
}
112+
} catch (final IOException ex) {
113+
throw new GitCommitIdExecutionException("Cannot create custom git properties file: " + gitPropsFile, ex);
114+
}
115+
}
116+
}

src/main/java/pl/project13/core/util/PropertyManager.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@
1919

2020
import nu.studer.java.util.OrderedProperties;
2121
import pl.project13.core.CannotReadFileException;
22-
import pl.project13.core.PropertiesFileGenerator;
2322

2423
import javax.annotation.Nonnull;
2524
import javax.annotation.Nullable;
2625
import java.io.*;
2726
import java.nio.charset.Charset;
2827
import java.nio.charset.StandardCharsets;
29-
import java.nio.file.Files;
3028
import java.util.Map;
3129
import java.util.Properties;
3230

33-
public class PropertyManager {
31+
public class PropertyManager {
3432
public static void putWithoutPrefix(@Nonnull Properties properties, String key, String value) {
3533
if (!isNotEmpty(value)) {
3634
value = "Unknown";
@@ -57,16 +55,6 @@ public static Properties readProperties(@Nonnull File propertiesFile, @Nonnull C
5755
}
5856
}
5957

60-
// For now used by the gradle plugin to expose properties to the project
61-
public static void dumpProperties(File gitPropsFile, Properties properties, boolean escapeUnicode) throws IOException {
62-
Files.createDirectories(gitPropsFile.getParentFile().toPath());
63-
try (OutputStream outputStream = new FileOutputStream(gitPropsFile)) {
64-
OrderedProperties sortedLocalProperties = PropertiesFileGenerator.createOrderedProperties();
65-
properties.forEach((key, value) -> sortedLocalProperties.setProperty((String) key, (String) value));
66-
PropertyManager.dumpProperties(outputStream, sortedLocalProperties, escapeUnicode);
67-
}
68-
}
69-
7058
public static void dumpProperties(OutputStream outputStream, OrderedProperties sortedLocalProperties, boolean escapeUnicode) throws IOException {
7159
try (Writer outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
7260
// use the OrderedProperties.store(Writer, ...)-method to avoid illegal reflective access warning

0 commit comments

Comments
 (0)