Skip to content

Commit

Permalink
back to simple parser; package extension example
Browse files Browse the repository at this point in the history
  • Loading branch information
jto committed Jul 9, 2012
1 parent 79edb6a commit 8b8cc33
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 58 deletions.
2 changes: 1 addition & 1 deletion app/src/main/scala/giter8.scala
Expand Up @@ -56,7 +56,7 @@ class Giter8 extends xsbti.AppMain with Apply {
| Resolves a template within a given branch | Resolves a template within a given branch
| --paramname=paramvalue | --paramname=paramvalue
| Set given parameter value and bypass interaction. | Set given parameter value and bypass interaction.
| |
| |
|Apply template and interactively fulfill parameters. |Apply template and interactively fulfill parameters.
| g8 n8han/giter8 | g8 n8han/giter8
Expand Down
@@ -0,0 +1,15 @@
package $package$

import play.api._

object $className$ extends GlobalSettings {

override def onStart(app: Application) {
Logger.info("Application has started")
}

override def onStop(app: Application) {
Logger.info("Application shutdown...")
}

}
2 changes: 2 additions & 0 deletions sample/.g8/global/default.properties
@@ -0,0 +1,2 @@
className=Global
package=global
3 changes: 1 addition & 2 deletions sample/.g8/model/default.properties
@@ -1,2 +1 @@
className=Application className=User
package=models
70 changes: 15 additions & 55 deletions scaffold/src/main/scala/scaffolding-plugin.scala
Expand Up @@ -13,70 +13,30 @@ object ScaffoldPlugin extends sbt.Plugin {


import ScaffoldingKeys._ import ScaffoldingKeys._
import complete._ import complete._
import complete.DefaultParsers._


val parser: sbt.Project.Initialize[State => Parser[(File, Map[String, String])]] = val parser: sbt.Project.Initialize[State => Parser[String]] =
(baseDirectory, templatesPath) { (b, t) => (baseDirectory, templatesPath) { (b, t) =>

import complete.DefaultParsers._
import java.io.FileInputStream

(state: State) => (state: State) =>
val folder = b / t val folder = b / t

val templates = folder.listFiles
val folders = folder.listFiles.filter(f => f.isDirectory && !f.isHidden) .filter(f => f.isDirectory && !f.isHidden)
val templatesParsers = folders .map(_.getName: Parser[String])
.map(_.getName: Parser[String])
.reduceLeft(_ | _)


val ex = for(
f <- folders;
k <- GIO.readProps(new FileInputStream(f / "default.properties")).keys.toList
) yield (k)

val eq: Parser[String] = "="

Space ~> templatesParsers.map(folder / _).flatMap { root =>


val params = GIO.readProps(new FileInputStream(root / "default.properties")) Space ~> templates.reduceLeft(_ | _)
val validParam = params
.keys
.map(x => x: Parser[String])
.reduceLeft(_ | _)

val p = ((validParam <~ eq): Parser[String]) ~ NotSpace

def filtered(consumed: List[String]): Parser[Map[String, String]] = (Space ~> p).map(Map(_))
.filter(
parsed => !consumed.contains(parsed.head._1),
c => "PAF" // ??
).flatMap { case parsed =>
(filtered(consumed :+ parsed.head._1) ?).map{
case Some(m) => m ++ parsed
case None => parsed
}
}

filtered(Nil).map{ (root, _) }.examples(ex:_*)

// Space ~> (p?) map { param =>
// (root, param.map(Map(_)).getOrElse(Map.empty))
// }
}
} }


val scafffoldTask = scaffold <<= InputTask(parser){ (templateData: TaskKey[(File, Map[String, String])]) => val scafffoldTask = scaffold <<= InputTask(parser){ (argTask: TaskKey[String]) =>
(baseDirectory, templateData) map { (b, data) => (baseDirectory, templatesPath, argTask) map { (b, t, name) =>
val (t, param) = data val folder = b / t
// TODO: how to handle packages names in java (folder structure) G8Helpers.applyRaw(folder / name, b, Nil).fold(
// Template hooks? package(toto.tutu) like ls(,,) e => error(e),
// G8Helpers.applyRaw(t, b, param).fold( r => println("Success :)")
// e => error(e), )
// r => println("Success :)")
// )
} }
} }



lazy val scaffoldSettings: Seq[sbt.Project.Setting[_]] = Seq( lazy val scaffoldSettings: Seq[sbt.Project.Setting[_]] = Seq(
templatesPath := ".g8", templatesPath := ".g8",
scafffoldTask scafffoldTask
Expand Down

0 comments on commit 8b8cc33

Please sign in to comment.