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

Implements #325 rejectVersionIf { ... } #340

Merged
merged 12 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 9 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
127 changes: 102 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,48 +85,112 @@ The strategy can be specified either on the task or as a system property for ad
```groovy
gradle dependencyUpdates -Drevision=release
```


The latest versions can be further filtered using [Component Selection Rules][component_selection_rules].
The current version of a component can be retrieved with `currentVersion` property. For example, to
disallow release candidates as upgradable versions from stable versions, a selection rule could be
defined as:
To further define which version to accept, you need to define what means an unstable version. Sadly, there are
no agreed standard on this, but this is a good starting point:

```groovy
dependencyUpdates.resolutionStrategy {
componentSelection { rules ->
rules.all { ComponentSelection selection ->
def isNonStable = { String version ->
['alpha', 'beta', 'rc', 'cr', 'm', 'preview', 'b', 'ea'].any { qualifier ->
version ==~ /(?i).*[.-]$qualifier[.\d-+]*/
}
}
<details open>
<summary>Groovy</summary>

if (isNonStable(candidate.version) && !isNonStable(currentVersion)) {
selection.reject('Release candidate')
}
}
}
```groovy
def isNonStable = { String version ->
def stableKeyword = ['RELEASE', 'FINAL', 'GA'].any { it -> version.toUpperCase().contains(it) }
jmfayard marked this conversation as resolved.
Show resolved Hide resolved
def regex = /^[0-9,.v-]+$/
jmfayard marked this conversation as resolved.
Show resolved Hide resolved
return !stableKeyword && !(version ==~ regex)
}
```

If using Gradle's [kotlin-dsl][kotlin_dsl], you could configure the `dependencyUpdates` like this:
</details>
<details>
<summary>Kotlin</summary>

```kotlin
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
fun isNonStable(version: String): Boolean {
val stableKeyword = listOf("RELEASE", "FINAL", "GA").any { version.toUpperCase().contains(it) }
val regex = "^[0-9,.v-]+$".toRegex()
val isStable = stableKeyword || regex.matches(version)
return isStable.not()
}
```

</details>

You can then configure [Component Selection Rules][component_selection_rules].
The current version of a component can be retrieved with the `currentVersion` property.
You can either use the simplified syntax `rejectVersionIf { ... }` or configure a complete resolution strategy.

tasks.named<DependencyUpdatesTask>("dependencyUpdates") {

<details open>
<summary>Groovy</summary>

```groovy
dependencyUpdates {
// Example 1: reject all non stable versions
rejectVersionIf { selection ->
isNonStable(selection.candidate.version)
}

// Example 2: disallow release candidates as upgradable versions from stable versions
rejectVersionIf { selection ->
isNonStable(selection.candidate.version) && !isNonStable(selection.currentVersion)
}

// Example 3: using the full syntax
resolutionStrategy {
componentSelection {
all {
fun isNonStable(version: String) = listOf("alpha", "beta", "rc", "cr", "m", "preview", "b", "ea").any { qualifier ->
version.matches(Regex("(?i).*[.-]$qualifier[.\\d-+]*"))
}
if (isNonStable(candidate.version) && !isNonStable(currentVersion)) {
reject("Release candidate")
reject('Release candidate')
}
}
}
}
}
```

</details>
<details>
<summary>Kotlin</summary>

```kotlin
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask

tasks.withType<DependencyUpdatesTask> {
// Example 1: reject all non stable versions
rejectVersionIf { selection ->
isNonStable(selection.candidate.version)
}

// Example 2: disallow release candidates as upgradable versions from stable versions
rejectVersionIf { selection ->
isNonStable(selection.candidate.version) && !isNonStable(selection.currentVersion)
}

// Example 3: using the full syntax
resolutionStrategy {
componentSelection {
all {
if (isNonStable(candidate.version) && !isNonStable(currentVersion)) {
reject("Release candidate")
}
}
}
}
}
```

</details>

#### Kotlin DSL

If using Gradle's [kotlin-dsl][kotlin_dsl], you could configure the `dependencyUpdates` like this:

```kotlin
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask

