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

Added support for old versions of gradle layout pattern in ivy repos #109

Merged
Merged
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 @@ -16,8 +16,15 @@
package nebula.test.dependencies

import nebula.test.functional.GradleRunnerFactory
import nebula.test.gradle.GradleVersionComparator
import org.gradle.api.invocation.Gradle

class GradleDependencyGenerator {
private static final String DEFAULT_GRADLE_VERSION = '5.2.1'
private static final String GRADLE_FIVE_ZERO = '5.0.0'
private static final String LEGACY_PATTERN_LAYOUT = "layout('pattern')"
private static final String PATTERN_LAYOUT = "patternLayout"

static final String STANDARD_SUBPROJECT_BLOCK = '''\
subprojects {
apply plugin: 'maven-publish'
Expand All @@ -41,6 +48,31 @@ class GradleDependencyGenerator {
}
}
'''.stripIndent()

static final String LEGACY_STANDARD_SUBPROJECT_BLOCK = '''\
subprojects {
apply plugin: 'maven-publish'
apply plugin: 'ivy-publish'
apply plugin: 'java'

publishing {
repositories {
maven {
url "../mavenrepo"
}
ivy {
url "../ivyrepo"
layout('pattern') {
ivy '[organisation]/[module]/[revision]/[module]-[revision]-ivy.[ext]'
artifact '[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'
m2compatible = true
}
}
}
}
}
'''.stripIndent()

static final String BUILD_GRADLE = 'build.gradle'

private boolean generated = false
Expand All @@ -49,13 +81,23 @@ class GradleDependencyGenerator {
File gradleRoot
File ivyRepoDir
File mavenRepoDir
String gradleVersion

GradleDependencyGenerator(DependencyGraph graph, String directory = 'build/testrepogen') {
GradleDependencyGenerator(String gradleVersion, DependencyGraph graph, String directory = 'build/testrepogen') {
this.graph = graph
this.gradleRoot = new File(directory)
this.ivyRepoDir = new File(directory, 'ivyrepo')
this.mavenRepoDir = new File(directory, 'mavenrepo')
generateGradleFiles()
this.gradleVersion = gradleVersion
generateGradleFiles(gradleVersion)
}

GradleDependencyGenerator(Gradle gradle, DependencyGraph graph, String directory = 'build/testrepogen') {
this(gradle.gradleVersion, graph, directory)
}

GradleDependencyGenerator(DependencyGraph graph, String directory = 'build/testrepogen') {
this(DEFAULT_GRADLE_VERSION, graph, directory)
}

File generateTestMavenRepo() {
Expand Down Expand Up @@ -93,39 +135,45 @@ class GradleDependencyGenerator {
}

String getIvyRepositoryBlock() {
"""\
use(GradleVersionComparator) {
boolean isGradleOlderThanGradleFive = gradleVersion.versionLessThan(GRADLE_FIVE_ZERO)
String layoutPattern = isGradleOlderThanGradleFive ? LEGACY_PATTERN_LAYOUT : PATTERN_LAYOUT
return """\
ivy {
url '${getIvyRepoUrl()}'
patternLayout {
${layoutPattern} {
ivy '[organisation]/[module]/[revision]/[module]-[revision]-ivy.[ext]'
artifact '[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'
m2compatible = true
}
}
""".stripIndent()
}
}

private void generateGradleFiles() {
if (generated) {
return
} else {
generated = true
}
private void generateGradleFiles(String gradleVersion) {
use(GradleVersionComparator) {
if (generated) {
return
} else {
generated = true
}

gradleRoot.mkdirs()
def rootBuildGradle = new File(gradleRoot, BUILD_GRADLE)
rootBuildGradle.text = STANDARD_SUBPROJECT_BLOCK
def includes = []
graph.nodes.each { DependencyGraphNode n ->
String subName = "${n.group}.${n.artifact}_${n.version.replaceAll(/\./, '_')}"
includes << subName
def subfolder = new File(gradleRoot, subName)
subfolder.mkdir()
def subBuildGradle = new File(subfolder, BUILD_GRADLE)
subBuildGradle.text = generateSubBuildGradle(n)
gradleRoot.mkdirs()
def rootBuildGradle = new File(gradleRoot, BUILD_GRADLE)
rootBuildGradle.text = gradleVersion.versionLessThan(GRADLE_FIVE_ZERO) ? LEGACY_STANDARD_SUBPROJECT_BLOCK : STANDARD_SUBPROJECT_BLOCK
def includes = []
graph.nodes.each { DependencyGraphNode n ->
String subName = "${n.group}.${n.artifact}_${n.version.replaceAll(/\./, '_')}"
includes << subName
def subfolder = new File(gradleRoot, subName)
subfolder.mkdir()
def subBuildGradle = new File(subfolder, BUILD_GRADLE)
subBuildGradle.text = generateSubBuildGradle(n)
}
def settingsGradle = new File(gradleRoot, 'settings.gradle')
settingsGradle.text = 'include ' + includes.collect { "'${it}'" }.join(', ')
}
def settingsGradle = new File(gradleRoot, 'settings.gradle')
settingsGradle.text = 'include ' + includes.collect { "'${it}'"}.join(', ')
}

private String generateSubBuildGradle(DependencyGraphNode node) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/groovy/nebula/test/gradle/GradleVersionComparator.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nebula.test.gradle

import org.gradle.util.GradleVersion

@Category(String)
class GradleVersionComparator {

boolean versionGreaterThan(String version) {
return versionCompareTo(this, version) > 0
}

boolean versionLessThan(String version) {
return versionCompareTo(this, version) < 0
}

int versionCompareTo(String v1, String v2) {
return GradleVersion.version(v1).compareTo(GradleVersion.version(v2))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package nebula.test.dependencies

import org.gradle.api.invocation.Gradle
import spock.lang.Specification

class GradleDependencyGeneratorSpec extends Specification {
Expand Down Expand Up @@ -111,6 +112,85 @@ class GradleDependencyGeneratorSpec extends Specification {
new File(ivyRepo, 'test/ivy/foo/1.0.0/foo-1.0.0.jar').exists()
}

def 'generate an ivy repo block - gradle version older than 5.x'() {
def directory = 'build/testdependencies/testivyrepo'
def graph = ['test.ivy:foo:1.0.0']
def generator = new GradleDependencyGenerator("4.10.3", new DependencyGraph(graph), directory)

when:
String block = generator.getIvyRepositoryBlock()

then:
block.contains("""
layout('pattern') {
ivy '[organisation]/[module]/[revision]/[module]-[revision]-ivy.[ext]'
artifact '[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'
m2compatible = true
}
""")
}

def 'generate an ivy repo block - uses Gradle object newer versions of Gradle'() {
Gradle gradleMock = Mock(Gradle)
def directory = 'build/testdependencies/testivyrepo'
def graph = ['test.ivy:foo:1.0.0']

when:
def generator = new GradleDependencyGenerator(gradleMock, new DependencyGraph(graph), directory)
String block = generator.getIvyRepositoryBlock()

then:
1 * gradleMock.getGradleVersion() >> "5.2.1"

block.contains("""
patternLayout {
ivy '[organisation]/[module]/[revision]/[module]-[revision]-ivy.[ext]'
artifact '[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'
m2compatible = true
}
""")

}

def 'generate an ivy repo block - uses Gradle object old versions of Gradle'() {
Gradle gradleMock = Mock(Gradle)
def directory = 'build/testdependencies/testivyrepo'
def graph = ['test.ivy:foo:1.0.0']

when:
def generator = new GradleDependencyGenerator(gradleMock, new DependencyGraph(graph), directory)
String block = generator.getIvyRepositoryBlock()

then:
1 * gradleMock.getGradleVersion() >> "4.10.3"

block.contains("""
layout('pattern') {
ivy '[organisation]/[module]/[revision]/[module]-[revision]-ivy.[ext]'
artifact '[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'
m2compatible = true
}
""")
}

def 'generate an ivy repo block - gradle version newer than 5.x'() {
def directory = 'build/testdependencies/testivyrepo'
def graph = ['test.ivy:foo:1.0.0']
def generator = new GradleDependencyGenerator("5.2.1", new DependencyGraph(graph), directory)

when:
String block = generator.getIvyRepositoryBlock()

then:
block.contains("""
patternLayout {
ivy '[organisation]/[module]/[revision]/[module]-[revision]-ivy.[ext]'
artifact '[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]'
m2compatible = true
}
""")
}

def 'check ivy status'() {
def directory = 'build/testdependencies/ivyxml'
def graph = ['test.ivy:foo:1.0.0']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package nebula.test.gradle

import spock.lang.Specification
import spock.lang.Subject

@Subject(GradleVersionComparator)
class GradleVersionComparatorSpec extends Specification {

def 'checks if version is greater than'() {
given:
String version = '5.0'
Boolean result

when:
use(GradleVersionComparator) {
result = version.versionGreaterThan('4.10.3')
}

then:
result
}

def 'checks if version is less than'() {
given:
String version = '5.0'
Boolean result

when:
use(GradleVersionComparator) {
result = version.versionLessThan('5.1')
}

then:
result
}
}