2020-03-09 16:44:40 +03:00
|
|
|
import sbt._
|
2020-07-10 13:57:42 +03:00
|
|
|
import sbt.internal.util.ManagedLogger
|
2020-03-09 16:44:40 +03:00
|
|
|
|
|
|
|
import scala.sys.process._
|
|
|
|
|
|
|
|
object BuildInfo {
|
2020-09-09 16:37:26 +03:00
|
|
|
|
2024-09-06 11:27:59 +03:00
|
|
|
/** Writes build-time information to a Java class that can be used by the
|
2020-09-09 16:37:26 +03:00
|
|
|
* components.
|
|
|
|
*
|
|
|
|
* If the `ENSO_RELEASE_MODE` environment variable is set to `true`, will set
|
|
|
|
* an `isRelease` flag to true. This flag can be used to disable
|
|
|
|
* development-specific features.
|
|
|
|
*
|
|
|
|
* @param file location where to write the Scala code
|
|
|
|
* @param log a logger instance for diagnostics
|
2022-02-15 18:34:33 +03:00
|
|
|
* @param defaultDevEnsoVersion the default version used for dev builds
|
2020-09-09 16:37:26 +03:00
|
|
|
* @param ensoVersion Enso version
|
|
|
|
* @param scalacVersion Scala compiler version used in the project
|
|
|
|
* @param graalVersion GraalVM version used in the project
|
2021-07-08 16:38:20 +03:00
|
|
|
* @param currentEdition name of the edition associated with the Enso
|
|
|
|
* version; this should be removed once #1831 is
|
|
|
|
* implemented
|
2020-09-09 16:37:26 +03:00
|
|
|
* @return sequence of modified files
|
|
|
|
*/
|
2020-03-09 16:44:40 +03:00
|
|
|
def writeBuildInfoFile(
|
|
|
|
file: File,
|
2020-07-10 13:57:42 +03:00
|
|
|
log: ManagedLogger,
|
2022-02-15 18:34:33 +03:00
|
|
|
defaultDevEnsoVersion: String,
|
2020-03-09 16:44:40 +03:00
|
|
|
ensoVersion: String,
|
|
|
|
scalacVersion: String,
|
2021-07-08 16:38:20 +03:00
|
|
|
graalVersion: String,
|
2022-02-07 17:14:32 +03:00
|
|
|
currentEdition: String
|
2020-03-09 16:44:40 +03:00
|
|
|
): Seq[File] = {
|
2020-09-09 16:37:26 +03:00
|
|
|
val gitInfo = getGitInformation(log).getOrElse(fallbackGitInformation)
|
|
|
|
val isRelease = isReleaseMode
|
2024-09-06 11:27:59 +03:00
|
|
|
val className = file.getName.stripSuffix(".java")
|
2020-03-09 16:44:40 +03:00
|
|
|
val fileContents =
|
|
|
|
s"""
|
2024-09-06 11:27:59 +03:00
|
|
|
|package org.enso.version;
|
2020-03-09 16:44:40 +03:00
|
|
|
|
|
2024-09-06 11:27:59 +03:00
|
|
|
|final class ${className} {
|
|
|
|
| private GeneratedVersion() {}
|
2020-03-09 16:44:40 +03:00
|
|
|
|
|
2024-09-06 11:27:59 +03:00
|
|
|
| static String defaultDevEnsoVersion() {
|
|
|
|
| return "${defaultDevEnsoVersion}";
|
|
|
|
| }
|
2020-03-09 16:44:40 +03:00
|
|
|
|
|
2024-09-06 11:27:59 +03:00
|
|
|
| static String ensoVersion() {
|
|
|
|
| return "${ensoVersion}";
|
|
|
|
| }
|
2020-09-09 16:37:26 +03:00
|
|
|
|
|
2024-09-06 11:27:59 +03:00
|
|
|
| static String scalacVersion() {
|
|
|
|
| return "${scalacVersion}";
|
|
|
|
| }
|
|
|
|
|
|
|
|
|
| static String graalVersion() {
|
|
|
|
| return "${graalVersion}";
|
|
|
|
| }
|
|
|
|
|
|
|
|
|
| static String currentEdition() {
|
|
|
|
| return "${currentEdition}";
|
|
|
|
| }
|
|
|
|
|
|
|
|
|
| static String commit() {
|
|
|
|
| return "${gitInfo.commitHash}";
|
|
|
|
| }
|
|
|
|
|
|
|
|
|
| static String ref() {
|
|
|
|
| return "${gitInfo.ref}";
|
|
|
|
| }
|
|
|
|
|
|
|
|
|
| static boolean isDirty() {
|
|
|
|
| return ${gitInfo.isDirty};
|
|
|
|
| }
|
|
|
|
|
|
|
|
|
| static String latestCommitDate() {
|
|
|
|
| return "${gitInfo.latestCommitDate}";
|
|
|
|
| }
|
|
|
|
|
|
|
|
|
| static boolean isRelease() {
|
|
|
|
| return ${isRelease};
|
|
|
|
| }
|
2020-03-09 16:44:40 +03:00
|
|
|
|}
|
|
|
|
|""".stripMargin
|
|
|
|
IO.write(file, fileContents)
|
2020-07-10 13:57:42 +03:00
|
|
|
log.debug("Build info updated.")
|
2020-03-09 16:44:40 +03:00
|
|
|
Seq(file)
|
|
|
|
}
|
2020-07-27 13:45:19 +03:00
|
|
|
|
2022-11-09 18:26:25 +03:00
|
|
|
def isReleaseMode: Boolean =
|
2022-02-15 18:34:33 +03:00
|
|
|
sys.env.get("ENSO_RELEASE_MODE").contains("true")
|
2020-09-09 16:37:26 +03:00
|
|
|
|
2020-10-22 17:12:28 +03:00
|
|
|
/** Information regarding the Git repository that was used in the build.
|
2020-09-09 16:37:26 +03:00
|
|
|
*
|
|
|
|
* @param ref if available, name of the branch that was checked out; if a
|
|
|
|
* branch is not available, but the current commit is tagged, name
|
|
|
|
* of that tag is used, otherwise falls back to `HEAD`
|
|
|
|
* @param commitHash hash of the currently checked out commit
|
|
|
|
* @param isDirty indicates if there are any uncommitted changes
|
|
|
|
* @param latestCommitDate date of the current commit
|
|
|
|
*/
|
2020-07-27 13:45:19 +03:00
|
|
|
private case class GitInformation(
|
|
|
|
ref: String,
|
2020-09-09 16:37:26 +03:00
|
|
|
commitHash: String,
|
2020-07-27 13:45:19 +03:00
|
|
|
isDirty: Boolean,
|
|
|
|
latestCommitDate: String
|
|
|
|
)
|
2020-09-09 16:37:26 +03:00
|
|
|
|
2020-07-27 13:45:19 +03:00
|
|
|
private def getGitInformation(log: ManagedLogger): Option[GitInformation] =
|
|
|
|
try {
|
2023-10-12 11:19:45 +03:00
|
|
|
val hash = "git rev-parse HEAD".!!.trim
|
2020-07-27 13:45:19 +03:00
|
|
|
val ref =
|
|
|
|
try {
|
|
|
|
val branchCommand = "git symbolic-ref -q --short HEAD"
|
|
|
|
val tagCommand = "git describe --tags --exact-match"
|
|
|
|
val refCommand =
|
|
|
|
branchCommand #|| tagCommand
|
2023-10-12 11:19:45 +03:00
|
|
|
refCommand.!!.trim
|
2020-07-27 13:45:19 +03:00
|
|
|
} catch {
|
|
|
|
case e: Exception =>
|
|
|
|
log.warn(
|
|
|
|
"Cannot get name of git branch/tag, defaulting to \"HEAD\". " +
|
|
|
|
s"(Caused by: $e)"
|
|
|
|
)
|
|
|
|
"HEAD"
|
|
|
|
}
|
2023-10-12 11:19:45 +03:00
|
|
|
val isDirty = "git status --porcelain".!!.trim.nonEmpty
|
|
|
|
val latestCommitDate = "git log HEAD -1 --format=%cd".!!.trim
|
2020-09-09 16:37:26 +03:00
|
|
|
Some(
|
|
|
|
GitInformation(
|
|
|
|
ref = ref,
|
|
|
|
commitHash = hash,
|
|
|
|
isDirty = isDirty,
|
|
|
|
latestCommitDate = latestCommitDate
|
|
|
|
)
|
|
|
|
)
|
2020-07-27 13:45:19 +03:00
|
|
|
} catch {
|
|
|
|
case e: Exception =>
|
|
|
|
log.warn(
|
|
|
|
"Could not get any git information. The build will proceed but it " +
|
|
|
|
s"will not contain the git metadata. (Caused by: $e)"
|
|
|
|
)
|
|
|
|
None
|
|
|
|
}
|
2020-09-09 16:37:26 +03:00
|
|
|
|
2020-10-22 17:12:28 +03:00
|
|
|
/** Fallback instance of [[GitInformation]] that can be used if the build is
|
2020-09-09 16:37:26 +03:00
|
|
|
* outside of a repository or the git information cannot be obtained for
|
|
|
|
* other reasons.
|
|
|
|
*/
|
2020-07-27 13:45:19 +03:00
|
|
|
private def fallbackGitInformation: GitInformation =
|
|
|
|
GitInformation(
|
|
|
|
"<built outside of a git repository>",
|
|
|
|
"<built outside of a git repository>",
|
|
|
|
isDirty = false,
|
|
|
|
"<built outside of a git repository>"
|
|
|
|
)
|
2020-03-09 16:44:40 +03:00
|
|
|
}
|