-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[crypt][properties] option to keep properties comments in output files
- Loading branch information
1 parent
6baca33
commit 2f76dd9
Showing
10 changed files
with
308 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
yupiik-tools-maven-plugin/src/main/java/io/yupiik/maven/properties/LightProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright (c) 2020 - 2023 - Yupiik SAS - https://www.yupiik.com | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.yupiik.maven.properties; | ||
|
||
import lombok.Data; | ||
import org.apache.maven.plugin.logging.Log; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.stream.Collector; | ||
|
||
/** | ||
* This class intends to be able to load properties but keep the original layout as much as possible | ||
* and rewrite it with the comments in place. | ||
*/ | ||
public class LightProperties { | ||
private final Log log; | ||
private final List<Line> lines = new ArrayList<>(); | ||
private final Properties properties = new Properties(); | ||
|
||
public LightProperties(final Log log) { | ||
this.log = log; | ||
} | ||
|
||
public void load(final BufferedReader reader, final boolean usePlainProperties) throws IOException { | ||
if (usePlainProperties) { | ||
properties.load(reader); | ||
} else { | ||
doLoad(reader); | ||
} | ||
} | ||
|
||
public Properties toWorkProperties() { | ||
return lines.isEmpty() ? properties : lines.stream() | ||
.filter(it -> it.key != null) | ||
.collect(Collector.of(Properties::new, (p, l) -> p.setProperty(l.key, l.value), (p1, p2) -> { | ||
p1.putAll(p2); | ||
return p1; | ||
})); | ||
} | ||
|
||
public void write(final Properties transformed, final Writer outputWriter) throws IOException { | ||
for (final var line : lines) { | ||
if (line.key == null) { | ||
outputWriter.write(line.comment + '\n'); | ||
} else { | ||
final var newValue = transformed.getProperty(line.key); | ||
if (newValue == null) { | ||
log.warn("Missing value for '" + line.key + "'"); | ||
continue; // ignore | ||
} | ||
|
||
// ensure we encode right | ||
final var tmp = new Properties(); | ||
tmp.setProperty(line.key, newValue); | ||
final var writer = new StringWriter(); | ||
try (final var out = new SimplePropertiesWriter(writer)) { | ||
tmp.store(out, "ignored"); | ||
} | ||
outputWriter.write((line.comment != null && !line.comment.isBlank() ? line.comment + '\n' : "") + writer.toString().strip() + '\n'); | ||
} | ||
} | ||
} | ||
|
||
private void doLoad(final BufferedReader reader) { | ||
final var iterator = reader.lines().iterator(); | ||
final var comments = new ArrayList<String>(); | ||
while (iterator.hasNext()) { | ||
final var line = iterator.next(); | ||
if (line.startsWith("#")) { | ||
comments.add(line); | ||
} else if (line.strip().isBlank()) { | ||
if (!comments.isEmpty()) { | ||
lines.add(new Line(String.join("\n", comments), null, null, null)); | ||
comments.clear(); | ||
} | ||
lines.add(new Line(line, null, null, null)); | ||
} else { | ||
boolean found = false; | ||
for (int i = 0; i < line.length(); i++) { | ||
switch (line.charAt(i)) { | ||
case '\\': | ||
i++; // escaped so don't check next char, can't be a separator | ||
break; | ||
case ':': | ||
case '=': | ||
var value = line.substring(i + 1); | ||
if (value.endsWith("\\") && iterator.hasNext()) { | ||
value += '\n' + iterator.next(); | ||
} | ||
lines.add(new Line( | ||
String.join("\n", comments), | ||
line.substring(0, i).strip(), | ||
Character.toString(line.charAt(i)), | ||
value.strip())); | ||
comments.clear(); | ||
found = true; | ||
break; | ||
default: | ||
} | ||
if (found) { | ||
break; | ||
} | ||
} | ||
if (!found) { | ||
lines.add(new Line(line, null, null, null)); | ||
} | ||
} | ||
} | ||
if (!comments.isEmpty()) { | ||
lines.add(new Line(String.join("\n", comments), null, null, null)); | ||
} | ||
} | ||
|
||
@Data | ||
private static class Line { | ||
private final String comment; | ||
private final String key; | ||
private final String separator; | ||
private final String value; | ||
} | ||
|
||
public static class SimplePropertiesWriter extends BufferedWriter { | ||
private int remainingNewLinesToSkip = 2; | ||
|
||
public SimplePropertiesWriter(final Writer outputWriter) { | ||
super(outputWriter); | ||
} | ||
|
||
@Override | ||
public void write(final String str) throws IOException { | ||
if (remainingNewLinesToSkip == 0) { | ||
super.write(str); | ||
} | ||
} | ||
|
||
@Override | ||
public void newLine() throws IOException { | ||
if (remainingNewLinesToSkip > 0) { | ||
remainingNewLinesToSkip--; | ||
} else { | ||
super.newLine(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.