Skip to content

Commit

Permalink
Add config-str flag, fixes #617 (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg authored Dec 19, 2016
1 parent b5f633a commit 4de2681
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
45 changes: 21 additions & 24 deletions cli/src/main/scala/org/scalafmt/cli/CliArgParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,20 @@ object CliArgParser {
@BuildTime val buildTimeMs: Long = ???

val usageExamples: String =
"""|scalafmt # Format all files in the current project, with config determined:
| # 1. .scalafmt.conf inside root directory of current git repo
| # 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 # Format all files in the current project, configuration is determined in this order:
| # 1. .scalafmt.conf file in current directory
| # 2. .scalafmt.conf inside root directory of current git repo
| # 3. no configuration, default style
|scalafmt --test # throw exception on mis-formatted files, won't write to files.
|scalafmt --diff # Format all files that were edited in git diff against master branch.
|scalafmt --diff-branch 2.x # same as --diff, except against branch 2.x
|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
|scalafmt --config .scalafmt.conf # read custom style from file
|scalafmt --config "style=IntelliJ" # define custom style as a flag, must be quoted.""".stripMargin
|scalafmt --config-str "style=IntelliJ" # define custom style as a flag, must be quoted.""".stripMargin

val scoptParser: OptionParser[CliOptions] =
new scopt.OptionParser[CliOptions]("scalafmt") {
Expand All @@ -54,13 +47,13 @@ object CliArgParser {
}

def readConfigFromFile(file: String, c: CliOptions): CliOptions = {
val contents =
if (file.startsWith("\""))
file.stripPrefix("\"").stripSuffix("\"")
else
FileOps.readFile(
AbsoluteFile.fromFile(new File(file), c.common.workingDirectory))

readConfig(
FileOps.readFile(
AbsoluteFile.fromFile(new File(file), c.common.workingDirectory)),
c
)
}
def readConfig(contents: String, c: CliOptions): CliOptions = {
Config.fromHocon(contents) match {
case Right(style) => c.copy(config = style)
case Left(e) => throw e
Expand All @@ -87,7 +80,11 @@ object CliArgParser {
opt[String]('c', "config")
.action(readConfigFromFile)
.text(
"either a file or configuration wrapped in quotes \" with no spaces.")
"a file path to .scalafmt.conf.")
opt[String]("config-str")
.action(readConfig)
.text(
"configuration defined as a string")
opt[Unit]("stdin")
.action((_, c) => c.copy(stdIn = true))
.text("read from stdin and print to stdout")
Expand Down
12 changes: 4 additions & 8 deletions cli/src/test/scala/org/scalafmt/cli/CliTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class CliTest extends FunSuite with DiffAssertions {
|}""".stripMargin
val customConfig =
"""
|project.git = true
|maxColumn = 2
""".stripMargin
val sbtOriginal =
Expand All @@ -84,8 +83,8 @@ class CliTest extends FunSuite with DiffAssertions {
val originalTmpFile = Files.createTempFile("prefix", ".scala")
Files.write(originalTmpFile, unformatted.getBytes)
val args = Array(
"--config",
"\"{maxColumn=7,style=IntelliJ}\"",
"--config-str",
"{maxColumn=7,style=IntelliJ}",
"--in-place",
"--files",
originalTmpFile.toFile.getPath
Expand Down Expand Up @@ -119,8 +118,8 @@ class CliTest extends FunSuite with DiffAssertions {
"--stdin",
"--assume-filename",
"build.sbt",
"--config",
"\"{maxColumn=7,style=IntelliJ}\""
"--config-str",
"{maxColumn=7,style=IntelliJ}"
)
val printToStdout = getConfig(args)
val bais = new ByteArrayInputStream(sbtOriginal.getBytes)
Expand Down Expand Up @@ -240,15 +239,13 @@ class CliTest extends FunSuite with DiffAssertions {
|object A { }
|/.scalafmt.conf
|maxColumn = 2
|project.git = true
|project.excludeFilters = [target]
|""".stripMargin
)

val expected =
"""|/.scalafmt.conf
|maxColumn = 2
|project.git = true
|project.excludeFilters = [target]
|
|/foo.scala
Expand Down Expand Up @@ -284,7 +281,6 @@ class CliTest extends FunSuite with DiffAssertions {
|$original
|/.scalafmt.conf
|maxColumn = 2
|project.git = true
|""".stripMargin
)
val workingDir = input / "nested"
Expand Down
5 changes: 2 additions & 3 deletions readme/Configuration.scalatex
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,9 @@

@sect{project}
@p
Controls the behavior of the CLI when run with no arguments, that is just "@code{scalafmt}".

Configure which source files should be formatted in this project.
@cliFlags
# reformat all .scala and .sbt files in current git repo.
# Only format files tracked by git.
project.git = true
# manually exclude files to format.
project.excludeFilters = [
Expand Down

0 comments on commit 4de2681

Please sign in to comment.