Skip to content

Commit

Permalink
Setup hocon configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Dec 13, 2016
1 parent c94fef1 commit a5b666a
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 50 deletions.
1 change: 1 addition & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ build:

- cp -a /root/.ivy2 /drone
- cp -a /root/.sbt /drone
- sbt clean test scripted
cache:
mount:
- /drone/.sbt
Expand Down
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ lazy val core = project
addCompilerPlugin(
"org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full),
libraryDependencies ++= Seq(
"com.typesafe" % "config" % "1.3.1",
"com.lihaoyi" %% "sourcecode" % "0.1.3",
"org.scalameta" %% "scalameta" % Build.metaV,
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
Expand Down
31 changes: 29 additions & 2 deletions core/src/main/scala/scalafix/ScalafixConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import scala.meta.Dialect
import scala.meta.Tree
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

import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory

case class ScalafixConfig(
rewrites: Seq[Rewrite] = Rewrite.allRewrites,
Expand All @@ -13,7 +20,27 @@ case class ScalafixConfig(
)

object ScalafixConfig {
def fromNames(names: List[String]): Either[String, ScalafixConfig] = {
private def saferThanTypesafe(
op: () => Config): Either[String, ScalafixConfig] =
try fromConfig(op())
catch {
case NonFatal(e) => Left(e.getMessage)
}

def fromFile(file: File): Either[String, ScalafixConfig] =
saferThanTypesafe(() => ConfigFactory.parseFile(file))

def fromString(contents: String): Either[String, ScalafixConfig] =
saferThanTypesafe(() => ConfigFactory.parseString(contents))

def fromConfig(config: Config): Either[String, ScalafixConfig] = {
import scala.collection.JavaConverters._
if (config.hasPath("rewrites"))
fromNames(config.getStringList("rewrites").asScala.toList)
else Right(ScalafixConfig())
}

def fromNames(names: List[String]): Either[String, ScalafixConfig] =
names match {
case "all" :: Nil =>
Right(ScalafixConfig(rewrites = Rewrite.allRewrites))
Expand All @@ -26,8 +53,8 @@ object ScalafixConfig {
s"Valid rules are: ${Rewrite.name2rewrite.keys.mkString(",")}")
} else {
val rewrites = names.map(Rewrite.name2rewrite)
logger.elem(rewrites)
Right(ScalafixConfig(rewrites = rewrites))
}
}
}
}
2 changes: 1 addition & 1 deletion core/src/test/resources/ExplicitImplicit/basic.source
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// foo
rewrites = [ExplicitImplicit]
<<< list
class A {
implicit val x = List(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 @@ -23,6 +24,7 @@ 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
27 changes: 19 additions & 8 deletions scalafix-nsc/src/main/scala/scalafix/nsc/ScalafixNscPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,33 @@ 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(rewrites = Rewrite.allRewrites)
var config: ScalafixConfig = ScalafixConfig()
val components: List[PluginComponent] =
new ScalafixNscComponent(this, global, () => config) :: Nil

override def init(options: List[String], error: (String) => Unit): Boolean = {
ScalafixConfig.fromNames(options) match {
case Left(msg) =>
error(msg)
false
case Right(userConfig) =>
config = userConfig
true
logger.elem(options)
options match {
case Nil => true
case file :: Nil =>
ScalafixConfig.fromFile(new File(file)) match {
case Left(msg) =>
error(msg)
false
case Right(userConfig) =>
config = userConfig
true
}
case els =>
super.init(els, error)
}
}
}
5 changes: 4 additions & 1 deletion scalafix-nsc/src/test/scala/scalafix/DiffTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ object DiffTest {

val style: ScalafixConfig = {
val firstLine = split.head
ScalafixConfig(rewrites = List(ExplicitImplicit))
ScalafixConfig.fromString(firstLine) match {
case Right(x) => x
case Left(e) => throw new IllegalArgumentException(e)
}
}

split.tail.map { t =>
Expand Down
10 changes: 5 additions & 5 deletions scalafix-nsc/src/test/scala/scalafix/SemanticTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class SemanticTests extends FunSuite {

private object fixer extends NscSemanticApi {
lazy val global: SemanticTests.this.g.type = SemanticTests.this.g
def apply(unit: g.CompilationUnit): Fixed =
fix(unit, ScalafixConfig(rewrites = List(rewrite)))
def apply(unit: g.CompilationUnit, config: ScalafixConfig): Fixed =
fix(unit, config)
}

def wrap(code: String, diffTest: DiffTest): String = {
Expand Down Expand Up @@ -79,8 +79,8 @@ class SemanticTests extends FunSuite {
unit
}

def fix(code: String): String = {
val Fixed.Success(fixed) = fixer(getTypedCompilationUnit(code))
def fix(code: String, config: ScalafixConfig): String = {
val Fixed.Success(fixed) = fixer(getTypedCompilationUnit(code), config)
fixed
}
case class MismatchException(details: String) extends Exception
Expand Down Expand Up @@ -141,7 +141,7 @@ class SemanticTests extends FunSuite {
}

def check(original: String, expectedStr: String, diffTest: DiffTest): Unit = {
val fixed = fix(wrap(original, diffTest))
val fixed = fix(wrap(original, diffTest), diffTest.config)
val obtained = parse(fixed)
val expected = parse(expectedStr)
try {
Expand Down
40 changes: 18 additions & 22 deletions scalafix-sbt/src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import sbt._
import sbt.plugins.JvmPlugin

trait ScalafixKeys {
val scalafixRewrites: SettingKey[Seq[String]] =
settingKey[Seq[String]]("Which scalafix rules should run?")
val scalafixConfig: SettingKey[Option[File]] =
settingKey[Option[File]](
".scalafix.conf file to specify which scalafix rules should run.")
val scalafixEnabled: SettingKey[Boolean] =
settingKey[Boolean]("Is scalafix enabled?")
val scalafixInternalJar: TaskKey[Option[File]] =
Expand All @@ -22,7 +23,7 @@ object rewrite {

object ScalafixPlugin extends AutoPlugin with ScalafixKeys {
object autoImport extends ScalafixKeys
private val Version = "2\\.(\\d\\d)\\.".r
private val Version = "2\\.(\\d\\d)\\..*".r
private val nightlyVersion = _root_.scalafix.Versions.nightly
private val disabled = sys.props.contains("scalafix.disable")
private def jar(report: UpdateReport): Option[File] =
Expand All @@ -48,20 +49,17 @@ object ScalafixPlugin extends AutoPlugin with ScalafixKeys {
override def trigger: PluginTrigger = AllRequirements

val scalafix: Command = Command.command("scalafix") { state =>
s"set scalafixEnabled := true" ::
s"set scalafixEnabled in Global := true" ::
"clean" ::
"test:compile" ::
s"set scalafixEnabled := false" ::
s"set scalafixEnabled in Global := false" ::
state
}

override def globalSettings: Seq[Def.Setting[_]] =
override def projectSettings: Seq[Def.Setting[_]] =
Seq(
commands += scalafix,
scalafixRewrites := Seq(
rewrite.ExplicitImplicit,
rewrite.ProcedureSyntax
),
scalafixConfig := None,
scalafixInternalJar :=
Def
.taskDyn[Option[File]] {
Expand All @@ -70,29 +68,27 @@ object ScalafixPlugin extends AutoPlugin with ScalafixKeys {
Def.task(jar((update in scalafix211).value))
case Version("12") =>
Def.task(jar((update in scalafix212).value))
case _ => Def.task(None)
case els =>
Def.task {
println("ELS: " + els)
None
}
}
}
.value,
scalafixEnabled := false,
scalafixEnabled in Global := false,
scalacOptions ++= {
// scalafix should not affect compilations outside of the scalafix task.
// The approach taken here is the same as scoverage uses, see:
// https://github.com/scoverage/sbt-scoverage/blob/45ac49583f5a32dfebdce23b94c5336da4906e59/src/main/scala/scoverage/ScoverageSbtPlugin.scala#L70-L83
if (!scalafixEnabled.value || scalaVersion.value.startsWith("2.10")) {
if (!(scalafixEnabled in Global).value) {
Nil
} else {
val rewrites = scalafixRewrites.value
val config: Option[String] =
if (rewrites.isEmpty) None
else {
val prefixed = rewrites.map(x => s"scalafix:$x")
Some(s"-P:${prefixed.mkString(",")}")
}
(scalafixInternalJar).value.map { jar =>
scalafixInternalJar.value.map { jar =>
Seq(
Some(s"-Xplugin:${jar.getAbsolutePath}"),
config
scalafixConfig.value.map(x =>
s"-P:scalafix:${x.getAbsolutePath}")
).flatten
}.getOrElse(Nil)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rewrites = [ExplicitImplicit]
27 changes: 16 additions & 11 deletions scalafix-sbt/src/sbt-test/sbt-scalafix/basic/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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)
Expand Down Expand Up @@ -31,17 +34,19 @@ TaskKey[Unit]("check") := {
))
).trim

if (obtained.diff(expected).nonEmpty) {
val msg =
s"""File: $file
|Obtained output:
|$obtained
|Expected:
|$expected
|Diff:
|${obtained.diff(expected)}
|${expected.diff(obtained)}
|""".stripMargin
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) {
streams.value.log.error(file)
throw new Exception(msg)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sbt.version=0.13.13

0 comments on commit a5b666a

Please sign in to comment.