Skip to content

Commit 6019787

Browse files
committed
NIT Refactor ScalaVersionUtil
1 parent 4ac17b5 commit 6019787

File tree

4 files changed

+71
-83
lines changed

4 files changed

+71
-83
lines changed

modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import scala.build.interactive.Interactive
1818
import scala.build.interactive.Interactive.{InteractiveAsk, InteractiveNop}
1919
import scala.build.internal.CsLoggerUtil.*
2020
import scala.build.internal.{Constants, FetchExternalBinary, OsLibc, Util}
21+
import scala.build.options.ScalaVersionUtil.fileWithTtl0
2122
import scala.build.options.{Platform, ScalacOpt, ShadowingSeq}
2223
import scala.build.options as bo
2324
import scala.cli.ScalaCli
@@ -36,14 +37,9 @@ object SharedOptionsUtil extends CommandHelpers {
3637
private def downloadInputs(cache: FileCache[Task]): String => Either[String, Array[Byte]] = {
3738
url =>
3839
val artifact = Artifact(url).withChanging(true)
39-
val res = cache.logger.use {
40-
try cache.withTtl(0.seconds).file(artifact).run.unsafeRun()(cache.ec)
41-
catch {
42-
case NonFatal(e) => throw new Exception(e)
43-
}
44-
}
45-
res
46-
.left.map(_.describe)
40+
cache.fileWithTtl0(artifact)
41+
.left
42+
.map(_.describe)
4743
.map(f => os.read.bytes(os.Path(f, Os.pwd)))
4844
}
4945

modules/cli/src/main/scala/scala/cli/launcher/LauncherCli.scala

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dependency._
88

99
import scala.build.internal.CsLoggerUtil.CsCacheExtensions
1010
import scala.build.internal.{Constants, OsLibc, Runner}
11+
import scala.build.options.ScalaVersionUtil.fileWithTtl0
1112
import scala.build.options.{BuildOptions, JavaOptions}
1213
import scala.build.{Artifacts, Os, Positioned}
1314
import scala.cli.commands.util.CommonOps._
@@ -83,14 +84,7 @@ object LauncherCli {
8384
val snapshotRepoUrl =
8485
s"https://oss.sonatype.org/content/repositories/snapshots/org/virtuslab/scala-cli/cli_${scalaParameters.scalaBinaryVersion}/"
8586
val artifact = Artifact(snapshotRepoUrl).withChanging(true)
86-
val res = cache.logger.use {
87-
try cache.withTtl(0.seconds).file(artifact).run.unsafeRun()(cache.ec)
88-
catch {
89-
case NonFatal(e) => throw new Exception(e)
90-
}
91-
}
92-
93-
res match {
87+
cache.fileWithTtl0(artifact) match {
9488
case Left(_) =>
9589
System.err.println("Unable to find nightly Scala CLI version")
9690
sys.exit(1)

modules/options/src/main/scala/scala/build/actionable/ActionableDependencyHandler.scala

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import scala.build.actionable.errors.ActionableHandlerError
1010
import scala.build.errors.BuildException
1111
import scala.build.internal.Util._
1212
import scala.build.options.BuildOptions
13+
import scala.build.options.ScalaVersionUtil.versionsWithTtl0
1314
import scala.concurrent.duration.DurationInt
1415

1516
case object ActionableDependencyHandler
@@ -43,15 +44,8 @@ case object ActionableDependencyHandler
4344
val cache = buildOptions.finalCache
4445
val csModule = value(dependency.toCs(scalaParams)).module
4546

46-
val res = cache.withTtl(0.seconds).logger.use {
47-
Versions(cache)
48-
.withModule(csModule)
49-
.result()
50-
.unsafeRun()(cache.ec)
51-
}
52-
5347
value {
54-
res.versions.latest(coursier.core.Latest.Release).toRight {
48+
cache.versionsWithTtl0(csModule).versions.latest(coursier.core.Latest.Release).toRight {
5549
new ActionableHandlerError(s"No latest version found for ${dependency.render}")
5650
}
5751
}

modules/options/src/main/scala/scala/build/options/ScalaVersionUtil.scala

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package scala.build.options
22

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.*
55
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}
88
import coursier.util.{Artifact, Task}
99

10-
import scala.build.CoursierUtils._
10+
import java.io.File
11+
12+
import scala.build.CoursierUtils.*
1113
import scala.build.EitherCps.{either, value}
1214
import scala.build.errors.{
1315
BuildException,
@@ -27,6 +29,44 @@ object ScalaVersionUtil {
2729
private def scala2Library = cmod"org.scala-lang:scala-library"
2830
private def scala3Library = cmod"org.scala-lang:scala3-library_3"
2931

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+
3070
object GetNightly {
3171

3272
private object Scala2Repo {
@@ -42,17 +82,13 @@ object ScalaVersionUtil {
4282
val scala2NightlyRepo =
4383
"https://scala-ci.typesafe.com/ui/api/v1/ui/nativeBrowser/scala-integration/org/scala-lang/scala-compiler"
4484
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)
4991
}
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-
}
5692

5793
val res0 = value(res)
5894
val content = os.read.bytes(os.Path(res0, os.pwd))
@@ -88,12 +124,8 @@ object ScalaVersionUtil {
88124
cache: FileCache[Task],
89125
latestSupportedStableVersions: Seq[String]
90126
): 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"))
97129

98130
val threeXNightlies = res.filter(_.startsWith(s"3.$threeSubBinaryNum.")).map(Version(_))
99131
if (threeXNightlies.nonEmpty) Right(threeXNightlies.max.repr)
@@ -105,15 +137,11 @@ object ScalaVersionUtil {
105137
/** @return
106138
* Either a BuildException or the calculated (ScalaVersion, ScalaBinaryVersion) tuple
107139
*/
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+
)
117145

118146
private def latestScalaVersionFrom(
119147
versions: CoreVersions,
@@ -136,40 +164,16 @@ object ScalaVersionUtil {
136164
versionString: String,
137165
cache: FileCache[Task],
138166
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)
154170

155171
def scala3(
156172
versionString: String,
157173
cache: FileCache[Task],
158174
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)
173177
}
174178

175179
def validateNonStable(

0 commit comments

Comments
 (0)