Skip to content

Request: replace string-based templating with lambda parameters, so I can programatically create the version #70

@aSemy

Description

@aSemy

At the moment the version is constructed based on a static string template. The plugin will do a find/replace in the version string for format placeholders, and replace them with existing format placeholder values.

However I would like to dynamically select the version, based on the format placeholder values. For example:

If CODEBUILD_WEBHOOK_TRIGGER is not available, then use the 'ref' placeholder.

version = "0.0.0-SNAPSHOT"
gitVersioning.apply {
    refs {
        branch(".+") {
            // if the env-var is unavailable, use the 'ref'
            version = "\${commit.timestamp.datetime}-\${env.CODEBUILD_WEBHOOK_TRIGGER.slug:-\${ref}}"
        }
    }
    rev { version = "\${commit.timestamp.datetime}-\${commit}" }
}

This resolves to

20220425.112958-${ref}

But I want it to be

20220425.112958-feat-add-widget

Suggestion

Perhaps the find/replace can be made more intelligent, but I'd recommend an alternative. Remove the string templating, and provide the git variables as parameters to the existing action.

public void branch(String pattern, Action<RefPatchDescription> action) {
RefPatchDescription ref = new RefPatchDescription(BRANCH, Pattern.compile(pattern));
action.execute(ref);
this.list.add(ref);
}

        public void branch(String pattern, Action<GitProperties, RefPatchDescription> action) {
            RefPatchDescription ref = new RefPatchDescription(BRANCH, Pattern.compile(pattern));
            // action.execute(ref); // don't execute immediately, evaluate it later, once the git properties are determined
            this.list.add(action);
        }
        
         // when required, evaluate the version
        public RefPatchDescription evaluateVersion() {
            for (var Action<> action in list) {
                return action.invoke(gitProperties)
            }
        }

(I'm not sure on the correct Action class to use, but in this case GitProperties would be provided as an argument and the Action must return a RefPatchDescription.

In build.gradle.kts it would be used something like this

version = "0.0.0-SNAPSHOT"
gitVersioning.apply {
    // List of ref configurations, ordered by priority. First matching configuration will be used.
    refs {
        branch(".+") { gitProperties ->
            var gitRef = gitProperties.env("CODEBUILD_WEBHOOK_TRIGGER")
            if (gitRef.isNullOrBlank()) gitRef = gitProperties.ref()
            version = "${gitProperties.commit.timestamp.datetime}-${gitRef}"
        }
    }
    rev { gitProperties -> 
      version = "${gitProperties.commit.timestamp.datetime}-${gitProperties.commit}" }
}

Other points

A quick question, rather than making a ticket just for for it, I am confused by this line

- `refs` List of ref configurations, ordered by priority.

Is it ordered by ascending priority, as in the first definition will be overridden by later definition, if they match?

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions