Skip to content

Commit

Permalink
Revive the sbt plugin (#610)
Browse files Browse the repository at this point in the history
* Revert "Remove SBT plugin, see #597. (#598)"

This reverts commit 308f393.

* Revert "Don't run scripted tests on travis"

This reverts commit 983b043.

* Implement sbt plugin as a thin wrapper around CLI.

* Use sbt-buildinfo so that I can use sbt-scalafmt

* Polish sbt pluging and documentation.

* Fix git diff bug.

* Re-enable scoverage plugin to stop flaky tests.

I have no idea why this seems to fix the flaky tests.
  • Loading branch information
olafurpg authored Dec 17, 2016
1 parent d7c7fdf commit 79e17f8
Show file tree
Hide file tree
Showing 34 changed files with 446 additions and 88 deletions.
21 changes: 18 additions & 3 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ build:
image: olafurpg/scalafix:0.0.1
commands:
- export JVM_OPTS="-Xms4000m -Xmx55g -XX:MaxPermSize=2000m -Xss4m -XX:ReservedCodeCacheSize=1024m -XX:+TieredCompilation -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC"
- pip install --user codecov
# configuring ivy.home doesn't seem to work. Maybe related:
# https://github.com/sbt/sbt/issues/1894
# After 10+ experiments I've given up on trying to use sbt.ivy.yhome and
# copy the files myself instead, as recommended here:
# http://readme.drone.io/usage/caching/
- test -d /drone/.sbt && cp -a /drone/.sbt /root
- rm -rf /drone/.sbt

- test -d /drone/.ivy2 && cp -a /drone/.ivy2 /root
- rm -rf /drone/.ivy2

- ./bin/testAll.sh
# I can't be bothered to hide this token.
# - $HOME/.local/bin/codecov -t 5f2117aa-0a01-4cf1-8bf7-631a62ccb47a

- cp -a /root/.ivy2 /drone
- cp -a /root/.sbt /drone
cache:
mount:
- /drone/.sbt
- /drone/.ivy2
- /drone/cache

5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ git:
submodules: false
scala:
- 2.11.8
before_install:
- pip install --user codecov
script:
- "./bin/testAll.sh"
#after_success:
#- "./bin/travisPublish.sh"
#- codecov
notifications:
email:
- [email protected]
Expand Down
2 changes: 1 addition & 1 deletion bin/publish.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -e

version=$(sed -n -e 's/.*val nightly = "\(.*\)"/\1/p' core/src/main/scala/org/scalafmt/Versions.scala)
version=$(sed -n -e 's/.*val nightly = "\(.*\)"/\1/p' core/target/scala-2.11/src_managed/main/sbt-buildinfo/Versions.scala)
tag="v${version}"
tarfile="cli/target/scalafmt.tar.gz"
current_branch=$(git rev-parse --abbrev-ref HEAD)
Expand Down
2 changes: 1 addition & 1 deletion bin/sonatypePublish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ set -e
if [ $TRAVIS_SECURE_ENV_VARS = "true" ]; then
echo "Publishing snapshot..."
# Assert that nighly is set to snapshot
grep "nightly.*SNAPSHOT" core/src/main/scala/org/scalafmt/Versions.scala
grep "nightly.*SNAPSHOT" core/target/scala-2.11/src_managed/main/sbt-buildinfo/Versions.scala
sbt publishSigned
else
echo "Skipping publish"
Expand Down
4 changes: 1 addition & 3 deletions bin/testAll.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/bin/bash
set -e

#sbt clean coverage test
sbt clean test
sbt "core/test:runMain org.scalafmt.FormatExperimentApp"
./bin/issue492.sh
#sbt coverageAggregate
sbt "; publishLocal ; scripted"

2 changes: 1 addition & 1 deletion bin/travisPublish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if [[ ${TRAVIS_SECURE_ENV_VARS} == "true" && ${TRAVIS_BRANCH} == "master" ]]; th
echo "Publishing snapshot..."
setupDeployKey
# only publish if nightly is a snapshot.
if grep "nightly.*SNAPSHOT" core/src/main/scala/org/scalafmt/Versions.scala; then
if grep "nightly.*SNAPSHOT" core/target/scala-2.11/src_managed/main/sbt-buildinfo/Versions.scala; then
echo "Publishing snapshot"
sbt publish # snapshot
else
Expand Down
61 changes: 54 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import scoverage.ScoverageSbtPlugin.ScoverageKeys.coverageHighlighting

// The version number used in docs.
def latestStableVersion: String = "0.4.10"
lazy val buildSettings = Seq(
organization := "com.geirsson",
// See core/src/main/scala/org/scalafmt/Versions.scala
version := org.scalafmt.Versions.nightly,
scalaVersion := org.scalafmt.Versions.scala,
version := "0.4.10-SNAPSHOT",
scalaVersion := "2.11.8",
updateOptions := updateOptions.value.withCachedResolution(true)
)

Expand Down Expand Up @@ -86,6 +87,20 @@ lazy val noPublish = Seq(
publishLocal := {}
)

lazy val buildInfoSettings: Seq[Def.Setting[_]] = Seq(
buildInfoKeys := Seq[BuildInfoKey](
name,
version,
"nightly" -> version.value,
"stable" -> latestStableVersion,
"scala" -> scalaVersion.value,
scalaVersion,
sbtVersion
),
buildInfoPackage := "org.scalafmt",
buildInfoObject := "Versions"
)

lazy val allSettings = commonSettings ++ buildSettings ++ publishSettings

lazy val root = project
Expand All @@ -108,14 +123,16 @@ lazy val root = project
utils,
core,
metaconfig,
readme
readme,
scalafmtSbt
)
.dependsOn(core)

