Skip to content
Browse files

Using scalaz for validation now.

  • Loading branch information...
1 parent 4aa29e6 commit d11b1cfb590feebf57651a371bfb4c5402f40032 Heiko Seeberger committed Mar 24, 2011
Showing with 88 additions and 63 deletions.
  1. +4 −0 build.sbt
  2. +84 −63 src/main/scala/SbtEclipsePlugin.scala
View
4 build.sbt
@@ -5,4 +5,8 @@ name := "sbteclipse"
version := "0.1-SNAPSHOT"
+resolvers += ScalaToolsSnapshots
+
+libraryDependencies += "org.scalaz" %% "scalaz-core" % "6.0-SNAPSHOT"
+
sbtPlugin := true
View
147 src/main/scala/SbtEclipsePlugin.scala
@@ -16,92 +16,113 @@
package com.weiglewilczek.sbteclipse
import java.io.File
-import sbt._
+import sbt.{ Command, Configuration, Configurations, IO, Keys, Plugin, Project, SettingKey, State }
import sbt.CommandSupport.logger
import scala.xml.{ NodeSeq, XML }
+import scalaz.{ Failure, NonEmptyList, Success, Validation }
+import scalaz.Scalaz._
object SbtEclipsePlugin extends Plugin {
override lazy val settings =
Seq(Keys.commands += eclipseCommand)
private lazy val eclipseCommand = {
- import Configurations._
- import Keys._
Command.command("eclipse") { state =>
- def setting[A](key: SettingKey[A], configuration: Configuration = Configurations.Compile) = {
+
+ def setting[A](
+ key: SettingKey[A],
+ errorMessage: => String,
+ configuration: Configuration = Configurations.Compile): Validation[NonEmptyList[String], A] = {
val extracted = Project.extract(state)
- key in (extracted.currentRef, configuration) get extracted.structure.data
+ key in (extracted.currentRef, configuration) get extracted.structure.data match {
+ case Some(a) => a.success
+ case None => errorMessage.failNel
+ }
}
- logger(state).debug("Trying to create an Eclipse project for you ...")
-
- if (!(setting(scalaVersion) getOrElse "UNKNOWN" startsWith "2.9")) {
- logger(state).error("Invalid Scala version! Fixed to 2.9 because of the Eclipse plugin.")
- state
- } else {
- XML.save(".project", projectXml(setting(name) getOrElse "default"), "UTF-8", true)
+ def saveEclipseFiles(
+ scalaVersion: String,
+ name: String,
+ sourceDirectories: Seq[File],
+ resourceDirectories: Seq[File],
+ testSourceDirectories: Seq[File],
+ testResourceDirectories: Seq[File],
+ baseDirectory: File) {
+ XML.save(".project", projectXml(name))
XML.save(
".classpath",
classpathXml(
- setting(sourceDirectories, Compile) getOrElse Nil,
- setting(resourceDirectories, Compile) getOrElse Nil,
- setting(sourceDirectories, Test) getOrElse Nil,
- setting(resourceDirectories, Test) getOrElse Nil,
- setting(baseDirectory) getOrElse new File("."), // TODO Remove hack!
- state),
+ sourceDirectories,
+ resourceDirectories,
+ testSourceDirectories,
+ testResourceDirectories,
+ baseDirectory),
"UTF-8",
true)
-
- logger(state).info("Successfully created an Eclipse project for you. Have fun!")
- state
}
- }
- }
- private def projectXml(name: String) =
- <projectDescription>
- <name>{ name }</name>
- <buildSpec>
- <buildCommand>
- <name>org.scala-ide.sdt.core.scalabuilder</name>
- </buildCommand>
- </buildSpec>
- <natures>
- <nature>org.scala-ide.sdt.core.scalanature</nature>
- <nature>org.eclipse.jdt.core.javanature</nature>
- </natures>
- </projectDescription>
+ def projectXml(name: String) =
+ <projectDescription>
+ <name>{ name }</name>
+ <buildSpec>
+ <buildCommand>
+ <name>org.scala-ide.sdt.core.scalabuilder</name>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.scala-ide.sdt.core.scalanature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+ </projectDescription>
- private def classpathXml(
- compileSourceDirectories: Seq[File],
- compileResourceDirectories: Seq[File],
- testSourceDirectories: Seq[File],
- testResourceDirectories: Seq[File],
- baseDirectory: File,
- state: State) = {
+ def classpathXml(
+ compileSourceDirectories: Seq[File],
+ compileResourceDirectories: Seq[File],
+ testSourceDirectories: Seq[File],
+ testResourceDirectories: Seq[File],
+ baseDirectory: File) = {
- def srcEntries(directories: Seq[File]): NodeSeq =
- directories flatMap { directory =>
- if (directory.exists) {
- logger(state).debug("""Creating src entry for directory "%s".""".format(directory))
- val relative = IO.relativize(baseDirectory, directory).get // TODO Better handling!
- <classpathentry kind="src" path={ relative.toString } output="target/classes"/>
- } else {
- logger(state).debug("""Skipping src entry for non-existent directory "%s".""".format(directory))
- NodeSeq.Empty
- }
+ def srcEntries(directories: Seq[File]): NodeSeq =
+ directories flatMap { directory =>
+ if (directory.exists) {
+ logger(state).debug("""Creating src entry for directory "%s".""".format(directory))
+ val relative = IO.relativize(baseDirectory, directory).get // TODO Better handling!
+ <classpathentry kind="src" path={ relative.toString } output="target/classes"/>
+ } else {
+ logger(state).debug("""Skipping src entry for non-existent directory "%s".""".format(directory))
+ NodeSeq.Empty
+ }
+ }
+
+ <classpath>{
+ srcEntries(compileSourceDirectories) ++
+ srcEntries(compileResourceDirectories) ++
+ srcEntries(testSourceDirectories) ++
+ srcEntries(testResourceDirectories) ++
+ <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="output" path="target/classes"/>
+ }</classpath>
}
- <classpath>{
- srcEntries(compileSourceDirectories) ++
- srcEntries(compileResourceDirectories) ++
- srcEntries(testSourceDirectories) ++
- srcEntries(testResourceDirectories) ++
- <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
- <classpathentry kind="output" path="target/classes"/>
- }</classpath>
- }
+ logger(state).debug("Trying to create an Eclipse project for you ...")
+ (setting(Keys.scalaVersion, "Only for Scala 2.9!") |@|
+ setting(Keys.name, "Missing name!") |@|
+ setting(Keys.sourceDirectories, "Missing source directories!") |@|
+ setting(Keys.resourceDirectories, "Missing resource directories!") |@|
+ setting(Keys.sourceDirectories, "Missing test source directories!", Configurations.Test) |@|
+ setting(Keys.resourceDirectories, "Missing test resource directories!", Configurations.Test) |@|
+ setting(Keys.baseDirectory, "Missing base directory!")) {
+ saveEclipseFiles
+ } match {
+ case Success(_) =>
+ logger(state).info("Successfully created an Eclipse project for you. Have fun!")
+ case Failure(errors) =>
+ logger(state).error(errors.list mkString ", ")
+ }
+ state
+ }
+ }
}

0 comments on commit d11b1cf

Please sign in to comment.
Something went wrong with that request. Please try again.