enso/project/GraalVersionCheck.scala
Pavel Marek efb39ad91c
Fix GraalVM distribution download on MacOS (#7364)
Follow-up of recent GraalVM update #7176 that fixes downloading of GraalVM for Mac - instead of "darwin", the releases are now named "macos"

# Important Notes
Also re-enables the JDK/GraalVM version check as onLoad hook to the `sbt` process. We used to have that check a long time ago. Provides errors like this one if the `sbt` is run with a different JVM version:
```
[error] GraalVM version mismatch - you are running Oracle GraalVM 20.0.1+9.1 but GraalVM 17.0.7 is expected.
[error] GraalVM version check failed.
```
2023-07-23 09:21:21 +00:00

81 lines
2.5 KiB
Scala

import java.io.IOException
import sbt.*
import sbt.internal.util.ManagedLogger
import scala.sys.process.*
import nl.gn0s1s.bump.SemVer
object GraalVersionCheck {
/** Compares the version of JVM running sbt with the GraalVM versions defined
* in project configuration and reports errors if the versions do not match.
*
* @param expectedGraalVersionRaw the GraalVM version that should be used for
* building this project
* @param log a logger used to report errors if the versions are mismatched
*/
def graalVersionOk(
expectedGraalVersionRaw: String,
log: ManagedLogger
): Boolean = {
val expectedGraalVersion = SemVer(expectedGraalVersionRaw)
require(expectedGraalVersion.isDefined, "Invalid version string")
val versionProperty = "java.vendor.version"
val rawGraalVersion = System.getProperty(versionProperty)
def graalVersion: Option[SemVer] = {
val versionRegex = """GraalVM (CE|EE) ([\d.]+.*)""".r
rawGraalVersion match {
case versionRegex(_, version) =>
SemVer(version)
case _ => None
}
}
if (rawGraalVersion == null) {
log.error(
s"Property $versionProperty is not defined. " +
s"Make sure your current JVM is set to " +
s"GraalVM $expectedGraalVersionRaw."
)
false
} else {
graalVersion match {
case Some(version)
if expectedGraalVersion.get.withoutBuildMetadata == version.withoutBuildMetadata =>
true
case _ =>
log.error(
s"GraalVM version mismatch - you are running $rawGraalVersion " +
s"but GraalVM $expectedGraalVersionRaw is expected."
)
false
}
}
}
/** Augments a state transition to do a Rust and GraalVM version check.
*
* @param graalVersion the GraalVM version that should be used for
* building this project
* @param oldTransition the state transition to be augmented
* @return an augmented state transition that does all the state changes of
* oldTransition but also runs the version checks
*/
def addVersionCheck(
graalVersion: String
)(
oldTransition: State => State
): State => State =
(state: State) => {
val newState = oldTransition(state)
val logger = newState.log
if (!graalVersionOk(graalVersion, logger)) {
logger.error("GraalVM version check failed.")
System.exit(1)
}
newState
}
}