Skip to content
This repository has been archived by the owner on Oct 5, 2018. It is now read-only.

Commit

Permalink
Default version strategies are undeprecated
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ajoberstar committed Jun 28, 2016
1 parent 56ee905 commit f3b4c19
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

/**
Expand Down Expand Up @@ -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.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -37,7 +38,7 @@ import org.slf4j.LoggerFactory
* @see <a href="https://github.com/ajoberstar/gradle-git/wiki/SemVer%20Support">Wiki Doc</a>
*/
@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'
Expand Down Expand Up @@ -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.
* <ul>
* <li>Return {@code false}, if the {@code release.stage} is not one listed in the {@code stages} property.</li>
* <li>Return {@code false}, if the repository has uncommitted changes and {@code allowDirtyRepo} is {@code false}.</li>
* <li>Return {@code true}, otherwise.</li>
* </ul>
*/
@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.
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ class ReleasePluginExtensionSpec extends Specification {
project.version.toString() == '1.0.0'
}

def 'infer uses default if it has default selector that passes when selector doesnt'() {
given:
Project project = ProjectBuilder.builder().build()
ReleasePluginExtension extension = new ReleasePluginExtension(project)
extension.grgit = GroovyMock(Grgit)
extension.versionStrategy([
getName: { 'b' },
selector: { proj, grgit -> false },
infer: { proj, grgit -> new ReleaseVersion('1.0.0', null, true) }] as VersionStrategy)
extension.defaultVersionStrategy = [
getName: { 'a' },
selector: { proj, grgit -> false },
defaultSelector: { proj, grgit -> true },
infer: { proj, grgit -> new ReleaseVersion('1.2.3', null, true) }] as DefaultVersionStrategy
expect:
project.version.toString() == '1.2.3'
}

def 'infer fails if no strategy selected including the default strategy'() {
given:
Project project = ProjectBuilder.builder().build()
Expand Down

0 comments on commit f3b4c19

Please # to comment.