Skip to content

Commit

Permalink
Merge pull request #90 from BlackPrincess/package-support
Browse files Browse the repository at this point in the history
Added packege support in generator tasks.
  • Loading branch information
seratch committed Mar 6, 2014
2 parents a6bc8fa + f55e47b commit acd2b62
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 172 deletions.
9 changes: 9 additions & 0 deletions task/src/main/scala/skinny/task/generator/CodeGenerator.scala
Expand Up @@ -13,6 +13,15 @@ trait CodeGenerator {

protected def toClassName(name: String) = name.head.toUpper + name.tail

protected def toNamespace(basePackage: String, namespaces: Seq[String]): String =
(Seq(basePackage) ++ namespaces).filter(!_.isEmpty).reduceLeft { (a, b) => a + "." + b }

protected def toDirectoryPath(baseDir: String, namespaces: Seq[String]): String =
(Seq(baseDir) ++ namespaces).filter(!_.isEmpty).reduceLeft { (a, b) => a + "/" + b }

protected def toResourcesBasePath(namespaces: Seq[String]): String = if (namespaces.filter(!_.isEmpty).isEmpty) ""
else "/" + namespaces.filter(!_.isEmpty).reduceLeft { (a, b) => a + "/" + b }

protected def toControllerClassName(name: String) = toClassName(name) + "Controller"

protected def isOptionClassName(t: String): Boolean = t.trim().startsWith("Option")
Expand Down
42 changes: 23 additions & 19 deletions task/src/main/scala/skinny/task/generator/ControllerGenerator.scala
Expand Up @@ -17,29 +17,33 @@ trait ControllerGenerator extends CodeGenerator {
}

def run(args: List[String]) {
args.toList match {
case name :: _ =>
val completedArgs = if (args.size == 1) Seq("") ++ args
else args

completedArgs match {
case namespace :: name :: _ =>
val namespaces = namespace.split('.')
showSkinnyGenerator()
generateApplicationControllerIfAbsent()
generate(name)
appendToControllers(name)
generate(namespaces, name)
appendToControllers(namespaces, name)
appendToScalatraBootstrap(name)
generateSpec(name)
generateSpec(namespaces, name)
println("")
case _ => showUsage
}
}

def code(name: String): String = {
s"""package controller
def code(namespaces: Seq[String], name: String): String = {
s"""package ${toNamespace("controller", namespaces)}
|
|import skinny._
|import skinny.validator._
|
|class ${toClassName(name)}Controller extends ApplicationController {
| protectFromForgery()
|
| def index = render("/${toVariable(name)}/index")
| def index = render("${toResourcesBasePath(namespaces)}/${toVariable(name)}/index")
|
|}
|""".stripMargin
Expand Down Expand Up @@ -69,17 +73,17 @@ trait ControllerGenerator extends CodeGenerator {
""".stripMargin)
}

def generate(name: String) {
val file = new File(s"src/main/scala/controller/${toClassName(name)}Controller.scala")
writeIfAbsent(file, code(name))
def generate(namespaces: Seq[String], name: String) {
val file = new File(s"src/main/scala/${toDirectoryPath("controller", namespaces)}/${toClassName(name)}Controller.scala")
writeIfAbsent(file, code(namespaces, name))
}

def appendToControllers(name: String) {
def appendToControllers(namespaces: Seq[String], name: String) {
val controllerClassName = s"${name.head.toUpper + name.tail}Controller"
val newCode =
s"""object Controllers {
| object ${toVariable(name)} extends ${controllerClassName} with Routes {
| val indexUrl = get("/${toVariable(name)}/?")(index).as('index)
| object ${toVariable(name)} extends ${toNamespace("controller", namespaces)}.${controllerClassName} with Routes {
| val indexUrl = get("/${toResourcesBasePath(namespaces)}/${toVariable(name)}/?")(index).as('index)
| }
|""".stripMargin
val file = new File("src/main/scala/controller/Controllers.scala")
Expand Down Expand Up @@ -115,8 +119,8 @@ trait ControllerGenerator extends CodeGenerator {
}
}

def spec(name: String): String = {
s"""package controller
def spec(namespaces: Seq[String], name: String): String = {
s"""package ${toNamespace("controller", namespaces)}
|
|import _root_.controller._
|import _root_.model._
Expand All @@ -130,9 +134,9 @@ trait ControllerGenerator extends CodeGenerator {
|""".stripMargin
}

def generateSpec(name: String) {
val specFile = new File(s"src/test/scala/controller/${toClassName(name)}ControllerSpec.scala")
writeIfAbsent(specFile, spec(name))
def generateSpec(namespaces: Seq[String], name: String) {
val specFile = new File(s"src/test/scala/${toDirectoryPath("controller", namespaces)}/${toClassName(name)}ControllerSpec.scala")
writeIfAbsent(specFile, spec(namespaces, name))
}

}
29 changes: 16 additions & 13 deletions task/src/main/scala/skinny/task/generator/ModelGenerator.scala
Expand Up @@ -26,23 +26,26 @@ trait ModelGenerator extends CodeGenerator {
}

def run(args: List[String]) {
args.toList match {
case name :: attributes =>
val completedArgs: Seq[String] = if (args(1).contains(":")) Seq("") ++ args
else args
completedArgs.toList match {
case namespace :: name :: attributes =>
showSkinnyGenerator()
val attributePairs: Seq[(String, String)] = attributes.flatMap { attribute =>
attribute.toString.split(":") match {
case Array(k, v) => Some(k -> v)
case _ => None
}
}
generate(name, None, attributePairs)
generate(namespace.split('.'), name, None, attributePairs)
println("")

case _ => showUsage
}
}

def code(name: String, tableName: Option[String], attributePairs: Seq[(String, String)]): String = {
def code(namespaces: Seq[String], name: String, tableName: Option[String], attributePairs: Seq[(String, String)]): String = {
val namespace = toNamespace("model", namespaces)
val modelClassName = toClassName(name)
val alias = modelClassName.filter(_.isUpper).map(_.toLower).mkString
val timestampsTraitIfExists = if (withTimestamps) s"with TimestampsFeature[${modelClassName}] " else ""
Expand All @@ -69,7 +72,7 @@ trait ModelGenerator extends CodeGenerator {
|${if (attributePairs.isEmpty) "" else attributePairs.map { case (k, t) => " " + k + " = rs.get(rn." + k + ")" }.mkString("", ",\n", ",\n")}${timestampsExtraction}
|""".stripMargin

s"""package model
s"""package ${namespace}
|
|import skinny.orm._, feature._
|import scalikejdbc._, SQLInterpolation._
Expand All @@ -89,13 +92,13 @@ trait ModelGenerator extends CodeGenerator {
|""".stripMargin
}

def generate(name: String, tableName: Option[String], attributePairs: Seq[(String, String)]) {
val productionFile = new File(s"src/main/scala/model/${toClassName(name)}.scala")
writeIfAbsent(productionFile, code(name, tableName, attributePairs))
def generate(namespaces: Seq[String], name: String, tableName: Option[String], attributePairs: Seq[(String, String)]) {
val productionFile = new File(s"src/main/scala/${toDirectoryPath("model", namespaces)}/${toClassName(name)}.scala")
writeIfAbsent(productionFile, code(namespaces, name, tableName, attributePairs))
}

def spec(name: String): String = {
s"""package model
def spec(namespaces: Seq[String], name: String): String = {
s"""package ${toNamespace("model", namespaces)}
|
|import skinny.test._
|import org.scalatest.fixture.FlatSpec
Expand All @@ -108,10 +111,10 @@ trait ModelGenerator extends CodeGenerator {
|""".stripMargin
}

def generateSpec(name: String, attributePairs: Seq[(String, String)]) {
val specFile = new File(s"src/test/scala/model/${toClassName(name)}Spec.scala")
def generateSpec(namespaces: Seq[String], name: String, attributePairs: Seq[(String, String)]) {
val specFile = new File(s"src/test/scala/${toDirectoryPath("model", namespaces)}/${toClassName(name)}Spec.scala")
FileUtils.forceMkdir(specFile.getParentFile)
writeIfAbsent(specFile, spec(name))
writeIfAbsent(specFile, spec(namespaces, name))
}

}

0 comments on commit acd2b62

Please sign in to comment.