Skip to content

Commit

Permalink
switched to sbt 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Oct 12, 2011
1 parent c0017b0 commit 16d3804
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 120 deletions.
3 changes: 3 additions & 0 deletions app/src/main/scala/giter8.scala
Expand Up @@ -36,6 +36,9 @@ class Giter8 extends xsbti.AppMain with Discover with Apply with Credentials {
def info(msg: String, items: Any*) {
jdklog.info(msg.format(items: _*))
}
def warn(msg: String, items: Any*) {
jdklog.warning(msg.format(items: _*))
}
}
}
def usage = """Usage: g8 [TEMPLATE] [OPTION]...
Expand Down
48 changes: 0 additions & 48 deletions library/src/main/scala/library.scala

This file was deleted.

1 change: 1 addition & 0 deletions notes/0.2.3.markdown
@@ -0,0 +1 @@
* switched to sbt 0.11
42 changes: 42 additions & 0 deletions plugin/src/main/scala/g8.scala
@@ -0,0 +1,42 @@
package giter8

import sbt._

object G8 {
import scala.collection.JavaConversions._
import org.antlr.stringtemplate.StringTemplate
import java.util.{Map=>JMap}

def apply(fromMapping: Seq[(File,String)], toPath: File, params: Map[_,_], log: Logger): Seq[File] = {
val jParams = new java.util.HashMap[Any,Any]()
for { (k, v) <- params.elements } jParams.put(k, v)
apply(fromMapping, toPath, jParams, log)
}
def apply(fromMapping: Seq[(File,String)], toPath: File, params: JMap[_,_], log: Logger): Seq[File] =
fromMapping filter { !_._1.isDirectory } flatMap { case (in, relative) =>
apply(in, expandPath(relative, toPath, params), params, log)
}
def apply(in: File, out: File, params: JMap[_,_], log: Logger): Seq[File] = {
log.debug("Applying " + in)
val st = new StringTemplate
var parseError = false
st.setErrorListener(new org.antlr.stringtemplate.StringTemplateErrorListener {
def error(msg: String, exc: Throwable) { parseError = true }
def warning(msg: String) { }
})
st.setTemplate(IO.read(in))
if (parseError) {
log.info("Unable to parse template %s, copying unmodified" format in)
IO.copyFile(in, out)
} else {
st.setAttributes(params: JMap[_,_])
IO.write(out, st.toString)
}
Seq(out)
}
private def expandPath(relative: String, toPath: File, params: JMap[_,_]): File = {
val out = new StringTemplate(relative)
out.setAttributes(params)
new File(toPath, out.toString)
}
}
75 changes: 39 additions & 36 deletions plugin/src/main/scala/giterate-plugin.scala
Expand Up @@ -2,42 +2,45 @@ package giter8

import sbt._

trait Template extends DefaultProject with Library {
import java.io.File
object Plugin extends sbt.Plugin {
import Keys._
import scala.io.Source
def templateSourcePath = (mainSourcePath / "g8") ##
def templateSources = descendents(templateSourcePath, "*") --- defaultProperties

def defaultProperties = templateSourcePath / "default.properties"

def templateOutput = outputPath / "g8"

override def cleanAction = super.cleanAction dependsOn cleanTask(templateOutput)

lazy val sbtTest = task {
import Process._
(new java.lang.ProcessBuilder("sbt", "update", "test") directory
templateOutput.asFile)! match {
case 0 => None
case code => Some("failed to run `sbt update test` in %s with code %d" format
(templateOutput, code))
}
} dependsOn writeTemplates describedAs
"Run `sbt update test` in %s to smoke-test the templates".format(templateOutput)

lazy val writeTemplates = applyTemplates(
templateSources,
templateOutput,
readProps(defaultProperties)
) describedAs "Apply default parameters to input templates and write to " +
templateOutput

private def readProps(f: Path) = {
val p = new java.util.Properties
FileUtilities.readStream(f.asFile, log) { stm =>
p.load(stm)
None
}
p

object G8Keys {
lazy val g8 = TaskKey[Seq[File]]("g8", "Apply default parameters to input templates and write to output.")
lazy val outputPath = SettingKey[File]("g8-output-path")
lazy val propertiesFile = SettingKey[File]("g8-properties-file")
lazy val properties = SettingKey[Map[Any, Any]]("g8-properties")
lazy val sbtTest = TaskKey[Unit]("g8-sbt-test", "Run `sbt test` in output to smoke-test the templates")
}

import G8Keys._

lazy val baseGiter8Settings: Seq[sbt.Project.Setting[_]] = Seq(
g8 <<= (unmanagedSourceDirectories in g8,
sources in g8, outputPath in g8,
properties in g8, streams) map { (base, srcs, out, props, s) =>
IO.delete(out)
G8(srcs x relativeTo(base), out, props, s.log) },
unmanagedSourceDirectories in g8 <<= (sourceDirectory) { dir => (dir / "g8").get },
sources in g8 <<= (unmanagedSourceDirectories in g8, propertiesFile in g8) map { (dirs, pf) =>
((dirs ** (-DirectoryFilter)) --- pf).get },
outputPath in g8 <<= (target) { dir => dir / "g8" },
propertiesFile in g8 <<= (unmanagedSourceDirectories in g8) { dirs => (dirs / "default.properties").get.head },
properties in g8 <<= (propertiesFile in g8) { f =>
import scala.collection.JavaConversions._
val p = new java.util.Properties
p.load(new java.io.ByteArrayInputStream(IO.readBytes(f)))
Map((for { k <- p.propertyNames } yield (k.toString, p.getProperty(k.toString))).toSeq:_*)
},
sbtTest <<= (g8, outputPath in g8) map { (g8, outputPath) =>
import Process._
(new java.lang.ProcessBuilder("sbt", "test") directory outputPath)! match {
case 0 => None
case code => error("failed to run `sbt update test` in %s with code %d" format
(outputPath, code))
}
}
)
lazy val giter8Settings: Seq[sbt.Project.Setting[_]] = inConfig(Compile)(baseGiter8Settings)
}
8 changes: 0 additions & 8 deletions project/build.properties

This file was deleted.

41 changes: 41 additions & 0 deletions project/build.scala
@@ -0,0 +1,41 @@
import sbt._

object Builds extends sbt.Build {
import Keys._

lazy val buildSettings = Defaults.defaultSettings ++ Seq(
version := "0.2.3-SNAPSHOT",
organization := "net.databinder",
scalaVersion := "2.9.1",
crossScalaVersions := Seq("2.9.1"),
publishArtifact in (Compile, packageBin) := true,
publishArtifact in (Test, packageBin) := false,
publishArtifact in (Compile, packageDoc) := false,
publishArtifact in (Compile, packageSrc) := false,
publishTo <<= version { (v: String) =>
val nexus = "http://nexus.scala-tools.org/content/repositories/"
if(v endsWith "-SNAPSHOT") Some("Scala Tools Nexus" at nexus + "snapshots/")
else Some("Scala Tools Nexus" at nexus + "releases/")
},
credentials += Credentials(Path.userHome / ".ivy2" / ".credentials"),
publishMavenStyle := true
)

// posterous title needs to be giter8, so both app and root are named giter8
lazy val root = Project("giter8", file("."))
lazy val app = Project("giter8", file("app"),
settings = buildSettings ++ Seq(
libraryDependencies ++= {
Seq("org.scala-tools.sbt" % "launcher-interface" % "0.7.4" % "provided",
"net.databinder" %% "dispatch-lift-json" % "0.8.5",
"org.clapper" %% "scalasti" % "0.5.5")
}
))
lazy val plugin = Project("giter8-plugin", file("plugin"),
settings = buildSettings ++ Seq(
sbtPlugin := true,
libraryDependencies <++= (sbtDependency) { sd =>
Seq(sd, "org.antlr" % "stringtemplate" % "3.2.1")
}
))
}
21 changes: 0 additions & 21 deletions project/build/project.scala

This file was deleted.

7 changes: 0 additions & 7 deletions project/plugins/plugins.scala

This file was deleted.

0 comments on commit 16d3804

Please sign in to comment.