Skip to content

Commit

Permalink
Allow to specify fixed source version for project to allow for compil…
Browse files Browse the repository at this point in the history
…ation of legacy/unmaintained projects
  • Loading branch information
WojciechMazur committed Jan 11, 2024
1 parent 1cc080d commit 3e019b4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions coordinator/configs/projects-config.conf
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ armanbilge_feral.projects.exclude = [
"com.armanbilge%feral-lambda-api-gateway-proxy-http4s"
]
armanbilge_gcp4s.tests = compile-only
armanbilge_van-cats.source-version=3.4 // override -source:future
assist-iot-sripas_scala-mqtt-wrapper {
sbt.commands = [
"excludeLibraryDependency org.wartremover:wartremover_{scalaVersion}",
Expand Down
3 changes: 3 additions & 0 deletions coordinator/src/main/resources/buildPlan.reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ projects {
overrides = {}
}

// A fixed Scala `-source` setting to be applied to the project.
source-version = null

// Mode of tests execution one of disabled|compile-only|full
tests = full

Expand Down
1 change: 1 addition & 0 deletions coordinator/src/main/scala/core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ case class ProjectBuildConfig(
sbt: SbtConfig = SbtConfig(),
mill: MillConfig = MillConfig(),
tests: TestingMode = TestingMode.Full,
sourceVersion: Option[String] = None,
// We should use Option[Int] here, but json4s fails to resolve signature https://github.com/json4s/json4s/issues/1035
sourcePatches: List[SourcePatch] = Nil
) derives ConfigReader
Expand Down
13 changes: 12 additions & 1 deletion project-builder/build-revision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ scalaBinaryVersionMajor=`echo $scalaVersion | cut -d . -f 1`
scalaBinaryVersionMinor=`echo $scalaVersion | cut -d . -f 2`
echo "Scala binary version found: $scalaBinaryVersion"

commonAppendScalacOptions="-source:$scalaBinaryVersion-migration,-Wconf:msg=can be rewritten automatically under:s"
sourceVersion=`echo $projectConfig | jq -r '.sourceVersion // ""'`
sourceVersionSetting=""
if [[ -z "$sourceVersion" ]]; then
sourceVersion="$scalaBinaryVersion-migration"
sourceVersionSetting="-source:$sourceVersion"
echo "Implicitly using source version $sourceVersion"
else
echo "Using configured source version: $sourceVersion"
sourceVersionSetting="REQUIRE:-source:$sourceVersion"
fi

commonAppendScalacOptions="$sourceVersionSetting,-Wconf:msg=can be rewritten automatically under:s"
commonRemoveScalacOptions="-deprecation,-feature,-Xfatal-warnings,-Werror,MATCH:.*-Wconf.*any:e,-migration,"
echo "Would try to apply common scalacOption (best-effort, sbt/mill only):"
echo "Append: $commonAppendScalacOptions"
Expand Down
18 changes: 13 additions & 5 deletions project-builder/shared/CommunityBuildCore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -349,32 +349,40 @@ object Scala3CommunityBuild {
): Seq[String] = {
val (removeMatchSettings, removeSettings) = remove.partition { _.startsWith("MATCH:") }
val matchPatterns = removeMatchSettings.map(_.stripPrefix("MATCH:"))

val (required, standardAppendSettings) = append.partition(_.startsWith("REQUIRE:"))
val requiredAppendSettings = required.map(_.stripPrefix("REQUIRE:"))

def isSourceVersion(v: String) = v.matches(raw"^-?-source(:(future|(\d\.\d+))(-migration)?)?")
def resolveSourceVersion(v: String): Option[String] = v.split(":").drop(1).headOption
val SourceVersionPattern = raw"^((3\.\d+|future)(-migration)?)$$".r
val definedSourceSetting = current.find(isSourceVersion)
lazy val definedSourceVersion: Option[String] =
definedSourceSetting.flatMap(_.split(":").drop(1).headOption) // -source:version
definedSourceSetting.flatMap(resolveSourceVersion) // -source:version
.orElse(current.find(SourceVersionPattern.findFirstIn(_).isDefined)) // -source version
.flatMap(SourceVersionPattern.findFirstMatchIn(_))
.flatMap(m => Option(m.group(1)))
val forceFutureMigrationVersion = definedSourceVersion.contains("future")
lazy val requiredSourceVersion = requiredAppendSettings.find(isSourceVersion).flatMap(resolveSourceVersion)
val forceSourceVersion = definedSourceVersion.contains("future") || requiredSourceVersion.isDefined

val appendSettings = {
def excludeIf(setting: String, expr: Boolean, reason: => String) = {
if(expr) logOnce(s"Would not apply setting `$setting`: $reason")
expr
}

val forcedAppendSettings: Seq[String] = Seq(
if(forceFutureMigrationVersion) Some("-source:future-migration") else None
if(forceSourceVersion) Some(s"-source:${requiredSourceVersion.getOrElse("future-migration")}") else None
).flatten
append.filterNot{ setting =>

standardAppendSettings.filterNot{ setting =>
excludeIf(setting,
isSourceVersion(setting) && (definedSourceSetting.nonEmpty || forceFutureMigrationVersion),
isSourceVersion(setting) && (definedSourceSetting.nonEmpty || forceSourceVersion),
s"Project has predefined source version: ${definedSourceSetting.get}"
)
} ++ forcedAppendSettings
}

val normalizedExcludePatterns = (appendSettings ++ removeSettings).distinct.map { setting =>
Seq[String => String](
setting => if (setting.startsWith("--")) setting.tail else setting,
Expand Down

0 comments on commit 3e019b4

Please sign in to comment.