From 047efd16b57f2086741f7c9a4bd63677a47bfb51 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Fri, 20 Sep 2024 14:43:55 +0200 Subject: [PATCH] refactor(sbt): Factor code out of `checkConfiguredSbtVersions()` Do less in the function to make it more generally useful. This also makes more clear where the version is configured, i.e. in the build itself, and not e.g in ORT configuration. Adjust related variable names and log statements accordingly. Signed-off-by: Sebastian Schuberth --- .../sbt/src/main/kotlin/Sbt.kt | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/plugins/package-managers/sbt/src/main/kotlin/Sbt.kt b/plugins/package-managers/sbt/src/main/kotlin/Sbt.kt index 9f7b4e460562..f2a752766954 100644 --- a/plugins/package-managers/sbt/src/main/kotlin/Sbt.kt +++ b/plugins/package-managers/sbt/src/main/kotlin/Sbt.kt @@ -22,6 +22,7 @@ package org.ossreviewtoolkit.plugins.packagemanagers.sbt import java.io.File import java.nio.file.StandardCopyOption import java.util.Properties +import java.util.SortedSet import kotlin.io.path.moveTo @@ -34,13 +35,17 @@ import org.ossreviewtoolkit.model.config.RepositoryConfiguration import org.ossreviewtoolkit.plugins.packagemanagers.maven.Maven import org.ossreviewtoolkit.utils.common.CommandLineTool import org.ossreviewtoolkit.utils.common.Os -import org.ossreviewtoolkit.utils.common.alsoIfNull import org.ossreviewtoolkit.utils.common.getCommonParentFile import org.ossreviewtoolkit.utils.common.searchUpwardsForSubdirectory import org.ossreviewtoolkit.utils.common.suppressInput import org.semver4j.Semver +// We need at least sbt version 0.13.0 to be able to use "makePom" instead of the deprecated hyphenated +// form "make-pom" and to support declaring Maven-style repositories, see +// http://www.scala-sbt.org/0.13/docs/Publishing.html#Modifying+the+generated+POM. +const val LOWEST_SUPPORTED_SBT_VERSION = "0.13.0" + /** * The [SBT](https://www.scala-sbt.org/) package manager for Scala. */ @@ -70,7 +75,22 @@ class Sbt( logger.info { "Determined '$workingDir' as the $managerName project root directory." } - checkConfiguredSbtVersions(workingDir) + val sbtVersions = getBuildSbtVersions(workingDir) + when { + sbtVersions.isEmpty() -> + logger.info { "The build does not configure any $managerName version to be used." } + + sbtVersions.size == 1 -> + logger.info { "The build configures $managerName version ${sbtVersions.first()} to be used." } + + else -> + logger.warn { "The build configures multiple different $managerName versions to be used: $sbtVersions" } + } + + val lowestSbtVersion = sbtVersions.firstOrNull() + require(lowestSbtVersion?.isLowerThan(Semver(LOWEST_SUPPORTED_SBT_VERSION)) != true) { + "Build $managerName version $lowestSbtVersion is lower than version $LOWEST_SUPPORTED_SBT_VERSION." + } fun runSbt(vararg command: String) = suppressInput { @@ -113,7 +133,7 @@ class Sbt( } } - private fun checkConfiguredSbtVersions(workingDir: File): Semver? { + private fun getBuildSbtVersions(workingDir: File): SortedSet { // Determine the SBT version(s) being used. val propertiesFiles = workingDir.walkBottomUp().filterTo(mutableListOf()) { it.isFile && it.name == "build.properties" @@ -127,27 +147,7 @@ class Sbt( props.getProperty("sbt.version")?.let { versions += Semver(it) } } - val lowestConfiguredSbtVersion = versions.firstOrNull().alsoIfNull { - logger.warn { "No $managerName version configured in any of: ${propertiesFiles.joinToString()}" } - } - - if (versions.size == 1) { - logger.info { "Configured $managerName version: $lowestConfiguredSbtVersion" } - } else { - logger.warn { "Different $managerName versions found: ${versions.joinToString()}" } - } - - // We need at least sbt version 0.13.0 to be able to use "makePom" instead of the deprecated hyphenated - // form "make-pom" and to support declaring Maven-style repositories, see - // http://www.scala-sbt.org/0.13/docs/Publishing.html#Modifying+the+generated+POM. - val lowestSupportedSbtVersion = Semver("0.13.0") - - require(lowestConfiguredSbtVersion?.isLowerThan(lowestSupportedSbtVersion) != true) { - "Configured $managerName version $lowestConfiguredSbtVersion is lower than version " + - "$lowestSupportedSbtVersion." - } - - return lowestConfiguredSbtVersion + return versions } override fun resolveDependencies(definitionFiles: List, labels: Map) =