Permalink
Browse files

Use booleans rather than 0/1s in command-line tool

  • Loading branch information...
1 parent e051005 commit 96ca89985d8f5be9432f39b1f00bd63a6aa235d8 @mdr mdr committed May 5, 2012
View
@@ -1,6 +1,7 @@
0.1.2 (..)
-* Add --recurse, --stdin, --stdout options to command-line tool
+* Revamp command-line tool with more intuitive behaviour
+* Add --quiet, --recurse, --stdin, --stdout options to command-line tool
* FIX: Scaladoc comment formatting could break nested comments (issue #36)
* Tidy up, optimise lexer code
* FIX: Parse 5.f, 5.d as floating points, unless in Scala 2.11+ mode
@@ -3,9 +3,7 @@ package scalariform.commandline
import java.io.File
import java.io.IOException
import java.nio.charset._
-
import scala.io.Source
-
import scalariform.formatter.preferences._
import scalariform.formatter.ScalaFormatter
import scalariform.parser.ScalaParserException
@@ -15,22 +13,22 @@ import scalariform.ScalaVersions
object Main {
def main(args: Array[String]) {
- exit(process(args))
+ exit(if (process(args)) 1 else 0)
}
- def process(args: Array[String]): Int = {
+ def process(args: Array[String]): Boolean = {
val parser = new CommandLineOptionParser
val arguments = args.toList map parser.getArgument
if (arguments contains Help) {
printUsage()
- return 0
+ return false
}
if (arguments contains Version) {
println("Scalariform " + scalariform.VERSION + " (runtime Scala " + ScalaVersions.DEFAULT_VERSION + ")")
- return 0
+ return false
}
var errors: List[String] = Nil
@@ -144,7 +142,7 @@ object Main {
System.err.println("Error: " + error)
if (showUsage)
printUsage()
- return 1
+ return true
}
def log(s: String) = if (!quiet && !stdout && !stdin) println(s)
@@ -170,9 +168,9 @@ object Main {
if (test)
if (stdin)
- checkSysIn(encoding, doFormat)
+ !checkSysIn(encoding, doFormat)
else
- checkFiles(files, encoding, doFormat, log)
+ !checkFiles(files, encoding, doFormat, log)
else {
if (stdin)
transformSysInToSysOut(encoding, forceOutput, doFormat)
@@ -195,81 +193,111 @@ object Main {
}
}
- private def checkSysIn(encoding: String, doFormat: String Option[String]): Int = {
+ /**
+ * @return true iff the source from stdin is formatted correctly
+ */
+ private def checkSysIn(encoding: String, doFormat: String Option[String]): Boolean = {
val source = Source.fromInputStream(System.in, encoding)
- if (checkSource(source, doFormat) == FormattedCorrectly) 0 else 1
+ checkSource(source, doFormat) == FormattedCorrectly
}
- private def transformSysInToSysOut(encoding: String, forceOutput: Boolean, doFormat: String Option[String]): Int = {
+ private def transformSysInToSysOut(encoding: String, forceOutput: Boolean, doFormat: String Option[String]): Boolean = {
val original = Source.fromInputStream(System.in, encoding).mkString
doFormat(original) match {
case Some(formatted)
print(formatted)
- 0
+ false
case None
if (forceOutput) {
print(original)
- 0
+ false
} else {
System.err.println("Error: Could not parse text as Scala.")
- 1
+ true
}
}
}
- private def transformFileToSysOut(file: File, encoding: String, forceOutput: Boolean, doFormat: String Option[String]): Int = {
+ private def transformFileToSysOut(file: File, encoding: String, forceOutput: Boolean, doFormat: String Option[String]): Boolean = {
val original = Source.fromFile(file, encoding).mkString
doFormat(original) match {
case Some(formatted)
print(formatted)
- 0
+ false
case None
if (forceOutput) {
print(original)
- 0
+ false
} else {
System.err.println("Error: Could not parse " + file.getPath + " as Scala")
- 1
+ true
}
+ }
+ }
+ private def transformFileInPlace(file: File, encoding: String, doFormat: String Option[String], log: String Unit): Boolean = {
+ val original = Source.fromFile(file, encoding).mkString
+ doFormat(original) match {
+ case Some(formatted)
+ if (formatted == original)
+ log(". " + file)
+ else {
+ log("[Reformatted] " + file)
+ writeText(file, formatted, Some(encoding))
+ }
+ false
+ case None
+ log("[Parse error] " + file.getPath)
+ true
}
}
- private def transformFilesInPlace(files: Seq[File], encoding: String, doFormat: String Option[String], log: String Unit): Int = {
+ private def transformFilesInPlace(files: Seq[File], encoding: String, doFormat: String Option[String], log: String Unit): Boolean = {
var problems = false
- for (file files) {
- val original = Source.fromFile(file, encoding).mkString
- doFormat(original) match {
- case Some(formatted)
- if (formatted == original)
- log(". " + file)
- else {
- log("[Reformatted] " + file)
- writeText(file, formatted, Some(encoding))
- }
- case None
- log("[Parse error] " + file.getPath)
- problems = true
- }
+ // val futures =
+ // for (file ← files) yield asyncExec {
+ // problems &= transformFileInPlace(file, encoding, doFormat, log)
+ // }
+ // futures.map(_.get())
+ for (file files)
+ problems &= transformFileInPlace(file, encoding, doFormat, log)
+ problems
+ }
+
+ // private val lock = new ReentrantLock
+ // private lazy val executorService = Executors.newCachedThreadPool()
+ // private def asyncExec(x: ⇒ Unit): Future[_] = executorService.submit(new Runnable { def run() = x })
+ // private def withLock[T](x: ⇒ T): T = {
+ // lock.lock()
+ // try
+ // x
+ // finally
+ // lock.unlock()
+ // }
+ /**
+ * @return true iff file is already formatted correctly
+ */
+ private def checkFile(file: File, encoding: String, doFormat: String Option[String], log: String Unit): Boolean = {
+ val source = Source.fromFile(file, encoding)
+ val formatResult = checkSource(source, doFormat)
+ val resultString = formatResult match {
+ case FormattedCorrectly "OK"
+ case NotFormattedCorrectly "FAILED"
+ case DidNotParse "ERROR"
}
- if (problems) 1 else 0
+ val padding = " " * (6 - resultString.length)
+ log("[" + resultString + "]" + padding + " " + file)
+ formatResult != FormattedCorrectly
}
- private def checkFiles(files: Seq[File], encoding: String, doFormat: String Option[String], log: String Unit): Int = {
- val passesFails: Seq[Boolean] =
- for (file files) yield {
- val source = Source.fromFile(file, encoding)
- val formatResult = checkSource(source, doFormat)
- val resultString = formatResult match {
- case FormattedCorrectly "OK"
- case NotFormattedCorrectly "FAILED"
- case DidNotParse "ERROR"
- }
- val padding = " " * (6 - resultString.length)
- log("[" + resultString + "]" + padding + " " + file)
- formatResult == FormattedCorrectly
- }
- if (passesFails.forall(identity)) 0 else 1
+ /**
+ * @return true iff all files are already formatted correctly
+ */
+ private def checkFiles(files: Seq[File], encoding: String, doFormat: String Option[String], log: String Unit): Boolean = {
+ var allPassed = true
+ for (file files)
+ allPassed &= checkFile(file, encoding, doFormat, log)
+ allPassed
}
private sealed trait FormatResult
@@ -86,11 +86,10 @@ object Utils {
import java.io.{ OutputStreamWriter, FileOutputStream }
val encoding = encodingOpt getOrElse (System getProperty "file.encoding")
val writer = new OutputStreamWriter(new FileOutputStream(file), encoding)
- try {
+ try
writer.write(text)
- } finally {
+ finally
writer.close()
- }
}
@throws(classOf[IOException])

0 comments on commit 96ca899

Please sign in to comment.