Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

106 lines (91 sloc) 4.175 kB
package giter8
import java.io.File
object G8 {
import scala.util.control.Exception.allCatch
import org.clapper.scalasti.StringTemplate
private val renderer = new StringRenderer
def apply(fromMapping: Seq[(File,String)], toPath: File, parameters: Map[String,String]): Seq[File] =
fromMapping filter { !_._1.isDirectory } flatMap { case (in, relative) =>
apply(in, expandPath(relative, toPath, parameters), parameters)
}
def apply(in: File, out: File, parameters: Map[String,String]) = {
println("Applying " + in)
try {
if (verbatim(in, parameters)) GIO.copyFile(in, out)
else {
write(out, GIO.read(in, "UTF-8"), parameters)
}
}
catch {
case e: Exception =>
println("Falling back to file copy for %s: %s" format(in.toString, e.getMessage))
GIO.copyFile(in, out)
}
allCatch opt {
if (in.canExecute) out.setExecutable(true)
}
Seq(out)
}
def write(out: File, template: String, parameters: Map[String, String]) {
val applied = new StringTemplate(template)
.setAttributes(parameters)
.registerRenderer(renderer)
.toString
GIO.write(out, applied, "UTF-8")
}
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
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") match {
case "\\" => "\\\\"
case sep => sep
}))
case x => x
}: _*)
new File(toPath, new StringTemplate(formatize(relative)).setAttributes(fileParams).registerRenderer(renderer).toString)
}
private def formatize(s: String) = s.replaceAll("""\$(\w+)__(\w+)\$""", """\$$1;format="$2"\$""")
def decapitalize(s: String) = if (s.isEmpty) s else s(0).toLower + s.substring(1)
def startCase(s: String) = s.toLowerCase.split(" ").map(_.capitalize).mkString(" ")
def wordOnly(s: String) = s.replaceAll("""\W""", "")
def upperCamel(s: String) = wordOnly(startCase(s))
def lowerCamel(s: String) = decapitalize(upperCamel(s))
def hyphenate(s: String) = s.replaceAll("""\s+""", "-")
def normalize(s: String) = hyphenate(s.toLowerCase)
def snakeCase(s: String) = s.replaceAll("""\s+""", "_")
def packageDir(s: String) = s.replace(".", System.getProperty("file.separator"))
def addRandomId(s: String) = s + "-" + new java.math.BigInteger(256, new java.security.SecureRandom).toString(32)
}
class StringRenderer extends org.clapper.scalasti.AttributeRenderer[String] {
import G8._
def toString(value: String): String = value
override def toString(value: String, formatName: String): String = {
val formats = formatName.split(",").map(_.trim)
formats.foldLeft(value)(format)
}
def format(value: String, formatName: String): String = formatName match {
case "upper" | "uppercase" => value.toUpperCase
case "lower" | "lowercase" => value.toLowerCase
case "cap" | "capitalize" => value.capitalize
case "decap" | "decapitalize" => decapitalize(value)
case "start" | "start-case" => startCase(value)
case "word" | "word-only" => wordOnly(value)
case "Camel" | "upper-camel" => upperCamel(value)
case "camel" | "lower-camel" => lowerCamel(value)
case "hyphen" | "hyphenate" => hyphenate(value)
case "norm" | "normalize" => normalize(value)
case "snake" | "snake-case" => snakeCase(value)
case "packaged" | "package-dir" => packageDir(value)
case "random" | "generate-random" => addRandomId(value)
case _ => value
}
}
Jump to Line
Something went wrong with that request. Please try again.