Skip to content

Commit

Permalink
Refactored special treatment for scala library and scala compiler int…
Browse files Browse the repository at this point in the history
…o classpathEntryCollector setting.
  • Loading branch information
Heiko Seeberger committed Dec 16, 2011
1 parent ddcc989 commit 80cc2db
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 52 deletions.
81 changes: 31 additions & 50 deletions src/main/scala/com/typesafe/sbteclipse/Eclipse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package com.typesafe.sbteclipse

import EclipsePlugin.EclipseExecutionEnvironment
import EclipsePlugin.{ ClasspathEntry, EclipseExecutionEnvironment }
import java.io.FileWriter
import java.util.Properties
import sbt.{ Command, Configurations, File, IO, Keys, Project, ProjectRef, ResolvedProject, State, richFile }
Expand All @@ -37,9 +37,10 @@ private object Eclipse {
executionEnvironment: Option[EclipseExecutionEnvironment.Value],
skipParents: Boolean,
/*target: String,*/
withSource: Boolean): Command =
withSource: Boolean,
classpathEntryCollector: PartialFunction[ClasspathEntry, ClasspathEntry]): Command =
Command(commandName)(_ => parser)((state, args) =>
action(executionEnvironment, skipParents, /*target,*/ withSource, args.toMap)(state)
action(executionEnvironment, skipParents, /*target,*/ withSource, classpathEntryCollector, args.toMap)(state)
)

