Skip to content

Commit fe68250

Browse files
authored
Merge pull request sbt#361 from adpi2/dotty-compat
Add for3Use2_13 and for2_13Use3
2 parents 42aae72 + 2b1806a commit fe68250

File tree

6 files changed

+214
-3
lines changed

6 files changed

+214
-3
lines changed

core/src/main/contraband-scala/sbt/librarymanagement/LibraryManagementCodec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ trait LibraryManagementCodec extends sjsonnew.BasicJsonProtocol
1919
with sbt.librarymanagement.ConstantFormats
2020
with sbt.librarymanagement.PatchFormats
2121
with sbt.librarymanagement.FullFormats
22+
with sbt.librarymanagement.For3Use2_13Formats
23+
with sbt.librarymanagement.For2_13Use3Formats
2224
with sbt.librarymanagement.InclExclRuleFormats
2325
with sbt.librarymanagement.ModuleIDFormats
2426
with sbt.librarymanagement.ConfigurationFormats

core/src/main/scala/sbt/librarymanagement/CrossVersion.scala

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,82 @@ object Full {
172172
def apply(prefix: String, suffix: String): Full = new Full(prefix, suffix)
173173
}
174174

175+
/**
176+
* Similar to Binary except that if the binary version is 3
177+
* (or if it is of the form 3.0.0-x) it uses 2.13 instead.
178+
* For example, if `prefix = "foo_"` and `suffix = "_bar"` and the binary version is "3",
179+
* the module is cross-versioned with "foo_2.13_bar".
180+
*/
181+
final class For3Use2_13 private (val prefix: String, val suffix: String)
182+
extends sbt.librarymanagement.CrossVersion()
183+
with Serializable {
184+
185+
private def this() = this("", "")
186+
187+
override def equals(o: Any): Boolean = o match {
188+
case x: For3Use2_13 => (this.prefix == x.prefix) && (this.suffix == x.suffix)
189+
case _ => false
190+
}
191+
override def hashCode: Int = {
192+
37 * (37 * (37 * (17 + "sbt.librarymanagement.For3Use2_13".##) + prefix.##) + suffix.##)
193+
}
194+
override def toString: String = {
195+
"For3Use2_13(" + prefix + ", " + suffix + ")"
196+
}
197+
private[this] def copy(prefix: String = prefix, suffix: String = suffix): For3Use2_13 = {
198+
new For3Use2_13(prefix, suffix)
199+
}
200+
def withPrefix(prefix: String): For3Use2_13 = {
201+
copy(prefix = prefix)
202+
}
203+
def withSuffix(suffix: String): For3Use2_13 = {
204+
copy(suffix = suffix)
205+
}
206+
}
207+
object For3Use2_13 {
208+
209+
def apply(): For3Use2_13 = new For3Use2_13()
210+
def apply(prefix: String, suffix: String): For3Use2_13 = new For3Use2_13(prefix, suffix)
211+
}
212+
213+
/**
214+
* Similar to Binary except that if the binary version is 2.13
215+
* it uses 3 instead.
216+
* For example, if `prefix = "foo_"` and `suffix = "_bar"` and the binary version is "2.13",
217+
* the module is cross-versioned with "foo_3_bar".
218+
*/
219+
final class For2_13Use3 private (val prefix: String, val suffix: String)
220+
extends sbt.librarymanagement.CrossVersion()
221+
with Serializable {
222+
223+
private def this() = this("", "")
224+
225+
override def equals(o: Any): Boolean = o match {
226+
case x: For2_13Use3 => (this.prefix == x.prefix) && (this.suffix == x.suffix)
227+
case _ => false
228+
}
229+
override def hashCode: Int = {
230+
37 * (37 * (37 * (17 + "sbt.librarymanagement.For3Use2_13".##) + prefix.##) + suffix.##)
231+
}
232+
override def toString: String = {
233+
"For3Use2_13(" + prefix + ", " + suffix + ")"
234+
}
235+
private[this] def copy(prefix: String = prefix, suffix: String = suffix): For2_13Use3 = {
236+
new For2_13Use3(prefix, suffix)
237+
}
238+
def withPrefix(prefix: String): For2_13Use3 = {
239+
copy(prefix = prefix)
240+
}
241+
def withSuffix(suffix: String): For2_13Use3 = {
242+
copy(suffix = suffix)
243+
}
244+
}
245+
object For2_13Use3 {
246+
247+
def apply(): For2_13Use3 = new For2_13Use3()
248+
def apply(prefix: String, suffix: String): For2_13Use3 = new For2_13Use3(prefix, suffix)
249+
}
250+
175251
trait DisabledFormats { self: sjsonnew.BasicJsonProtocol =>
176252
implicit lazy val DisabledFormat: JsonFormat[sbt.librarymanagement.Disabled] =
177253
new JsonFormat[sbt.librarymanagement.Disabled] {
@@ -324,22 +400,80 @@ trait FullFormats { self: sjsonnew.BasicJsonProtocol =>
324400
}
325401
}
326402

403+
trait For3Use2_13Formats { self: sjsonnew.BasicJsonProtocol =>
404+
implicit lazy val For3Use2_13Format: JsonFormat[sbt.librarymanagement.For3Use2_13] =
405+
new JsonFormat[sbt.librarymanagement.For3Use2_13] {
406+
override def read[J](
407+
jsOpt: Option[J],
408+
unbuilder: Unbuilder[J]
409+
): sbt.librarymanagement.For3Use2_13 = {
410+
jsOpt match {
411+
case Some(js) =>
412+
unbuilder.beginObject(js)
413+
val prefix = unbuilder.readField[String]("prefix")
414+
val suffix = unbuilder.readField[String]("suffix")
415+
unbuilder.endObject()
416+
sbt.librarymanagement.For3Use2_13(prefix, suffix)
417+
case None =>
418+
deserializationError("Expected JsObject but found None")
419+
}
420+
}
421+
override def write[J](obj: sbt.librarymanagement.For3Use2_13, builder: Builder[J]): Unit = {
422+
builder.beginObject()
423+
builder.addField("prefix", obj.prefix)
424+
builder.addField("suffix", obj.suffix)
425+
builder.endObject()
426+
}
427+
}
428+
}
429+
430+
trait For2_13Use3Formats { self: sjsonnew.BasicJsonProtocol =>
431+
implicit lazy val For2_13Use3Format: JsonFormat[sbt.librarymanagement.For2_13Use3] =
432+
new JsonFormat[sbt.librarymanagement.For2_13Use3] {
433+
override def read[J](
434+
jsOpt: Option[J],
435+
unbuilder: Unbuilder[J]
436+
): sbt.librarymanagement.For2_13Use3 = {
437+
jsOpt match {
438+
case Some(js) =>
439+
unbuilder.beginObject(js)
440+
val prefix = unbuilder.readField[String]("prefix")
441+
val suffix = unbuilder.readField[String]("suffix")
442+
unbuilder.endObject()
443+
sbt.librarymanagement.For2_13Use3(prefix, suffix)
444+
case None =>
445+
deserializationError("Expected JsObject but found None")
446+
}
447+
}
448+
override def write[J](obj: sbt.librarymanagement.For2_13Use3, builder: Builder[J]): Unit = {
449+
builder.beginObject()
450+
builder.addField("prefix", obj.prefix)
451+
builder.addField("suffix", obj.suffix)
452+
builder.endObject()
453+
}
454+
}
455+
}
456+
327457
trait CrossVersionFormats {
328458
self: sjsonnew.BasicJsonProtocol
329459
with sbt.librarymanagement.DisabledFormats
330460
with sbt.librarymanagement.BinaryFormats
331461
with sbt.librarymanagement.ConstantFormats
332462
with sbt.librarymanagement.PatchFormats
333-
with sbt.librarymanagement.FullFormats =>
463+
with sbt.librarymanagement.FullFormats
464+
with sbt.librarymanagement.For3Use2_13Formats
465+
with sbt.librarymanagement.For2_13Use3Formats =>
334466
implicit lazy val CrossVersionFormat: JsonFormat[CrossVersion] = {
335-
val format = flatUnionFormat6[
467+
val format = flatUnionFormat8[
336468
CrossVersion,
337469
Disabled,
338470
Disabled.type,
339471
Binary,
340472
Constant,
341473
Patch,
342-
Full
474+
Full,
475+
For3Use2_13,
476+
For2_13Use3
343477
]("type")
344478
// This is a hand-crafted formatter to avoid Disabled$ showing up in JSON
345479
new JsonFormat[CrossVersion] {

core/src/main/scala/sbt/librarymanagement/CrossVersionExtra.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ private[librarymanagement] abstract class CrossVersionFunctions {
1717
final val Constant = sbt.librarymanagement.Constant
1818
final val Full = sbt.librarymanagement.Full
1919
final val Patch = sbt.librarymanagement.Patch
20+
final val For3Use2_13 = sbt.librarymanagement.For3Use2_13
21+
final val For2_13Use3 = sbt.librarymanagement.For2_13Use3
2022
type Binary = sbt.librarymanagement.Binary
2123
type Constant = sbt.librarymanagement.Constant
2224
type Full = sbt.librarymanagement.Full
2325
type Patch = sbt.librarymanagement.Patch
26+
type For3Use2_13 = sbt.librarymanagement.For3Use2_13
27+
type For2_13Use3 = sbt.librarymanagement.For2_13Use3
2428

2529
/** The first `major.minor` Scala version that the Scala binary version should be used for cross-versioning instead of the full version. */
2630
val TransitionScalaVersion = CrossVersionUtil.TransitionScalaVersion
@@ -57,6 +61,32 @@ private[librarymanagement] abstract class CrossVersionFunctions {
5761
*/
5862
def patch: CrossVersion = Patch()
5963

64+
/**
65+
* Cross-versions a module with the binary version but
66+
* if the binary version is 3 (or of the form 3.0.0-x), cross-versions it with 2.13 instead
67+
*/
68+
def for3Use2_13: CrossVersion = For3Use2_13()
69+
70+
/**
71+
* Cross-versions a module with the binary version but
72+
* if the binary version is 3 (or of the form 3.0.0-x), cross-versions it with 2.13 instead
73+
* Always prepend `prefix` and append `suffix`
74+
*/
75+
def for3Use2_13With(prefix: String, suffix: String): CrossVersion = For3Use2_13(prefix, suffix)
76+
77+
/**
78+
* Cross-versions a module with the binary version but
79+
* if the binary version is 2.13 cross-versions it with 3 instead
80+
*/
81+
def for2_13Use3: CrossVersion = For2_13Use3()
82+
83+
/**
84+
* Cross-versions a module with the binary version but
85+
* if the binary version is 2.13 cross-versions it with 3 instead
86+
* Always prepend `prefix` and append `suffix`
87+
*/
88+
def for2_13Use3With(prefix: String, suffix: String): CrossVersion = For2_13Use3(prefix, suffix)
89+
6090
private[sbt] def patchFun(fullVersion: String): String = {
6191
val BinCompatV = """(\d+)\.(\d+)\.(\d+)(-\w+)??-bin(-.*)?""".r
6292
fullVersion match {
@@ -83,6 +113,16 @@ private[librarymanagement] abstract class CrossVersionFunctions {
83113
case c: Constant => append(c.value)
84114
case _: Patch => append(patchFun(fullVersion))
85115
case f: Full => append(f.prefix + fullVersion + f.suffix)
116+
case c: For3Use2_13 =>
117+
val compat =
118+
if (binaryVersion == "3" || binaryVersion.startsWith("3.0.0")) "2.13"
119+
else binaryVersion
120+
append(c.prefix + compat + c.suffix)
121+
case c: For2_13Use3 =>
122+
val compat =
123+
if (binaryVersion == "2.13") "3"
124+
else binaryVersion
125+
append(c.prefix + compat + c.suffix)
86126
}
87127

88128
/** Constructs the cross-version function defined by `module` and `is`, if one is configured. */

core/src/test/scala/sbt/librarymanagement/CrossVersionTest.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,29 @@ class CrossVersionTest extends UnitSpec {
279279
"CrossVersion.constant" should "have structural equality" in {
280280
CrossVersion.constant("duck") shouldBe CrossVersion.constant("duck")
281281
}
282+
283+
"CrossVersion.for3Use2_13" should "have structural equality" in {
284+
CrossVersion.for3Use2_13 shouldBe CrossVersion.for3Use2_13
285+
CrossVersion.for3Use2_13With("_sjs1", "") shouldBe CrossVersion.for3Use2_13With("_sjs1", "")
286+
}
287+
it should "use the cross version 2.13 instead of 3" in {
288+
CrossVersion(CrossVersion.for3Use2_13, "3.0.0", "3").map(_("artefact")) shouldBe Some(
289+
"artefact_2.13"
290+
)
291+
}
292+
it should "use the cross version 2.13 instead of 3.0.0-M3" in {
293+
CrossVersion(CrossVersion.for3Use2_13, "3.0.0-M3", "3.0.0-M3").map(_("artefact")) shouldBe Some(
294+
"artefact_2.13"
295+
)
296+
}
297+
298+
"CrossVersion.for2_13Use3" should "have structural equality" in {
299+
CrossVersion.for2_13Use3 shouldBe CrossVersion.for2_13Use3
300+
CrossVersion.for2_13Use3With("_sjs1", "") shouldBe CrossVersion.for2_13Use3With("_sjs1", "")
301+
}
302+
it should "use the cross version 3 instead of 2.13" in {
303+
CrossVersion(CrossVersion.for2_13Use3, "2.13.4", "2.13").map(_("artefact")) shouldBe Some(
304+
"artefact_3"
305+
)
306+
}
282307
}

ivy/src/test/scala/sbt/internal/librarymanagement/DMSerializationSpec.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ object DMSerializationSpec extends BasicTestSuite {
1818
roundtripStr(CrossVersion.binary: CrossVersion)
1919
}
2020

21+
test("CrossVersion.for3Use2_13 should roundtrip") {
22+
roundtripStr(CrossVersion.for3Use2_13: CrossVersion)
23+
}
24+
25+
test("CrossVersion.for2_13Use3 with prefix should roundtrip") {
26+
roundtripStr(CrossVersion.for2_13Use3With("_sjs1", ""): CrossVersion)
27+
}
28+
2129
test("CrossVersion.Disabled should roundtrip") {
2230
roundtrip(Disabled(): CrossVersion)
2331
}

project/DatatypeConfig.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ object DatatypeConfig {
4949
"sbt.librarymanagement.ConstantFormats" ::
5050
"sbt.librarymanagement.PatchFormats" ::
5151
"sbt.librarymanagement.FullFormats" ::
52+
"sbt.librarymanagement.For3Use2_13Formats" ::
53+
"sbt.librarymanagement.For2_13Use3Formats" ::
5254
Nil
5355
}
5456

0 commit comments

Comments
 (0)