Skip to content

Commit c92c9a8

Browse files
committed
Use proper Ivy notation rather than MAX_VERSION kludge (#364)
1 parent 07370f4 commit c92c9a8

File tree

4 files changed

+87
-46
lines changed

4 files changed

+87
-46
lines changed

core-plugin/src/integTest/groovy/com/github/jrubygradle/api/core/IvyXmlProxyServerIntegrationSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class IvyXmlProxyServerIntegrationSpec extends Specification {
5959

6060
then:
6161
result.output.contains('rubygems:credit_card_validator:1.3.2')
62-
result.output.contains('rubygems:base_app:[1.0.5,99999.0.0] ->')
62+
result.output.contains('rubygems:base_app:[1.0.5,) ->')
6363
}
6464

6565
void 'Download a collection of GEMs'() {

core-plugin/src/main/groovy/com/github/jrubygradle/api/gems/GemVersion.groovy

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ import static com.github.jrubygradle.api.gems.GemVersion.Boundary.OPEN_ENDED
2020
*
2121
* When converting a GemSpec into a Ivy ivy.xml the translation of a
2222
* gem version range into an Ivy version range. typically '~> 1.0' from ruby
23-
* becomes [1.0, 1.99999] or 1.0.+ on the Ivy side. so most dependencies from
23+
* becomes {@code [1.0.0,2.0[} on the Ivy side. so most dependencies from
2424
* gem artifacts will use such version ranges.
2525
*
2626
* To help gradle to be closer to the rubygems world when resolving gem
2727
* artifacts, it needs to calculate intersection between version ranges
2828
* in maven manner.
2929
*
30-
* this class basically represents an Ivy version range with boundary
31-
* (exclusive vs. inclusive) and its lower and upper bounded version and
30+
* This class basically represents an Ivy version range with boundary
31+
* (exclusive vs. inclusive or open-ended) and its lower and upper bounded version and
3232
* allows to intersect its range with another version range.
3333
*
34-
* it also translate fixed version '1.0' to [1.0, 1.0] or the gradle notation
34+
* It also translate fixed version '1.0' to [1.0, 1.0] or the gradle notation
3535
* 1.2+ to [1.2, 1.99999] or 1.+ to [1.0, 1.99999] following the gemspec-to-pom
3636
* pattern.
3737
*
@@ -125,54 +125,44 @@ class GemVersion implements Comparable<GemVersion> {
125125
static GemVersion gemVersionFromGemRequirement(String singleRequirement) {
126126
if (singleRequirement.matches(GREATER_EQUAL)) {
127127
new GemVersion(
128-
true,
128+
INCLUSIVE,
129129
getVersionFromRequirement(singleRequirement, GREATER_EQUAL),
130-
"${MAX_VERSION.toString()}.0.0",
131-
true
130+
null,
131+
OPEN_ENDED
132132
)
133133
} else if (singleRequirement.matches(GREATER)) {
134134
new GemVersion(
135-
false,
135+
EXCLUSIVE,
136136
getVersionFromRequirement(singleRequirement, GREATER),
137-
"${MAX_VERSION.toString()}.0.0",
138-
true
137+
null,
138+
OPEN_ENDED
139139
)
140140
} else if (singleRequirement.matches(EQUAL)) {
141141
String exact = getVersionFromRequirement(singleRequirement, EQUAL)
142142
new GemVersion(
143-
true,
143+
INCLUSIVE,
144144
exact,
145145
exact,
146-
true
147-
)
148-
} else if (singleRequirement.matches(LESS)) {
149-
new GemVersion(
150-
true,
151-
'0.0.0',
152-
getVersionFromRequirement(singleRequirement, LESS),
153-
false
146+
INCLUSIVE
154147
)
155148
} else if (singleRequirement.matches(LESS_EQUAL)) {
156149
new GemVersion(
157-
true,
158-
'0.0.0',
150+
OPEN_ENDED,
151+
null,
159152
getVersionFromRequirement(singleRequirement, LESS_EQUAL),
160-
true
153+
INCLUSIVE
161154
)
162-
} else if (singleRequirement.matches(TWIDDLE_WAKKA)) {
163-
String base = getVersionFromRequirement(singleRequirement, TWIDDLE_WAKKA)
164-
int adds = 3 - base.tokenize('.').size()
165-
if (adds < 0) {
166-
adds = 0
167-
}
155+
} else if (singleRequirement.matches(LESS)) {
168156
new GemVersion(
169-
true,
170-
"${base}${'.0' * adds} ",
171-
"${base}${('.' + MAX_VERSION) * adds}",
172-
true
157+
OPEN_ENDED,
158+
null,
159+
getVersionFromRequirement(singleRequirement, LESS),
160+
EXCLUSIVE
173161
)
162+
} else if (singleRequirement.matches(TWIDDLE_WAKKA)) {
163+
parseTwiddleWakka(singleRequirement)
174164
} else {
175-
throw new GemVersionException("Do not not how to process ${singleRequirement} as a version string")
165+
throw new GemVersionException("'${singleRequirement}' does not look like a GEM version requirement")
176166
}
177167
}
178168

@@ -356,19 +346,53 @@ class GemVersion implements Comparable<GemVersion> {
356346
}
357347
}
358348

349+
private static GemVersion parseTwiddleWakka(String singleRequirement) {
350+
String base = getVersionFromRequirement(singleRequirement, TWIDDLE_WAKKA)
351+
List<String> parts = base.tokenize('.')
352+
if(1 == parts) {
353+
throw new GemVersionException(
354+
"'${singleRequirement}' does not look like a correctly formattedGEM twiddle-wakka requirement"
355+
)
356+
}
357+
String lastNumberPart = parts[0..-2].reverse().find {
358+
it =~ ONLY_DIGITS
359+
}
360+
if(lastNumberPart == null) {
361+
throw new GemVersionException("Cannot extract last number part from '${singleRequirement}'. " +
362+
'This does not look like a standard GEM version requirement')
363+
}
364+
int bottomAdds = 3 - parts.size()
365+
if (bottomAdds < 0) {
366+
bottomAdds = 0
367+
}
368+
try {
369+
Integer nextUp = lastNumberPart.toInteger() + 1
370+
String leader = parts.size() <= 2 ? '' : "${parts[0..-3].join('.')}."
371+
new GemVersion(
372+
INCLUSIVE,
373+
"${base}${'.0' * bottomAdds}",
374+
"${leader}${nextUp}.0",
375+
EXCLUSIVE
376+
)
377+
} catch (NumberFormatException e) {
378+
throw new GemVersionException("Can extract last number part from '${singleRequirement}'. " +
379+
'This does not look like a standard GEM version requirement', e)
380+
}
381+
}
382+
359383
@CompileDynamic
360384
@SuppressWarnings('NoDef')
361385
private static String getVersionFromRequirement(String gemRevision, Pattern matchPattern) {
362386
def matcher = gemRevision =~ matchPattern
363387
matcher[0][1]
364388
}
365389

366-
private GemVersion(Boolean lowInclusive, String low, String high, Boolean highInclusive) {
367-
this.lowBoundary = lowInclusive ? INCLUSIVE : EXCLUSIVE
368-
this.low = low
369-
this.high = high
370-
this.highBoundary = highInclusive ? INCLUSIVE : EXCLUSIVE
371-
}
390+
// private GemVersion(Boolean lowInclusive, String low, String high, Boolean highInclusive) {
391+
// this.lowBoundary = lowInclusive ? INCLUSIVE : EXCLUSIVE
392+
// this.low = low
393+
// this.high = high
394+
// this.highBoundary = highInclusive ? INCLUSIVE : EXCLUSIVE
395+
// }
372396

373397
private GemVersion(Boundary pre, String low, String high, Boundary post) {
374398
this.lowBoundary = pre

core-plugin/src/main/groovy/com/github/jrubygradle/internal/gems/GemToIvy.groovy

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,12 @@ class GemToIvy {
112112
}
113113

114114
if (reqs.size() > 1) {
115-
ivyFormatFromRange(reqs.min().union(reqs.max()))
115+
reqs.min().union(reqs.max()).toString()
116116
} else {
117-
ivyFormatFromRange(reqs[0])
117+
reqs[0].toString()
118118
}
119119
}
120120

121-
private String ivyFormatFromRange(GemVersion range) {
122-
"${range.lowInclusive ? '[' : ']'}${range.low},${range.high}${range.highInclusive ? ']' : '['}"
123-
}
124-
125121
private final String serverUri
126122
private final String org = 'rubygems'
127123
}

core-plugin/src/test/groovy/com/github/jrubygradle/api/gems/GemVersionSpec.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ import spock.lang.Unroll
77
import static com.github.jrubygradle.api.gems.GemVersion.gemVersionFromGradleIvyRequirement
88

99
class GemVersionSpec extends Specification {
10+
11+
@Unroll
12+
void "#gemRequirement (gem) ⇒ #ivyNotation (ivy)"() {
13+
when:
14+
String ivy = GemVersion.gemVersionFromGemRequirement(gemRequirement).toString()
15+
16+
then:
17+
ivy == ivyNotation
18+
19+
where:
20+
gemRequirement | ivyNotation
21+
'= 1.0.0' | '1.0.0'
22+
'> 2.0' | ']2.0,)'
23+
'>= 2.2.0' | '[2.2.0,)'
24+
'<= 3.0' | '(,3.0]'
25+
'< 2.3.0' | '(,2.3.0['
26+
'~> 1.0' | '[1.0.0,2.0['
27+
'~> 2.2' | '[2.2.0,3.0['
28+
'~> 2.2.0' | '[2.2.0,2.3.0['
29+
}
30+
1031
@Unroll
1132
void "toString: #ivyVer (ivy) ⇒ #ivyRange (range)"() {
1233
when:

0 commit comments

Comments
 (0)