Permalink
Browse files

improved the templates to make a usable project

  • Loading branch information...
1 parent a19ed56 commit 24f2743778efbc599ecc2daebbd3baf7a3f9c880 @ajselvig ajselvig committed Dec 19, 2012
@@ -0,0 +1,9 @@
+<component name="libraryTable">
+ <library name="guava-11.0.2">
+ <CLASSES>
+ <root url="jar://$PROJECT_DIR$/lib/guava-11.0.2.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+</component>
@@ -1,11 +0,0 @@
-<component name="libraryTable">
- <library name="slf4j-api-1.6.1">
- <CLASSES>
- <root url="jar://$PROJECT_DIR$/lib/slf4j-api-1.6.1.jar!/" />
- <root url="jar://$PROJECT_DIR$/lib/slf4j-log4j12-1.6.6.jar!/" />
- <root url="jar://$PROJECT_DIR$/lib/log4j-1.2.17.jar!/" />
- </CLASSES>
- <JAVADOC />
- <SOURCES />
- </library>
-</component>
@@ -0,0 +1,9 @@
+<component name="libraryTable">
+ <library name="slf4j-api-1.6.6">
+ <CLASSES>
+ <root url="jar://$PROJECT_DIR$/lib/slf4j-api-1.6.6.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+</component>
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,5 @@
+{
+ "kara": {
+ "port": 3000
+ }
+}
View
@@ -11,9 +11,10 @@
<orderEntry type="module" module-name="KaraLib" />
<orderEntry type="library" name="javax.servlet-3.0.0.v201112011016" level="project" />
<orderEntry type="library" name="jetty-all-server-8.1.5.v20120716" level="project" />
- <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="slf4j-api-1.6.6" level="project" />
+ <orderEntry type="library" name="guava-11.0.2" level="project" />
</component>
</module>
@@ -7,6 +7,7 @@ import java.io.*
import java.util.Properties
import kara.config.AppConfig
import generators.templates.*
+import com.google.common.io.Files
/** Possbile named tasks that the generator can perform. */
enum class GeneratorTask(val name : String) {
@@ -97,7 +98,6 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
}
}
}
- //context.put("appPackage", appConfig.appPackage)
}
@@ -120,6 +120,7 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
// setup the project directory structure
createDir("bin")
createDir("config")
+ createDir("lib")
createDir("public")
createDir("public/images")
createDir("public/javascripts")
@@ -133,13 +134,21 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
createDir("src/$appPackagePath/views")
createDir("tmp")
+ // copy the necessary libraries
+ copyFile("lib/kotlin-runtime.jar", "lib/kotlin-runtime.jar")
+ copyFile("out/jars/KaraLib.jar", "lib/KaraLib.jar")
+
// render the templates
+ renderTemplate(moduleImlTemplate(this), "${appConfig.appPackage}.iml")
renderTemplate(buildxmlTemplate(this), "build.xml")
renderTemplate(appconfigTemplate(this), "config/appconfig.json")
+ renderTemplate(appconfigDevelopmentTemplate(this), "config/appconfig.development.json")
renderTemplate(applicationTemplate(this), "src/$appPackagePath/Application.kt")
// make the default controller and view
execController("Home")
+
+ println("\nYour project has been created! Now you're ready to import it into your favorite IDE and start coding.")
}
@@ -182,7 +191,9 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
ensureDir("src/$appPackagePath/views")
ensureDir("src/$appPackagePath/views/$controllerSlug")
- renderTemplate(viewTemplate(this), "src/$appPackagePath/views/$controllerSlug/${viewName}.kt")
+ val outPath = "src/$appPackagePath/views/$controllerSlug/${viewName}.kt"
+ var isLanding = controllerName == "Home" && vName == "Index"
+ renderTemplate(viewTemplate(this, outPath, isLanding), outPath)
}
@@ -218,4 +229,13 @@ class Generator(val appConfig : AppConfig, val task : GeneratorTask, val args :
logger.info("Creating $outPath")
outFile.writeText(template, "UTF-8")
}
+
+ /** Copies the file at srcPath to dstPath */
+ fun copyFile(srcPath : String, dstPath : String) {
+ val srcFile = File(karaHome, srcPath)
+ if (!srcFile.exists())
+ throw RuntimeException("File $srcPath does not exist in the Kara distribution")
+ val dstFile = File(appConfig.appRoot, dstPath)
+ Files.copy(srcFile, dstFile)
+ }
}
@@ -20,6 +20,16 @@ fun appconfigTemplate(gen : Generator) : String {
"""
}
+fun appconfigDevelopmentTemplate(gen : Generator) : String {
+ return """
+{
+ "kara": {
+ "port": 3000
+ }
+}
+"""
+}
+
fun buildxmlTemplate(gen : Generator) : String {
return """
@@ -62,10 +72,11 @@ fun controllerTemplate(gen : Generator) : String {
package ${gen.appPackage}.controllers
import kara.controllers.*
+import ${gen.appPackage}.views.*
import ${gen.appPackage}.views.${gen.controllerSlug}.*
-class ${gen.controllerClassName}() : BaseController() {
- Get("index") fun index() : ActionResult {
+class ${gen.controllerClassName}() : BaseController(DefaultLayout()) {
+ Get("/") fun index() : ActionResult {
return Index()
}
@@ -0,0 +1,28 @@
+package generators.templates
+
+import kara.generators.Generator
+
+/**
+ * Templates for project generation.
+ */
+
+val MODULE_DIR = "\$MODULE_DIR"
+
+fun moduleImlTemplate(gen : Generator) : String {
+ return """<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+ <component name="NewModuleRootManager" inherit-compiler-output="false">
+ <output url="file://$MODULE_DIR$/bin" />
+ <output-test url="file://$MODULE_DIR$/bin/test" />
+ <exclude-output />
+ <content url="file://$MODULE_DIR$">
+ <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+ </content>
+ <orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ <orderEntry type="library" name="KotlinRuntime" level="project" />
+ <orderEntry type="library" name="KaraLib" level="project" />
+ </component>
+</module>
+"""
+}
@@ -2,19 +2,34 @@ package generators.templates
import kara.generators.Generator
+
+fun generalViewBody(filePath : String) : String {
+ return """h2("This is a Kara view")
+p("You can find me here: $filePath")
+"""
+}
+
+fun landingViewBody(filePath : String) : String {
+ return """h2("Welcome to Kara")
+p("Your app is up and running, now it's time to make something!")
+p("Start by editing this file here: $filePath")
+"""
+}
+
+
/**
* Templates for generating views.
*/
-fun viewTemplate(gen : Generator) : String {
+fun viewTemplate(gen : Generator, filePath : String, isLanding : Boolean = false) : String {
return """
package ${gen.appPackage}.views.${gen.controllerSlug}
import kara.views.*
class ${gen.viewName}() : HtmlView() {
override fun render(context: ActionContext) {
- h2("This is a Kara view")
+ ${if (isLanding) landingViewBody(filePath) else generalViewBody(filePath)}
}
}
@@ -27,6 +42,7 @@ fun layoutTemplate(gen : Generator) : String {
package ${gen.appPackage}.views
import kara.views.*
+import ${gen.appPackage}.styles.*
class ${gen.viewName}() : HtmlLayout() {
override fun render(context: ActionContext, mainView: HtmlView) {
@@ -57,6 +73,10 @@ class ${gen.stylesheetName}() : Stylesheet() {
override fun render() {
s("body") {
backgroundColor = c("#f0f0f0")
+ fontFamily = "\"Lucida Sans Unicode\", \"Lucida Grande\", sans-serif"
+ }
+ s("h1") {
+ marginLeft = 1.em
}
s("#main") {
width = 85.percent
@@ -60,7 +60,7 @@ class AppLoader(val appConfig : AppConfig) : FileWatchListener {
if (application == null || application?.dispatcher == null)
throw RuntimeException("Trying to load controllers without a valid application or dispatcher")
val dispatcher = application?.dispatcher as Dispatcher
- val controllerDir = File(binDir, "${appConfig.appPackage}${File.separator}controllers")
+ val controllerDir = File(binDir, "${appConfig.appPackagePath}${File.separator}controllers")
if (!controllerDir.exists()) {
throw RuntimeException("App ${appConfig.appPackage} does not have a controllers directory (should be ${controllerDir.toString()})")
}
@@ -68,10 +68,17 @@ public class JettyRunner(val appConfig: AppConfig) : AppLoadListener {
}
public fun start() {
- server = Server(8080)
+ var port : Int
+ try {
+ port = appConfig.port.toInt()
+ }
+ catch (ex : Exception) {
+ throw RuntimeException("${appConfig.port} is not a valid port number")
+ }
+ server = Server(port)
resourceHandler.setDirectoriesListed(false)
- resourceHandler.setResourceBase("./public")
+ resourceHandler.setResourceBase("./${appConfig.publicDir}")
resourceHandler.setWelcomeFiles(array("index.html"))
server?.setHandler(sessionHandler)
View
@@ -10,11 +10,11 @@
<orderEntry type="library" name="KotlinRuntime" level="project" />
<orderEntry type="library" name="javax.servlet-3.0.0.v201112011016" level="project" />
<orderEntry type="library" name="reflections-0.9.8" level="project" />
- <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="jackson-mapper-asl-1.9.9" level="project" />
<orderEntry type="library" name="jackson-core-asl-1.9.9" level="project" />
+ <orderEntry type="library" name="slf4j-api-1.6.6" level="project" />
</component>
</module>
@@ -11,6 +11,7 @@ public class AppConfig(appRoot : String, val environment : String = "development
{
this["kara.appRoot"] = appRoot
+ this["kara.port"] = "8080"
// read the main appconfig file and also look for an environment-specific one
var file = File(appRoot, "config/appconfig.json")
@@ -26,12 +27,25 @@ public class AppConfig(appRoot : String, val environment : String = "development
return environment == "development"
}
+ /** Returns true if the application is running in the test environment. */
+ fun isTest() : Boolean {
+ return environment == "test"
+ }
+
+ /** Returns true if the application is running in the production environment. */
+ fun isProduction() : Boolean {
+ return environment == "production"
+ }
+
public val appRoot : String
get() = this["kara.appRoot"]
public val appPackage : String
get() = this["kara.appPackage"]
+ public val appPackagePath : String
+ get() = this.appPackage.replace(".", "/")
+
public val paramDeserializer : ParamDeserializer = ParamDeserializer()
/** The directory where publicly available files (like stylesheets, scripts, and images) will go. */
@@ -48,4 +62,10 @@ public class AppConfig(appRoot : String, val environment : String = "development
public val absSessionDir : String
get() = File(appRoot, sessionDir).toString()
+
+
+ /** The port to run the server on. */
+ public val port : String
+ get() = this["kara.port"]
+
}
@@ -34,8 +34,10 @@ class ConfigReader(val config : Config) {
for (key in keyStack) {
path = "${key}." + path
}
- val value = child.getTextValue()!!
- config[path] = value
+ if (child.isInt())
+ config[path] = child.getIntValue().toString()
+ else
+ config[path] = child.getTextValue()!!
}
}
}
View
13 ivy.xml
@@ -1,10 +1,13 @@
<ivy-module version="2.0">
<info organisation="com.tinymission" module="kara"/>
+ <configurations>
+ <conf name="compile" description="Compile classpath"/>
+ </configurations>
<dependencies>
- <dependency org="org.reflections" name="reflections" rev="0.9.8"/>
- <dependency org="org.slf4j" name="slf4j-api" rev="1.6.6"/>
- <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.6.6"/>
- <dependency org="org.eclipse.jetty.aggregate" name="jetty-all-server" rev="8.1.5.v20120716"/>
- <dependency org="org.codehaus.jackson" name="jackson-mapper-asl" rev="1.9.9"/>
+ <dependency org="org.reflections" name="reflections" rev="0.9.8" conf="compile->default"/>
+ <dependency org="org.slf4j" name="slf4j-api" rev="1.6.6" conf="compile->default"/>
+ <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.6.6" conf="compile->default"/>
+ <dependency org="org.eclipse.jetty.aggregate" name="jetty-all-server" rev="8.1.5.v20120716" conf="compile->default"/>
+ <dependency org="org.codehaus.jackson" name="jackson-mapper-asl" rev="1.9.9" conf="compile->default"/>
</dependencies>
</ivy-module>
View
1 kara
@@ -21,4 +21,5 @@ do
esac
done
+#echo $CP_JARS
java -DKARA_HOME="$KARA_HOME" -classpath "$CP_JARS" kara.namespace "$@"

0 comments on commit 24f2743

Please sign in to comment.