Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ditch synthetic project in favor of coursier. #658

Merged
merged 1 commit into from
Dec 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
align.tokens = [
{ code = "%", owner = "Infix" }
{ code = "%%", owner = "Infix" }
]
assumeStandardLibraryStripMargin = true
optIn.breakChainOnFirstMethodDot = true
project.git = true
project.excludeFilters = [
benchmarks/src/resources,
target
sbt-test
bin/issue
metaconfig/src/main/scala
]
project.git = true
# These are disabled by default because vertical
# alignment still has some false positives/negatives.
align.tokens = [
"%"
"%%"
"%%%"
]
# Disabled in default since this operation is potentially
# dangerous if you define your own stripMargin with different
# semantics from the stdlib stripMargin.
assumeStandardLibraryStripMargin = true
75 changes: 20 additions & 55 deletions bootstrap/src/main/scala/org/scalafmt/bootstrap/Bootstrap.scala
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
package org.scalafmt.bootstrap

import scala.language.reflectiveCalls
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, File, PrintStream}
import java.net.URLClassLoader

import coursier._
import org.scalafmt.Versions

import scala.collection.immutable.Nil
import scala.collection.mutable
import scala.util.Failure
import scala.util.Success
import scala.util.Try
import scalaz.\/
import scalaz.\/-
import scala.language.reflectiveCalls
import scala.util.{Failure, Success}
import scalaz.{\/, \/-}
import scalaz.concurrent.Task

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream
import java.net.URLClassLoader

import com.typesafe.config.ConfigFactory
import coursier._
import org.scalafmt.util.AbsoluteFile
import org.scalafmt.util.GitOps

class FetchError(errors: Seq[(Dependency, Seq[String])])
extends Exception(errors.toString())