lazy val core = project
.settings(allSettings)
.settings(
allSettings,
metaMacroSettings,
buildInfoSettings,
moduleName := "scalafmt-core",
libraryDependencies ++= Seq(
"com.lihaoyi" %% "sourcecode" % "0.1.2",
Expand All @@ -135,6 +152,7 @@ lazy val core = project
"org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
)
.dependsOn(metaconfig, utils)
.enablePlugins(BuildInfoPlugin)

lazy val cliJvmOptions = Seq(
"-Xss4m"
Expand All @@ -161,10 +179,9 @@ lazy val cli = project
lazy val bootstrap = project
.settings(
allSettings,
buildInfoSettings,
// crossScalaVersions := Seq("2.10.6", "2.11.8"),
moduleName := "scalafmt-bootstrap",
sources in Compile +=
baseDirectory.value / "../core/src/main/scala/org/scalafmt/Versions.scala",
libraryDependencies ++= Seq(
"com.martiansoftware" % "nailgun-server" % "0.9.1",
"io.get-coursier" %% "coursier" % "1.0.0-M14",
Expand All @@ -173,6 +190,36 @@ lazy val bootstrap = project
)
)
.dependsOn(utils)
.enablePlugins(BuildInfoPlugin)

lazy val scalafmtSbt = project
.settings(
allSettings,
buildInfoSettings,
ScriptedPlugin.scriptedSettings,
scripted := scripted
.dependsOn(
publishLocal in cli,
publishLocal in core,
publishLocal in metaconfig,
publishLocal in utils
)
.evaluated,
sbtPlugin := true,
coverageHighlighting := false,
scalaVersion := "2.10.5",
// In convention of sbt plugins, the module is sbt-scalafmt instead of scalafmt-sbt.
moduleName := "sbt-scalafmt",
scriptedLaunchOpts := Seq(
"-Dplugin.version=" + version.value,
// .jvmopts is ignored, simulate here
"-XX:MaxPermSize=256m",
"-Xmx2g",
"-Xss2m"
),
scriptedBufferLog := false
)
.enablePlugins(BuildInfoPlugin)

lazy val benchmarks = project
.settings(moduleName := "scalafmt-benchmarks")
Expand Down
28 changes: 11 additions & 17 deletions cli/src/main/scala/org/scalafmt/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.util.FileOps
import org.scalafmt.util.LogLevel
import com.martiansoftware.nailgun.NGContext
import org.scalafmt.config.FilterMatcher
import org.scalafmt.util.AbsoluteFile
import org.scalafmt.util.logger

object Cli {
def nailMain(nGContext: NGContext): Unit = {
Expand Down Expand Up @@ -77,13 +79,6 @@ object Cli {
CliArgParser.scoptParser.parse(args, init).map(CliOptions.auto(init))
}

private def mkRegexp(filters: Seq[String]): Regex =
filters match {
case Nil => "$a".r // will never match anything
case head :: Nil => head.r
case _ => filters.mkString("(", "|", ")").r
}

def canFormat(path: AbsoluteFile): Boolean =
canFormat(path.path)

Expand All @@ -102,22 +97,20 @@ object Cli {

/** Returns file paths defined via options.{customFiles,customExclude} */
def getFilesFromCliOptions(options: CliOptions): Seq[AbsoluteFile] = {
val exclude = mkRegexp(options.customExcludes)
val exclude = FilterMatcher.mkRegexp(options.customExcludes)
expandCustomFiles(options.common.workingDirectory, options.customFiles)
.filter(x => exclude.findFirstIn(x.path).isEmpty)
}

private def getFilesFromDiff(options: CliOptions): Seq[AbsoluteFile] = {
val branch = options.diff.get
options.gitOps.diff(branch).filter(options.config.project.matcher.matches)
}

/** Returns file paths defined via options.project */
private def getFilesFromProject(options: CliOptions): Seq[AbsoluteFile] = {
val project = options.config.project
val include = mkRegexp(project.includeFilters)
val exclude = mkRegexp(project.excludeFilters)

def matches(file: AbsoluteFile): Boolean = {
val path = file.path
include.findFirstIn(path).isDefined &&
exclude.findFirstIn(path).isEmpty
}
val matcher = project.matcher
val projectFiles: Seq[AbsoluteFile] = (
if (project.git) {
options.gitOps.lsTree
Expand All @@ -130,7 +123,7 @@ object Cli {
options.common.workingDirectory,
AbsoluteFile.fromFiles(project.files.map(new File(_)),
options.common.workingDirectory))
(customFiles ++ projectFiles).filter(matches)
(customFiles ++ projectFiles).filter(matcher.matches)
}

private def getInputMethods(options: CliOptions): Seq[InputMethod] = {
Expand All @@ -139,6 +132,7 @@ object Cli {
} else {
val projectFiles: Seq[AbsoluteFile] =
if (options.customFiles.nonEmpty) getFilesFromCliOptions(options)
else if (options.diff.isDefined) getFilesFromDiff(options)
else getFilesFromProject(options)
projectFiles.map(InputMethod.FileContents.apply)
}
Expand Down
16 changes: 13 additions & 3 deletions cli/src/main/scala/org/scalafmt/cli/CliArgParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ object CliArgParser {
@GitCommit val gitCommit: String = ???
@BuildTime val buildTimeMs: Long = ???

val usageExamples =
val usageExamples: String =
"""|scalafmt # Format all files in the current project, with config determined:
| # 1. .scalafmt.conf inside root directory of current git repo
| # IF the following setting is enabled: project.git = true
| # 2. .scalafmt.conf file in current directory
| # 3. default style
| # from .scalafmt.conf in the root directory, if the file exists.
|scalafmt --test # same as without --test, except
| # 1. throws an exception on the first mis-formatted file
| # 2. does not write to any files.
|scalafmt --diff # same as without --diff, except only format files
| # in git diff against master branch.
|scalafmt --diff --test # same as --test, except only for edited files
| # in git diff against master branch.
|scalafmt --diff-branch 2.x # same as --diff, except against branch 2.x
|scalafmt --diff-branch 2.x --test # same as --diff
|scalafmt --stdin # read from stdin and print to stdout
|scalafmt --stdin --assume-filename foo.sbt # required to format .sbt files
|
|scalafmt -f Code.scala # print formatted contents to stdout.
|scalafmt -i -f Code1.scala,A.scala # write formatted contents to file.
|scalafmt -i -f . --exclude target # format all files in directory excluding target
Expand Down Expand Up @@ -101,6 +105,12 @@ object CliArgParser {
c.copy(migrate =
Some(AbsoluteFile.fromFile(file, c.common.workingDirectory))))
.text("""migrate .scalafmt CLI style configuration to hocon style configuration in .scalafmt.conf""")
opt[Unit]("diff")
.action((_, c) => c.copy(diff = Some("master")))
.text("If set, only format edited files in git diff against master.")
opt[String]("diff-branch")
.action((branch, c) => c.copy(diff = Some(branch)))
.text("If set, only format edited files in git diff against provided branch.")
opt[Unit]("build-info")
.action({
case (_, c) =>
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/scala/org/scalafmt/cli/CliOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object CliOptions {
val style: Option[ScalafmtConfig] = if (init.config != parsed.config) {
Option(parsed.config)
} else {
tryGit(parsed).orElse(tryCurrentDirectory(parsed))
tryCurrentDirectory(parsed).orElse(tryGit(parsed))
}
val inplace =
!parsed.testing && (
Expand Down Expand Up @@ -61,7 +61,6 @@ object CliOptions {
case Left(e) => throw e
}
}
if parsedConfig.project.git
} yield parsedConfig
}

Expand Down Expand Up @@ -89,6 +88,7 @@ case class CliOptions(
inPlace: Boolean = false,
testing: Boolean = false,
stdIn: Boolean = false,
diff: Option[String] = None,
assumeFilename: String = "stdin.scala", // used when read from stdin
migrate: Option[AbsoluteFile] = None,
common: CommonOptions = CommonOptions(),
Expand Down
6 changes: 5 additions & 1 deletion cli/src/main/scala/org/scalafmt/cli/Scalafmt210.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.scalafmt.cli

import scala.util.matching.Regex

import java.io.File

import org.scalafmt.Error.InvalidScalafmtConfiguration
Expand All @@ -15,8 +17,10 @@ import org.scalafmt.util.LoggerOps._
* plugin.
*/
class Scalafmt210 {
val oldConfig = "--".r
val oldConfig: Regex = "--".r
def main(args: Array[String]): Unit = Cli.main(args)

// The rest is for scalafmtIncremental.
def format(code: String, configFile: String, filename: String): String = {
StyleCache.getStyleForFileOrError(configFile) match {
case Left(throwable) => {
Expand Down
16 changes: 9 additions & 7 deletions cli/src/test/scala/org/scalafmt/cli/CliTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,15 @@ class CliTest extends FunSuite with DiffAssertions {
|object A { }
|""".stripMargin

val init: CliOptions = getMockOptions(input)
val config = Cli.getConfig(Array.empty[String], init).get
Cli.run(config)
val obtained = dir2string(input)
assertNoDiff(obtained, expected)
val configTest = Cli.getConfig(Array("--test"), init).get
Cli.run(configTest)
Seq(Array.empty[String], Array("--diff")).foreach { args =>
val init: CliOptions = getMockOptions(input)
val config = Cli.getConfig(args, init).get
Cli.run(config)
val obtained = dir2string(input)
assertNoDiff(obtained, expected)
val configTest = Cli.getConfig(Array("--test"), init).get
Cli.run(configTest)
}
}

test("config is read even from nested dir") {
Expand Down
1 change: 1 addition & 0 deletions cli/src/test/scala/org/scalafmt/cli/FakeGitOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import org.scalafmt.util.logger
class FakeGitOps(root: AbsoluteFile) extends GitOps {
override def lsTree: Vector[AbsoluteFile] = FileOps.listFiles(root)
override def rootDir: Option[AbsoluteFile] = Some(root)
override def diff(branch: String): Seq[AbsoluteFile] = lsTree
}
12 changes: 0 additions & 12 deletions core/src/main/scala/org/scalafmt/Versions.scala

This file was deleted.

Loading

0 comments on commit 79e17f8

Please sign in to comment.