Skip to content

Commit

Permalink
Merge pull request #70 from heremaps/script-order
Browse files Browse the repository at this point in the history
Script order
  • Loading branch information
sschuberth authored Feb 7, 2018
2 parents 1564e8c + 3c99d2d commit 55d7e87
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## 3.1.0 (under development)

- Upgrade job-dsl-core to 1.67.
- Process Job DSL scripts in specific order. Scripts from the same folder are processed in alphabetical order and before
any scripts from subfolders. Subfolders are also processed in alphabetical order.

## 3.0.0 (2018-01-10)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ class GenerateXmlTest extends AbstractTaskTest {
new File(testProjectDir.root, 'build/jobdsl/xml/folder/job.xml').file
}

def 'creating job and folder in separate files works'() {
given:
buildFile << readBuildGradle('generateXml/build.gradle')
testProjectDir.newFolder('src', 'jobdsl', 'folder')
copyResourceToTestDir('generateXml/job-in-folder-without-folder.groovy', 'src/jobdsl/folder/job.groovy')
copyResourceToTestDir('generateXml/folder.groovy', 'src/jobdsl/folder.groovy')

when:
def result = gradleRunner
.withArguments('dslGenerateXml', '--stacktrace')
.build()

then:
result.task(':dslGenerateXml').outcome == TaskOutcome.SUCCESS

new File(testProjectDir.root, 'build/jobdsl/xml/folder.xml').file
new File(testProjectDir.root, 'build/jobdsl/xml/folder/job.xml').file
}

def 'filter applies to folders'() {
given:
buildFile << readBuildGradle('generateXml/build.gradle')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
freeStyleJob('folder/job')
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.here.gradle.plugins.jobdsl.tasks

import com.here.gradle.plugins.jobdsl.ServerDefinition
import com.here.gradle.plugins.jobdsl.util.PathComparator
import groovy.json.JsonBuilder
import org.gradle.api.GradleException
import org.gradle.api.internal.tasks.options.Option
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.JavaExec
import org.gradle.util.CollectionUtils

/**
* Shared code for all tasks of the plugin that call an implementation of
Expand Down Expand Up @@ -46,11 +48,14 @@ abstract class AbstractDslTask extends JavaExec {
}
}

def groovyFiles = project.sourceSets.jobdsl.allGroovy.files*.absolutePath
groovyFiles.sort(new PathComparator())

def properties = getProperties()
properties['buildDirectory'] = buildDirectoryPath
properties['configuration'] = encodeBase64(new JsonBuilder(project.jobdsl.configuration).toString())
properties['filter'] = encodeBase64(filter)
properties['inputFiles'] = project.sourceSets.jobdsl.allGroovy.asPath
properties['inputFiles'] = CollectionUtils.join(File.pathSeparator, groovyFiles)
properties['serverConfiguration'] = server != null ?
encodeBase64(new JsonBuilder(server.configuration).toString()) : ''
systemProperties = properties
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.here.gradle.plugins.jobdsl.util

/**
* A comparator that orders path names. Files in the same directory are ordered alphabetically, and always come before
* files in subdirectories. Subdirectories are also ordered alphabetically.
*/
class PathComparator implements Comparator<String> {
@Override
int compare(String o1, String o2) {
def tokens1 = o1.tokenize('/')
def tokens2 = o2.tokenize('/')
for (int i = 0; i < Math.max(tokens1.size(), tokens2.size()); ++i) {
def hasNext1 = i + 1 < tokens1.size()
def hasNext2 = i + 1 < tokens2.size()
def comp = tokens1[i] <=> tokens2[i]
if (hasNext1 && hasNext2) {
if (comp != 0) {
return comp
}
} else if (hasNext1) {
return 1
} else if (hasNext2) {
return -1
} else {
return comp
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.here.gradle.plugins.jobdsl.util

import spock.lang.Specification

/**
* Tests for the {@link com.here.gradle.plugins.jobdsl.util.PathComparator}.
*/
class PathComparatorSpec extends Specification {

def "files in same folder are ordered alphabetically"() {
given:
def list = ['c', 'a', 'd', 'b']

when:
list.sort(new PathComparator())

then:
list.join(', ') == ['a', 'b', 'c', 'd'].join(', ')
}

def "files in subfolders come after files in parent folders"() {
given:
def list = ['a', 'a/a', 'a/b', 'a/a/a', 'b', 'b/a', 'b/a/a', 'c']

when:
list.sort(new PathComparator())

then:
list.join(', ') == ['a', 'b', 'c', 'a/a', 'a/b', 'a/a/a', 'b/a', 'b/a/a'].join(', ')
}

}

0 comments on commit 55d7e87

Please sign in to comment.