diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-03-27 23:25:43 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:20 +0100 |
commit | dcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a (patch) | |
tree | f5ab0b7e1ab3e2186dd864230419c5dc20404c1f /Scala/example/project/StyleChecker.scala | |
parent | ab61b59311fc1428a8edbe99b103ab77627c7dc8 (diff) | |
download | coursera-dcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a.zip coursera-dcdc4d3e6ed08f37f7acd75c79ddea607d2e8e2a.tar.gz |
Scala : add scala/example
Diffstat (limited to 'Scala/example/project/StyleChecker.scala')
-rw-r--r-- | Scala/example/project/StyleChecker.scala | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/Scala/example/project/StyleChecker.scala b/Scala/example/project/StyleChecker.scala new file mode 100644 index 0000000..c7b57a2 --- /dev/null +++ b/Scala/example/project/StyleChecker.scala @@ -0,0 +1,77 @@ +import sbt.File +import java.io.ByteArrayOutputStream +import java.io.PrintStream +import org.scalastyle._ + +object StyleChecker { + val maxResult = 100 + + class CustomTextOutput[T <: FileSpec]() extends Output[T] { + private val messageHelper = new MessageHelper(this.getClass().getClassLoader()) + + var fileCount: Int = _ + override def message(m: Message[T]): Unit = m match { + case StartWork() => + case EndWork() => + case StartFile(file) => + print("Checking file " + file + "...") + fileCount = 0 + case EndFile(file) => + if (fileCount == 0) println(" OK!") + case StyleError(file, clazz, key, level, args, line, column, customMessage) => + report(line, column, messageHelper.text(level.name), + findMessage(messageHelper, clazz, key, args, customMessage)) + case StyleException(file, clazz, message, stacktrace, line, column) => + report(line, column, "error", message) + } + + private def report(line: Option[Int], column: Option[Int], level: String, message: String) { + if (fileCount == 0) println("") + fileCount += 1 + println(" " + fileCount + ". " + level + pos(line, column) + ":") + println(" " + message) + } + + private def pos(line: Option[Int], column: Option[Int]): String = line match { + case Some(line) => " at line " + line + (column match { + case Some(column) => " character " + column + case None => "" + }) + case None => "" + } + } + + def score(outputResult: OutputResult) = { + val penalties = outputResult.errors + outputResult.warnings + scala.math.max(maxResult - penalties, 0) + } + + def assess(allSources: Seq[File]): (String, Int) = { + val configFile = new File("project/scalastyle_config.xml").getAbsolutePath + + val sources = allSources.filterNot{ f => + val path = f.getAbsolutePath + path.contains("interpreter") || + path.contains("simulations") || + path.contains("fetchtweets") + } + + val messages = new ScalastyleChecker().checkFiles( + ScalastyleConfiguration.readFromXml(configFile), + Directory.getFiles(sources : _*)) + + val output = new ByteArrayOutputStream() + val outputResult = Console.withOut(new PrintStream(output)) { + new CustomTextOutput().output(messages) + } + + val msg = + output.toString + + "Processed " + outputResult.files + " file(s)\n" + + "Found " + outputResult.errors + " errors\n" + + "Found " + outputResult.warnings + " warnings\n" + + (if (outputResult.errors+outputResult.warnings > 0) "Consult the style guide at http://goo.gl/5CIum" else "") + + (msg, score(outputResult)) + } +} |