Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #8 from palantir/feature/kniktas_patterns
Browse files Browse the repository at this point in the history
Add option to enforce a pattern for the domain
  • Loading branch information
kostasniktas committed May 13, 2015
2 parents 762fa7d + 0c6a425 commit 31a0f9d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,23 @@ If your build infrastructure has a HEAD value set, then Flex Version will just w
In this example, we assume we have two build infrastructures. The first is using some kind of Gerrit plugin that sets the branch in `GERRIT_BRANCH` with the form `gerrit/BRANCHNAME`. The second is Bamboo and we assume it sets the environment variable `BAMBOO_REPO_BRANCH`.


apply plugin: 'gradle-flexversion'
version flexVersion()

flexversion {
envvarSources << "GERRIT_BRANCH" << "BAMBOO_REPO_BRANCH"
stripRefs << "gerrit/"
}

Flex Version will first check for `FLEX_VERSION_DOMAIN_OVERRIDE`, the tag condition, and then the environment variables `GERRIT_BRANCH` and `BAMBOO_REPO_BRANCH` for a branch value. If it finds it in one of the environment variables we passed in, it will strip off the `gerrit/` at the beginning. The property `stripRefs` has a default value of `["refs/tags/", refs/heads/", "origin/"]`. The order values to `envvarSources` is also the order of priority (decreasing). It will pick the first one it finds.

## Enforcing certain domains

While Flex Versioning is all about allowing almost any domain, it still provides a way to enforce a pattern. There is an extra property in the `flexversion` closure that will take a Java/Groovy Pattern and enforce that the domain matches it. For example, semver.org Version 2 can be matched with `~/([0-9]|(?:[1-9]\d*))\.([0-9]|(?:[1-9]\d*))\.([0-9]|(?:[1-9]\d*))(?:\-([a-zA-Z1-9][a-zA-Z0-9-]*(?:\.[a-zA-Z1-9][a-zA-Z0-9-]*)*))?/`.

flexversion {
domainPattern = ~/([0-9]|(?:[1-9]\d*))\.([0-9]|(?:[1-9]\d*))\.([0-9]|(?:[1-9]\d*))(?:\-([a-zA-Z1-9][a-zA-Z0-9-]*(?:\.[a-zA-Z1-9][a-zA-Z0-9-]*)*))?/
}

Before returning the version string, if the found domain doesn't match that lovely pattern, it will fail the build.

# LICENSE

Gradle Flex Version is released by Palantir Technologies, Inc. under the Apache 2.0 License. see the included LICENSE file for details.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.palantir.gradle.versions.flexversioning
class FlexVersionExtension {
List envvarSources = [];
List stripRefs = ["refs/tags/", "refs/heads/", "origin/"];
java.util.regex.Pattern domainPattern = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ class FlexVersionPlugin implements Plugin<Project> {
* 6. unspecified
*/
String domain = "unspecified";
boolean tag = false;
if (System.env[DOMAIN_OVERRIDE_PROPERTY] != null) {
domain = System.env[DOMAIN_OVERRIDE_PROPERTY];
} else if (System.env[DOMAIN_TAG_PROPERTY] != null && domainIfTag != null) {
domain = domainIfTag;
return "${domain}${dirty}";
tag = true;
} else if (envvarDomain != null) {
domain = envvarDomain;
} else if (userDomain != null && !userDomain.trim().isEmpty()) {
Expand All @@ -130,6 +131,19 @@ class FlexVersionPlugin implements Plugin<Project> {
domain = targetName.replaceAll("/", "-");
}


// Check if the domain matches the required pattern
if (flexExtension.domainPattern != null) {
if (!(flexExtension.domainPattern.matcher(domain).matches())) {
throw new Exception("Domain [${domain}] does not match the required pattern ${flexExtension.domainPattern}.");
}
}


if (tag) {
return "${domain}${dirty}"
}

return "${domain}-${commitCount}-g${headSha1.substring(0,12)}${dirty}";
}

Expand Down

0 comments on commit 31a0f9d

Please sign in to comment.