From f3b4c19bdb11b56aa395c60323375862361effc6 Mon Sep 17 00:00:00 2001 From: Andrew Oberstar Date: Mon, 27 Jun 2016 21:26:43 -0500 Subject: [PATCH] Default version strategies are undeprecated This fixes #182. The change for #180 was short-sighted and ignored the reason default strategies existed in the first place. This new fix puts the old behavior back while still preserving the slightly better error messages introduced in 1.5.0. This solution is not pretty, and I'd like to use something else but this will do for now (probably until 2.0.0). --- .../base/DefaultVersionStrategy.groovy | 39 +++++++++++++++++++ .../base/ReleasePluginExtension.groovy | 15 ++++--- .../git/release/semver/SemVerStrategy.groovy | 25 +++++++++++- .../base/ReleasePluginExtensionSpec.groovy | 18 +++++++++ 4 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 src/main/groovy/org/ajoberstar/gradle/git/release/base/DefaultVersionStrategy.groovy diff --git a/src/main/groovy/org/ajoberstar/gradle/git/release/base/DefaultVersionStrategy.groovy b/src/main/groovy/org/ajoberstar/gradle/git/release/base/DefaultVersionStrategy.groovy new file mode 100644 index 0000000..a8f6a59 --- /dev/null +++ b/src/main/groovy/org/ajoberstar/gradle/git/release/base/DefaultVersionStrategy.groovy @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ajoberstar.gradle.git.release.base + +import org.ajoberstar.grgit.Grgit + +import org.gradle.api.Project + +/** + * Strategy to infer a version from the project's and Git repository's state. This + * also supports being selected as a default strategy. This is a temporary interface + * and should be replaced in some other way in gradle-git 2.0.0. + * @see org.ajoberstar.gradle.git.release.semver.SemVerStrategy + * @see org.ajoberstar.gradle.git.release.opinion.Strategies + */ +interface DefaultVersionStrategy extends VersionStrategy { + /** + * Determines if the strategy can be used as a default strategy for inferring + * the project's version. A return of {@code false} does not mean that the + * strategy cannot be used as the default. + * @param project the project the version should be inferred for + * @param grgit the repository the version should be inferred from + * @return {@code true} if the strategy can be used to infer the version + */ + boolean defaultSelector(Project project, Grgit grgit) +} diff --git a/src/main/groovy/org/ajoberstar/gradle/git/release/base/ReleasePluginExtension.groovy b/src/main/groovy/org/ajoberstar/gradle/git/release/base/ReleasePluginExtension.groovy index 135878d..23084c9 100644 --- a/src/main/groovy/org/ajoberstar/gradle/git/release/base/ReleasePluginExtension.groovy +++ b/src/main/groovy/org/ajoberstar/gradle/git/release/base/ReleasePluginExtension.groovy @@ -17,6 +17,7 @@ package org.ajoberstar.gradle.git.release.base import org.ajoberstar.gradle.git.release.base.ReleaseVersion import org.ajoberstar.gradle.git.release.base.TagStrategy +import org.ajoberstar.gradle.git.release.base.DefaultVersionStrategy import org.ajoberstar.gradle.git.release.base.VersionStrategy import org.ajoberstar.grgit.Grgit import org.ajoberstar.grgit.util.ConfigureUtil @@ -53,11 +54,7 @@ class ReleasePluginExtension { * The strategy to use if all of the ones in {@code versionStrategies} return * false from their {@code selector()} methods. This strategy can be, but is * not required to be, one from {@code versionStrategies}. - * - * This is deprecated as of 1.5.0, as version strategies that return false often - * will fail when their {@code infer()} method is called. */ - @Deprecated VersionStrategy defaultVersionStrategy /** @@ -110,9 +107,15 @@ class ReleasePluginExtension { } if (!selectedStrategy) { - if (defaultVersionStrategy?.selector(project, grgit)) { + boolean useDefault + if (defaultVersionStrategy instanceof DefaultVersionStrategy) { + useDefault = defaultVersionStrategy.defaultSelector(project, grgit) + } else { + useDefault = defaultVersionStrategy?.selector(project, grgit) + } + + if (useDefault) { logger.info('Falling back to default strategy: {}', defaultVersionStrategy.name) - logger.warn('Use of a default version strategy is deprecated and will be removed in gradle-git 2.0.0') selectedStrategy = defaultVersionStrategy } else { throw new GradleException('No version strategies were selected. Run build with --info for more detail.') diff --git a/src/main/groovy/org/ajoberstar/gradle/git/release/semver/SemVerStrategy.groovy b/src/main/groovy/org/ajoberstar/gradle/git/release/semver/SemVerStrategy.groovy index a5e4c29..35feaa6 100644 --- a/src/main/groovy/org/ajoberstar/gradle/git/release/semver/SemVerStrategy.groovy +++ b/src/main/groovy/org/ajoberstar/gradle/git/release/semver/SemVerStrategy.groovy @@ -21,6 +21,7 @@ import groovy.transform.PackageScope import com.github.zafarkhaja.semver.Version import org.ajoberstar.gradle.git.release.base.ReleasePluginExtension import org.ajoberstar.gradle.git.release.base.ReleaseVersion +import org.ajoberstar.gradle.git.release.base.DefaultVersionStrategy import org.ajoberstar.gradle.git.release.base.VersionStrategy import org.ajoberstar.grgit.Grgit @@ -37,7 +38,7 @@ import org.slf4j.LoggerFactory * @see Wiki Doc */ @Immutable(copyWith=true, knownImmutableClasses=[PartialSemVerStrategy]) -final class SemVerStrategy implements VersionStrategy { +final class SemVerStrategy implements DefaultVersionStrategy { private static final Logger logger = LoggerFactory.getLogger(SemVerStrategy) static final String SCOPE_PROP = 'release.scope' static final String STAGE_PROP = 'release.stage' @@ -86,6 +87,28 @@ final class SemVerStrategy implements VersionStrategy { */ boolean enforcePrecedence + /** + * Determines whether this strategy can be used to infer the version as a default. + * + */ + @Override + boolean defaultSelector(Project project, Grgit grgit) { + String stage = getPropertyOrNull(project, STAGE_PROP) + if (!stages.contains(stage)) { + logger.info('Skipping {} default strategy because stage ({}) is not one of: {}', name, stage, stages) + return false + } else if (!allowDirtyRepo && !grgit.status().clean) { + logger.info('Skipping {} default strategy because repo is dirty.', name) + return false + } else { + return true + } + } + /** * Determines whether this strategy should be used to infer the version. *