1
1
package scala .build .options
2
2
3
- import com .github .plokhotnyuk .jsoniter_scala .core ._
4
- import com .github .plokhotnyuk .jsoniter_scala .macros ._
3
+ import com .github .plokhotnyuk .jsoniter_scala .core .*
4
+ import com .github .plokhotnyuk .jsoniter_scala .macros .*
5
5
import coursier .Versions
6
- import coursier .cache .FileCache
7
- import coursier .core .{Version , Versions => CoreVersions }
6
+ import coursier .cache .{ ArtifactError , FileCache }
7
+ import coursier .core .{Module , Repository , Version , Versions as CoreVersions }
8
8
import coursier .util .{Artifact , Task }
9
9
10
- import scala .build .CoursierUtils ._
10
+ import java .io .File
11
+
12
+ import scala .build .CoursierUtils .*
11
13
import scala .build .EitherCps .{either , value }
12
14
import scala .build .errors .{
13
15
BuildException ,
@@ -27,6 +29,44 @@ object ScalaVersionUtil {
27
29
private def scala2Library = cmod " org.scala-lang:scala-library "
28
30
private def scala3Library = cmod " org.scala-lang:scala3-library_3 "
29
31
32
+ extension (cache : FileCache [Task ]) {
33
+ def fileWithTtl0 (artifact : Artifact ): Either [ArtifactError , File ] =
34
+ cache.logger.use {
35
+ try cache.withTtl(0 .seconds).file(artifact).run.unsafeRun()(cache.ec)
36
+ catch {
37
+ case NonFatal (e) => throw new Exception (e)
38
+ }
39
+ }
40
+
41
+ def versionsWithTtl0 (
42
+ module : Module ,
43
+ repositories : Seq [Repository ] = Seq .empty
44
+ ): Versions .Result =
45
+ cache.withTtl(0 .seconds).logger.use {
46
+ val versionsWithModule = Versions (cache)
47
+ .withModule(module)
48
+ val versionsWithRepositories =
49
+ if repositories.nonEmpty then versionsWithModule.withRepositories(repositories)
50
+ else versionsWithModule
51
+ versionsWithRepositories
52
+ .result()
53
+ .unsafeRun()(cache.ec)
54
+ }
55
+ }
56
+
57
+ extension (versionsResult : Versions .Result ) {
58
+ def verify (
59
+ versionString : String ,
60
+ latestSupportedStableVersions : Seq [String ]
61
+ ): Either [BuildException , Unit ] =
62
+ if versionsResult.versions.available.contains(versionString) then Right (())
63
+ else
64
+ Left (new NoValidScalaVersionFoundError (
65
+ versionsResult.versions.available,
66
+ latestSupportedStableVersions
67
+ ))
68
+ }
69
+
30
70
object GetNightly {
31
71
32
72
private object Scala2Repo {
@@ -42,17 +82,13 @@ object ScalaVersionUtil {
42
82
val scala2NightlyRepo =
43
83
" https://scala-ci.typesafe.com/ui/api/v1/ui/nativeBrowser/scala-integration/org/scala-lang/scala-compiler"
44
84
val artifact = Artifact (scala2NightlyRepo).withChanging(true )
45
- val res = cache.logger.use {
46
- try cache.withTtl(0 .seconds).file(artifact).run.unsafeRun()(cache.ec)
47
- catch {
48
- case NonFatal (e) => throw new Exception (e)
85
+ val res = cache.fileWithTtl0(artifact)
86
+ .left.map { err =>
87
+ val msg =
88
+ """ |Unable to compute the latest Scala 2 nightly version.
89
+ |Throws error during downloading web page repository for Scala 2.""" .stripMargin
90
+ new ScalaVersionError (msg, cause = err)
49
91
}
50
- }.left.map { err =>
51
- val msg =
52
- """ |Unable to compute the latest Scala 2 nightly version.
53
- |Throws error during downloading web page repository for Scala 2.""" .stripMargin
54
- new ScalaVersionError (msg, cause = err)
55
- }
56
92
57
93
val res0 = value(res)
58
94
val content = os.read.bytes(os.Path (res0, os.pwd))
@@ -88,12 +124,8 @@ object ScalaVersionUtil {
88
124
cache : FileCache [Task ],
89
125
latestSupportedStableVersions : Seq [String ]
90
126
): Either [BuildException , String ] = {
91
- val res = cache.withTtl(0 .seconds).logger.use {
92
- Versions (cache)
93
- .withModule(scala3Library)
94
- .result()
95
- .unsafeRun()(cache.ec)
96
- }.versions.available.filter(_.endsWith(" -NIGHTLY" ))
127
+ val res = cache.versionsWithTtl0(scala3Library)
128
+ .versions.available.filter(_.endsWith(" -NIGHTLY" ))
97
129
98
130
val threeXNightlies = res.filter(_.startsWith(s " 3. $threeSubBinaryNum. " )).map(Version (_))
99
131
if (threeXNightlies.nonEmpty) Right (threeXNightlies.max.repr)
@@ -105,15 +137,11 @@ object ScalaVersionUtil {
105
137
/** @return
106
138
* Either a BuildException or the calculated (ScalaVersion, ScalaBinaryVersion) tuple
107
139
*/
108
- def scala3 (cache : FileCache [Task ]): Either [BuildException , String ] = {
109
- val res = cache.withTtl(0 .seconds).logger.use {
110
- Versions (cache)
111
- .withModule(scala3Library)
112
- .result()
113
- .unsafeRun()(cache.ec)
114
- }
115
- latestScalaVersionFrom(res.versions, " latest Scala 3 nightly build" )
116
- }
140
+ def scala3 (cache : FileCache [Task ]): Either [BuildException , String ] =
141
+ latestScalaVersionFrom(
142
+ cache.versionsWithTtl0(scala3Library).versions,
143
+ " latest Scala 3 nightly build"
144
+ )
117
145
118
146
private def latestScalaVersionFrom (
119
147
versions : CoreVersions ,
@@ -136,40 +164,16 @@ object ScalaVersionUtil {
136
164
versionString : String ,
137
165
cache : FileCache [Task ],
138
166
latestSupportedStableVersions : Seq [String ]
139
- ): Either [BuildException , Unit ] = {
140
- val res = cache.withTtl(0 .seconds).logger.use {
141
- Versions (cache)
142
- .withModule(scala2Library)
143
- .withRepositories(Seq (coursier.Repositories .scalaIntegration))
144
- .result()
145
- .unsafeRun()(cache.ec)
146
- }
147
- if (res.versions.available.contains(versionString))
148
- Right (())
149
- else
150
- Left (
151
- new NoValidScalaVersionFoundError (res.versions.available, latestSupportedStableVersions)
152
- )
153
- }
167
+ ): Either [BuildException , Unit ] =
168
+ cache.versionsWithTtl0(scala2Library, Seq (coursier.Repositories .scalaIntegration))
169
+ .verify(versionString, latestSupportedStableVersions)
154
170
155
171
def scala3 (
156
172
versionString : String ,
157
173
cache : FileCache [Task ],
158
174
latestSupportedStableVersions : Seq [String ]
159
- ): Either [BuildException , Unit ] = {
160
- val res = cache.withTtl(0 .seconds).logger.use {
161
- Versions (cache)
162
- .withModule(scala3Library)
163
- .result()
164
- .unsafeRun()(cache.ec)
165
- }
166
- if (res.versions.available.contains(versionString))
167
- Right (())
168
- else
169
- Left (
170
- new NoValidScalaVersionFoundError (res.versions.available, latestSupportedStableVersions)
171
- )
172
- }
175
+ ): Either [BuildException , Unit ] =
176
+ cache.versionsWithTtl0(scala3Library).verify(versionString, latestSupportedStableVersions)
173
177
}
174
178
175
179
def validateNonStable (
@@ -293,9 +297,6 @@ object ScalaVersionUtil {
293
297
}
294
298
295
299
extension (sv : String ) {
296
- def maybeScalaPatchVersion : Option [Int ] = sv
297
- .split('.' ).drop(2 ).headOption
298
- .flatMap(_.split('-' ).headOption)
299
- .flatMap(pv => Try (pv.toInt).toOption)
300
+ def asVersion : Version = Version (sv)
300
301
}
301
302
}
0 commit comments