def parser = {
Expand All @@ -52,6 +53,7 @@ private object Eclipse {
skipParents: Boolean,
/*target: String,*/
withSource: Boolean,
classpathEntryCollector: PartialFunction[ClasspathEntry, ClasspathEntry],
args: Map[String, Boolean])(
implicit state: State) = {

Expand All @@ -61,10 +63,14 @@ private object Eclipse {
val sp = args get SkipParents getOrElse skipParents
val ws = args get WithSource getOrElse withSource

contentsForAllProjects(sp /*, target*/ ).fold(onFailure, onSuccess)
contentsForAllProjects(sp, /*target,*/ classpathEntryCollector).fold(onFailure, onSuccess)
}

def contentsForAllProjects(skipParents: Boolean /*, target: String*/ )(implicit state: State) = {
def contentsForAllProjects(
skipParents: Boolean,
/*target: String,*/
classpathEntryCollector: PartialFunction[ClasspathEntry, ClasspathEntry])(
implicit state: State) = {
val contents = for {
ref <- structure.allProjectRefs
project <- Project.getProject(ref, structure) if project.aggregate.isEmpty || !skipParents
Expand All @@ -75,7 +81,7 @@ private object Eclipse {
testSrcDirectories(ref) |@|
scalacOptions(ref) |@|
externalDependencies(ref) |@|
projectDependencies(ref, project))(content( /*target*/ ))
projectDependencies(ref, project))(content( /*target,*/ classpathEntryCollector))
}
contents.sequence[ValidationNELS, Content]
}
Expand All @@ -95,20 +101,23 @@ private object Eclipse {
state
}

def content( /*target: String*/ )(
name: String,
baseDirectory: File,
compileSrcDirectories: (Seq[File], File),
testSrcDirectories: (Seq[File], File),
scalacOptions: Seq[(String, String)],
externalDependencies: Seq[File],
projectDependencies: Seq[String])(
implicit state: State) =
def content(
/*target: String,*/
classpathEntryCollector: PartialFunction[ClasspathEntry, ClasspathEntry])(
name: String,
baseDirectory: File,
compileSrcDirectories: (Seq[File], File),
testSrcDirectories: (Seq[File], File),
scalacOptions: Seq[(String, String)],
externalDependencies: Seq[File],
projectDependencies: Seq[String])(
implicit state: State) =
Content(
name,
baseDirectory,
projectXml(name),
classpath(
classpathEntryCollector,
baseDirectory,
compileSrcDirectories,
testSrcDirectories,
Expand All @@ -132,6 +141,7 @@ private object Eclipse {
</projectDescription>

def classpath(
classpathEntryCollector: PartialFunction[ClasspathEntry, ClasspathEntry],
baseDirectory: File,
compileSrcDirectories: (Seq[File], File),
testSrcDirectories: (Seq[File], File),
Expand All @@ -144,9 +154,9 @@ private object Eclipse {
testSrcDirectories._1 flatMap srcEntry(baseDirectory, testSrcDirectories._2),
externalDependencies map (file => ClasspathEntry.Lib(file.getAbsolutePath)),
projectDependencies map ClasspathEntry.Project,
Seq("org.scala-ide.sdt.launching.SCALA_CONTAINER", "org.eclipse.jdt.launching.JRE_CONTAINER") map ClasspathEntry.Con,
Seq("org.eclipse.jdt.launching.JRE_CONTAINER") map ClasspathEntry.Con, // TODO Optionally use execution env!
Seq(output(baseDirectory, compileSrcDirectories._2)) map ClasspathEntry.Output
).flatten map (_.toXml)
).flatten collect classpathEntryCollector map (_.toXml)
<classpath>{ entries }</classpath>
}

Expand Down Expand Up @@ -174,12 +184,12 @@ private object Eclipse {
def compileSrcDirectories(ref: ProjectRef)(implicit state: State) =
(setting(Keys.sourceDirectories, ref) |@|
setting(Keys.resourceDirectories, ref) |@|
setting(Keys.classDirectory, ref))(srcToOutput)
setting(Keys.classDirectory, ref))(srcDirsToOutput)

def testSrcDirectories(ref: ProjectRef)(implicit state: State) =
(setting(Keys.sourceDirectories, ref, Configurations.Test) |@|
setting(Keys.resourceDirectories, ref, Configurations.Test) |@|
setting(Keys.classDirectory, ref, Configurations.Test))(srcToOutput)
setting(Keys.classDirectory, ref, Configurations.Test))(srcDirsToOutput)

def scalacOptions(ref: ProjectRef)(implicit state: State) =
evaluateTask(Keys.scalacOptions, ref) map { options =>
Expand All @@ -197,9 +207,7 @@ private object Eclipse {
}

def externalDependencies(ref: ProjectRef)(implicit state: State) =
evaluateTask(Keys.externalDependencyClasspath, ref, Configurations.Test) map (attributedFiles =>
attributedFiles.files filterNot (_.getAbsolutePath contains "scala-library.jar")
)
evaluateTask(Keys.externalDependencyClasspath, ref, Configurations.Test) map (_.files)

def projectDependencies(ref: ProjectRef, project: ResolvedProject)(implicit state: State) = {
val projectDependencies = project.dependencies map (dependency => setting(Keys.name, dependency.project))
Expand Down Expand Up @@ -233,7 +241,7 @@ private object Eclipse {

// Utilities

def srcToOutput(sourceDirectories: Seq[File], resourceDirectories: Seq[File], output: File) =
def srcDirsToOutput(sourceDirectories: Seq[File], resourceDirectories: Seq[File], output: File) =
(sourceDirectories ++ resourceDirectories).distinct -> output
}

Expand All @@ -252,30 +260,3 @@ private case class Content(
project: Elem,
classpath: Elem,
scalacOptions: Seq[(String, String)])

private sealed trait ClasspathEntry {
def toXml: Elem
}

private object ClasspathEntry {

case class Src(path: String, output: String) extends ClasspathEntry {
override def toXml = <classpathentry kind="src" path={ path } output={ output }/>
}

case class Lib(path: String, source: Option[String] = None) extends ClasspathEntry {
override def toXml = <classpathentry kind="lib" path={ path }/>
}

case class Project(name: String) extends ClasspathEntry {
override def toXml = <classpathentry kind="src" path={ "/" + name } exported="true" combineaccessrules="false"/>
}

case class Con(path: String) extends ClasspathEntry {
override def toXml = <classpathentry kind="con" path={ path }/>
}

case class Output(path: String) extends ClasspathEntry {
override def toXml = <classpathentry kind="output" path={ path }/>
}
}
52 changes: 51 additions & 1 deletion src/main/scala/com/typesafe/sbteclipse/EclipsePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.typesafe.sbteclipse

import sbt.{ File, Plugin, Setting, SettingKey }
import sbt.Keys.{ baseDirectory, commands }
import scala.xml.Elem

object EclipsePlugin extends Plugin {

Expand All @@ -31,10 +32,27 @@ object EclipsePlugin extends Plugin {
// target := ".target",
skipParents := true,
withSource := false,
commands <+= (commandName, executionEnvironment, skipParents, /*target,*/ withSource)(Eclipse.eclipseCommand)
classpathEntryCollector := eclipseDefaultClasspathEntryCollector,
commands <+= (
commandName,
executionEnvironment,
skipParents,
/*target,*/
withSource,
classpathEntryCollector
)(Eclipse.eclipseCommand)
)
}

def eclipseDefaultClasspathEntryCollector: PartialFunction[ClasspathEntry, ClasspathEntry] = {
case ClasspathEntry.Lib(path, _) if path contains "scala-library.jar" =>
ClasspathEntry.Con("org.scala-ide.sdt.launching.SCALA_CONTAINER")
case ClasspathEntry.Lib(path, _) if path contains "scala-compiler.jar" =>
ClasspathEntry.Con("org.scala-ide.sdt.launching.SCALA_COMPILER_CONTAINER")
case entry =>
entry
}

object EclipseKeys {
import EclipseOpts._

Expand Down Expand Up @@ -63,6 +81,11 @@ object EclipsePlugin extends Plugin {
prefix(WithSource),
"Download and link sources for library dependencies?")

val classpathEntryCollector: SettingKey[PartialFunction[ClasspathEntry, ClasspathEntry]] =
SettingKey[PartialFunction[ClasspathEntry, ClasspathEntry]](
prefix("classpathEntryCollector"),
"Determines how classpath entries are filtered and transformed before written into XML.")

private def prefix(key: String) = "eclipse-" + key
}

Expand All @@ -82,4 +105,31 @@ object EclipsePlugin extends Plugin {

val JRE11 = Value("JRE-1.1")
}

sealed trait ClasspathEntry {
def toXml: Elem
}

object ClasspathEntry {

case class Src(path: String, output: String) extends ClasspathEntry {
override def toXml = <classpathentry kind="src" path={ path } output={ output }/>
}

case class Lib(path: String, source: Option[String] = None) extends ClasspathEntry {
override def toXml = <classpathentry kind="lib" path={ path }/>
}

case class Project(name: String) extends ClasspathEntry {
override def toXml = <classpathentry kind="src" path={ "/" + name } exported="true" combineaccessrules="false"/>
}

case class Con(path: String) extends ClasspathEntry {
override def toXml = <classpathentry kind="con" path={ path }/>
}

case class Output(path: String) extends ClasspathEntry {
override def toXml = <classpathentry kind="output" path={ path }/>
}
}
}
6 changes: 6 additions & 0 deletions src/sbt-test/sbteclipse/02-contents/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ TaskKey[Unit]("verify-classpath-xml-root") <<= baseDirectory map { dir =>
if ((classpath \ "classpathentry" \\ "@path") map (_.text) contains "src/test/resources")
error("""Not expected .classpath of root project to contain <classpathentry kind="..." path="src/test/resources" output="..." /> """)
// other entries
if ((classpath \ "classpathentry" \\ "@path") map (_.text) contains "scala-library.jar")
error("""Not expected .classpath of root project to contain <classpathentry path="...scala-library.jar" ... /> """)
if ((classpath \ "classpathentry" \\ "@path") map (_.text) contains "scala-compiler.jar")
error("""Not expected .classpath of root project to contain <classpathentry path="...scala-compiler.jar" ... /> """)
if (!(classpath.child contains <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>))
error("""Expected .classpath of root project to contain <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/> """)
if (!(classpath.child contains <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_COMPILER_CONTAINER"/>))
error("""Expected .classpath of root project to contain <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_COMPILER_CONTAINER"/> """)
if (!(classpath.child contains <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>))
error("""Expected .classpath of root project to contain <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> """)
if (!(classpath.child contains <classpathentry kind="output" path="target/scala-2.9.1/classes"/>))
Expand Down
5 changes: 4 additions & 1 deletion src/sbt-test/sbteclipse/02-contents/project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ object Build extends Build {
new File("."),
settings = Project.defaultSettings ++ Seq(
unmanagedSourceDirectories in Compile <+= baseDirectory(new File(_, "src/main/scala")),
unmanagedSourceDirectories in Test <+= baseDirectory(new File(_, "src/test/scala"))
unmanagedSourceDirectories in Test <+= baseDirectory(new File(_, "src/test/scala")),
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-compiler" % "2.9.1"
)
),
aggregate = Seq(sub)
)
Expand Down

0 comments on commit 80cc2db

Please sign in to comment.