Skip to content

Commit

Permalink
introduce scalafixAll to run scalafix across configurations
Browse files Browse the repository at this point in the history
* explicitly scope all value lookups to the current configuration in
  order to prevent scalafixAll from lookuping up in the Zero config
* make sure scalafix and scalafixAll share the same cache for a given
  configuration
  • Loading branch information
github-brice-jaglin committed Jun 13, 2020
1 parent 2ca68e4 commit 57847c7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 17 deletions.
78 changes: 62 additions & 16 deletions src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ object ScalafixPlugin extends AutoPlugin {

val scalafix: InputKey[Unit] =
inputKey[Unit](
"Run scalafix rule in this project and configuration. " +
"Run scalafix rule(s) in this project and configuration. " +
"For example: scalafix RemoveUnusedImports. " +
"To run on test sources use test:scalafix."
"To run on test sources use test:scalafix or scalafixAll."
)
val scalafixAll: InputKey[Unit] =
inputKey[Unit](
"Run scalafix rule(s) in this project, for all configurations where scalafix is enabled. " +
"Compile and Test are enabled by default, other configurations can be enabled via scalafixConfigSettings."
)
val scalafixCaching: SettingKey[Boolean] =
settingKey[Boolean](
Expand Down Expand Up @@ -101,7 +106,10 @@ object ScalafixPlugin extends AutoPlugin {
override lazy val projectSettings: Seq[Def.Setting[_]] =
Seq(Compile, Test).flatMap(c => inConfig(c)(scalafixConfigSettings(c))) ++
inConfig(ScalafixConfig)(Defaults.configSettings) ++
Seq(ivyConfigurations += ScalafixConfig)
Seq(
ivyConfigurations += ScalafixConfig,
scalafixAll := scalafixAllInputTask.evaluated
)

override lazy val globalSettings: Seq[Def.Setting[_]] = Seq(
scalafixConfig := None, // let scalafix-cli try to infer $CWD/.scalafix.conf
Expand Down Expand Up @@ -162,6 +170,31 @@ object ScalafixPlugin extends AutoPlugin {

}

private def scalafixAllInputTask(): Def.Initialize[InputTask[Unit]] =
// workaround https://github.com/sbt/sbt/issues/3572 by invoking directly what Def.inputTaskDyn would via macro
InputTask
.createDyn(InputTask.initParserAsInput(parser))(
Def.task(shellArgs => scalafixAllTask(shellArgs))
)

private def scalafixAllTask(
shellArgs: ShellArgs
): Def.Initialize[Task[Unit]] =
Def.taskDyn {
val configs = Project
.extract(state.value)
.currentProject
.settings
.map(_.key)
.filter(_.key == scalafix.key)
.flatMap(_.scope.config.toOption)

configs
.map(config => scalafixTask(shellArgs, config))
.joinWith(_.join)
.map(_ => ())
}

private def scalafixInputTask(
config: Configuration
): Def.Initialize[InputTask[Unit]] =
Expand All @@ -173,11 +206,16 @@ object ScalafixPlugin extends AutoPlugin {

private def scalafixTask(
shellArgs: ShellArgs,
config: Configuration
config: ConfigKey
): Def.Initialize[Task[Unit]] =
Def.taskDyn {
val errorLogger =
new PrintStream(LoggingOutputStream(streams.value.log, Level.Error))
new PrintStream(
LoggingOutputStream(
streams.in(config, scalafix).value.log,
Level.Error
)
)
val projectDepsInternal = products.in(ScalafixConfig).value ++
internalDependencyClasspath.in(ScalafixConfig).value.map(_.data)
val projectDepsExternal =
Expand All @@ -189,7 +227,7 @@ object ScalafixPlugin extends AutoPlugin {
if (shellArgs.rules.isEmpty && shellArgs.extra == List("--help")) {
scalafixHelp
} else {
val scalafixConf = scalafixConfig.value.map(_.toPath)
val scalafixConf = scalafixConfig.in(config).value.map(_.toPath)
val (shell, mainInterface0) = scalafixArgsFromShell(
shellArgs,
scalafixInterface.value,
Expand All @@ -198,7 +236,9 @@ object ScalafixPlugin extends AutoPlugin {
projectDepsInternal
)
val maybeNoCache =
if (shell.noCache || !scalafixCaching.value) Seq(Arg.NoCache) else Nil
if (shell.noCache || !scalafixCaching.in(config).value)
Seq(Arg.NoCache)
else Nil
val mainInterface = mainInterface0
.withArgs(maybeNoCache: _*)
.withArgs(
Expand Down Expand Up @@ -226,25 +266,28 @@ object ScalafixPlugin extends AutoPlugin {
private def scalafixSyntactic(
mainInterface: ScalafixInterface,
shellArgs: ShellArgs,
config: Configuration
config: ConfigKey
): Def.Initialize[Task[Unit]] =
Def.task {
val files = filesToFix(shellArgs, config).value
runArgs(mainInterface.withArgs(Arg.Paths(files)), streams.value)
runArgs(
mainInterface.withArgs(Arg.Paths(files)),
streams.in(config, scalafix).value
)
}

private def scalafixSemantic(
ruleNames: Seq[String],
mainArgs: ScalafixInterface,
shellArgs: ShellArgs,
config: Configuration
config: ConfigKey
): Def.Initialize[Task[Unit]] =
Def.taskDyn {
val dependencies = allDependencies.value
val dependencies = allDependencies.in(config).value
val files = filesToFix(shellArgs, config).value
val withScalaInterface = mainArgs.withArgs(
Arg.ScalaVersion(scalaVersion.value),
Arg.ScalacOptions(scalacOptions.value)
Arg.ScalacOptions(scalacOptions.in(config).value)
)
val errors = new SemanticRuleValidator(
new SemanticdbNotFound(ruleNames, scalaVersion.value, sbtVersion.value)
Expand All @@ -253,9 +296,12 @@ object ScalafixPlugin extends AutoPlugin {
Def.task {
val semanticInterface = withScalaInterface.withArgs(
Arg.Paths(files),
Arg.Classpath(fullClasspath.value.map(_.data.toPath))
Arg.Classpath(fullClasspath.in(config).value.map(_.data.toPath))
)
runArgs(
semanticInterface,
streams.in(config, scalafix).value
)
runArgs(semanticInterface, streams.value)
}
} else {
Def.task {
Expand Down Expand Up @@ -396,7 +442,7 @@ object ScalafixPlugin extends AutoPlugin {
}
private def filesToFix(
shellArgs: ShellArgs,
config: Configuration
config: ConfigKey
): Def.Initialize[Task[Seq[Path]]] =
Def.taskDyn {
// Dynamic task to avoid redundantly computing `unmanagedSources.value`
Expand All @@ -407,7 +453,7 @@ object ScalafixPlugin extends AutoPlugin {
} else {
Def.task {
for {
source <- unmanagedSources.in(config).in(scalafix).value
source <- unmanagedSources.in(config, scalafix).value
if source.exists()
if isScalaFile(source)
} yield source.toPath
Expand Down
3 changes: 2 additions & 1 deletion src/sbt-test/sbt-scalafix/cross-build/test
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# compile should not affect scalafix
> compile

-> scalafixAll --test ProcedureSyntax
-> scalafix --test ProcedureSyntax
-> compile:scalafix --test ProcedureSyntax
-> test:scalafix --test ProcedureSyntax
Expand All @@ -17,7 +18,7 @@
> test:scalafix --test ProcedureSyntax
-> it:scalafix --test ProcedureSyntax

> it:scalafix ProcedureSyntax
> scalafixAll ProcedureSyntax
> it:scalafix --test ProcedureSyntax

> javaProject/compile:scalafix ProcedureSyntax
Expand Down
4 changes: 4 additions & 0 deletions src/sbt-test/sbt-scalafix/inconfig/test
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
-> ; example/scalafix --test ; example/it:scalafix --test
-> example/scalafixAll --test
> example/it:scalafix
> example/it:scalafix --test
-> example/scalafix --test
> tests/test
> example/scalafixAll
> example/scalafixAll --test
> example/scalafix --test
18 changes: 18 additions & 0 deletions src/sbt-test/skip-windows/caching/test
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ $ exec chmod 000 src/test/scala/Valid.scala
$ delete src/main/scala
$ delete src/test/scala

# scalafixAll and scalafix should share the same cache per configuration
> set scalafixConfig := None
$ mkdir src/main/scala
$ mkdir src/test/scala
$ copy-file files/UnusedImports.scala src/main/scala/Valid.scala
$ copy-file files/ProcedureSyntax.scala src/test/scala/InitiallyInvalid.scala
-> scalafixAll --check ProcedureSyntax
$ exec chmod 000 src/main/scala/Valid.scala
> scalafix --check ProcedureSyntax
-> test:scalafix --check ProcedureSyntax
> test:scalafix ProcedureSyntax
$ exec chmod 000 src/test/scala/InitiallyInvalid.scala
> scalafix --check ProcedureSyntax
> test:scalafix --check ProcedureSyntax
> scalafixAll --check ProcedureSyntax
$ delete src/main/scala
$ delete src/test/scala

# make sure the cache is disabled when using `--stdout`
> set scalafixConfig := None
$ mkdir src/main/scala
Expand Down

0 comments on commit 57847c7

Please sign in to comment.