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

add classifier and extension to coordinate #87

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion src/main/groovy/nebula/test/dependencies/Coordinate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,22 @@ class Coordinate {
String group
String artifact
String version
String classifier
String extension

@Override
String toString() {
"${group}:${artifact}:${version}"
def sb = "${group}:${artifact}:${version}"
if (classifier) sb <<= ":${classifier}"
if (extension) sb <<= "@${extension}"
return sb
}

public static Coordinate of(String s) {
def (substr, extension) = s.trim().tokenize("@")

def (group, artifact, version, classifier) = substr.tokenize(':')
return new Coordinate(
group: group, artifact: artifact, version: version, classifier: classifier, extension: extension)
}
}
10 changes: 5 additions & 5 deletions src/main/groovy/nebula/test/dependencies/DependencyGraph.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ class DependencyGraph {
}

private DependencyGraphNode parseNode(String s) {
// Don't use tokenize, it'll make each character a possible delimeter, e.g. \t\n would tokenize on both
// Don't use tokenize, it'll make each character a possible delimiter, e.g. \t\n would tokenize on both
// \t OR \n, not the combination of \t\n.
def parts = s.split('->')
def (group, artifact, version) = parts[0].trim().tokenize(':')
def coordinate = new Coordinate(group: group, artifact: artifact, version: version)
def coordinate = Coordinate.of(parts[0])
def dependencies = (parts.size() > 1) ? parseDependencies(parts[1]) : []

new DependencyGraphNode(coordinate: coordinate, dependencies: dependencies)
Expand All @@ -44,10 +43,11 @@ class DependencyGraph {
private List<Coordinate> parseDependencies(String s) {
List<Coordinate> dependencies = []
s.tokenize('|').each { String dependency ->
def (group, artifact, version) = dependency.trim().tokenize(':')
dependencies << new Coordinate(group: group, artifact: artifact, version: version)
dependencies << Coordinate.of(dependency)
}

dependencies
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray newlines


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@ package nebula.test.dependencies
class DependencyGraphBuilder {
Map<String, DependencyGraphNode> modules = [:]

DependencyGraphBuilder addModule(String coordinate) {
def (group, artifact, version) = coordinate.trim().tokenize(':')
addModule(group, artifact, version)
DependencyGraphBuilder addModule(String coordinateString) {
Coordinate coordinate = Coordinate.of(coordinateString)
modules[coordinate.toString()] = new DependencyGraphNode(coordinate: coordinate)

this
}

DependencyGraphBuilder addModule(String group, String artifact, String version) {
String key = "${group}:${artifact}:${version}".toString()
modules[key] = new DependencyGraphNode(coordinate: new Coordinate(group: group, artifact: artifact, version: version))
Coordinate coordinate = new Coordinate(group: group, artifact: artifact, version: version);
modules[coordinate.toString()] = new DependencyGraphNode(coordinate: coordinate)

this
}

DependencyGraphBuilder addModule(String group, String artifact, String version, String classifier, String extension) {
Coordinate coordinate = new Coordinate(
group: group, artifact: artifact, version: version, classifier: classifier, extension: extension);
modules[coordinate.toString()] = new DependencyGraphNode(coordinate: coordinate)

this
}

DependencyGraphBuilder addModule(DependencyGraphNode node) {
modules[node.toString()] = node
modules[node.coordinate.toString()] = node

node.dependencies.each { Coordinate dep ->
if (!modules.containsKey(dep.toString())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class DependencyGraphNode {

@Override
String toString() {
"${group}:${artifact}:${version}"
coordinate.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class GradleDependencyGenerator {
apply plugin: 'ivy-publish'
apply plugin: 'java'

task sourceJar(type: Jar) {
}

publishing {
repositories {
maven {
Expand All @@ -43,11 +46,25 @@ class GradleDependencyGenerator {
artifactId artifactName

from components.java

if (classifierName != null || extensionName != 'jar') {
artifact(sourceJar) {
classifier classifierName
extension extensionName
}
}
}
ivy(IvyPublication) {
module artifactName

from components.java

if (classifierName != null || extensionName != 'jar') {
artifact(sourceJar) {
classifier classifierName
extension extensionName
}
}
}
}
}
Expand Down Expand Up @@ -126,6 +143,7 @@ class GradleDependencyGenerator {

gradleRoot.mkdirs()
def rootBuildGradle = new File(gradleRoot, BUILD_GRADLE)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray newline

rootBuildGradle.text = STANDARD_SUBPROJECT_BLOCK
def includes = []
graph.nodes.each { DependencyGraphNode n ->
Expand Down Expand Up @@ -156,6 +174,8 @@ class GradleDependencyGenerator {
version = '${node.version}'
ext {
artifactName = '${node.artifact}'
extensionName = '${node.extension? node.extension: "jar"}'
classifierName = ${node.classifier? "'" + node.classifier + "'": null}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use string interpolation

}
""".stripIndent() + block.toString()
}
Expand Down
21 changes: 16 additions & 5 deletions src/main/groovy/nebula/test/dependencies/ModuleBuilder.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,34 @@ class ModuleBuilder {
List<Coordinate> dependencies = []

ModuleBuilder(String coordinate) {
def (group, artifact, version) = coordinate.tokenize(':')
module = new Coordinate(group: group, artifact: artifact, version: version)
module = Coordinate.of(coordinate)
}

ModuleBuilder(String group, String artifact, String version) {
module = new Coordinate(group: group, artifact: artifact, version: version)
}

ModuleBuilder(String group, String artifact, String version, String classifier, String extension) {
module = new Coordinate(
group: group, artifact: artifact, version: version, classifier: classifier, extension: extension)
}

ModuleBuilder addDependency(String dependency) {
def (group, artifact, version) = dependency.tokenize(':')
dependencies << new Coordinate(group: group, artifact: artifact, version: version)
dependencies << Coordinate.of(dependency)

this
}

ModuleBuilder addDependency(String group, String artifact, String version) {
dependencies << new Coordinate(group: group, artifact: artifact, version: version)
dependencies << new Coordinate(
group: group, artifact: artifact, version: version)

this
}

ModuleBuilder addDependency(String group, String artifact, String version, String classifier, String extension) {
dependencies << new Coordinate(
group: group, artifact: artifact, version: version, classifier: classifier, extension: extension)

this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,59 @@ class DependencyGraphBuilderSpec extends Specification {
foo.group == 'test.nebula'
foo.artifact == 'foo'
foo.version == '1.0.0'
foo.classifier == null
foo.extension == null
}

def 'add one dependency with classifier'() {
def builder = new DependencyGraphBuilder()
builder.addModule('test.nebula:foo:1.0.0:bar')

when:
DependencyGraph graph = builder.build()

then:
graph.nodes.size() == 1
Coordinate foo = graph.nodes.find().coordinate
foo.group == 'test.nebula'
foo.artifact == 'foo'
foo.version == '1.0.0'
foo.classifier == 'bar'
foo.extension == null
}

def 'add one dependency with extension'() {
def builder = new DependencyGraphBuilder()
builder.addModule('test.nebula:foo:1.0.0@zip')

when:
DependencyGraph graph = builder.build()

then:
graph.nodes.size() == 1
Coordinate foo = graph.nodes.find().coordinate
foo.group == 'test.nebula'
foo.artifact == 'foo'
foo.version == '1.0.0'
foo.classifier == null
foo.extension == 'zip'
}

def 'add one dependency with classifier, extension'() {
def builder = new DependencyGraphBuilder()
builder.addModule('test.nebula:foo:1.0.0:bar@zip')

when:
DependencyGraph graph = builder.build()

then:
graph.nodes.size() == 1
Coordinate foo = graph.nodes.find().coordinate
foo.group == 'test.nebula'
foo.artifact == 'foo'
foo.version == '1.0.0'
foo.classifier == 'bar'
foo.extension == 'zip'
}

def 'add one dependency with group, artifact, version syntax'() {
Expand All @@ -31,6 +84,25 @@ class DependencyGraphBuilderSpec extends Specification {
foo.group == 'test.nebula'
foo.artifact == 'foo'
foo.version == '1.0.0'
foo.classifier == null
foo.extension == null
}

def 'add one dependency with group, artifact, version, classifier, extension syntax'() {
def builder = new DependencyGraphBuilder()
builder.addModule('test.nebula', 'foo', '1.0.0', 'bar', 'zip')

when:
DependencyGraph graph = builder.build()

then:
graph.nodes.size() == 1
Coordinate foo = graph.nodes.find().coordinate
foo.group == 'test.nebula'
foo.artifact == 'foo'
foo.version == '1.0.0'
foo.classifier == 'bar'
foo.extension == 'zip'
}

def 'add multiple dependencies'() {
Expand All @@ -47,10 +119,14 @@ class DependencyGraphBuilderSpec extends Specification {
foo.group == 'test.nebula'
foo.artifact == 'foo'
foo.version == '1.0.0'
foo.classifier == null
foo.extension == null
Coordinate bar = graph.nodes.find { it.coordinate.artifact == 'bar' }.coordinate
bar.group == 'a.nebula'
bar.artifact == 'bar'
bar.version == '2.0.0'
bar.classifier == null
bar.extension == null
}

def 'add module with dependencies'() {
Expand Down Expand Up @@ -81,6 +157,8 @@ class DependencyGraphBuilderSpec extends Specification {
dep.group == 'test.nebula'
dep.artifact == 'baz'
dep.version == '23.1.3'
dep.classifier == null
dep.extension == null
}

def 'add module with dependencies, verify modules are not replaced with placeholder'() {
Expand All @@ -100,5 +178,7 @@ class DependencyGraphBuilderSpec extends Specification {
dep.group == 'test.nebula'
dep.artifact == 'baz'
dep.version == '23.1.3'
dep.classifier == null
dep.extension == null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,31 @@ class DependencyGraphSpec extends Specification {
node.group == 'test'
node.artifact == 'foo'
node.version == '1.0.0'
node.classifier == null
node.extension == null
node.dependencies.size() == 0
node.toString() == 'test:foo:1.0.0'
}

def 'node with dependencies'() {
when:
def graph = new DependencyGraph(['test:foo:1.0.0 -> test:bar:1.+'])
def graph = new DependencyGraph(['test:foo:1.0.0:baz@zip -> test:bar:1.+:bat@jar'])

then:
graph.nodes.size() == 1
DependencyGraphNode node = graph.nodes[0]
node.group == 'test'
node.artifact == 'foo'
node.version == '1.0.0'
node.classifier == 'baz'
node.extension == 'zip'
node.dependencies.size() == 1
Coordinate dependency = node.dependencies[0]
dependency.group == 'test'
dependency.artifact == 'bar'
dependency.version == '1.+'
dependency.version == '1.+'
dependency.classifier == 'bat'
dependency.extension == 'jar'
}

def 'node with multiple dependencies'() {
Expand Down
Loading