-
Notifications
You must be signed in to change notification settings - Fork 9
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 #70 from heremaps/script-order
Script order
- Loading branch information
Showing
6 changed files
with
89 additions
and
1 deletion.
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
1 change: 1 addition & 0 deletions
1
plugin/src/functionalTest/resources/generateXml/job-in-folder-without-folder.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 @@ | ||
freeStyleJob('folder/job') |
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
29 changes: 29 additions & 0 deletions
29
plugin/src/main/groovy/com/here/gradle/plugins/jobdsl/util/PathComparator.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,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 | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
plugin/src/test/groovy/com/here/gradle/plugins/jobdsl/util/PathComparatorSpec.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,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(', ') | ||
} | ||
|
||
} |