From f8696e7a1df06736126a561a436010d03ee81c93 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] Test 2.12 support --- build.sbt | 4 +- .../scala/scalafix/sbt/ScalafixPlugin.scala | 48 +++++++++++-------- .../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 +++ 5 files changed, 51 insertions(+), 24 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/build.sbt b/build.sbt index 4e898fcc65..f5085ea6f8 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/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala b/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala index 35576aeb7c..694a7d7fe7 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,20 +22,27 @@ 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 @@ -48,7 +55,7 @@ object ScalafixPlugin extends AutoPlugin with ScalafixKeys { state } - override def projectSettings: Seq[Def.Setting[_]] = + override def globalSettings: Seq[Def.Setting[_]] = Seq( commands += scalafix, scalafixRewrites := Seq( @@ -56,13 +63,13 @@ object ScalafixPlugin extends AutoPlugin with ScalafixKeys { 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!") - } + scalaVersion.value match { + case Version("11") => + jar((update in scalafix211).value) + case Version("12") => + jar((update in scalafix212).value) + case _ => None + } }, scalafixEnabled in Global := false, scalacOptions ++= { @@ -79,11 +86,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 in Global).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") + } +}