Permalink
Browse files

Use new optional task and setting getters.

  • Loading branch information...
1 parent 90d9303 commit 6a469eefd852ab1d67b67d6e26fc5fdd03ea150c @aemoncannon aemoncannon committed Dec 21, 2011
View
2 build.sbt
@@ -4,7 +4,7 @@ name := "ensime-sbt-cmd"
organization := "org.ensime"
-version := "0.0.6-SNAPSHOT"
+version := "0.0.7-SNAPSHOT"
libraryDependencies += "net.liftweb" %% "lift-json" % "2.4-M4"
View
128 src/main/scala/org/ensime/EnsimeCommand.scala
@@ -1,128 +0,0 @@
-package org.ensime
-import util._
-import util.SExp._
-
-object EnsimeCommand {
- import java.io._
- import sbt._
- import Keys._
- import complete.Parser
- import complete.Parser._
- import complete.DefaultParsers.{some,success,token,Space,ID}
- import CommandSupport.logger
- import net.liftweb.json.JsonAST
- import net.liftweb.json.Printer._
- import net.liftweb.json.JsonDSL._
-
- val ensimeCommand = "ensime"
- val ensimeBrief = (ensimeCommand + " dump <project> <outputFile>",
- "Dump project for <project> information to <outputFile>.")
- val ensimeDetailed = ""
-
- // def extracted(implicit state: State): Extracted = Project extract state
- // def structure(implicit state: State): BuildStructure = extracted.structure
- //
- // def setting[A](key: SettingKey[A],
- // errorMessage: => String,
- // reference: ProjectReference,
- // configuration: Configuration = Configurations.Compile)(
- // implicit state: State): List[String] = {
- // key in (reference, configuration) get structure.data match {
- // case Some(a) =>
- // logDebug("Setting for key %s = %s".format(key.key, a))
- // a.success
- // case None => errorMessage.failNel
- // }
- // }
- //
- // logger(state).debug(message)
- //
- def ensime = Command.args(ensimeCommand, ensimeBrief, ensimeDetailed, "huh?"){
- case (s,"generate"::rest) => {
-
- logger(s).info("Gathering project information...")
-
- val initX = Project extract s
-
- val projs = initX.structure.allProjects.map{
- proj =>
-
- implicit val show:Show[ScopedKey[_]] = Project.showContextKey(s)
-
- val x = Extracted(
- initX.structure,
- initX.session,
- ProjectRef(proj.base, proj.id))
-
- def taskFiles(key:TaskKey[Classpath]):List[String] = {
- val (newS,cp) = try{ x.runTask(key, s) }
- catch{ case e =>
- e.printStackTrace
- (s,List())
- }
- cp.map{ a => (a.data).getAbsolutePath()}.toList
- }
-
- def settingFiles(key:SettingKey[Seq[java.io.File]]):List[String] = {
- val r = x.get(key)
- r.map{ f => f.getAbsolutePath()}.toList
- }
-
- val name = x.get(Keys.name)
- val org = x.get(organization)
- val projectVersion = x.get(version)
- val buildScalaVersion = x.get(scalaVersion)
-
- val compileDeps = (
- taskFiles(unmanagedClasspath in Compile) ++
- taskFiles(managedClasspath in Compile) ++
- taskFiles(internalDependencyClasspath in Compile)
- )
- val testDeps = (
- taskFiles(unmanagedClasspath in Test) ++
- taskFiles(managedClasspath in Test) ++
- taskFiles(internalDependencyClasspath in Test) ++
- taskFiles(exportedProducts in Test)
- )
- val runtimeDeps = (
- taskFiles(unmanagedClasspath in Runtime) ++
- taskFiles(managedClasspath in Runtime) ++
- taskFiles(internalDependencyClasspath in Runtime) ++
- taskFiles(exportedProducts in Runtime)
- )
-
- val sourceRoots = (
- settingFiles(sourceDirectories in Compile) ++
- settingFiles(sourceDirectories in Test)
- )
-
- val targetPath = x.get(classDirectory in Compile).toString()
- val target = new java.io.File(targetPath).getCanonicalPath
-
- Map[KeywordAtom,SExp](
- key(":name") -> name,
- key(":package") -> org,
- key(":version") -> projectVersion,
- key(":compile-deps") -> SExp(compileDeps.map(SExp.apply)),
- key(":runtime-deps") -> SExp(runtimeDeps.map(SExp.apply)),
- key(":test-deps") -> SExp(testDeps.map(SExp.apply)),
- key(":source-roots") -> SExp(sourceRoots.map(SExp.apply)),
- key(":target") -> target)
-
- }
-
- val result = SExp(Map(
- key(":projects") -> SExp(projs.map{p => SExp(p)})
- )).toPPReadableString
- val file = rest.headOption.getOrElse(".ensime")
- IO.write(new java.io.File(file), result)
- logger(s).info("Wrote project to " + file)
-
- s
- }
- case (s,args) => {
- logger(s).info(ensimeBrief._1)
- s
- }
- }
-}
View
7 src/main/scala/org/ensime/Plugin.scala
@@ -1,7 +0,0 @@
-package org.ensime
-import sbt._
-import Keys._
-
-object Plugin extends sbt.Plugin {
- override lazy val settings = Seq(commands ++= Seq(EnsimeCommand.ensime))
-}
View
128 src/main/scala/org/ensime/sbt/EnsimeCommand.scala
@@ -0,0 +1,128 @@
+package org.ensime.sbt
+import util._
+import util.SExp._
+
+object EnsimeCommand {
+ import java.io.{File => JavaFile}
+ import sbt._
+ import Keys._
+ import CommandSupport.logger
+ import net.liftweb.json.JsonAST
+ import net.liftweb.json.Printer._
+ import net.liftweb.json.JsonDSL._
+
+ val ensimeConfig = SettingKey[Map[KeywordAtom,SExp]]("ensime-config")
+
+ val ensimeCommand = "ensime"
+ val ensimeBrief = (ensimeCommand + " dump <project> <outputFile>",
+ "Dump project for <project> information to <outputFile>.")
+ val ensimeDetailed = ""
+
+ def ensime = Command.args(ensimeCommand, ensimeBrief, ensimeDetailed, "huh?"){
+ case (s,"generate"::rest) => {
+
+ def logInfo(message: String) {
+ logger(s).info(message);
+ }
+
+ def logErrorAndFail(errorMessage: String): Nothing = {
+ logger(s).error(errorMessage);
+ throw new IllegalArgumentException()
+ }
+
+ logInfo("Gathering project information...")
+
+ val initX = Project extract s
+ val buildStruct = initX.structure
+ val session = initX.session
+
+ val projs = initX.structure.allProjects.map{
+ proj =>
+
+ implicit val show:Show[ScopedKey[_]] = Project.showContextKey(s)
+
+ val projRef = ProjectRef(proj.base, proj.id)
+ val x = Extracted(buildStruct,session,projRef)
+
+ logInfo(proj.id + "...")
+
+ def evaluateTask[T](taskKey: sbt.Project.ScopedKey[sbt.Task[T]]):Option[(State, Result[T])] =
+ EvaluateTask(buildStruct, taskKey, s, projRef)
+
+ def optSetting[A](key: SettingKey[A]) = key in projRef get buildStruct.data
+
+ def reqSetting[A](key: SettingKey[A], errorMessage: => String) = {
+ optSetting(key) getOrElse {
+ logErrorAndFail(errorMessage)
+ }
+ }
+
+ def taskFiles(key:TaskKey[Classpath]):List[String] = {
+ val cp = evaluateTask(key) match {
+ case Some((s,Value(deps))) => deps
+ case _ => logErrorAndFail("Failed to obtain classpath for: " + key)
+ }
+ cp.map{ att => (att.data).getAbsolutePath }.toList
+ }
+
+ def settingFiles[A](key: SettingKey[Seq[File]]):List[String] = {
+ optSetting(key).map(fs => fs.map(_.getAbsolutePath).toList).getOrElse(List[String]())
+ }
+
+ val name = optSetting(Keys.name)
+ val org = optSetting(organization)
+ val projectVersion = optSetting(version)
+ val buildScalaVersion = optSetting(scalaVersion)
+
+ val compileDeps = (
+ taskFiles(unmanagedClasspath in Compile) ++
+ taskFiles(managedClasspath in Compile) ++
+ taskFiles(internalDependencyClasspath in Compile)
+ )
+ val testDeps = (
+ taskFiles(unmanagedClasspath in Test) ++
+ taskFiles(managedClasspath in Test) ++
+ taskFiles(internalDependencyClasspath in Test) ++
+ taskFiles(exportedProducts in Test)
+ )
+ val runtimeDeps = (
+ taskFiles(unmanagedClasspath in Runtime) ++
+ taskFiles(managedClasspath in Runtime) ++
+ taskFiles(internalDependencyClasspath in Runtime) ++
+ taskFiles(exportedProducts in Runtime)
+ )
+
+ val sourceRoots = (
+ settingFiles(sourceDirectories in Compile) ++
+ settingFiles(sourceDirectories in Test)
+ )
+
+ val target = optSetting(classDirectory in Compile).map(_.getCanonicalPath)
+
+ reqSetting(ensimeConfig,"Setting ensime-config not defined!") ++ Map[KeywordAtom,SExp](
+ key(":name") -> name.map(SExp.apply).getOrElse(NilAtom()),
+ key(":package") -> org.map(SExp.apply).getOrElse(NilAtom()),
+ key(":version") -> projectVersion.map(SExp.apply).getOrElse(NilAtom()),
+ key(":compile-deps") -> SExp(compileDeps.map(SExp.apply)),
+ key(":runtime-deps") -> SExp(runtimeDeps.map(SExp.apply)),
+ key(":test-deps") -> SExp(testDeps.map(SExp.apply)),
+ key(":source-roots") -> SExp(sourceRoots.map(SExp.apply)),
+ key(":target") -> target.map(SExp.apply).getOrElse(NilAtom()))
+ }
+
+ val result = SExp(Map(
+ key(":use-sbt") -> SExp(true),
+ key(":sbt-subprojects") -> SExp(projs.map{p => SExp(p)})
+ )).toPPReadableString
+
+ val file = rest.headOption.getOrElse(".ensime")
+ IO.write(new JavaFile(file), result)
+ logger(s).info("Wrote project to " + file)
+ s
+ }
+ case (s,args) => {
+ logger(s).info(ensimeBrief._1)
+ s
+ }
+ }
+}
View
13 src/main/scala/org/ensime/sbt/Plugin.scala
@@ -0,0 +1,13 @@
+package org.ensime.sbt
+import sbt._
+import Keys._
+import util.{KeywordAtom, SExp}
+
+object Plugin extends sbt.Plugin {
+
+ override lazy val settings = Seq(
+ Keys.commands += EnsimeCommand.ensime,
+ EnsimeCommand.ensimeConfig := Map[KeywordAtom,SExp]()
+ )
+
+}
View
16 src/main/scala/org/ensime/SExp.scala → src/main/scala/org/ensime/sbt/SExp.scala
@@ -25,7 +25,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-package org.ensime.util
+package org.ensime.sbt.util
import scala.collection.immutable.Map
import scala.util.parsing.combinator._
import scala.util.parsing.input
@@ -165,9 +165,11 @@ object SExp extends RegexParsers {
}
}
- def apply(s: String): StringAtom = {
- StringAtom(s)
- }
+ def apply(s: String): StringAtom = strToSExp(s)
+
+ def apply(i: Int): IntAtom = intToSExp(i)
+
+ def apply(b: Boolean): BooleanAtom = boolToSExp(b)
def apply(items: SExp*): SExpList = {
SExpList(items)
@@ -200,19 +202,19 @@ object SExp extends RegexParsers {
SExpList(nonNil.flatMap(ea => List(key(ea._1), ea._2)))
}
- implicit def strToSExp(str: String): SExp = {
+ implicit def strToSExp(str: String): StringAtom = {
StringAtom(str)
}
def key(str: String): KeywordAtom = {
KeywordAtom(str)
}
- implicit def intToSExp(value: Int): SExp = {
+ implicit def intToSExp(value: Int): IntAtom = {
IntAtom(value)
}
- implicit def boolToSExp(value: Boolean): SExp = {
+ implicit def boolToSExp(value: Boolean): BooleanAtom = {
if (value) {
TruthAtom()
} else {

0 comments on commit 6a469ee

Please sign in to comment.