diff --git a/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala b/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala index 509f9d59a..a95fcec3e 100644 --- a/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala +++ b/scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala @@ -28,6 +28,19 @@ object ScalafixPlugin extends AutoPlugin { val scalafixConfig: SettingKey[Option[File]] = settingKey[Option[File]]( ".scalafix.conf file to specify which scalafix rules should run.") + + /** Configures the scalafix/scalafixTest commands to run in configs. */ + def scalafixConfigure(configs: Configuration*): Seq[Setting[_]] = + List( + configureForConfigurations( + configs, + scalafix, + c => scalafixTaskImpl(c, Nil)), + configureForConfigurations( + configs, + scalafixTest, + c => scalafixTaskImpl(c, Seq("--test"))) + ).flatten val scalafixEnabled: SettingKey[Boolean] = settingKey[Boolean]( "If false, scalafix will not enable the semanticdb-scalac compiler plugin, which is necessary for semantic rules.") @@ -174,18 +187,27 @@ object ScalafixPlugin extends AutoPlugin { } } ) + lazy val scalafixTaskSettings: Seq[Def.Setting[InputTask[Unit]]] = - configureForCompileAndTest(scalafix, scalafixTaskImpl(_)) + configureForCompileAndTest(scalafix, c => scalafixTaskImpl(c)) lazy val scalafixTestTaskSettings: Seq[Def.Setting[InputTask[Unit]]] = - configureForCompileAndTest(scalafixTest, scalafixTaskImpl(_, Seq("--test"))) + configureForCompileAndTest( + scalafixTest, + c => scalafixTaskImpl(c, Seq("--test"))) + + def configureForConfigurations( + configurations: Seq[Configuration], + task: InputKey[Unit], + impl: Seq[Configuration] => Def.Initialize[InputTask[Unit]] + ): Seq[Def.Setting[InputTask[Unit]]] = + (task := impl(configurations).evaluated) +: + configurations.map(c => task.in(c) := impl(Seq(c)).evaluated) def configureForCompileAndTest( task: InputKey[Unit], - impl: Seq[Configuration] => Def.Initialize[InputTask[Unit]]) = Seq( - task.in(Compile) := impl(Seq(Compile)).evaluated, - task.in(Test) := impl(Seq(Test)).evaluated, - task := impl(Seq(Compile, Test)).evaluated - ) + impl: Seq[Configuration] => Def.Initialize[InputTask[Unit]] + ): Seq[Def.Setting[InputTask[Unit]]] = + configureForConfigurations(List(Compile, Test), task, impl) def scalafixTaskImpl( config: Seq[Configuration], diff --git a/scalafix-sbt/src/sbt-test/sbt-scalafix/cross-build/build.sbt b/scalafix-sbt/src/sbt-test/sbt-scalafix/cross-build/build.sbt index 42bfa8e0a..31c76466e 100644 --- a/scalafix-sbt/src/sbt-test/sbt-scalafix/cross-build/build.sbt +++ b/scalafix-sbt/src/sbt-test/sbt-scalafix/cross-build/build.sbt @@ -17,7 +17,13 @@ lazy val root = project lazy val scala210 = project.settings(scalaVersion := "2.10.5") lazy val scala211 = project.settings(scalaVersion := Versions.scala211) -lazy val scala212 = project.settings(scalaVersion := Versions.scala212) +lazy val scala212 = project + .configs(IntegrationTest) + .settings( + Defaults.itSettings, + scalafixConfigure(Test, Compile, IntegrationTest), + scalaVersion := Versions.scala212 + ) lazy val customSourceroot = project.settings( scalaVersion := Versions.scala212, scalafixSourceroot := sourceDirectory.value @@ -44,7 +50,11 @@ TaskKey[Unit]("check") := { .replace("\"))", "\")") val results: Seq[Boolean] = - Seq(scala210, scala211, scala212, customSourceroot).flatMap { project => + assertContentMatches( + "scala212/src/it/scala/Main.scala", + expected + ) +: + Seq(scala210, scala211, scala212, customSourceroot).flatMap { project => val prefix = project.id Seq( assertContentMatches( diff --git a/scalafix-sbt/src/sbt-test/sbt-scalafix/cross-build/scala212/src/it/scala/Main.scala b/scalafix-sbt/src/sbt-test/sbt-scalafix/cross-build/scala212/src/it/scala/Main.scala new file mode 100644 index 000000000..4abc907dc --- /dev/null +++ b/scalafix-sbt/src/sbt-test/sbt-scalafix/cross-build/scala212/src/it/scala/Main.scala @@ -0,0 +1,7 @@ +object Main { + def foo(a: (Int, String)) = a + foo(1, "str") + def main(args: Array[String]) { + println(1) + } +} diff --git a/website/src/main/tut/docs/users/installation.md b/website/src/main/tut/docs/users/installation.md index bf9d962e7..cccd1f860 100644 --- a/website/src/main/tut/docs/users/installation.md +++ b/website/src/main/tut/docs/users/installation.md @@ -27,6 +27,10 @@ scalacOptions ++= List(...) // change := to ++= scalacOptions := List(...) // keep unchanged scalacOptions ++= scalafixScalacOptions.value // add this line +// enable scalafix in custom configurations, only Test and Compile are +// enabled by default. +scalafixConfigure(Compile, Test, IntegrationTest) + // ===> sbt shell (example usage) > scalafix // Run .scalafix.conf rules > scalafix RemoveUnusedImports // Run specific rule