mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 02:14:44 +00:00
Config import: add support for environment variables
This commit is contained in:
1 parent
df4501a648
commit
0e583c0122
8 files changed
+61
-10
No files matched your search
@@ -27,13 +27,19 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public abstract class ConfigurationFile {
|
||||
private static final Pattern VARIABLE = Pattern.compile("\\<(.+?)\\>");
|
||||
private static final String LOMBOK_CONFIG_FILENAME = "lombok.config";
|
||||
private static final Map<String, String> ENV = new HashMap<String, String>(System.getenv());;
|
||||
|
||||
private static final ThreadLocal<byte[]> buffers = new ThreadLocal<byte[]>() {
|
||||
protected byte[] initialValue() {
|
||||
@@ -41,6 +47,10 @@ public abstract class ConfigurationFile {
|
||||
}
|
||||
};
|
||||
|
||||
static void setEnvironment(String key, String value) {
|
||||
ENV.put(key, value);
|
||||
}
|
||||
|
||||
private final String identifier;
|
||||
|
||||
public static ConfigurationFile forFile(File file) {
|
||||
@@ -48,7 +58,7 @@ public abstract class ConfigurationFile {
|
||||
}
|
||||
|
||||
public static ConfigurationFile forDirectory(File directory) {
|
||||
return ConfigurationFile.forFile(new File(directory, LOMBOK_CONFIG_FILENAME));
|
||||
return forFile(new File(directory, LOMBOK_CONFIG_FILENAME));
|
||||
}
|
||||
|
||||
public static ConfigurationFile fromCharSequence(String identifier, CharSequence contents, long lastModified) {
|
||||
@@ -117,7 +127,7 @@ public abstract class ConfigurationFile {
|
||||
if (parts.length > 2) return null;
|
||||
|
||||
String realFileName = parts[0];
|
||||
File file = resolveFile(realFileName);
|
||||
File file = resolveFile(replaceEnvironmentVariables(realFileName));
|
||||
if (realFileName.endsWith(".zip") || realFileName.endsWith(".jar")) {
|
||||
try {
|
||||
return ArchivedConfigurationFile.create(file, URI.create(parts.length == 1 ? LOMBOK_CONFIG_FILENAME : parts[1]));
|
||||
@@ -166,6 +176,24 @@ public abstract class ConfigurationFile {
|
||||
File parent = file.getParentFile().getParentFile();
|
||||
return parent == null ? null : forDirectory(parent);
|
||||
}
|
||||
|
||||
private static String replaceEnvironmentVariables(String fileName) {
|
||||
int start = 0;;
|
||||
StringBuffer result = new StringBuffer();
|
||||
if (fileName.startsWith("~")) {
|
||||
start = 1;
|
||||
result.append(System.getProperty("user.home", "~"));
|
||||
}
|
||||
Matcher matcher = VARIABLE.matcher(fileName.substring(start));
|
||||
while (matcher.find()) {
|
||||
String key = matcher.group(1);
|
||||
String value = ENV.get(key);
|
||||
if (value == null) value = "<" + key + ">";
|
||||
matcher.appendReplacement(result, value);
|
||||
}
|
||||
matcher.appendTail(result);
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private static class ArchivedConfigurationFile extends ConfigurationFile {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
lombok.accessors.prefix += x_
|
||||
@@ -1,6 +1,9 @@
|
||||
import missing
|
||||
import import1.config
|
||||
import ../d111/import1.config
|
||||
import ../../../archives/a1.jar
|
||||
lombok.accessors.prefix += z_
|
||||
import missing
|
||||
import import1.config
|
||||
import ../d111/import1.config
|
||||
import ../../../archives/a1.jar
|
||||
import directory
|
||||
import <env>/environment.config
|
||||
import ~/home.config
|
||||
lombok.accessors.prefix += z_
|
||||
lombok.accessors.prefix += f
|
||||
@@ -0,0 +1 @@
|
||||
lombok.accessors.prefix += e_
|
||||
@@ -1,4 +1,5 @@
|
||||
Problems in the configuration files:
|
||||
- Imported file does not exist: import missing (BASE/d1/d11/d111/import1.config:1)
|
||||
- Imported file does not exist: import directory (BASE/d1/d11/d111/import1.config:5)
|
||||
- Import is not valid: import /illegal (BASE/archives/a1.jar!lombok.config:2)
|
||||
- Imported file does not exist: import nonexisting (BASE/archives/a1.jar!d1/include.config:2)
|
||||
@@ -0,0 +1 @@
|
||||
lombok.accessors.prefix += h_
|
||||
@@ -51,15 +51,23 @@ clear lombok.accessors.chain
|
||||
# 3: clear lombok.accessors.chain
|
||||
|
||||
# Strip this field prefix, like 'f' or 'm_', from the names of generated getters and setters.
|
||||
lombok.accessors.prefix += e_
|
||||
lombok.accessors.prefix += h_
|
||||
lombok.accessors.prefix += z_
|
||||
lombok.accessors.prefix += f
|
||||
lombok.accessors.prefix += m_
|
||||
# BASE/d1/d11/lombok.config (stopped bubbling):
|
||||
# 5: lombok.accessors.prefix += f
|
||||
#
|
||||
# BASE/e1/environment.config (imported from BASE/d1/d11/d111/import1.config:6):
|
||||
# 1: lombok.accessors.prefix += e_
|
||||
#
|
||||
# BASE/home/home.config (imported from BASE/d1/d11/d111/import1.config:7):
|
||||
# 1: lombok.accessors.prefix += h_
|
||||
#
|
||||
# BASE/d1/d11/d111/import1.config (imported from BASE/d1/d11/d111/lombok.config:1):
|
||||
# 5: lombok.accessors.prefix += z_
|
||||
# 6: lombok.accessors.prefix += f
|
||||
# 8: lombok.accessors.prefix += z_
|
||||
# 9: lombok.accessors.prefix += f
|
||||
#
|
||||
# BASE/d1/d11/d111/lombok.config:
|
||||
# 4: lombok.accessors.prefix += m_
|
||||
|
||||
@@ -52,7 +52,15 @@ public class TestConfiguration {
|
||||
PrintStream outStream = new PrintStream(rawOut);
|
||||
PrintStream errStream = new PrintStream(rawErr);
|
||||
|
||||
int result = new ConfigurationApp().redirectOutput(outStream, errStream).display(keys, true, paths, true, false);
|
||||
ConfigurationFile.setEnvironment("env", normalizedName + "/e1");
|
||||
String userHome = System.getProperty("user.home");
|
||||
int result = -1;
|
||||
try {
|
||||
System.setProperty("user.home", normalizedName + "/home");
|
||||
result = new ConfigurationApp().redirectOutput(outStream, errStream).display(keys, true, paths, true, false);
|
||||
} finally {
|
||||
System.setProperty("user.home", userHome);
|
||||
}
|
||||
|
||||
outStream.flush();
|
||||
errStream.flush();
|
||||
|
||||
Reference in new issue
Block a user