enso/project/EnvironmentCheck.scala

122 lines
4.0 KiB
Scala
Raw Normal View History

import java.io.IOException
2020-07-01 14:21:13 +03:00
import sbt._
import sbt.internal.util.ManagedLogger
import scala.sys.process._
2020-07-01 14:21:13 +03:00
object EnvironmentCheck {
/** 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 expectedGraalVersion the GraalVM version that should be used for
* building this project
* @param expectedJavaVersion the Java version of the used GraalVM
* distribution
* @param log a logger used to report errors if the versions are mismatched
*/
def graalVersionOk(
2020-07-01 14:21:13 +03:00
expectedGraalVersion: String,
expectedJavaVersion: String,
log: ManagedLogger
): Boolean = {
2020-07-01 14:21:13 +03:00
val javaSpecificationVersion =
System.getProperty("java.vm.specification.version")
val versionProperty = "java.vendor.version"
val rawGraalVersion = System.getProperty(versionProperty)
def graalVersion: Option[String] = {
val versionRegex = """GraalVM (CE|EE) ([\d.]+.*)""".r
rawGraalVersion match {
case versionRegex(_, version) => Some(version)
case _ => None
}
}
2020-07-01 14:21:13 +03:00
val graalOk = if (rawGraalVersion == null) {
log.error(
s"Property $versionProperty is not defined. " +
s"Make sure your current JVM is set to " +
s"GraalVM $expectedGraalVersion Java $expectedJavaVersion."
)
false
} else
graalVersion match {
case Some(version) if version == expectedGraalVersion => true
case _ =>
log.error(
s"GraalVM version mismatch - you are running $rawGraalVersion " +
s"but GraalVM $expectedGraalVersion is expected."
)
false
}
2020-07-01 14:21:13 +03:00
val javaOk =
if (javaSpecificationVersion != expectedJavaVersion) {
log.error(
s"Java version mismatch - you are running " +
s"Java $javaSpecificationVersion " +
s"but Java $expectedJavaVersion is expected."
2020-07-01 14:21:13 +03:00
)
false
} else true
graalOk && javaOk
}
/** Runs `rustc --version` to ensure that it is properly installed and
* checks if the reported version is consistent with expectations.
*
* @param expectedVersion rust version that is expected to be installed,
* should be based on project settings
* @return either an error message explaining what is wrong with the rust
* version or Unit meaning it is correct
*/
def rustVersionOk(expectedVersion: String, log: ManagedLogger): Boolean = {
val cmd = "rustc --version"
try {
2021-05-14 15:08:39 +03:00
val versionStr = cmd.!!.trim
2021-05-14 15:08:39 +03:00
val contained = versionStr.contains(expectedVersion)
if (!contained) {
log.error(
s"Rust version mismatch. $expectedVersion is expected, " +
s"but it seems $versionStr is installed."
)
2021-05-14 15:08:39 +03:00
}
contained
} catch {
case _ @(_: RuntimeException | _: IOException) =>
log.error("Rust version check failed. Make sure rustc is in your PATH.")
false
2020-07-01 14:21:13 +03:00
}
}
/** Augments a state transition to do a Rust and GraalVM version check.
2020-07-01 14:21:13 +03:00
*
* @param graalVersion the GraalVM version that should be used for
* building this project
* @param javaVersion the Java version of the used GraalVM distribution
* @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
2020-07-01 14:21:13 +03:00
*/
def addVersionCheck(
graalVersion: String,
2020-07-10 13:57:42 +03:00
javaVersion: String
2020-07-01 14:21:13 +03:00
)(
oldTransition: State => State
): State => State =
(state: State) => {
val newState = oldTransition(state)
val logger = newState.log
val graalOk = graalVersionOk(graalVersion, javaVersion, logger)
2020-08-07 12:18:09 +03:00
if (!graalOk)
throw new RuntimeException("Some versions checks failed.")
2020-07-01 14:21:13 +03:00
newState
}
}