Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gradle task rule for marking the next version #750

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class BaseAxionTask extends DefaultTask {
@Inject
protected abstract ProviderFactory getProviders();

protected VersionResolutionContext resolutionContext() {
return VersionResolutionContext.create(versionConfig, layout.projectDirectory)
protected VersionResolutionContext resolutionContext(String nextVersion = null, String versionIncrementerName = null) {
return VersionResolutionContext.create(versionConfig, layout.projectDirectory, nextVersion, versionIncrementerName)
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package pl.allegro.tech.build.axion.release


import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import pl.allegro.tech.build.axion.release.domain.NextVersionMarker
import pl.allegro.tech.build.axion.release.domain.properties.Properties
import pl.allegro.tech.build.axion.release.infrastructure.di.VersionResolutionContext

abstract class MarkNextVersionTask extends BaseAxionTask {
@Input
String versionIncrementerName = null;

@TaskAction
void release() {
VersionResolutionContext context = resolutionContext()
VersionResolutionContext context = resolutionContext(null, versionIncrementerName)
NextVersionMarker marker = new NextVersionMarker(context.scmService())

Properties rules = context.rules()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ abstract class ReleasePlugin implements Plugin<Project> {
description = 'Creates next version marker tag and pushes it to remote.'
}

project.tasks.addRule("Pattern: " + MARK_NEXT_VERSION_TASK + "<VersionIncrementer>: Creates next version marker tag and pushes it to remote, using the specified incrementer.") { String taskName ->
if (taskName.startsWith(MARK_NEXT_VERSION_TASK)) {
project.tasks.register(taskName, MarkNextVersionTask) {
group = 'Release'
description = 'Creates next version marker tag and pushes it to remote.'

versionIncrementerName = taskName.substring(MARK_NEXT_VERSION_TASK.length()).uncapitalize()
}
}
}

project.tasks.register(CURRENT_VERSION_TASK, OutputCurrentVersionTask) {
group = 'Help'
description = 'Prints current project version extracted from SCM.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import static java.lang.String.format

enum PredefinedVersionIncrementer {

INCREMENT_PATCH('incrementPatch', { VersionIncrementerContext context, Map config ->
INCREMENT_PATCH('incrementPatch', ['patch'], { VersionIncrementerContext context, Map config ->
return context.currentVersion.incrementPatchVersion()
}),

INCREMENT_MINOR('incrementMinor', { VersionIncrementerContext context, Map config ->
INCREMENT_MINOR('incrementMinor', ['minor'], { VersionIncrementerContext context, Map config ->
return context.currentVersion.incrementMinorVersion()
}),

INCREMENT_MAJOR('incrementMajor', { VersionIncrementerContext context, Map config ->
INCREMENT_MAJOR('incrementMajor', ['major'], { VersionIncrementerContext context, Map config ->
return context.currentVersion.incrementMajorVersion()
}),

INCREMENT_MINOR_IF_NOT_ON_RELEASE_BRANCH('incrementMinorIfNotOnRelease', { VersionIncrementerContext context, Map config ->
INCREMENT_MINOR_IF_NOT_ON_RELEASE_BRANCH('incrementMinorIfNotOnRelease', ['minorIfNotRelease'], { VersionIncrementerContext context, Map config ->
if (!config.releaseBranchPattern) {
config.releaseBranchPattern = context.isLegacyDefTagnameRepo() ? TagPrefixConf.DEFAULT_LEGACY_PREFIX + '/.+' : TagPrefixConf.defaultPrefix() + '/.+'
}
Expand All @@ -33,7 +33,7 @@ enum PredefinedVersionIncrementer {
return context.currentVersion.incrementMinorVersion()
}),

INCREMENT_PRERELEASE('incrementPrerelease', { VersionIncrementerContext context, Map config ->
INCREMENT_PRERELEASE('incrementPrerelease', ['prerelease'], { VersionIncrementerContext context, Map config ->
if (context.currentVersion.preReleaseVersion) {
Matcher matcher = context.currentVersion.preReleaseVersion =~ /^(.*?)(\d+)$/
if (matcher.matches()) {
Expand All @@ -55,25 +55,31 @@ enum PredefinedVersionIncrementer {
return context.currentVersion.incrementPatchVersion()
}),

BRANCH_SPECIFIC('branchSpecific', { VersionIncrementerContext context, Map config ->
BRANCH_SPECIFIC('branchSpecific', ['branch'], { VersionIncrementerContext context, Map config ->
def incrementer = config.find { context.scmPosition.branch ==~ it.key }
return versionIncrementerFor(incrementer.value.toString(), config).apply(context)
})

private final String name
private final List<String> aliases

final VersionIncrementer versionIncrementer

private PredefinedVersionIncrementer(String name, Closure<Version> c) {
private PredefinedVersionIncrementer(String name, List<String> aliases, Closure<Version> c) {
this.name = name
this.aliases = aliases
this.versionIncrementer = c
}

static VersionProperties.Incrementer versionIncrementerFor(String name, Map configuration = [:]) {
PredefinedVersionIncrementer creator = values().find { it.name == name }
if (creator == null) {
throw new IllegalArgumentException("There is no predefined version incrementer with $name name. " +
"You can choose from: ${values().collect { it.name }}")
creator = values().find { it.aliases.contains(name) }

if (creator == null) { // if *still* null (no aliases and no names match)
throw new IllegalArgumentException("There is no predefined version incrementer with $name name. " +
"You can choose from: ${values().collect { it.name }}")
}
}
return { VersionIncrementerContext context -> creator.versionIncrementer.apply(context, configuration) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import pl.allegro.tech.build.axion.release.domain.scm.ScmRepository

class RulesFactory {

static Properties create(VersionConfig versionConfig, ScmRepository repository) {
static Properties create(VersionConfig versionConfig, ScmRepository repository, String nextVersion = null, String versionIncrementerName = null) {

ScmPosition position = repository.currentPosition()
setDefaultPrefix(versionConfig.tag, repository)
Expand All @@ -20,7 +20,7 @@ class RulesFactory {
VersionPropertiesFactory.create(versionConfig, position.branch),
TagPropertiesFactory.create(versionConfig.tag, position.branch),
versionConfig.checks,
versionConfig.nextVersion.nextVersionProperties(),
versionConfig.nextVersion.nextVersionProperties(nextVersion, versionIncrementerName),
HooksPropertiesFactory.create(versionConfig, versionConfig.hooks)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class VersionResolutionContext {
scmProperties.directory.toPath().relativize(projectRoot.toPath()).toString()))
}

static VersionResolutionContext create(VersionConfig versionConfig, Directory projectDirectory) {
static VersionResolutionContext create(VersionConfig versionConfig, Directory projectDirectory, String nextVersion = null, String versionIncrementerName = null) {
ScmProperties scmProperties = ScmPropertiesFactory.create(versionConfig)
ScmRepository scmRepository = ScmRepositoryFactory.create(scmProperties)

return new VersionResolutionContext(
RulesFactory.create(versionConfig, scmRepository),
RulesFactory.create(versionConfig, scmRepository, nextVersion, versionIncrementerName),
scmRepository,
scmProperties,
projectDirectory.asFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.allegro.tech.build.axion.release.domain.properties.NextVersionProperties;
import pl.allegro.tech.build.axion.release.domain.properties.NextVersionProperties.*;
import pl.allegro.tech.build.axion.release.domain.properties.NextVersionProperties.Deserializer;
import pl.allegro.tech.build.axion.release.domain.properties.NextVersionProperties.Serializer;

import javax.inject.Inject;

Expand Down Expand Up @@ -56,22 +56,25 @@ public void deserializer(Deserializer deserializer) {
getDeserializer().set(deserializer);
}

public NextVersionProperties nextVersionProperties() {

public NextVersionProperties nextVersionProperties(String nextVersion, String versionIncrementerName) {
if (getSuffix().get().isEmpty()) {
String message = "scmVersion.nextVersion.suffix can't be empty! Empty suffix will prevent axion-release from distinguishing nextVersion from regular versions";
throw new IllegalArgumentException(message);
}

return new NextVersionProperties(nextVersion().getOrNull(),
return new NextVersionProperties(nextVersion().isPresent() ? versionIncrementerName().getOrNull() : nextVersion,
getSuffix().get(),
getSeparator().get(),
versionIncrementerName().getOrNull(),
versionIncrementerName().isPresent() ? versionIncrementerName().getOrNull() : versionIncrementerName,
getSerializer().get(),
getDeserializer().get()
);
}

public NextVersionProperties nextVersionProperties() {
return nextVersionProperties(null, null);
}

private Provider<String> versionIncrementerName() {
return gradleProperty(NEXT_VERSION_INCREMENTER_PROPERTY);
}
Expand Down