Browse files

verbatim. fixes #21

  • Loading branch information...
1 parent 6a36a0f commit 79de0581030e3c295bdb6d31bf96e52acb767b10 @eed3si9n eed3si9n committed Oct 19, 2011
Showing with 46 additions and 10 deletions.
  1. +9 −0 README.markdown
  2. +11 −0 app/src/main/scala/apply.scala
  3. +9 −7 notes/0.2.3.markdown
  4. +17 −3 plugin/src/main/scala/g8.scala
View
9 README.markdown
@@ -130,6 +130,15 @@ template's source directory are reproduced in its output. File and
directory names also participate in template expansion, e.g.
src/main/g8/src/main/scala/$classname$.scala
+
+The `package` field, if defined, is assumed to be the package name
+of the user's source. A directory named `$package$` expands out to
+package directory structure. For example, `net.databinder` becomes
+`net/databinder`.
+
+The `verbatim` field, if defined, is assumed to be the space delimited
+list of file patterns such as `*.html *.js`. Files matching `verbatim`
+pattern are excluded from string template processing.
If you enter sbt's interactive mode in the base directory of a
template project, the action "g8-test" will apply the template in the
View
11 app/src/main/scala/apply.scala
@@ -103,6 +103,7 @@ trait Apply { self: Giter8 =>
else {
f.getParentFile.mkdirs()
(mime match {
+ case x if verbatim(f, parameters) => None
case Text(_) =>
try {
http(show(repo, hash) >- { in =>
@@ -138,6 +139,16 @@ trait Apply { self: Giter8 =>
Right("Applied %s in %s" format (repo, base.toString))
}
+ def verbatim(file: File, parameters: Map[String,String]): Boolean =
+ parameters.get("verbatim") map { s => globMatch(file, s.split(' ').toSeq) } getOrElse {false}
+ def globMatch(file: File, patterns: Seq[String]): Boolean =
+ patterns exists { globRegex(_).findFirstIn(file.getName).isDefined }
+ def globRegex(pattern: String) = "^%s$".format(pattern flatMap {
+ case '*' => """.*"""
+ case '?' => """."""
+ case '.' => """\."""
+ case x => x.toString
+ }).r
def setFileMode(f: File, mode: String) = allCatch opt {
if ((mode(3).toString.toInt & 0x4) > 0) {
f.setExecutable(true)
View
16 notes/0.2.3.markdown
@@ -1,15 +1,17 @@
## new features
-* giter8-plugin for sbt 0.11 adds `g8-test` task, which runs `src/test/g8/test` using scripted framework.
-* a directory named `$package$` expands out to package dir structure (for example, `net.databinder` becomes `net/databinder`). [#19][19]
+- giter8-plugin for sbt 0.11 adds `g8-test` task, which runs `src/test/g8/test` using scripted framework.
+- a directory named `$package$` expands out to package dir structure (for example, `net.databinder` becomes `net/databinder`). [#19][19]
+- files matching the patterns listed in `verbatim` are copied without processing (for example, `verbatim: *.js` leaves js files alone). [#21][21]
## bug fixes and minor updates
-* switched to sbt 0.11.
-* support for copying permission bits. [#24][24]
-* fixes charset issue. [#26][26]
-* changes github credential storage to `git config --global` or env vars. see [local github config][1]. [#10][10]
+- switched to sbt 0.11.
+- support for copying permission bits. [#24][24]
+- fixes charset issue. [#26][26]
+- changes github credential storage to `git config --global` or env vars. see [local github config][1]. [#10][10]
[1]: https://github.com/blog/180-local-github-config
+ [10]: https://github.com/n8han/giter8/issues/10
[19]: https://github.com/n8han/giter8/issues/19
+ [21]: https://github.com/n8han/giter8/issues/21
[24]: https://github.com/n8han/giter8/issues/24
[26]: https://github.com/n8han/giter8/issues/26
- [10]: https://github.com/n8han/giter8/issues/10
View
20 plugin/src/main/scala/g8.scala
@@ -5,7 +5,7 @@ import sbt._
object G8 {
import scala.util.control.Exception.allCatch
import org.clapper.scalasti.StringTemplate
-
+
def apply(fromMapping: Seq[(File,String)], toPath: File, parameters: Map[String,String], log: Logger): Seq[File] =
fromMapping filter { !_._1.isDirectory } flatMap { case (in, relative) =>
apply(in, expandPath(relative, toPath, parameters), parameters, log)
@@ -15,8 +15,11 @@ object G8 {
import java.nio.charset.{MalformedInputException, Charset}
allCatch opt {
- val template = IO.read(in, Charset forName "UTF-8")
- IO.write(out, new StringTemplate(template).setAttributes(parameters).toString)
+ if (verbatim(in, parameters)) IO.copyFile(in, out)
+ else {
+ val template = IO.read(in, Charset forName "UTF-8")
+ IO.write(out, new StringTemplate(template).setAttributes(parameters).toString)
+ }
} getOrElse {
log.info("Unable to parse template %s, copying unmodified" format in)
IO.copyFile(in, out)
@@ -26,6 +29,17 @@ object G8 {
}
Seq(out)
}
+
+ private def verbatim(file: File, parameters: Map[String,String]): Boolean =
+ parameters.get("verbatim") map { s => globMatch(file, s.split(' ').toSeq) } getOrElse {false}
+ private def globMatch(file: File, patterns: Seq[String]): Boolean =
+ patterns exists { globRegex(_).findFirstIn(file.getName).isDefined }
+ private def globRegex(pattern: String) = "^%s$".format(pattern flatMap {
+ case '*' => """.*"""
+ case '?' => """."""
+ case '.' => """\."""
+ case x => x.toString
+ }).r
private def expandPath(relative: String, toPath: File, parameters: Map[String,String]): File = {
val fileParams = Map(parameters.toSeq map {
case (k, v) if k == "package" => (k, v.replaceAll("""\.""", System.getProperty("file.separator")))

0 comments on commit 79de058

Please sign in to comment.