Skip to content

Commit

Permalink
Fix #331, make is easy to configure custom configurations.
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Nov 1, 2017
1 parent f7e9d10 commit 9f7a49f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
36 changes: 29 additions & 7 deletions scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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],
Expand Down
14 changes: 12 additions & 2 deletions scalafix-sbt/src/sbt-test/sbt-scalafix/cross-build/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Main {
def foo(a: (Int, String)) = a
foo(1, "str")
def main(args: Array[String]) {
println(1)
}
}
4 changes: 4 additions & 0 deletions website/src/main/tut/docs/users/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9f7a49f

Please sign in to comment.