Skip to content

Commit

Permalink
Minor refactorings to improve readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
olim7t committed Sep 14, 2010
1 parent 7550c29 commit 76cb3a0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
15 changes: 15 additions & 0 deletions src/main/scala/ChainedAction.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* sbt Scalariform plugin
* Copyright 2010 Olivier Michallat
*/
package com.github.olim7t.sbtscalariform

/** The sole purpose of this file is to rename Option's "orElse" method to "andThen".
* This is so much clearer when chaining functions that return Some[String] to indicate an error.
*/
object ChainedAction {
implicit def optionToChainedAction(o: Option[String]) = new ChainedAction(o)
}

class ChainedAction(lastResult: Option[String]) {
def andThen(nextAction: => Option[String]) = lastResult orElse nextAction
}
75 changes: 39 additions & 36 deletions src/main/scala/ScalariformPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,20 @@ import scala.io.Source
import java.io.File
import FileUtilities.{ Newline, write }
import Actions._
import ChainedAction._

trait ScalariformPlugin extends BasicScalaProject with SourceTasks {
import ScalariformPlugin._

// Find the Scalariform jar, which has been downloaded as a dependency of the plugin
private def sfClasspath: Option[String] = {
def pluginDeps(p: Project) = p.info.pluginsManagedDependencyPath
val projectSearchPath = pluginDeps(this)
// The plugin might be declared on the parent project
val parentSearchPath = info.parent.map(pluginDeps(_)) getOrElse Path.emptyPathFinder

val jarFinder = descendents(projectSearchPath +++ parentSearchPath, "scalariform*.jar")
val jars = jarFinder.get
if (jars.size > 1) log.warn("Multiple scalariform jars found: " + jars)
if (jars.size == 0)
None
else
Some(Path.makeString(jars))
}

private def sfScalaJars = {
val si = getScalaInstance(ScalaVersion)
si.libraryJar :: si.compilerJar :: Nil
}

def scalariformOptions = Seq[ScalariformOption]()
def scalariformTestOptions = scalariformOptions

def scalaSourcesEncoding = "UTF-8"
def sourcesTimestamp = "sources.lastFormatted"
def testSourcesTimestamp = "testSources.lastFormatted"

lazy val formatSources = formatSourcesAction
lazy val testFormatSources = testFormatSourcesAction

def sourcesTimestamp = "sources.lastFormatted"
def testSourcesTimestamp = "testSources.lastFormatted"

def formatSourcesAction = forAllSourcesTask(sourcesTimestamp from mainSources) { sources =>
format(sources, scalariformOptions)
} describedAs ("Format main Scala sources")
Expand All @@ -56,27 +34,52 @@ trait ScalariformPlugin extends BasicScalaProject with SourceTasks {
override def compileAction = super.compileAction dependsOn (formatSources)
override def testCompileAction = super.testCompileAction dependsOn (testFormatSources)

private val Scalariform = new ForkScala(ScalariformMainClass)
private val NoJavaHome = None

private def format(sources: Iterable[Path], options: Seq[ScalariformOption]): Option[String] = sfClasspath match {
case None => Some("Scalariform jar not found. Try running `;clean-plugins;reload`.")
case Some(cp) =>
def run(fileList: File): Option[String] = {
val fork = new ForkScala(ScalariformMainClass)
// Assume InPlace if neither InPlace nor Test are provided
val finalOpts = if ((options contains InPlace) || (options contains Test)) options else options ++ Seq(InPlace)
val args = finalOpts.map(_.asArgument)
val jvmOptions = Seq("-cp", cp, "-Dfile.encoding=" + scalaSourcesEncoding)
val finalOpts = completeWithDefaults(options)
val arguments = finalOpts.map(_.asArgument) ++ Seq("-l=" + fileList.getAbsolutePath)

withSuccessCode(0, "Scalariform invocation failed") {
fork(None,
Seq("-cp", cp, "-Dfile.encoding=" + scalaSourcesEncoding),
sfScalaJars,
args ++ Seq("-l=" + fileList.getAbsolutePath),
log)
Scalariform(NoJavaHome, jvmOptions, sfScalaJars, arguments, log)
}
}
withTemporaryFile(log, "sbt-scalariform", ".lst") { file =>
write(file, sources.map(_.absolutePath).mkString(Newline), log) orElse
run(file)
write(file, sources.map(_.absolutePath).mkString(Newline), log) andThen run(file)
}
}

// Find the Scalariform jar, which has been downloaded as a dependency of the plugin
private def sfClasspath: Option[String] = {
def pluginDeps(p: Project) = p.info.pluginsManagedDependencyPath
val projectSearchPath = pluginDeps(this)
// The plugin might be declared on the parent project
val parentSearchPath = info.parent.map(pluginDeps(_)) getOrElse Path.emptyPathFinder

val jarFinder = descendents(projectSearchPath +++ parentSearchPath, "scalariform*.jar")
val jars = jarFinder.get
if (jars.size > 1) log.warn("Multiple scalariform jars found: " + jars)
if (jars.size == 0)
None
else
Some(Path.makeString(jars))
}

private def sfScalaJars = {
val si = getScalaInstance(ScalaVersion)
si.libraryJar :: si.compilerJar :: Nil
}

private def completeWithDefaults(options: Seq[ScalariformOption]) =
if ((options contains InPlace) || (options contains Test))
options
else
options ++ Seq(InPlace)
}
object ScalariformPlugin {
/** The version of Scala used to run Scalariform.*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/SourceTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ object SourceTasks {
}
}
def processAll(label: String, files: TimestampSources, log: Logger)(globalAction: Iterable[Path] => Option[String]): Option[String] = {
import ChainedAction._
import files._
val modified = sources.filter(_.lastModified > timestamp.lastModified)
if (modified isEmpty)
None
else
globalAction(modified) orElse
FileUtilities.touch(timestamp, log)
globalAction(modified) andThen FileUtilities.touch(timestamp, log)
}
}

0 comments on commit 76cb3a0

Please sign in to comment.