Skip to content

Commit

Permalink
Merge pull request #6 from scalacenter/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
olafurpg authored Sep 5, 2016
2 parents db52438 + 9fbcdb5 commit 833f16d
Show file tree
Hide file tree
Showing 25 changed files with 412 additions and 362 deletions.
3 changes: 3 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
build:
image: olafurpg/scalafix:0.0.1
commands:
- apt-get install nailgun
- alias ng=ng-nailgun
- export PATH=$PATH:/root/local/bin
- pip install --user codecov
- ./bin/testAll.sh
# I can't be bothered to hide this token.
Expand Down
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

41 changes: 41 additions & 0 deletions bin/nailgun_integration_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
set -e

echo "Running nailgun integration test..."

cwd=$(pwd)
cd cli/target/pack
make install
cd $cwd

echoOriginalContents() {
cat << EOF
object app {
def main() {
println(1)
}
}
EOF
}

echoExpectedContents() {
cat << EOF
object app {
def main(): Unit = {
println(1)
}
}
EOF
}
original=target/original.scala
expected=target/expected.scala

mkdir -p target
echoOriginalContents > ${original}
echoExpectedContents > ${expected}
./bin/scalafix_ng -i ${original}

# Assert there is no diff
diff ${original} ${expected}

echo "Done!"
32 changes: 32 additions & 0 deletions bin/scalafix_ng
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -e

function assert-installed() {
binary=$1
command -v ${binary} >/dev/null 2>&1 || { echo >&2 "Missing dependency ${binary}, exiting."; exit 1; }
}

NAILGUN="ng"
if ! type "${NAILGUN}" > /dev/null; then
NAILGUN="ng-nailgun"
fi

assert-installed $NAILGUN
assert-installed scalafix_ng_server

startNailgun() {
echo "Starting nailgun..."
scalafix_ng_server &
sleep 1
$NAILGUN ng-alias scalafix scalafix.cli.Cli
}


if [ "$1" = "restart" ]; then
$NAILGUN ng-stop || true
sleep 1
startNailgun
else
$NAILGUN ng-stats &> /dev/null || startNailgun
$NAILGUN scalafix "$@"
fi
5 changes: 4 additions & 1 deletion bin/testAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
set -e

sbt clean coverage test
sbt "; publishLocal ; scripted"
sbt "; publishLocal ; scripted ; cli/pack"
sbt coverageAggregate

# Integration tests
./bin/nailgun_integration_test.sh

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_server" -> jvmOptions
),
mainClass in assembly := Some("scalafix.cli.Cli"),
packMain := Map(
"scalafix" -> "scalafix.cli.Cli",
"scalafix_ng_server" -> "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))

}
Loading

0 comments on commit 833f16d

Please sign in to comment.