diff options
author | Roel Spilker <r.spilker@gmail.com> | 2020-01-18 01:58:38 +0100 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2020-01-20 14:12:03 +0100 |
commit | 0e583c0122d18285b858ec8cecc2e91a4f4feda0 (patch) | |
tree | d4d133231963f08f48f43621cad61e7dab7ce50b | |
parent | df4501a648f32111c6ee3ace1c826b3d3eef55a1 (diff) | |
download | lombok-0e583c0122d18285b858ec8cecc2e91a4f4feda0.tar.gz lombok-0e583c0122d18285b858ec8cecc2e91a4f4feda0.tar.bz2 lombok-0e583c0122d18285b858ec8cecc2e91a4f4feda0.zip |
Config import: add support for environment variables
8 files changed, 61 insertions, 10 deletions
diff --git a/src/core/lombok/core/configuration/ConfigurationFile.java b/src/core/lombok/core/configuration/ConfigurationFile.java index bb15d177..a6a3cb76 100644 --- a/src/core/lombok/core/configuration/ConfigurationFile.java +++ b/src/core/lombok/core/configuration/ConfigurationFile.java @@ -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 { diff --git a/test/configuration/resource/configurationRoot/d1/d11/d111/directory/lombok.config b/test/configuration/resource/configurationRoot/d1/d11/d111/directory/lombok.config new file mode 100644 index 00000000..bc262c59 --- /dev/null +++ b/test/configuration/resource/configurationRoot/d1/d11/d111/directory/lombok.config @@ -0,0 +1 @@ +lombok.accessors.prefix += x_
\ No newline at end of file diff --git a/test/configuration/resource/configurationRoot/d1/d11/d111/import1.config b/test/configuration/resource/configurationRoot/d1/d11/d111/import1.config index e94bd991..02e6cfae 100644 --- a/test/configuration/resource/configurationRoot/d1/d11/d111/import1.config +++ b/test/configuration/resource/configurationRoot/d1/d11/d111/import1.config @@ -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
\ No newline at end of file diff --git a/test/configuration/resource/configurationRoot/e1/environment.config b/test/configuration/resource/configurationRoot/e1/environment.config new file mode 100644 index 00000000..1436021c --- /dev/null +++ b/test/configuration/resource/configurationRoot/e1/environment.config @@ -0,0 +1 @@ +lombok.accessors.prefix += e_
\ No newline at end of file diff --git a/test/configuration/resource/configurationRoot/err.txt b/test/configuration/resource/configurationRoot/err.txt index c17a6dea..4505dced 100644 --- a/test/configuration/resource/configurationRoot/err.txt +++ b/test/configuration/resource/configurationRoot/err.txt @@ -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)
\ No newline at end of file diff --git a/test/configuration/resource/configurationRoot/home/home.config b/test/configuration/resource/configurationRoot/home/home.config new file mode 100644 index 00000000..4a190b56 --- /dev/null +++ b/test/configuration/resource/configurationRoot/home/home.config @@ -0,0 +1 @@ +lombok.accessors.prefix += h_
\ No newline at end of file diff --git a/test/configuration/resource/configurationRoot/out.txt b/test/configuration/resource/configurationRoot/out.txt index 19dc1e2d..bcb468c2 100644 --- a/test/configuration/resource/configurationRoot/out.txt +++ b/test/configuration/resource/configurationRoot/out.txt @@ -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_ diff --git a/test/configuration/src/lombok/core/configuration/TestConfiguration.java b/test/configuration/src/lombok/core/configuration/TestConfiguration.java index 22e3cfe6..3032daf3 100644 --- a/test/configuration/src/lombok/core/configuration/TestConfiguration.java +++ b/test/configuration/src/lombok/core/configuration/TestConfiguration.java @@ -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(); |