tasks.withType<DependencyUpdatesTask> {

// optional parameters
checkForGradleUpdate = true
outputFormatter = "json"
Expand All @@ -137,6 +201,19 @@ tasks.named<DependencyUpdatesTask>("dependencyUpdates") {

Note: Do use the `plugins { .. }` syntax if you use the Kotlin DSL.

#### Try out the samples

Have a look at [`examples/groovy`](https://github.com/ben-manes/gradle-versions-plugin/tree/master/examples/groovy) and [`examples/kotlin`](https://github.com/ben-manes/gradle-versions-plugin/tree/master/examples/kotlin)

```bash
# Pubish the latest version of the plugin to mavenCentral
jmfayard marked this conversation as resolved.
Show resolved Hide resolved
$ ./gradlew install

# Try out the samples
$ ./gradlew -p examples/groovy dependencyUpdate
$ ./gradlew -p examples/kotlin dependencyUpdate
```

#### Report format

The task property `outputFormatter` controls the report output format. The following values are supported:
Expand Down
1 change: 0 additions & 1 deletion examples/.gitignore

This file was deleted.

24 changes: 16 additions & 8 deletions examples/build.gradle → examples/groovy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defaultTasks 'dependencyUpdates'
buildscript {
repositories {
// Use 'gradle install' to install latest
jcenter()
mavenLocal()
jcenter()
}

dependencies {
Expand All @@ -29,20 +29,28 @@ configurations {
unresolvable2
}

def isNonStable = { String version ->
def stableKeyword = ['RELEASE', 'FINAL', 'GA'].any { it -> version.toUpperCase().contains(it) }
def regex = /^[0-9,.v-]+$/
return !stableKeyword && !(version ==~ regex)
}

dependencyUpdates {
checkForGradleUpdate = true

resolutionStrategy {
componentSelection { rules ->
rules.all { ComponentSelection selection ->
boolean rejected = ['alpha', 'beta', 'rc', 'cr', 'm', 'preview', 'b', 'ea'].any { qualifier ->
selection.candidate.version ==~ /(?i).*[.-]$qualifier[.\d-+]*/
}
if (rejected) {
selection.reject('Release candidate')
componentSelection {
all {
if (isNonStable(candidate.version) && !isNonStable(currentVersion)) {
reject('Release candidate')
}
}
}
}

// rejectVersionIf { selection ->
// isNonStable(selection.candidate.version) && !isNonStable(selection.currentVersion)
// }
}

dependencies {
Expand Down
3 changes: 3 additions & 0 deletions examples/groovy/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rootProject.name = 'gradle-versions-sample-groovy'
enableFeaturePreview("IMPROVED_POM_SUPPORT")

26 changes: 19 additions & 7 deletions examples/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask

// Use 'gradle install' to install latest plugin version
plugins {
id("com.github.ben-manes.versions") version "0.20.0"
id("com.github.ben-manes.versions") version "0.24.0"
}


repositories {
jcenter()
}
Expand All @@ -19,19 +19,31 @@ configurations {
register("unresolvable2")
}

tasks.named<DependencyUpdatesTask>("dependencyUpdates") {
fun isNonStable(version: String): Boolean {
val stableKeyword = listOf("RELEASE", "FINAL", "GA").any { version.toUpperCase().contains(it) }
val regex = "^[0-9,.v-]+$".toRegex()
val isStable = stableKeyword || regex.matches(version)
return isStable.not()
}



tasks.withType<DependencyUpdatesTask> {

resolutionStrategy {
componentSelection {
all {
val rejected = listOf("alpha", "beta", "rc", "cr", "m", "preview", "b", "ea").any { qualifier ->
candidate.version.matches(Regex("(?i).*[.-]$qualifier[.\\d-+]*"))
}
if (rejected) {
if (isNonStable(candidate.version) && !isNonStable(currentVersion)) {
reject("Release candidate")
}
}
}
}

rejectVersionIf { selection ->
isNonStable(selection.candidate.version) && isNonStable(selection.currentVersion).not()
}

// optional parameters
checkForGradleUpdate = true
outputFormatter = "json"
Expand Down
11 changes: 11 additions & 0 deletions examples/kotlin/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@file:Suppress("UnstableApiUsage")

// Use 'gradle install' to install latest plugin version
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
}
}
rootProject.name = "gradle-versions-sample-kotlin"

1 change: 0 additions & 1 deletion examples/settings.gradle

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.github.benmanes.gradle.versions.updates

import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ComponentFilter
import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ComponentSelectionRulesWithCurrent
import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ComponentSelectionWithCurrent
import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ResolutionStrategyWithCurrent
import groovy.transform.TypeChecked
import org.gradle.api.Action
Expand Down Expand Up @@ -91,6 +94,19 @@ class DependencyUpdatesTask extends DefaultTask {
this.resolutionStrategy = null
}

void rejectVersionIf(final ComponentFilter filter) {
resolutionStrategy { ResolutionStrategyWithCurrent strategy ->
strategy.componentSelection { ComponentSelectionRulesWithCurrent selection ->
selection.all { ComponentSelectionWithCurrent current ->
def isNotNull = current.currentVersion != null && current.candidate.version != null
if (isNotNull && filter.reject(current)) {
current.reject("Rejected by rejectVersionIf ")
}
}
}
}
}

/** Returns the resolution revision level. */
String revisionLevel() { System.properties['revision'] ?: revision }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.benmanes.gradle.versions.updates.resolutionstrategy

interface ComponentFilter {
jmfayard marked this conversation as resolved.
Show resolved Hide resolved

boolean reject(ComponentSelectionWithCurrent candidate)

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package com.github.benmanes.gradle.versions.updates.resolutionstrategy


import groovy.transform.TupleConstructor
import org.gradle.api.artifacts.ComponentSelection

@TupleConstructor(includeFields=true)
@TupleConstructor(includeFields = true)
class ComponentSelectionWithCurrent {

final String currentVersion

@Delegate
jmfayard marked this conversation as resolved.
Show resolved Hide resolved
private final ComponentSelection delegate


@Override
public String toString() {
jmfayard marked this conversation as resolved.
Show resolved Hide resolved
return """\
ComponentSelectionWithCurrent{
group="${candidate.group}",
module="${candidate.module}",
version="${candidate.version}",
currentVersion="$currentVersion",
}"""
}
}