From 56837281143bbf3cb55bf4bf926eb75aee44a069 Mon Sep 17 00:00:00 2001 From: Ivan Grund 601625 Date: Mon, 14 Oct 2024 18:31:09 +0200 Subject: [PATCH] feat(StringUtil): implement format placeholders closer to _posix parameter expansion_ - i.e. implement optional colon --- README.md | 8 ++++---- .../java/me/qoomon/gitversioning/commons/StringUtil.java | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8b2a4af8..3e3783eb 100644 --- a/README.md +++ b/README.md @@ -156,11 +156,11 @@ You can configure the version and properties adjustments for specific branches a ℹ Final `version` will be slugified automatically, so no need to use `${….slug}` placeholders in `` format. -ℹ define placeholder default value (placeholder is not defined) like this `${name:-DEFAULT_VALUE}`
-e.g `${env.BUILD_NUMBER:-0}` or `${env.BUILD_NUMBER:-local}` +ℹ define placeholder default value (placeholder is not defined '-' or empty ':-') like this `${name:-DEFAULT_VALUE}`
+e.g `${env.BUILD_NUMBER-0}` or `${env.BUILD_NUMBER:-local}` -ℹ define placeholder overwrite value (placeholder is defined) like this `${name:+OVERWRITE_VALUE}`
-e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY` +ℹ define placeholder overwrite value (placeholder is defined '+' and non-empty ':+' ) like this `${name:+OVERWRITE_VALUE}`
+e.g `${dirty:+SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY` ###### Placeholders diff --git a/src/main/java/me/qoomon/gitversioning/commons/StringUtil.java b/src/main/java/me/qoomon/gitversioning/commons/StringUtil.java index fda329d3..f4a1a5e1 100644 --- a/src/main/java/me/qoomon/gitversioning/commons/StringUtil.java +++ b/src/main/java/me/qoomon/gitversioning/commons/StringUtil.java @@ -12,7 +12,7 @@ public final class StringUtil { public static String substituteText(String text, Map> replacements) { StringBuffer result = new StringBuffer(); - Pattern placeholderPattern = Pattern.compile("\\$\\{(?[^}:]+)(?::(?[-+])(?[^}]*))?}"); + Pattern placeholderPattern = Pattern.compile("\\$\\{(?[^}:]+)(?:?[-+])?(?[^}]*)?}"); Matcher placeholderMatcher = placeholderPattern.matcher(text); while (placeholderMatcher.find()) { String placeholderKey = placeholderMatcher.group("key"); @@ -26,6 +26,12 @@ public static String substituteText(String text, Map> r if (placeholderModifier.equals("+") && replacement != null) { replacement = placeholderMatcher.group("value"); } + if (placeholderModifier.equals(":-") && (replacement == null || replacement.isEmpty())) { + replacement = placeholderMatcher.group("value"); + } + if (placeholderModifier.equals(":+") && replacement != null && !replacement.isEmpty()) { + replacement = placeholderMatcher.group("value"); + } } if (replacement != null) { // avoid group name replacement behaviour of replacement parameter value