Skip to content

Commit

Permalink
feat: add ability to define default or overwrite values for version a…
Browse files Browse the repository at this point in the history
…nd property format
  • Loading branch information
Bengt Brodersen committed Feb 27, 2021
1 parent 6eaaa00 commit af4a9db
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

# Changelog

## 6.2.0
* **Features**
* add ability to define default or overwrite values for version and property format.
* default value if parameter value is not set `${paramter:-<DEFAULT_VALUE>}` e.g. `${buildNumber:-0}`
* overwrite value if parameter has a value `${paramter:+<OVERWRITE_VALUE>}` e.g. `${dirty:+-SNAPSHOT}`

## 6.1.1
* **Fixes**
* fixed wrong dependency management version updates.
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Maven Git Versioning Extension
# Maven Git Versioning Extension [![Sparkline](https://stars.medv.io/qoomon/maven-git-versioning-extension.svg?)](https://stars.medv.io/qoomon/maven-git-versioning-extension)

[![Maven Central](https://img.shields.io/maven-central/v/me.qoomon/maven-git-versioning-extension.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22me.qoomon%22%20AND%20a%3A%22maven-git-versioning-extension%22)
[![Changelog](https://badgen.net/badge/changelog/%E2%98%85/blue)](CHANGELOG.md)
Expand Down Expand Up @@ -113,6 +113,12 @@ Create `${basedir}/.mvn/maven-git-versioning-extension.xml`.

ℹ whole `versionFormat` will be slugified automatically, that means all `/` characters replaced by `-`

ℹ define placeholder default value (placeholder is not defined) like this `${name:-default_value}`<br>
e.g `${buildNumber:-0}` or `${buildNumber:-local}`

ℹ define placeholder overwrite value (placeholder is defined) like this `${name:+overwrite_value}`<br>
e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`

- `${ref}`
- current ref name (branch name, tag name or commit hash)
- `${ref.slug}`
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>me.qoomon</groupId>
<artifactId>maven-git-versioning-extension</artifactId>
<version>6.1.1</version>
<version>6.2.0</version>
<packaging>maven-plugin</packaging>

<name>Maven Git Versioning Extension</name>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/me/qoomon/gitversioning/commons/GitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public static List<String> tag_pointsAt(Repository repository, String revstr) th
String tagName = ref.getName().replaceFirst("^" + R_TAGS, "");
tagNames.add(tagName);
}
System.out.println("");
}
return tagNames;
}
Expand Down
32 changes: 21 additions & 11 deletions src/main/java/me/qoomon/gitversioning/commons/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@

public final class StringUtil {

public static String substituteText(String text, Map<String, String> substitutionMap) {
String result = text;

final Pattern placeholderPattern = Pattern.compile("\\$\\{(.+?)}");
final Matcher placeholderMatcher = placeholderPattern.matcher(text);
public static String substituteText(String text, Map<String, String> replacements) {
StringBuffer result = new StringBuffer();
Pattern placeholderPattern = Pattern.compile("\\$\\{(?<key>[^}:]+)(?::(?<modifier>[-+])(?<value>[^}]*))?}");
Matcher placeholderMatcher = placeholderPattern.matcher(text);
while (placeholderMatcher.find()) {
String substitutionKey = placeholderMatcher.group(1);
String substitutionValue = substitutionMap.get(substitutionKey);
if(substitutionValue != null) {
result = result.replaceAll("\\$\\{" + substitutionKey + "}", substitutionValue.replace("$", "\\$"));
String placeholderKey = placeholderMatcher.group("key");
String replacement = replacements.get(placeholderKey);
String placeholderModifier = placeholderMatcher.group("modifier");
if(placeholderModifier != null){
if (placeholderModifier.equals("-") && replacement == null) {
replacement = placeholderMatcher.group("value");
}
if (placeholderModifier.equals("+") && replacement != null) {
replacement = placeholderMatcher.group("value");
}
}
if (replacement != null) {
// avoid group name replacement behaviour of replacement parameter value
placeholderMatcher.appendReplacement(result, "");
result.append(replacement);
}
}

return result;
placeholderMatcher.appendTail(result);
return result.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.emptyMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

Expand Down Expand Up @@ -39,7 +41,7 @@ void substituteText_missingValue() {
}

@Test
void substituteText_escape_replacement_value() {
void substituteText_handle_replacement_value_with_placeholder_syntax() {

// Given
String givenText = "${version}";
Expand All @@ -53,6 +55,35 @@ void substituteText_escape_replacement_value() {
assertThat(outputText).isEqualTo("${something}");
}

@Test
void substituteText_default_value() {

// Given
String givenText = "${foo:-xxx}";
Map<String, String> givenSubstitutionMap = emptyMap();

// When
String outputText = StringUtil.substituteText(givenText, givenSubstitutionMap);

// Then
assertThat(outputText).isEqualTo("xxx");
}

@Test
void substituteText_overwrite_value() {

// Given
String givenText = "${foo:+xxx}";
Map<String, String> givenSubstitutionMap = new HashMap<>();
givenSubstitutionMap.put("foo", "aaa");

// When
String outputText = StringUtil.substituteText(givenText, givenSubstitutionMap);

// Then
assertThat(outputText).isEqualTo("xxx");
}


@Test
void valueGroupMap() {
Expand Down

0 comments on commit af4a9db

Please sign in to comment.