Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
- Optimize imports.
- Setup scalafmt, reformat project.
- Rename LoggerOps to logger.
  • Loading branch information
olafurpg committed Sep 5, 2016
1 parent db52438 commit e6cbc75
Show file tree
Hide file tree
Showing 21 changed files with 291 additions and 299 deletions.
2 changes: 2 additions & 0 deletions .scalafmt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
--assumeStandardLibraryStripMargin true
--alignTokens %;Infix,%%;Infix,%%%;Infix

132 changes: 67 additions & 65 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import sbt.ScriptedPlugin
import sbt.ScriptedPlugin._
import scoverage.ScoverageSbtPlugin.ScoverageKeys._

scalafmtConfig in ThisBuild := Some(file(".scalafmt"))

lazy val buildSettings = Seq(
organization := "ch.epfl.scala",
assemblyJarName in assembly := "scalafix.jar",
// See core/src/main/scala/ch/epfl/scala/Versions.scala
version := scalafix.Versions.nightly,
scalaVersion := scalafix.Versions.scala,
version := scalafix.Versions.nightly,
scalaVersion := scalafix.Versions.scala,
updateOptions := updateOptions.value.withCachedResolution(true)
)

Expand All @@ -17,7 +19,8 @@ lazy val jvmOptions = Seq(

lazy val compilerOptions = Seq(
"-deprecation",
"-encoding", "UTF-8",
"-encoding",
"UTF-8",
"-feature",
"-language:existentials",
"-language:higherKinds",
Expand All @@ -32,7 +35,7 @@ lazy val compilerOptions = Seq(

lazy val commonSettings = Seq(
ScoverageSbtPlugin.ScoverageKeys.coverageExcludedPackages :=
".*Versions;scalafix\\.(sbt|util)",
".*Versions;scalafix\\.(sbt|util)",
triggeredMessage in ThisBuild := Watched.clearWhenTriggered,
scalacOptions in (Compile, console) := compilerOptions :+ "-Yrepl-class-based",
testOptions in Test += Tests.Argument("-oD")
Expand All @@ -47,10 +50,11 @@ lazy val publishSettings = Seq(
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
Some("releases" at nexus + "service/local/staging/deploy/maven2")
},
publishArtifact in Test := false,
licenses := Seq("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")),
licenses := Seq(
"Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")),
homepage := Some(url("https://github.com/scalacenter/scalafix")),
autoAPIMappings := true,
apiURL := Some(url("https://scalacenter.github.io/scalafix/docs/")),
Expand All @@ -77,8 +81,8 @@ lazy val noPublish = Seq(

lazy val allSettings = commonSettings ++ buildSettings ++ publishSettings


lazy val root = project.in(file("."))
lazy val root = project
.in(file("."))
.settings(moduleName := "scalafix")
.settings(allSettings)
.settings(noPublish)
Expand All @@ -102,71 +106,69 @@ lazy val core = project
.settings(
moduleName := "scalafix-core",
libraryDependencies ++= Seq(
"com.lihaoyi" %% "sourcecode" % "0.1.2",
"org.scalameta" %% "scalameta" % "1.0.0",
"com.lihaoyi" %% "sourcecode" % "0.1.2",
"org.scalameta" %% "scalameta" % "1.0.0",
"org.scala-lang" % "scala-reflect" % scalaVersion.value,

// Test dependencies
"org.scalatest" %% "scalatest" % "3.0.0" % "test",
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0" % "test"
"org.scalatest" %% "scalatest" % "3.0.0" % "test",
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0" % "test"
)
)

lazy val cli = project
.settings(allSettings)
.settings(packSettings)
.settings(
moduleName := "scalafix-cli",
packJvmOpts := Map(
"scalafix" -> jvmOptions,
"scalafix_ng" -> jvmOptions
),
mainClass in assembly := Some("scalafix.cli.Cli"),
packMain := Map(
"scalafix" -> "scalafix.cli.Cli",
"scalafix_ng" -> "com.martiansoftware.nailgun.NGServer"
),
libraryDependencies ++= Seq(
"com.github.scopt" %% "scopt" % "3.5.0",
"com.github.alexarchambault" %% "case-app" % "1.0.0-RC3",
"com.martiansoftware" % "nailgun-server" % "0.9.1"
)
.settings(allSettings)
.settings(packSettings)
.settings(
moduleName := "scalafix-cli",
packJvmOpts := Map(
"scalafix" -> jvmOptions,
"scalafix_ng" -> jvmOptions
),
mainClass in assembly := Some("scalafix.cli.Cli"),
packMain := Map(
"scalafix" -> "scalafix.cli.Cli",
"scalafix_ng" -> "com.martiansoftware.nailgun.NGServer"
),
libraryDependencies ++= Seq(
"com.github.scopt" %% "scopt" % "3.5.0",
"com.github.alexarchambault" %% "case-app" % "1.1.0-RC3",
"com.martiansoftware" % "nailgun-server" % "0.9.1"
)
.dependsOn(core % "compile->compile;test->test")
)
.dependsOn(core % "compile->compile;test->test")

lazy val sbtScalafix = project
.settings(allSettings)
.settings(ScriptedPlugin.scriptedSettings)
.settings(
sbtPlugin := true,
coverageHighlighting := false,
scalaVersion := "2.10.5",
moduleName := "sbt-scalafix",
sources in Compile +=
baseDirectory.value / "../core/src/main/scala/scalafix/Versions.scala",
scriptedLaunchOpts := Seq(
"-Dplugin.version=" + version.value,
// .jvmopts is ignored, simulate here
"-XX:MaxPermSize=256m", "-Xmx2g", "-Xss2m"
),
scriptedBufferLog := false
)

.settings(allSettings)
.settings(ScriptedPlugin.scriptedSettings)
.settings(
sbtPlugin := true,
coverageHighlighting := false,
scalaVersion := "2.10.5",
moduleName := "sbt-scalafix",
sources in Compile +=
baseDirectory.value / "../core/src/main/scala/scalafix/Versions.scala",
scriptedLaunchOpts := Seq(
"-Dplugin.version=" + version.value,
// .jvmopts is ignored, simulate here
"-XX:MaxPermSize=256m",
"-Xmx2g",
"-Xss2m"
),
scriptedBufferLog := false
)

lazy val readme = scalatex
.ScalatexReadme(
projectId = "readme",
wd = file(""),
url = "https://github.com/scalacenter/scalafix/tree/master",
source = "Readme")
.settings(allSettings)
.settings(noPublish)
.dependsOn(core)
.dependsOn(cli)
.settings(
libraryDependencies ++= Seq(
"com.twitter" %% "util-eval" % "6.34.0"
),
dependencyOverrides += "com.lihaoyi" %% "scalaparse" % "0.3.1"
)

.ScalatexReadme(projectId = "readme",
wd = file(""),
url = "https://github.com/scalacenter/scalafix/tree/master",
source = "Readme")
.settings(allSettings)
.settings(noPublish)
.dependsOn(core)
.dependsOn(cli)
.settings(
libraryDependencies ++= Seq(
"com.twitter" %% "util-eval" % "6.34.0"
),
dependencyOverrides += "com.lihaoyi" %% "scalaparse" % "0.3.1"
)
35 changes: 35 additions & 0 deletions cli/src/main/scala/scalafix/cli/ArgParserImplicits.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package scalafix.cli

import scalafix.rewrite.ProcedureSyntax
import scalafix.rewrite.Rewrite

import java.io.InputStream
import java.io.PrintStream

import caseapp.core.ArgParser

object ArgParserImplicits {
def nameMap[T](t: sourcecode.Text[T]*): Map[String, T] = {
t.map(x => x.source -> x.value).toMap
}

val rewriteMap: Map[String, Rewrite] = nameMap(
ProcedureSyntax
)
implicit val rewriteRead: ArgParser[Rewrite] = ArgParser.instance[Rewrite] {
str =>
rewriteMap.get(str) match {
case Some(x) => Right(x)
case _ =>
Left(
s"invalid input $str, must be one of ${rewriteMap.keys.mkString(", ")}")
}
}

implicit val inputStreamRead: ArgParser[InputStream] =
ArgParser.instance[InputStream](x => Right(System.in))

implicit val printStreamRead: ArgParser[PrintStream] =
ArgParser.instance[PrintStream](x => Right(System.out))

}
57 changes: 15 additions & 42 deletions cli/src/main/scala/scalafix/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,38 @@ package scalafix.cli
import scala.collection.GenSeq
import scalafix.FixResult
import scalafix.Scalafix
import scalafix.rewrite.ProcedureSyntax
import scalafix.cli.ArgParserImplicits._
import scalafix.rewrite.Rewrite
import scalafix.util.FileOps
import scalafix.util.LoggerOps._

import java.io.File
import java.io.InputStream
import java.io.OutputStreamWriter
import java.io.PrintStream
import java.util.concurrent.atomic.AtomicInteger

import caseapp.core.ArgParser
import caseapp._
import caseapp.core.Messages
import com.martiansoftware.nailgun.NGContext
import caseapp._
import ScalafixOptions._

@AppName("scalafix")
@AppVersion(scalafix.Versions.nightly)
@ProgName("scalafix")
case class ScalafixOptions(
@HelpMessage(
s"Rules to run, one of: ${Rewrite.default.mkString(", ")}"
s"Rules to run, one of: ${Rewrite.default.mkString(", ")}"
) rewrites: List[Rewrite] = Rewrite.default,
@Hidden @HelpMessage(
"Files to fix. Runs on all *.scala files if given a directory."
"Files to fix. Runs on all *.scala files if given a directory."
) @ExtraName("f") files: List[String] = List.empty[String],
@HelpMessage(
"If true, writes changes to files instead of printing to stdout."
"If true, writes changes to files instead of printing to stdout."
) @ExtraName("i") inPlace: Boolean = false,
@HelpMessage(
"If true, uses all available CPUs. If false, runs in single thread."
"If true, uses all available CPUs. If false, runs in single thread."
) parallel: Boolean = true,
@HelpMessage(
"If true, prints out debug information."
"If true, prints out debug information."
) debug: Boolean = false,
@Hidden workingDirectory: String = System.getProperty("user.dir"),
@Hidden out: PrintStream = System.out,
Expand All @@ -47,31 +44,7 @@ case class ScalafixOptions(
Cli.runOn(this)
}

object ScalafixOptions {
def nameMap[T](t: sourcecode.Text[T]*): Map[String, T] = {
t.map(x => x.source -> x.value).toMap
}

val rewriteMap: Map[String, Rewrite] = nameMap(
ProcedureSyntax
)
implicit val rewriteRead: ArgParser[Rewrite] = ArgParser.instance[Rewrite] {
str =>
rewriteMap.get(str) match {
case Some(x) => Right(x)
case _ =>
Left(
s"invalid input $str, must be one of ${rewriteMap.keys.mkString(", ")}")
}
}

implicit val inputStreamRead: ArgParser[InputStream] =
ArgParser.instance[InputStream](x => Right(System.in))

implicit val printStreamRead: ArgParser[PrintStream] =
ArgParser.instance[PrintStream](x => Right(System.out))

}
object ScalafixOptions {}

object Cli extends AppOf[ScalafixOptions] {
val helpMessage: String = Messages[ScalafixOptions].withHelp.helpMessage
Expand Down Expand Up @@ -141,13 +114,13 @@ object Cli extends AppOf[ScalafixOptions] {

def nailMain(nGContext: NGContext): Unit = {
runMain(
nGContext.getArgs,
ScalafixOptions(
workingDirectory = nGContext.getWorkingDirectory,
out = nGContext.out,
in = nGContext.in,
err = nGContext.err
)
nGContext.getArgs,
ScalafixOptions(
workingDirectory = nGContext.getWorkingDirectory,
out = nGContext.out,
in = nGContext.in,
err = nGContext.err
)
)
}

Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/scala/scalafix/cli/Scalafix210.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package scalafix.cli
import scalafix.FixResult
import scalafix.Scalafix
import scalafix.rewrite.Rewrite
import scalafix.util.logger

class Scalafix210 {
def fix(originalContents: String, filename: String): String = {
Scalafix.fix(originalContents, Rewrite.default) match {
case FixResult.Success(fixedCode) => fixedCode
case FixResult.Error(e) =>
import scalafix.util.LoggerOps._
logger.warn(s"Failed to fix $filename. Cause ${e.getMessage}")
originalContents
}
Expand Down
Loading

0 comments on commit e6cbc75

Please sign in to comment.