-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from gradle/atual/allow-prerelease
Add allowPreRelease option to control latest Maven/Gradle version
- Loading branch information
Showing
20 changed files
with
5,006 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- [NEW] - Add `options.allowPreRelease` to control whether to upgrade to pre-release versions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.10.3 | ||
0.11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 13 additions & 15 deletions
28
src/main/java/org/gradle/wrapperupgrade/GradleBuildToolStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/org/gradle/wrapperupgrade/GradleMetadataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.gradle.wrapperupgrade; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.gradle.util.internal.VersionNumber; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Comparator; | ||
import java.util.Optional; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class GradleMetadataFetcher { | ||
private final URL gradleAllVersionsMetadata; | ||
private final URL gradleCurrentVersionMetadata; | ||
|
||
GradleMetadataFetcher(URL gradleAllVersionsMetadata, URL gradleCurrentVersionMetadata) { | ||
this.gradleAllVersionsMetadata = gradleAllVersionsMetadata; | ||
this.gradleCurrentVersionMetadata = gradleCurrentVersionMetadata; | ||
} | ||
|
||
GradleMetadataFetcher() { | ||
try { | ||
this.gradleAllVersionsMetadata = new URL("https://services.gradle.org/versions/all"); | ||
this.gradleCurrentVersionMetadata = new URL("https://services.gradle.org/versions/current"); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
Optional<JsonNode> fetchLatestVersion(boolean allowPreRelease) throws IOException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
if (allowPreRelease) { | ||
return StreamSupport.stream(mapper.readTree(gradleAllVersionsMetadata).spliterator(), false) | ||
.max(Comparator.comparing(n -> VersionNumber.parse(n.path("version").asText()))) | ||
.flatMap(n -> n.path("version").isMissingNode() ? Optional.empty() : Optional.of(n)); | ||
} else { | ||
JsonNode gradleMetadata = mapper.readTree(gradleCurrentVersionMetadata); | ||
return gradleMetadata.path("version").isMissingNode() ? Optional.empty() : Optional.of(gradleMetadata); | ||
} | ||
} | ||
} |
19 changes: 7 additions & 12 deletions
19
src/main/java/org/gradle/wrapperupgrade/MavenBuildToolStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/org/gradle/wrapperupgrade/MavenMetadataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.gradle.wrapperupgrade; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.dataformat.xml.XmlMapper; | ||
import org.gradle.util.internal.VersionNumber; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Comparator; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class MavenMetadataFetcher { | ||
|
||
private final URL mavenMetadataUrl; | ||
|
||
MavenMetadataFetcher(URL mavenMetadataUrl) { | ||
this.mavenMetadataUrl = mavenMetadataUrl; | ||
} | ||
|
||
MavenMetadataFetcher() { | ||
try { | ||
this.mavenMetadataUrl = new URL("https://repo.maven.apache.org/maven2/org/apache/maven/maven-core/maven-metadata.xml"); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
Optional<VersionNumber> fetchLatestVersion(boolean allowPreRelease) throws IOException { | ||
XmlMapper mapper = new XmlMapper(); | ||
JsonNode mavenMetadata = mapper.readTree(mavenMetadataUrl).path("versioning").path("versions").path("version"); | ||
if (mavenMetadata.isMissingNode()) { | ||
return Optional.empty(); | ||
} | ||
Predicate<VersionNumber> isReleaseOrIdentity = allowPreRelease ? v -> true : v -> v.getQualifier() == null; | ||
return StreamSupport.stream(mavenMetadata.spliterator(), false) | ||
.map(n -> VersionNumber.parse(n.asText())) | ||
.sorted(Comparator.reverseOrder()) | ||
.filter(isReleaseOrIdentity) | ||
.findFirst(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,16 +50,21 @@ public void options(Action<Options> action) { | |
public static class Options { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
etiennestuder
Member
|
||
|
||
private final ListProperty<String> gitCommitExtraArgs; | ||
private final Property<Boolean> allowPreRelease; | ||
|
||
@Inject | ||
public Options(ObjectFactory objects) { | ||
this.gitCommitExtraArgs = objects.listProperty(String.class); | ||
this.allowPreRelease = objects.property(Boolean.class); | ||
} | ||
|
||
public ListProperty<String> getGitCommitExtraArgs() { | ||
return gitCommitExtraArgs; | ||
} | ||
|
||
public Property<Boolean> getAllowPreRelease() { | ||
return allowPreRelease; | ||
} | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/test/groovy/org/gradle/wrapperupgrade/GradleMetadataFetcherTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.gradle.wrapperupgrade | ||
|
||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class GradleMetadataFetcherTest extends Specification { | ||
|
||
@Shared | ||
def gradleMetadataFetcher = new GradleMetadataFetcher(getClass().getResource('/gradle-metadata-all.json'), | ||
getClass().getResource('/gradle-metadata-current.json')) | ||
|
||
def "fetch latest version allowing pre-releases"() { | ||
when: | ||
def version = gradleMetadataFetcher.fetchLatestVersion(true) | ||
|
||
then: | ||
version.map(node -> node.get('version').asText()).orElse(null) == '8.0-milestone-2' | ||
} | ||
|
||
def "fetch latest version ignoring pre-releases"() { | ||
when: | ||
def version = gradleMetadataFetcher.fetchLatestVersion(false) | ||
|
||
then: | ||
version.map(node -> node.get('version').asText()).orElse(null) == '7.5.1' | ||
} | ||
|
||
def "fetch unknown latest version allowing pre-releases"() { | ||
when: | ||
def version = new GradleMetadataFetcher(getClass().getResource('/gradle-metadata-all-unknown.json'), | ||
getClass().getResource('/gradle-metadata-current.json')).fetchLatestVersion(true) | ||
|
||
then: | ||
version == Optional.empty() | ||
} | ||
|
||
def "fetch unknown latest version ignoring pre-releases"() { | ||
when: | ||
def version = new GradleMetadataFetcher(getClass().getResource('/gradle-metadata-all.json'), | ||
getClass().getResource('/gradle-metadata-current-unknown.json')).fetchLatestVersion(false) | ||
|
||
then: | ||
version == Optional.empty() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/groovy/org/gradle/wrapperupgrade/MavenMetadataFetcherTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.gradle.wrapperupgrade | ||
|
||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class MavenMetadataFetcherTest extends Specification { | ||
|
||
@Shared | ||
def mavenMetadataFetcher = new MavenMetadataFetcher(getClass().getResource('/maven-metadata.xml')) | ||
|
||
def "fetch latest version allowing pre-releases"() { | ||
when: | ||
def version = mavenMetadataFetcher.fetchLatestVersion(true) | ||
|
||
then: | ||
version.map(v -> v as String).orElse(null) == '4.0.0-alpha-2' | ||
} | ||
|
||
def "fetch latest version ignoring pre-releases"() { | ||
when: | ||
def version = mavenMetadataFetcher.fetchLatestVersion(false) | ||
|
||
then: | ||
version.map(v -> v as String).orElse(null) == '3.8.6' | ||
} | ||
|
||
def "fetch unknown latest version"() { | ||
when: | ||
def version = new MavenMetadataFetcher(getClass().getResource('/maven-metadata-none.xml')).fetchLatestVersion(false) | ||
|
||
then: | ||
version == Optional.empty() | ||
} | ||
|
||
} |
Oops, something went wrong.
I believe this extension class can be simplified by using an abstract class: https://docs.gradle.org/current/userguide/custom_plugins.html#sec:getting_input_from_the_build