Allow the version to be emitted to file.

This commit is contained in:
Pat Garrity 2024-03-20 08:17:47 -05:00
parent 2adf6ce66d
commit ef34c9b398
Signed by: pfm
GPG key ID: 5CA5D21BAB7F3A76
3 changed files with 31 additions and 5 deletions

View file

@ -1 +1 @@
sbt.version=1.9.8 sbt.version=1.9.9

View file

@ -60,6 +60,13 @@ object SemVerKeys {
"True or false depending on whether the current build is a pre-release (snapshot) build." "True or false depending on whether the current build is a pre-release (snapshot) build."
) )
/** SBT Setting that defines the filename for dumping the selected SemVer to a
* file.
*/
lazy val semVerOutputFile = settingKey[Option[String]](
"Name of the file where the calculated SemVer will be stored (if requested)."
)
/** Task which emits the current and selected versions at the informational /** Task which emits the current and selected versions at the informational
* log level. * log level.
*/ */
@ -67,4 +74,10 @@ object SemVerKeys {
"Dump the calculated version information." "Dump the calculated version information."
) )
/** Task which writes the selected SemVer to a file.
*/
lazy val semVerWriteVersionToFile = taskKey[Unit](
"Write the selected SemVer to a file."
)
} }

View file

@ -1,5 +1,8 @@
package gs package gs
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Paths
import sbt._ import sbt._
object SemVerPlugin extends AutoPlugin { object SemVerPlugin extends AutoPlugin {
@ -9,6 +12,8 @@ object SemVerPlugin extends AutoPlugin {
import autoImport._ import autoImport._
val DefaultOutputFile: String = ".version"
// Perform all version calculations and expose as a variable. // Perform all version calculations and expose as a variable.
lazy val semVerDefaults: Seq[Setting[_]] = { lazy val semVerDefaults: Seq[Setting[_]] = {
// The latest semantic version relevant to this project, calculated by // The latest semantic version relevant to this project, calculated by
@ -43,10 +48,11 @@ object SemVerPlugin extends AutoPlugin {
// Expose the relevant values as setting keys. // Expose the relevant values as setting keys.
Seq( Seq(
semVerCurrent := latestSemVer.toString(), semVerCurrent := latestSemVer.toString(),
semVerSelected := selectedSemVer.render(isSnapshot), semVerSelected := selectedSemVer.render(isSnapshot),
semVerMajor := selectedSemVer.major, semVerMajor := selectedSemVer.major,
semVerSnapshot := isSnapshot semVerSnapshot := isSnapshot,
semVerOutputFile := Some(DefaultOutputFile)
) )
} }
@ -59,6 +65,13 @@ object SemVerPlugin extends AutoPlugin {
val log = Keys.streams.value.log val log = Keys.streams.value.log
log.info(s"[SemVer] Current: ${semVerCurrent.value}") log.info(s"[SemVer] Current: ${semVerCurrent.value}")
log.info(s"[SemVer] Selected: ${semVerSelected.value}") log.info(s"[SemVer] Selected: ${semVerSelected.value}")
},
semVerWriteVersionToFile := {
val outputFile = semVerOutputFile.value.getOrElse(DefaultOutputFile)
Files.write(
Paths.get(outputFile),
semVerSelected.value.getBytes(StandardCharsets.UTF_8)
)
} }
) )