Skip to content

Commit

Permalink
Gracefully handle 2.11 and 2.12 in sbt plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Dec 12, 2016
1 parent 772ff62 commit d7b2ae8
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 36 deletions.
11 changes: 5 additions & 6 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
build:
image: danieletorelli/sbt
image: scalaplatform/scala:0.5
environment:
- COURSIER_CACHE=/drone/cache/coursier
commands:
- export SBT_OPTS="-Xmx24G -XX:MaxPermSize=4G -Xss4M"
- sbt ++$SCALA_VERSION clean test scripted
matrix:
SCALA_VERSION:
- 2.11.8
- 2.12.1
- sbt clean test scripted
cache:
mount:
- /drone/.git
- /drone/.sbt
- /drone/.ivy2
- /drone/cache
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ lazy val root = project
`scalafix-nsc`,
`scalafix-tests`,
core,
cli,
// cli, // superseded by sbt plugin
readme,
`scalafix-sbt`
)
Expand All @@ -104,7 +104,7 @@ lazy val core = project
addCompilerPlugin(
"org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full),
libraryDependencies ++= Seq(
"com.lihaoyi" %% "sourcecode" % "0.1.2",
"com.lihaoyi" %% "sourcecode" % "0.1.3",
"org.scalameta" %% "scalameta" % Build.metaV,
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
// Test dependencies
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/scalafix/Versions.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scalafix

object Versions {
val nightly = "0.2.0-RC1"
val nightly = "0.2.0-RC2-SNAPSHOT"
val stable: String = nightly
val scala = "2.11.8"
}
59 changes: 35 additions & 24 deletions scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ trait ScalafixKeys {
settingKey[Seq[String]]("Which scalafix rules should run?")
val scalafixEnabled: SettingKey[Boolean] =
settingKey[Boolean]("Is scalafix enabled?")
val scalafixInternalJar: TaskKey[File] =
taskKey[File]("Path to scalafix-nsc compiler plugin jar.")
val scalafixInternalJar: TaskKey[Option[File]] =
taskKey[Option[File]]("Path to scalafix-nsc compiler plugin jar.")
}

object rewrite {
Expand All @@ -22,49 +22,59 @@ object rewrite {

object ScalafixPlugin extends AutoPlugin with ScalafixKeys {
object autoImport extends ScalafixKeys
private val Version = "2\\.(\\d\\d)\\.".r
private val nightlyVersion = _root_.scalafix.Versions.nightly
private val disabled = sys.props.contains("scalafix.disable")
private val scalafixStub =
private def jar(report: UpdateReport): Option[File] =
report.allFiles.find(
_.getAbsolutePath.matches(s".*scalafix-nsc_2.[12].jar$$"))
private def stub(version: String) =
Project(id = "scalafix-stub", base = file("project/scalafix")).settings(
description :=
"""Serves as a caching layer for extracting the jar location of the
|scalafix-nsc compiler plugin. If the dependecy was added to all
|projects, the (slow) update task will be re-run for every project.""".stripMargin,
scalaVersion := "2.11.8", // TODO(olafur) 2.12 support
libraryDependencies +=
"ch.epfl.scala" %% "scalafix-nsc" % nightlyVersion % Compile
scalaVersion := version,
libraryDependencies ++= Seq(
"ch.epfl.scala" %% "scalafix-nsc" % nightlyVersion
)
)
private val scalafix211 = stub("2.11.8")
private val scalafix212 = stub("2.12.1")

override def extraProjects: Seq[Project] = Seq(scalafixStub)
override def extraProjects: Seq[Project] = Seq(scalafix211, scalafix212)

override def requires = JvmPlugin
override def trigger: PluginTrigger = AllRequirements

val scalafix: Command = Command.command("scalafix") { state =>
s"set scalafixEnabled in Global := true" ::
s"set scalafixEnabled := true" ::
"clean" ::
"test:compile" ::
s"set scalafixEnabled in Global := false" ::
s"set scalafixEnabled := false" ::
state
}

override def projectSettings: Seq[Def.Setting[_]] =
override def globalSettings: Seq[Def.Setting[_]] =
Seq(
commands += scalafix,
scalafixRewrites := Seq(
rewrite.ExplicitImplicit,
rewrite.ProcedureSyntax
),
scalafixInternalJar in Global := {
// TODO(olafur) 2.12 support
(update in scalafixStub).value.allFiles
.find(_.getAbsolutePath.matches(".*scalafix-nsc[^-]*.jar$"))
.getOrElse {
throw new IllegalStateException(
"Unable to find scalafix-nsc in library dependencies!")
scalafixInternalJar :=
Def
.taskDyn[Option[File]] {
scalaVersion.value match {
case Version("11") =>
Def.task(jar((update in scalafix211).value))
case Version("12") =>
Def.task(jar((update in scalafix212).value))
case _ => Def.task(None)
}
}
},
scalafixEnabled in Global := false,
.value,
scalafixEnabled := false,
scalacOptions ++= {
// scalafix should not affect compilations outside of the scalafix task.
// The approach taken here is the same as scoverage uses, see:
Expand All @@ -79,11 +89,12 @@ object ScalafixPlugin extends AutoPlugin with ScalafixKeys {
val prefixed = rewrites.map(x => s"scalafix:$x")
Some(s"-P:${prefixed.mkString(",")}")
}
val jar = (scalafixInternalJar in Global).value.getAbsolutePath
Seq(
Some(s"-Xplugin:$jar"),
config
).flatten
(scalafixInternalJar).value.map { jar =>
Seq(
Some(s"-Xplugin:${jar.getAbsolutePath}"),
config
).flatten
}.getOrElse(Nil)
}
}
)
Expand Down
9 changes: 7 additions & 2 deletions scalafix-sbt/src/sbt-test/sbt-scalafix/basic/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ lazy val root = project
.settings(commonSettings)
.aggregate(
p1,
p2
p2,
p3
)

lazy val p1 = project.settings(
Expand All @@ -16,6 +17,10 @@ lazy val p2 = project.settings(
scalaVersion := "2.10.5"
)

lazy val p3 = project.settings(
scalaVersion := "2.12.1"
)

TaskKey[Unit]("check") := {
def assertContentMatches(file: String, expectedUntrimmed: String): Unit = {
val expected = expectedUntrimmed.trim
Expand Down Expand Up @@ -52,7 +57,7 @@ TaskKey[Unit]("check") := {
| }
|}""".stripMargin
val testExpected = expected.replaceFirst("Main", "TestMain")
Seq("", "p1/").foreach { prefix =>
Seq("", "p1/", "p3/").foreach { prefix =>
assertContentMatches(
prefix + "src/test/scala/Test.scala",
testExpected
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Main {
implicit val x = 2
lazy val y = 2
def main(args: Array[String]) {
println("hello")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object TestMain {
implicit val x = 2
lazy val y = 2
def main(args: Array[String]) {
println("hello")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,5 @@ class ScalacheckShapeless
ItTest(
name = "scalacheck-shapeless",
repo = "https://github.com/alexarchambault/scalacheck-shapeless.git",
hash = "1027b07ea07fe4ca4b1171d55e995d71201b2e6f"
hash = "bb25ecee23c42148f66d9b27920a89ba5cc189d2"
))

0 comments on commit d7b2ae8

Please sign in to comment.