Permalink
Browse files

removed the velocity dependency and switched to Kotlin templates for …

…the generator.
  • Loading branch information...
1 parent ecf767a commit a19ed5639d7a5ba5d231feda705b03873ed976fb @ajselvig ajselvig committed Dec 19, 2012
@@ -1,9 +0,0 @@
-<component name="libraryTable">
- <library name="gson-2.2.2">
- <CLASSES>
- <root url="jar://$PROJECT_DIR$/lib/gson-2.2.2.jar!/" />
- </CLASSES>
- <JAVADOC />
- <SOURCES />
- </library>
-</component>
@@ -1,9 +0,0 @@
-<component name="libraryTable">
- <library name="velocity-1.7">
- <CLASSES>
- <root url="jar://$PROJECT_DIR$/lib/velocity-1.7.jar!/" />
- </CLASSES>
- <JAVADOC />
- <SOURCES />
- </library>
-</component>
View
Oops, something went wrong.
@@ -1,6 +1,6 @@
{
"kara": {
- "package": "karademo",
+ "appPackage": "karademo",
"publicDir": "public",
"stylesheetDir": "stylesheets",
"sessionDir": "tmp/sessions"
@@ -14,7 +14,6 @@
<orderEntry type="library" name="slf4j-api-1.6.1" level="project" />
<orderEntry type="library" name="slf4j-log4j12-1.6.6" level="project" />
<orderEntry type="library" name="log4j-1.2.17" level="project" />
- <orderEntry type="library" name="velocity-1.7" level="project" />
</component>
</module>
View
@@ -21,24 +21,35 @@ fun generator(appConfig : AppConfig, task : GeneratorTask, args : List<String>)
generator.exec()
}
+fun config(appCongig : AppConfig) {
+ println(appCongig.toString())
+}
+
fun help() {
val s = """Welcome to the Kara command line.
Usage:
kara [-options] command args
Commands:
+ c, config Show the application's configuration for the current environment
g, generate Generates a new project or file (see below)
h, help Show this help message
s, server Run the Kara server on the current directory
Options:
-d, --debug Show debug log messages
+ -e, --env Specify the environment (default is --env=development)
-i, --info Show info log messages (default)
-w, --warn Shwo only warning log messages
Generators:
- project <name> Generates a new Kara project with the given name.
+ project <name> Generates a new Kara project with the given name.
+ Use the --package=<package> option to specify a package
+ that's different than the project name.
+ controller <name> Generates a new controller with the given name.
+ "Controller" will be automatically appended to the name)
+ view <controller> <view> Generate a new view for the given controller.
"""
println(s)
@@ -47,8 +58,6 @@ Generators:
fun main(args: Array<String>) {
BasicConfigurator.configure()
- val appConfig = AppConfig(System.getProperty("user.dir")!!)
-
val logger = Logger.getLogger("Kara.Main")!!
println("################################################################")
@@ -60,8 +69,10 @@ fun main(args: Array<String>) {
var startServer = false
var runGenerator = false
var showHelp = false
+ var showConfig = false
var generatorTask : GeneratorTask? = null
val generatorArgs = ArrayList<String>()
+ var env = "development"
// var appRoot = System.getProperty("user.dir") as String
// AppConfig.current = AppConfig(appRoot, "development") // this breaks the runtime for some reason
@@ -70,32 +81,54 @@ fun main(args: Array<String>) {
// parse command line arguments
try {
- for (arg in args) {
+ for (var arg in args) {
+ val comps = arg.split("=")
+ var value : String? = null
+ if (comps.size == 2) {
+ arg = comps[0]
+ value = comps[1]
+ }
when (arg) {
+ "c", "config" -> showConfig = true
"s", "server" -> startServer = true
"g", "generator" -> runGenerator = true
"h", "help" -> showHelp = true
"project" -> generatorTask = GeneratorTask.project
"controller" -> generatorTask = GeneratorTask.controller
"view" -> generatorTask = GeneratorTask.view
"-d", "--debug" -> logLevel = Level.DEBUG
+ "-e", "env" -> {
+ if (value == null)
+ throw RuntimeException("Must provide a value for the environment argument")
+ env = value!!
+ }
"-i", "--info" -> logLevel = Level.INFO
"-w", "--warn" -> logLevel = Level.WARN
else -> {
- if (runGenerator)
- generatorArgs.add(arg)
+ if (runGenerator) {
+ if (value == null)
+ generatorArgs.add(arg)
+ else
+ generatorArgs.add("$arg=$value")
+ }
else
logger.error("Unkown argument: $arg")
}
}
}
+ // create the app config
+ val appConfig = AppConfig(System.getProperty("user.dir")!!, env)
+
// set the log level
LogManager.getRootLogger()?.setLevel(logLevel)
if (showHelp) {
help()
}
+ else if (showConfig) {
+ config(appConfig)
+ }
else if (startServer) {
server(appConfig)
}
@@ -3,11 +3,10 @@ package kara.generators
import org.apache.log4j.Logger
import java.io.File
import kara.generators.Permissions
-import org.apache.velocity.VelocityContext
-import org.apache.velocity.app.Velocity
import java.io.*
import java.util.Properties
import kara.config.AppConfig
+import generators.templates.*
/** Possbile named tasks that the generator can perform. */
enum class GeneratorTask(val name : String) {
@@ -28,12 +27,23 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
val permissions = Permissions()
var karaHome = ""
- val context = VelocityContext()
+ val appPackage : String
+ get() = appConfig.appPackage
/** The app package converted to a path. */
val appPackagePath : String
get() = appConfig.appPackage.replace(".", "/")
+ /** Some strings used in various templates. */
+
+ var controllerSlug : String = ""
+
+ var controllerClassName : String = ""
+
+ var viewName : String = ""
+
+ var stylesheetName : String = ""
+
/** Executes the generator task. */
public fun exec() {
// get the Kara home
@@ -42,12 +52,6 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
throw RuntimeException("The KARA_HOME environment variable needs to be defined to use generators.")
karaHome = home!!
- // setup velocity
- val velocityProps = Properties()
- velocityProps.set("file.resource.loader.path", File(karaHome, "KaraExec/templates/").toString())
- Velocity.init(velocityProps)
- context.put("gen", this)
-
// parse the arguments
parseArgs()
@@ -87,20 +91,20 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
when (name) {
"package", "p" -> {
logger.info("Using application package $value")
- appConfig["kara.package"] = value
+ appConfig["kara.appPackage"] = value
}
else -> throw RuntimeException("Unkown generator argument $name")
}
}
}
- context.put("appPackage", appConfig.appPackage)
+ //context.put("appPackage", appConfig.appPackage)
}
/** Executes the task to create a new project. */
fun execProject(val projectName : String) {
- if (appConfig.appPackage.length == 0)
- appConfig["kara.appPackage"] = projectName
+ if (!appConfig.contains("kara.package"))
+ appConfig["kara.package"] = projectName
// create the project root directory
val projectRoot = File(appConfig.appRoot, projectName)
@@ -115,6 +119,7 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
// setup the project directory structure
createDir("bin")
+ createDir("config")
createDir("public")
createDir("public/images")
createDir("public/javascripts")
@@ -129,9 +134,9 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
createDir("tmp")
// render the templates
- renderTemplate("build.xml")
- renderTemplate("appconfig.json")
- renderTemplate("src.appPackage.Application.kt")
+ renderTemplate(buildxmlTemplate(this), "build.xml")
+ renderTemplate(appconfigTemplate(this), "config/appconfig.json")
+ renderTemplate(applicationTemplate(this), "src/$appPackagePath/Application.kt")
// make the default controller and view
execController("Home")
@@ -142,33 +147,42 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
fun execController(var controllerName : String) {
controllerName = controllerName.capitalize()
- val controllerSlug = controllerName.toLowerCase()
- context.put("controllerSlug", controllerSlug)
- val controllerClassName = "${controllerName}Controller"
- context.put("controllerClassName", controllerClassName)
+ controllerSlug = controllerName.toLowerCase()
+ controllerClassName = "${controllerName}Controller"
ensureDir("src/$appPackagePath/controllers")
- renderTemplate("src.appPackage.controllers.Controller.kt", "src/$appPackagePath/controllers/${controllerClassName}.kt")
+ renderTemplate(controllerTemplate(this), "src/$appPackagePath/controllers/${controllerClassName}.kt")
+ execLayout("Default")
execView(controllerName, "Index")
}
/** Executes the task to create a new view. */
- fun execView(var controllerName : String, var viewName : String) {
+ fun execLayout(var name : String) {
+ viewName = name.capitalize() + "Layout"
+ stylesheetName = name.capitalize() + "Styles"
+
+ ensureDir("src/$appPackagePath/views")
+ ensureDir("src/$appPackagePath/styles")
+
+ renderTemplate(layoutTemplate(this), "src/$appPackagePath/views/${viewName}.kt")
+ renderTemplate(stylesheetTemplate(this), "src/$appPackagePath/styles/${stylesheetName}.kt")
+ }
+
+
+ /** Executes the task to create a new view. */
+ fun execView(var controllerName : String, var vName : String) {
controllerName = controllerName.capitalize()
- viewName = viewName.capitalize()
- context.put("viewName", viewName)
+ viewName = vName.capitalize()
- val controllerSlug = controllerName.toLowerCase()
- context.put("controllerSlug", controllerSlug)
- val controllerClassName = "${controllerName}Controller"
- context.put("controllerClassName", controllerClassName)
+ controllerSlug = controllerName.toLowerCase()
+ controllerClassName = "${controllerName}Controller"
ensureDir("src/$appPackagePath/views")
ensureDir("src/$appPackagePath/views/$controllerSlug")
- renderTemplate("src.appPackage.views.View.kt", "src/$appPackagePath/views/$controllerSlug/${viewName}.kt")
+ renderTemplate(viewTemplate(this), "src/$appPackagePath/views/$controllerSlug/${viewName}.kt")
}
@@ -194,32 +208,14 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
}
}
-
/** Renders a template to the target project. */
- fun renderTemplate(template : String, var outPath : String = "") {
- if (!Velocity.templateExists("$template.vm"))
- throw RuntimeException("Template $template.vm doesn't exist.")
-
- // compute the output file path
- if (outPath.length == 0) {
- val comps = template.split("\\.")
- val fileName = "${comps[comps.size-2]}.${comps[comps.size-1]}"
- val outDir = template.replace(fileName, "").replace(".", "/").replace("appPackage", appPackagePath)
- var outFile = File(appConfig.appRoot, "$outDir/$fileName")
- if (outFile.exists()) {
- permissions.ask("file_overwrite", "$outFile already exists and will be overwritten.")
- }
- outPath = outFile.toString()
+ fun renderTemplate(template : String, var outPath : String) {
+ // write the template to the file
+ val outFile = File(appConfig.appRoot, outPath)
+ if (outFile.exists()) {
+ permissions.ask("file_overwrite", "$outPath already exists and will be overwritten.")
}
- else {
- outPath = File(appConfig.appRoot, outPath).toString()
- }
-
- // render the template
logger.info("Creating $outPath")
- val writer = FileWriter(outPath)
- Velocity.mergeTemplate("$template.vm", context, writer)
- writer.flush()
-
+ outFile.writeText(template, "UTF-8")
}
}
Oops, something went wrong.

0 comments on commit a19ed56

Please sign in to comment.