enso/project/BuildInfo.scala
Pavel Marek 19ff2a2bb7
Prepare for JPMS - rename packages (#10974)
* Rename packages in logging-utils-akka

* Migrate buildInfo to Java

* Rename packages in logging

* Rename package in scala-yaml

* No usage of CompilerDirectives inside pkg

* log errors of initialization of directory watcher

* HashCodeNode does not use com.google.common.base.Objects

* Rename rest of the packages

* fmt

* Fix dependencies on version-output

* Add necessary dependencies to testkit

* Rename instruments in runtime-fat-jar module-info

* Fix compilation errors because of BuildVersion

* Fix logger renames

* Use java.util.List directly

* Fixes after merge

* Improve error message in NativeLauncherSpec

* Fix logger renames

* Fix json version formatting

* Revert "No usage of CompilerDirectives inside pkg"

This reverts commit cc7e078416.

* fmt
2024-09-06 10:27:59 +02:00

159 lines
5.0 KiB
Scala

import sbt._
import sbt.internal.util.ManagedLogger
import scala.sys.process._
object BuildInfo {
/** Writes build-time information to a Java class that can be used by the
* 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
* @param defaultDevEnsoVersion the default version used for dev builds
* @param ensoVersion Enso version
* @param scalacVersion Scala compiler version used in the project
* @param graalVersion GraalVM version used in the project
* @param currentEdition name of the edition associated with the Enso
* version; this should be removed once #1831 is
* implemented
* @return sequence of modified files
*/
def writeBuildInfoFile(
file: File,
log: ManagedLogger,
defaultDevEnsoVersion: String,
ensoVersion: String,
scalacVersion: String,
graalVersion: String,
currentEdition: String
): Seq[File] = {
val gitInfo = getGitInformation(log).getOrElse(fallbackGitInformation)
val isRelease = isReleaseMode
val className = file.getName.stripSuffix(".java")
val fileContents =
s"""
|package org.enso.version;
|
|final class ${className} {
| private GeneratedVersion() {}
|
| static String defaultDevEnsoVersion() {
| return "${defaultDevEnsoVersion}";
| }
|
| static String ensoVersion() {
| return "${ensoVersion}";
| }
|
| 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};
| }
|}
|""".stripMargin
IO.write(file, fileContents)
log.debug("Build info updated.")
Seq(file)
}
def isReleaseMode: Boolean =
sys.env.get("ENSO_RELEASE_MODE").contains("true")
/** Information regarding the Git repository that was used in the build.
*
* @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
*/
private case class GitInformation(
ref: String,
commitHash: String,
isDirty: Boolean,
latestCommitDate: String
)
private def getGitInformation(log: ManagedLogger): Option[GitInformation] =
try {
val hash = "git rev-parse HEAD".!!.trim
val ref =
try {
val branchCommand = "git symbolic-ref -q --short HEAD"
val tagCommand = "git describe --tags --exact-match"
val refCommand =
branchCommand #|| tagCommand
refCommand.!!.trim
} catch {
case e: Exception =>
log.warn(
"Cannot get name of git branch/tag, defaulting to \"HEAD\". " +
s"(Caused by: $e)"
)
"HEAD"
}
val isDirty = "git status --porcelain".!!.trim.nonEmpty
val latestCommitDate = "git log HEAD -1 --format=%cd".!!.trim
Some(
GitInformation(
ref = ref,
commitHash = hash,
isDirty = isDirty,
latestCommitDate = latestCommitDate
)
)
} 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
}
/** Fallback instance of [[GitInformation]] that can be used if the build is
* outside of a repository or the git information cannot be obtained for
* other reasons.
*/
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>"
)
}