Skip to content

Commit c2e9ebb

Browse files
committed
Update build.sbt: Improve the latest tagged version handling and update mdoc variable creation
1 parent 6e4b2bd commit c2e9ebb

1 file changed

Lines changed: 58 additions & 33 deletions

File tree

build.sbt

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ lazy val coreJs = core
114114
libs.tests.munit.value
115115
)
116116
)
117-
lazy val coreNative = core.native.settings(nativeSettings)
117+
lazy val coreNative = core
118+
.native
119+
.settings(nativeSettings)
118120
.settings(
119121
libraryDependencies ++= List(
120122
libs.tests.extrasConcurrent.value,
@@ -483,7 +485,8 @@ lazy val docs = (project in file("docs-gen-tmp/docs"))
483485
cleanFiles += ((ThisBuild / baseDirectory).value / "generated-docs" / "docs"),
484486
scalacOptions ~= (_.filterNot(props.isScala3IncompatibleScalacOption).filter(opt => opt != "-Xfatal-warnings")),
485487
libraryDependencies ++= {
486-
val latestTag = getTheLatestTaggedVersion()
488+
implicit val logger: Logger = sLog.value
489+
val latestTag = getTheLatestTaggedVersion(version.value)
487490
List(
488491
"io.kevinlee" %% "effectie-cats-effect2" % latestTag,
489492
"io.kevinlee" %% "effectie-monix3" % latestTag,
@@ -496,24 +499,20 @@ lazy val docs = (project in file("docs-gen-tmp/docs"))
496499
libraryDependencies.value,
497500
),
498501
mdocVariables := {
499-
val latestVersion = getTheLatestTaggedVersion()
500-
501-
val websiteDir = docusaurDir.value
502-
val latestVersionFile = websiteDir / "latestVersion.json"
503-
val latestVersionJson = raw"""{"version":"$latestVersion"}"""
504-
505-
val websiteDirRelativePath =
506-
s"${latestVersionFile.getParentFile.getParentFile.getName.cyan}/${latestVersionFile.getParentFile.getName.yellow}"
507-
sLog
508-
.value
509-
.info(
510-
s""">> Writing ${"the latest version".blue} to $websiteDirRelativePath/${latestVersionFile.getName.green}.
511-
|>> Content: ${latestVersionJson.blue}
512-
|""".stripMargin
513-
)
514-
IO.write(latestVersionFile, latestVersionJson)
502+
implicit val logger: Logger = sLog.value
503+
504+
val latestVersion = getTheLatestTaggedVersion(version.value)
515505

516-
createMdocVariables(latestVersion.some)
506+
val envVarCi = sys.env.get("CI")
507+
val ciResult = s"""sys.env.get("CI")=${envVarCi}"""
508+
envVarCi.foreach {
509+
case "true" =>
510+
logger.info(s">> ${ciResult.yellow} so ${"run".green} `${"writeLatestVersion".blue}`.")
511+
writeLatestVersion(docusaurDir.value, latestVersion)
512+
case _ =>
513+
logger.info(s">> ${ciResult.yellow} so it will ${"not run".red} `${"writeLatestVersion".cyan}`.")
514+
}
515+
createMdocVariables(latestVersion)
517516
},
518517
docusaurDir := (ThisBuild / baseDirectory).value / "website",
519518
docusaurBuildDir := docusaurDir.value / "build",
@@ -529,7 +528,8 @@ lazy val docsCe3 = (project in file("docs-gen-tmp/docs-ce3"))
529528
cleanFiles += ((ThisBuild / baseDirectory).value / "generated-docs" / "docs" / "cats-effect3"),
530529
scalacOptions ~= (_.filterNot(props.isScala3IncompatibleScalacOption).filter(opt => opt != "-Xfatal-warnings")),
531530
libraryDependencies ++= {
532-
val latestTag = getTheLatestTaggedVersion()
531+
implicit val logger: Logger = sLog.value
532+
val latestTag = getTheLatestTaggedVersion(version.value)
533533
List(
534534
"org.typelevel" %% "cats-effect" % "3.6.3",
535535
"io.kevinlee" %% "effectie-cats-effect3" % latestTag,
@@ -542,8 +542,9 @@ lazy val docsCe3 = (project in file("docs-gen-tmp/docs-ce3"))
542542
libraryDependencies.value,
543543
),
544544
mdocVariables := {
545-
val latestVersion = getTheLatestTaggedVersion()
546-
createMdocVariables(latestVersion.some)
545+
implicit val logger: Logger = sLog.value
546+
val latestVersion = getTheLatestTaggedVersion(version.value)
547+
createMdocVariables(latestVersion)
547548
},
548549
)
549550
.settings(noPublish)
@@ -565,7 +566,7 @@ lazy val docsV1 = (project in file("docs-gen-tmp/docs-v1"))
565566
isScala3(scalaVersion.value),
566567
libraryDependencies.value,
567568
),
568-
mdocVariables := createMdocVariables("1.16.0".some),
569+
mdocVariables := createMdocVariables("1.16.0"),
569570
)
570571
.settings(noPublish)
571572

@@ -578,18 +579,42 @@ addCommandAlias(
578579
"; docs/mdoc; docsV1/mdoc; docsCe3/mdoc",
579580
)
580581

581-
def getTheLatestTaggedVersion(): String = {
582+
def getTheLatestTaggedVersion(version: String)(implicit logger: Logger): String = {
582583
import sys.process.*
583-
"git fetch --tags".!
584+
val envVarCi = sys.env.get("CI")
585+
val ciResult = s"""sys.env.get("CI")=${envVarCi}"""
586+
envVarCi.foreach {
587+
case "true" =>
588+
val gitFetchTagsCmd = "git fetch --tags"
589+
logger.info(s">> ${ciResult.yellow} so ${"run".green} `${gitFetchTagsCmd.blue}`")
590+
gitFetchTagsCmd.!
591+
case _ =>
592+
logger.info(s">> ${ciResult.yellow} so ${"skip fetching tags".red}")
593+
}
594+
584595
val tag = "git rev-list --tags --max-count=1".!!.trim
585-
s"git describe --tags $tag".!!.trim.stripPrefix("v")
596+
if (tag.nonEmpty)
597+
s"git describe --tags $tag".!!.trim.stripPrefix("v")
598+
else
599+
version
586600
}
587601

588-
def createMdocVariables(version: Option[String]): Map[String, String] = {
589-
val versionForDoc = version match {
590-
case Some(version) => version
591-
case None => getTheLatestTaggedVersion()
592-
}
602+
def writeLatestVersion(websiteDir: File, latestVersion: String)(implicit logger: Logger): Unit = {
603+
val latestVersionFile = websiteDir / "latestVersion.json"
604+
val latestVersionJson = raw"""{"version":"$latestVersion"}"""
605+
606+
val websiteDirRelativePath =
607+
s"${latestVersionFile.getParentFile.getParentFile.getName.cyan}/${latestVersionFile.getParentFile.getName.yellow}"
608+
logger.info(
609+
s""">> Writing ${"the latest version".blue} to $websiteDirRelativePath/${latestVersionFile.getName.green}.
610+
|>> Content: ${latestVersionJson.blue}
611+
|""".stripMargin
612+
)
613+
IO.write(latestVersionFile, latestVersionJson)
614+
}
615+
616+
def createMdocVariables(version: String): Map[String, String] = {
617+
val versionForDoc = version
593618

594619
Map(
595620
"VERSION" -> versionForDoc,
@@ -632,8 +657,8 @@ lazy val props =
632657
final val Scala3Version = "3.3.3"
633658

634659
// final val ProjectScalaVersion = "2.12.13"
635-
// final val ProjectScalaVersion = Scala2Version
636-
final val ProjectScalaVersion = Scala3Version
660+
val ProjectScalaVersion = Scala2Version
661+
// val ProjectScalaVersion = Scala3Version
637662

638663
lazy val licenses = List("MIT" -> url("http://opensource.org/licenses/MIT"))
639664

0 commit comments

Comments
 (0)