Skip to content

Commit

Permalink
refactor(sbt): Factor code out of checkConfiguredSbtVersions()
Browse files Browse the repository at this point in the history
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 <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Sep 20, 2024
1 parent 922e42f commit 047efd1
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions plugins/package-managers/sbt/src/main/kotlin/Sbt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
*/
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -113,7 +133,7 @@ class Sbt(
}
}

private fun checkConfiguredSbtVersions(workingDir: File): Semver? {
private fun getBuildSbtVersions(workingDir: File): SortedSet<Semver> {
// Determine the SBT version(s) being used.
val propertiesFiles = workingDir.walkBottomUp().filterTo(mutableListOf()) {
it.isFile && it.name == "build.properties"
Expand All @@ -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<File>, labels: Map<String, String>) =
Expand Down

0 comments on commit 047efd1

Please sign in to comment.