Skip to content
This repository
Browse code

First simple yet working version. Libraries, sub-projects, etc. still…

… missing.
  • Loading branch information...
commit 4aa29e6bd997850cc995d657e3e8296fe066f546 0 parents
Heiko Seeberger authored
24 .gitignore
... ... @@ -0,0 +1,24 @@
  1 +# SBT
  2 +boot
  3 +target
  4 +
  5 +# Eclipse
  6 +.classpath
  7 +.launch
  8 +.metadata
  9 +.project
  10 +.scala_dependencies
  11 +.settings
  12 +
  13 +# IntelliJ
  14 +.idea
  15 +*.iml
  16 +*.ipr
  17 +*.iws
  18 +out
  19 +
  20 +# TextMate
  21 +*.tmproj
  22 +
  23 +# Mac
  24 +.DS_Store
8 build.sbt
... ... @@ -0,0 +1,8 @@
  1 +
  2 +organization := "com.weiglewilczek.sbteclipse"
  3 +
  4 +name := "sbteclipse"
  5 +
  6 +version := "0.1-SNAPSHOT"
  7 +
  8 +sbtPlugin := true
107 src/main/scala/SbtEclipsePlugin.scala
... ... @@ -0,0 +1,107 @@
  1 +/*
  2 + * Copyright 2011 Weigle Wilczek GmbH
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package com.weiglewilczek.sbteclipse
  17 +
  18 +import java.io.File
  19 +import sbt._
  20 +import sbt.CommandSupport.logger
  21 +import scala.xml.{ NodeSeq, XML }
  22 +
  23 +object SbtEclipsePlugin extends Plugin {
  24 +
  25 + override lazy val settings =
  26 + Seq(Keys.commands += eclipseCommand)
  27 +
  28 + private lazy val eclipseCommand = {
  29 + import Configurations._
  30 + import Keys._
  31 +
  32 + Command.command("eclipse") { state =>
  33 + def setting[A](key: SettingKey[A], configuration: Configuration = Configurations.Compile) = {
  34 + val extracted = Project.extract(state)
  35 + key in (extracted.currentRef, configuration) get extracted.structure.data
  36 + }
  37 +
  38 + logger(state).debug("Trying to create an Eclipse project for you ...")
  39 +
  40 + if (!(setting(scalaVersion) getOrElse "UNKNOWN" startsWith "2.9")) {
  41 + logger(state).error("Invalid Scala version! Fixed to 2.9 because of the Eclipse plugin.")
  42 + state
  43 + } else {
  44 + XML.save(".project", projectXml(setting(name) getOrElse "default"), "UTF-8", true)
  45 + XML.save(
  46 + ".classpath",
  47 + classpathXml(
  48 + setting(sourceDirectories, Compile) getOrElse Nil,
  49 + setting(resourceDirectories, Compile) getOrElse Nil,
  50 + setting(sourceDirectories, Test) getOrElse Nil,
  51 + setting(resourceDirectories, Test) getOrElse Nil,
  52 + setting(baseDirectory) getOrElse new File("."), // TODO Remove hack!
  53 + state),
  54 + "UTF-8",
  55 + true)
  56 +
  57 + logger(state).info("Successfully created an Eclipse project for you. Have fun!")
  58 + state
  59 + }
  60 + }
  61 + }
  62 +
  63 + private def projectXml(name: String) =
  64 + <projectDescription>
  65 + <name>{ name }</name>
  66 + <buildSpec>
  67 + <buildCommand>
  68 + <name>org.scala-ide.sdt.core.scalabuilder</name>
  69 + </buildCommand>
  70 + </buildSpec>
  71 + <natures>
  72 + <nature>org.scala-ide.sdt.core.scalanature</nature>
  73 + <nature>org.eclipse.jdt.core.javanature</nature>
  74 + </natures>
  75 + </projectDescription>
  76 +
  77 + private def classpathXml(
  78 + compileSourceDirectories: Seq[File],
  79 + compileResourceDirectories: Seq[File],
  80 + testSourceDirectories: Seq[File],
  81 + testResourceDirectories: Seq[File],
  82 + baseDirectory: File,
  83 + state: State) = {
  84 +
  85 + def srcEntries(directories: Seq[File]): NodeSeq =
  86 + directories flatMap { directory =>
  87 + if (directory.exists) {
  88 + logger(state).debug("""Creating src entry for directory "%s".""".format(directory))
  89 + val relative = IO.relativize(baseDirectory, directory).get // TODO Better handling!
  90 + <classpathentry kind="src" path={ relative.toString } output="target/classes"/>
  91 + } else {
  92 + logger(state).debug("""Skipping src entry for non-existent directory "%s".""".format(directory))
  93 + NodeSeq.Empty
  94 + }
  95 + }
  96 +
  97 + <classpath>{
  98 + srcEntries(compileSourceDirectories) ++
  99 + srcEntries(compileResourceDirectories) ++
  100 + srcEntries(testSourceDirectories) ++
  101 + srcEntries(testResourceDirectories) ++
  102 + <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
  103 + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
  104 + <classpathentry kind="output" path="target/classes"/>
  105 + }</classpath>
  106 + }
  107 +}

0 comments on commit 4aa29e6

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