sealed abstract class Scalafmt(val cli: ScalafmtCli) {
sealed abstract class ScalafmtBootstrap(val cli: ScalafmtCli) {

def main(array: Array[String]): Unit = {
cli.main(array)
def main(array: Seq[String]): Unit = {
cli.main(array.to[Array])
}

def format(code: String): String = {
Expand All @@ -45,39 +36,15 @@ sealed abstract class Scalafmt(val cli: ScalafmtCli) {
}
}

object DefinesVersion {
def unapply(arg: AbsoluteFile): Option[String] =
Scalafmt.getVersion(arg / ".scalafmt.conf")
}

object Scalafmt {
private val cliCache = mutable.Map.empty[String, Either[Throwable, Scalafmt]]

def getVersion(file: AbsoluteFile): Option[String] =
Try {
val config = ConfigFactory.parseFile(file.jfile)
config.getString("version")
}.toOption
object ScalafmtBootstrap {
private val cliCache =
mutable.Map.empty[String, Either[Throwable, ScalafmtBootstrap]]

def fromGitOps(gitOps: GitOps): Either[Throwable, Scalafmt] = {
val version = Seq(Some(AbsoluteFile.userDir),
gitOps.rootDir,
Some(AbsoluteFile.homeDir))
.collectFirst {
case Some(DefinesVersion(v)) => v
}
.getOrElse(org.scalafmt.Versions.stable)
fromVersion(version)
}

def fromAuto: Either[Throwable, Scalafmt] = {
fromGitOps(GitOps())
}

def fromVersion(version: String): Either[Throwable, Scalafmt] =
def fromVersion(version: String): Either[Throwable, ScalafmtBootstrap] =
cliCache.getOrElseUpdate(version, fromVersionUncached(version))

def fromVersionUncached(version: String): Either[Throwable, Scalafmt] = {
def fromVersionUncached(
version: String): Either[Throwable, ScalafmtBootstrap] = {
val start =
Resolution(
Set(
Expand Down Expand Up @@ -109,16 +76,14 @@ object Scalafmt {
reflectiveDynamicAccess
.createInstanceFor[ScalafmtCli]("org.scalafmt.cli.Cli$", Nil)
loadedClass match {
case Success(cli) => Right(new Scalafmt(cli) {})
case Success(cli) => Right(new ScalafmtBootstrap(cli) {})
case Failure(e) => Left(e)
}
}
}
}

object Bootstrap {
def main(args: Array[String]): Unit = {
Scalafmt.fromAuto match {
def main(args: Seq[String]): Unit = {
fromVersion(Versions.nightly) match {
case Right(cli) => cli.main(args)
case Left(e) => throw e
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class BootstrapTest extends FunSuite {
ignore("bootstrap works") {
val original = "object A { }"
val expected = "object A {}\n\n"
val Right(cli) = Scalafmt.fromVersion("0.4.9-RC3")
val Right(cli) = ScalafmtBootstrap.fromVersion("0.4.9-RC3")
val obtained = cli.format(original)
assert(obtained === expected)
}
Expand Down
14 changes: 7 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scoverage.ScoverageSbtPlugin.ScoverageKeys.coverageHighlighting
def latestStableVersion: String = "0.5.1"
lazy val buildSettings = Seq(
organization := "com.geirsson",
version := "0.5.1",
version := "0.5.2-SNAPSHOT",
scalaVersion := "2.11.8",
updateOptions := updateOptions.value.withCachedResolution(true)
)
Expand Down Expand Up @@ -94,6 +94,7 @@ lazy val buildInfoSettings: Seq[Def.Setting[_]] = Seq(
"nightly" -> version.value,
"stable" -> latestStableVersion,
"scala" -> scalaVersion.value,
"coursier" -> Deps.coursier,
scalaVersion,
sbtVersion
),
Expand Down Expand Up @@ -182,28 +183,27 @@ lazy val bootstrap = project
.settings(
allSettings,
buildInfoSettings,
// crossScalaVersions := Seq("2.10.6", "2.11.8"),
scalaVersion := "2.10.6",
moduleName := "scalafmt-bootstrap",
libraryDependencies ++= Seq(
"com.martiansoftware" % "nailgun-server" % "0.9.1",
"io.get-coursier" %% "coursier" % "1.0.0-M14",
"io.get-coursier" %% "coursier-cache" % "1.0.0-M14",
"io.get-coursier" %% "coursier" % Deps.coursier,
"io.get-coursier" %% "coursier-cache" % Deps.coursier,
"org.scalatest" %% "scalatest" % Deps.scalatest % Test
)
)
.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 bootstrap,
publishLocal in utils
)
.evaluated,
Expand All @@ -221,7 +221,7 @@ lazy val scalafmtSbt = project
),
scriptedBufferLog := false
)
.enablePlugins(BuildInfoPlugin)
.dependsOn(bootstrap)

lazy val intellij = project
.settings(
Expand Down
16 changes: 13 additions & 3 deletions cli/src/main/scala/org/scalafmt/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.scalafmt.cli

import scala.meta.Dialect
import scala.meta.dialects.Sbt0137

import java.io.File
import java.io.InputStream
import java.io.OutputStreamWriter
Expand All @@ -13,7 +12,7 @@ import com.martiansoftware.nailgun.NGContext
import org.scalafmt.Error.UnableToParseCliOptions
import org.scalafmt.Formatted
import org.scalafmt.Scalafmt
import org.scalafmt.config.FilterMatcher
import org.scalafmt.config.{Config, FilterMatcher}
import org.scalafmt.util.AbsoluteFile
import org.scalafmt.util.FileOps
import org.scalafmt.util.LogLevel
Expand Down Expand Up @@ -174,7 +173,9 @@ object Cli {
msg: String): TermDisplay = {
val termDisplay = new TermDisplay(
new OutputStreamWriter(options.common.err))
if ((options.inPlace || options.testing) && inputMethods.length > 5) {
if (!options.quiet &&
(options.inPlace || options.testing) &&
inputMethods.length > 5) {
termDisplay.init()
termDisplay.startTask(msg, options.common.workingDirectory.jfile)
termDisplay.taskLength(msg, inputMethods.length, 0)
Expand All @@ -188,6 +189,15 @@ object Cli {
val termDisplayMessage =
if (options.testing) "Looking for unformatted files..."
else "Reformatting..."
if (options.debug) {
val pwd = options.common.workingDirectory.jfile.getPath
options.common.err.println("Working directory: " + pwd)
options.common.err.println("Formatting files: " + inputMethods.toList)
options.common.err.println(
"Configuration: \n" + Config
.toHocon(options.config.fields)
.mkString("\n"))
}

val sbtOptions = options.copy(
config = options.config.copy(
Expand Down
6 changes: 6 additions & 0 deletions cli/src/main/scala/org/scalafmt/cli/CliArgParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ object CliArgParser {
sys.exit
})
.text("prints build information")
opt[Unit]("quiet")
.action((_, c) => c.copy(quiet = true))
.text("don't print out stuff to console.")
opt[Unit]("debug")
.action((_, c) => c.copy(debug = true))
.text("print out diagnostics to console.")
opt[(Int, Int)]("range")
.hidden()
.action({
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main/scala/org/scalafmt/cli/CliOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ case class CliOptions(
inPlace: Boolean = false,
testing: Boolean = false,
stdIn: Boolean = false,
quiet: Boolean = false,
debug: Boolean = false,
diff: Option[String] = None,
assumeFilename: String = "stdin.scala", // used when read from stdin
migrate: Option[AbsoluteFile] = None,
Expand Down
1 change: 1 addition & 0 deletions project/Deps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ object Deps {
val scalameta = "1.3.0"
val scalatest = "2.2.1"
val scalariform = "0.1.8"
val coursier = "1.0.0-M15-1"
}
8 changes: 5 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
resolvers ++= Seq(Classpaths.sbtPluginReleases, Resolver.url("dancingrobot84-bintray",
url("http://dl.bintray.com/dancingrobot84/sbt-plugins/"))(Resolver.ivyStylePatterns))
resolvers ++= Seq(
Classpaths.sbtPluginReleases,
Resolver.bintrayIvyRepo("dancingrobot84","sbt-plugins")
)

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-M14")
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.0-M15-1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.15")
addSbtPlugin("com.lihaoyi" % "scalatex-sbt-plugin" % "0.3.5")
Expand Down
60 changes: 21 additions & 39 deletions scalafmtSbt/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
@@ -1,54 +1,36 @@
package org.scalafmt.sbt

import org.scalafmt.Versions
import org.scalafmt.bootstrap.ScalafmtBootstrap
import sbt.Keys._
import sbt._
import sbt.plugins.JvmPlugin

object ScalafmtPlugin extends AutoPlugin {
private def baseCmd = "scalafmt-stub/runMain org.scalafmt.cli.Cli "
private def cmd(args: String*)(s: State): State =
s"$baseCmd ${args.mkString(" ")}" :: s
object autoImport {

lazy val scalafmt: Command =
Command.args("scalafmt", "run the scalafmt command line interface.") {
case (s, args) => s"$baseCmd ${args.mkString(" ")}" :: s
case (s, args) =>
try {
ScalafmtBootstrap.main(args)
} catch {
case e: java.lang.NoSuchMethodError
if e.getMessage.startsWith("coursier") =>
System.err.println(
"Error. Found conflicting version of coursier, sbt-scalafmt requires" +
s" coursier version ${Versions.coursier}.")
}
s
}
// These are not strictly necessary, since they can be run with the
// scalafmt command. However, they're convenient to avoid the need for
// parentheses when running `sbt "scalafmt --test"`.
lazy val scalafmtTest: Command =
Command.command("scalafmtTest")(cmd("--test"))
lazy val scalafmtDiff: Command =
Command.command("scalafmtDiff")(cmd("--diff"))
lazy val scalafmtDiffTest: Command =
Command.command("scalafmtDiffTest")(cmd("--diff", "--test"))
}
import autoImport._
lazy val scalafmtStub: Project = Project(
id = "scalafmt-stub",
base = file("project/scalafmt")
).settings(
scalaVersion := "2.11.8",
libraryDependencies +=
"com.geirsson" %% "scalafmt-cli" % _root_.org.scalafmt.Versions.nightly
)
override def globalSettings: Seq[Def.Setting[_]] = Seq(
commands ++= {
if (sbtVersion.value < "0.13.13") {
// can't do streams.value.log.warn since streams is a task and
// settings like `commands` can't depend on tasks.
val warn = s"[${scala.Console.YELLOW}warn${scala.Console.RESET}]"
System.err.println(
s"$warn sbt-scalafmt requires sbt.version=0.13.13 or higher. " +
s"Please upgrade sbt to expose the `scalafmt` command.")
Nil
} else {
Seq(scalafmt, scalafmtTest, scalafmtDiff, scalafmtDiffTest)
}
}
)
override def extraProjects: Seq[Project] = Seq(scalafmtStub)
override def globalSettings: Seq[Def.Setting[_]] =
Seq(
commands += autoImport.scalafmt
) ++
addCommandAlias("scalafmtTest", "scalafmt --test") ++
addCommandAlias("scalafmtDiffTest", "scalafmt --diff --test") ++
addCommandAlias("scalafmtDiff", "scalafmt --diff")

override def trigger: PluginTrigger = allRequirements
override def requires = JvmPlugin
}
2 changes: 2 additions & 0 deletions scalafmtSbt/src/sbt-test/scalafmt-sbt/helloworld/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import java.io.File

fork in ThisBuild := true

lazy val root = project
.in(file("."))
.aggregate(
Expand Down