From d7b2ae84d946ad3b647d01705df3e8718e217f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Fri, 9 Dec 2016 00:17:50 +0000 Subject: [PATCH] Gracefully handle 2.11 and 2.12 in sbt plugin. --- .drone.yml | 11 ++-- build.sbt | 4 +- core/src/main/scala/scalafix/Versions.scala | 2 +- .../scala/scalafix/sbt/ScalafixPlugin.scala | 59 +++++++++++-------- .../src/sbt-test/sbt-scalafix/basic/build.sbt | 9 ++- .../basic/p3/src/main/scala/Test.scala | 7 +++ .../basic/p3/src/test/scala/Test.scala | 7 +++ .../tests/IntegrationPropertyTest.scala | 2 +- 8 files changed, 65 insertions(+), 36 deletions(-) create mode 100644 scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/main/scala/Test.scala create mode 100644 scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/test/scala/Test.scala diff --git a/.drone.yml b/.drone.yml index 3bff3b2ca5..a5d5adee78 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 diff --git a/build.sbt b/build.sbt index 180f41ece6..f3c199f418 100644 --- a/build.sbt +++ b/build.sbt @@ -90,7 +90,7 @@ lazy val root = project `scalafix-nsc`, `scalafix-tests`, core, - cli, +// cli, // superseded by sbt plugin readme, `scalafix-sbt` ) @@ -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 diff --git a/core/src/main/scala/scalafix/Versions.scala b/core/src/main/scala/scalafix/Versions.scala index fb1e4b9e92..79bb397aae 100644 --- a/core/src/main/scala/scalafix/Versions.scala +++ b/core/src/main/scala/scalafix/Versions.scala @@ -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" } diff --git a/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala b/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala index 35576aeb7c..b300508779 100644 --- a/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala +++ b/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala @@ -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 { @@ -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: @@ -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) } } ) diff --git a/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/build.sbt b/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/build.sbt index 4c51949001..b676e213fa 100644 --- a/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/build.sbt +++ b/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/build.sbt @@ -6,7 +6,8 @@ lazy val root = project .settings(commonSettings) .aggregate( p1, - p2 + p2, + p3 ) lazy val p1 = project.settings( @@ -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 @@ -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 diff --git a/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/main/scala/Test.scala b/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/main/scala/Test.scala new file mode 100644 index 0000000000..06e0855ee6 --- /dev/null +++ b/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/main/scala/Test.scala @@ -0,0 +1,7 @@ +object Main { + implicit val x = 2 + lazy val y = 2 + def main(args: Array[String]) { + println("hello") + } +} diff --git a/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/test/scala/Test.scala b/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/test/scala/Test.scala new file mode 100644 index 0000000000..1ebbfbddb7 --- /dev/null +++ b/scalafix-sbt/src/sbt-test/sbt-scalafix/basic/p3/src/test/scala/Test.scala @@ -0,0 +1,7 @@ +object TestMain { + implicit val x = 2 + lazy val y = 2 + def main(args: Array[String]) { + println("hello") + } +} diff --git a/scalafix-tests/src/test/scala/scalafix/tests/IntegrationPropertyTest.scala b/scalafix-tests/src/test/scala/scalafix/tests/IntegrationPropertyTest.scala index e022822f26..759d6a44bf 100644 --- a/scalafix-tests/src/test/scala/scalafix/tests/IntegrationPropertyTest.scala +++ b/scalafix-tests/src/test/scala/scalafix/tests/IntegrationPropertyTest.scala @@ -165,5 +165,5 @@ class ScalacheckShapeless ItTest( name = "scalacheck-shapeless", repo = "https://github.com/alexarchambault/scalacheck-shapeless.git", - hash = "1027b07ea07fe4ca4b1171d55e995d71201b2e6f" + hash = "bb25ecee23c42148f66d9b27920a89ba5cc189d2" ))