Skip to content

Commit

Permalink
Pick up configuration in sbt plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Dec 13, 2016
1 parent ab34355 commit 40caaa3
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 88 deletions.
1 change: 1 addition & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ build:
- test -d /drone/.ivy2 && cp -a /drone/.ivy2 /root
- rm -rf /drone/.ivy2

- sbt "very publishLocal" # necessary for 2.11/2.12 cross publish.
- sbt clean test scripted

- cp -a /root/.ivy2 /drone
Expand Down
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ lazy val `scalafix-sbt` = project
buildInfoSettings,
ScriptedPlugin.scriptedSettings,
sbtPlugin := true,
scripted := scripted.dependsOn(publishedArtifacts: _*).evaluated,
// Doesn't work because we need to publish 2.11 and 2.12.
// scripted := scripted.dependsOn(publishedArtifacts: _*).evaluated,
scalaVersion := "2.10.5",
crossScalaVersions := Seq(scalaVersion.value),
moduleName := "sbt-scalafix",
scriptedLaunchOpts ++= Seq(
"-Dplugin.version=" + version.value,
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/scala/scalafix/ScalafixConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import scala.meta.dialects.Scala211
import scala.meta.parsers.Parse
import scala.util.control.NonFatal
import scalafix.rewrite.Rewrite
import scalafix.util.logger

import java.io.File

Expand Down Expand Up @@ -53,7 +52,6 @@ object ScalafixConfig {
s"Valid rules are: ${Rewrite.name2rewrite.keys.mkString(",")}")
} else {
val rewrites = names.map(Rewrite.name2rewrite)
logger.elem(rewrites)
Right(ScalafixConfig(rewrites = rewrites))
}
}
Expand Down
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
addSbtPlugin("com.eed3si9n" % "sbt-doge" % "0.1.5")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3")
addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.3.5")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import scala.util.control.NonFatal
import scalafix.Fixed
import scalafix.ScalafixConfig
import scalafix.util.FileOps
import scalafix.util.logger

