Skip to content

Commit

Permalink
add sbt 2.x cross build setting
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k authored and kitbellew committed Oct 19, 2024
1 parent 0344d19 commit f512f88
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- run:
# for git tests
git config --global user.email "[email protected]" && git config --global user.name "scalafmt"
- run: sbt plugin/scripted
- run: sbt plugin/scripted "++ 3.x" compile

formatting:
runs-on: ubuntu-latest
Expand Down
20 changes: 17 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ inThisBuild(List(
),
resolvers ++= Resolver.sonatypeOssRepos("public"),
scalaVersion := "2.12.20",
crossScalaVersions += "3.3.4",
packageDoc / publishArtifact := insideCI.value,
packageSrc / publishArtifact := insideCI.value,
))
Expand All @@ -47,14 +48,27 @@ lazy val plugin = project.enablePlugins(SbtPlugin).settings(
moduleName := "sbt-scalafmt",
libraryDependencies ++= List(
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0",
"org.scalameta" %% "scalafmt-sysops" % scalafmtVersion,
"org.scalameta" %% "scalafmt-dynamic" % scalafmtVersion,
"org.scalameta" %% "scalafmt-sysops" % scalafmtVersion cross
CrossVersion.for3Use2_13,
"org.scalameta" %% "scalafmt-dynamic" % scalafmtVersion cross
CrossVersion.for3Use2_13,
),
scriptedBufferLog := false,
scriptedLaunchOpts += s"-Dplugin.version=${version.value}",
// For compat reasons we have this in here to ensure we are testing against 1.2.8
// We honestly probably don't need to, so if this ever causes issues, rip it out.
pluginCrossBuild / sbtVersion := "1.2.8",
pluginCrossBuild / sbtVersion := {
scalaBinaryVersion.value match {
case "2.12" => "1.2.8"
case _ => "2.0.0-M2"
}
},
conflictWarning := {
scalaBinaryVersion.value match {
case "3" => ConflictWarning("warn", Level.Warn, false)
case _ => conflictWarning.value
}
},
)

// For some reason, it doesn't work if this is defined in globalSettings in PublishPlugin.
Expand Down
14 changes: 8 additions & 6 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import java.io.OutputStreamWriter
import java.nio.file.Path

import sbt.Keys._
import sbt._
// format: off
import sbt.{given, _}
// format: on
import sbt.librarymanagement.MavenRepository
import sbt.util.CacheImplicits._
import sbt.util.CacheStoreFactory
Expand Down Expand Up @@ -396,8 +398,8 @@ object ScalafmtPlugin extends AutoPlugin {
private def getScalafmtSourcesTask(
f: (Seq[File], Seq[File], FormatSession) => InitTask,
) = Def.taskDyn[Unit] {
val sources = unmanagedSources.in(scalafmt).?.value.getOrElse(Seq.empty)
val dirs = unmanagedSourceDirectories.in(scalafmt).?.value.getOrElse(Nil)
val sources = (scalafmt / unmanagedSources).?.value.getOrElse(Seq.empty)
val dirs = (scalafmt / unmanagedSourceDirectories).?.value.getOrElse(Nil)
getScalafmtTask(f)(sources, dirs, scalaConfig.value)
}

Expand Down Expand Up @@ -462,10 +464,10 @@ object ScalafmtPlugin extends AutoPlugin {
scalafmtCheck := getScalafmtSourcesTask(scalafmtCheckTask).value,
scalafmtSbtCheck := getScalafmtSbtTasks(scalafmtSbtCheckTask).value,
scalafmtDoFormatOnCompile := Def.settingDyn {
if (scalafmtOnCompile.value) scalafmt.in(resolvedScoped.value.scope)
if (scalafmtOnCompile.value) resolvedScoped.value.scope / scalafmt
else Def.task(())
}.value,
sources.in(Compile) := sources.in(Compile)
Compile / sources := (Compile / sources)
.dependsOn(scalafmtDoFormatOnCompile).value,
scalafmtOnly := {
val files = spaceDelimited("<files>").parsed
Expand Down Expand Up @@ -509,7 +511,7 @@ object ScalafmtPlugin extends AutoPlugin {
)

override def buildSettings: Seq[Def.Setting[_]] =
Seq(scalafmtConfig := baseDirectory.in(ThisBuild).value / ".scalafmt.conf")
Seq(scalafmtConfig := (ThisBuild / baseDirectory).value / ".scalafmt.conf")

override def globalSettings: Seq[Def.Setting[_]] = Seq(
scalafmtFilter := "",
Expand Down

0 comments on commit f512f88

Please sign in to comment.