class ScalafixNscComponent(plugin: Plugin,
val global: Global,
Expand All @@ -24,7 +23,6 @@ class ScalafixNscComponent(plugin: Plugin,
if (unit.source.file.exists &&
unit.source.file.file.isFile &&
!unit.isJava) {
logger.elem(getConfig())
fix(unit, getConfig()) match {
case Fixed.Success(fixed) =>
if (fixed.nonEmpty && fixed != new String(unit.source.content)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ import scala.tools.nsc.Global
import scala.tools.nsc.plugins.Plugin
import scala.tools.nsc.plugins.PluginComponent
import scalafix.ScalafixConfig
import scalafix.rewrite.Rewrite
import scalafix.util.logger

import java.io.File

class ScalafixNscPlugin(val global: Global) extends Plugin {
logger.info("HELLO FROM SCALAFIX!!!")
val name = "scalafix"
val description = "Refactoring tool."
var config: ScalafixConfig = ScalafixConfig()
val components: List[PluginComponent] =
new ScalafixNscComponent(this, global, () => config) :: Nil

override def init(options: List[String], error: (String) => Unit): Boolean = {
logger.elem(options)
options match {
case Nil => true
case file :: Nil =>
Expand Down
21 changes: 12 additions & 9 deletions scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ object ScalafixPlugin extends AutoPlugin with ScalafixKeys {
private val disabled = sys.props.contains("scalafix.disable")
private def jar(report: UpdateReport): Option[File] =
report.allFiles.find { x =>
println("X: " + x)
x.getAbsolutePath.matches(s".*scalafix-nsc_2.1[12].jar$$")
}

Expand Down Expand Up @@ -62,10 +61,12 @@ object ScalafixPlugin extends AutoPlugin with ScalafixKeys {
state
}

override def globalSettings: Seq[Def.Setting[_]] = Seq(
scalafixConfig := None
)
override def projectSettings: Seq[Def.Setting[_]] =
Seq(
commands += scalafix,
scalafixConfig := None,
scalafixInternalJar :=
Def
.taskDyn[Option[File]] {
Expand All @@ -74,11 +75,8 @@ object ScalafixPlugin extends AutoPlugin with ScalafixKeys {
Def.task(jar((update in scalafix211).value))
case Version("12") =>
Def.task(jar((update in scalafix212).value))
case els =>
Def.task {
println("ELS: " + els)
None
}
case _ =>
Def.task(None)
}
}
.value,
Expand All @@ -90,11 +88,16 @@ object ScalafixPlugin extends AutoPlugin with ScalafixKeys {
if (!(scalafixEnabled in Global).value) {
Nil
} else {
val config =
scalafixConfig.value.map { x =>
if (!x.isFile) streams.value.log.warn(s"File does not exist: $x")
s"-P:scalafix:${x.getAbsolutePath}"
}
streams.value.log.info("CONFIG: " + config)
scalafixInternalJar.value.map { jar =>
Seq(
Some(s"-Xplugin:${jar.getAbsolutePath}"),
scalafixConfig.value.map(x =>
s"-P:scalafix:${x.getAbsolutePath}")
config
).flatten
}.getOrElse(Nil)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rewrites = [ExplicitImplicit]
rewrites = [ExplicitImplicit, ProcedureSyntax]
92 changes: 43 additions & 49 deletions scalafix-sbt/src/sbt-test/sbt-scalafix/basic/build.sbt
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
val commonSettings: Seq[Def.Setting[String]] = Seq(
scalaVersion := "2.11.8"
)

scalafixConfig in ThisBuild := Some(baseDirectory.value / ".scalafix.conf")

lazy val root = project
.in(file("."))
.settings(commonSettings)
.aggregate(
p1,
p2,
p3
)

lazy val p1 = project.settings(
commonSettings
)
lazy val p2 = project.settings(
scalaVersion := "2.10.5"
)

lazy val p3 = project.settings(
scalaVersion := "2.12.1"
)
lazy val p1 = project.settings(scalaVersion := "2.11.8")
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 = {
// returns true if test succeeded, else false.
def assertContentMatches(file: String, expectedUntrimmed: String): Boolean = {
val expected = expectedUntrimmed.trim
val obtained = new String(
java.nio.file.Files.readAllBytes(
Expand All @@ -34,44 +23,35 @@ TaskKey[Unit]("check") := {
))
).trim

val msg =
s"""File: $file
|Obtained output:
|$obtained
|Expected:
|$expected
|Diff:
|${obtained.diff(expected)}
|${expected.diff(obtained)}
|""".stripMargin
println(msg)
if (obtained.diff(expected).nonEmpty ||
expected.diff(obtained).nonEmpty) {
val msg =
s"""File: $file
|Obtained output:
|$obtained
|Expected:
|$expected
|Diff:
|${obtained.diff(expected)}
|${expected.diff(obtained)}
|""".stripMargin
streams.value.log.error(file)
throw new Exception(msg)
streams.value.log.error(msg)
false
} else {
streams.value.log.success(file)
true
}
}
val expected =
"""object Main {
| implicit val x: Int = 23
| implicit val x: Int = 2
| lazy val y = 2
| def main(args: Array[String]): Unit = {
| println("hello")
| }
|}""".stripMargin
val testExpected = expected.replaceFirst("Main", "TestMain")
Seq("", "p1/", "p3/").foreach { prefix =>
assertContentMatches(
prefix + "src/test/scala/Test.scala",
testExpected
)
assertContentMatches(
prefix + "src/main/scala/Test.scala",
expected
)
}
val unchanged =
"""object Main {
| implicit val x = 2
Expand All @@ -81,16 +61,30 @@ TaskKey[Unit]("check") := {
| }
|}
""".stripMargin

val unchangedTest = unchanged.replaceFirst("Main", "TestMain")

// 2.10 projects are left unchanged.
assertContentMatches(
"p2/src/main/scala/Test.scala",
unchanged
)
assertContentMatches(
"p2/src/test/scala/Test.scala",
unchangedTest
)
val results: Seq[Boolean] =
Seq("p1/", "p3/").flatMap { prefix =>
Seq(
assertContentMatches(
prefix + "src/test/scala/Test.scala",
testExpected
),
assertContentMatches(
prefix + "src/main/scala/Test.scala",
expected
)
)
} ++ Seq(
// 2.10 projects are left unchanged.
assertContentMatches(
"p2/src/main/scala/Test.scala",
unchanged
),
assertContentMatches(
"p2/src/test/scala/Test.scala",
unchangedTest
)
)
if (results.contains(false)) ??? // non-zero exit code
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
object TestMain {
implicit val x = 2
lazy val y = 2
def main(args: Array[String]) {
println("hello")
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ abstract class IntegrationPropertyTest(t: ItTest, skip: Boolean = false)
write.append(
t.workingPath / "project" / "plugins.sbt",
s"""
|addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "${scalafix.Versions.nightly}")
|addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "${scalafix.Versions.version}")
|""".stripMargin
)
}
Expand Down

0 comments on commit 40caaa3

Please sign in to comment.