Created
August 1, 2011 11:11
-
-
Save Centaur/1117947 to your computer and use it in GitHub Desktop.
script to create a minimal lift project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
exec scala $0 $@ | |
!# | |
import java.io._ | |
def cwd = System.getProperty("user.dir") | |
def setCwd(s:String) { | |
if(!new File(s).exists()) | |
new File(s).mkdirs() | |
System.setProperty("user.dir", s) | |
} | |
args.length match { | |
case 0 => | |
case 1 => setCwd(new File(args(0)).getAbsolutePath) | |
} | |
def writeStringToFile(s:String, path:String) { | |
val writer = new BufferedWriter(new FileWriter(new File(path))) | |
writer.write(s) | |
writer.close() | |
} | |
def askFor(prompt:String, default:String) = { | |
print(prompt + "(default=%s) : ".format(default)) | |
readLine match { | |
case "" => default | |
case input => input | |
} | |
} | |
val projectName = askFor("project name", "Dummy") | |
val sbtVersion = askFor("sbt version", "0.10.1") | |
val rootPackage = askFor("root package", "com.gtan") | |
val scalaVersion = askFor("scala version", "2.9.0-1") | |
val liftVersion = askFor("lift version", "2.4-M1") | |
// Create Dirs | |
val scalaSrc = cwd :: "src" :: "main" :: "scala" :: Nil mkString "/" | |
val bootstrapLiftweb = scalaSrc :: "bootstrap" :: "liftweb" :: Nil mkString "/" | |
val packageRoot = scalaSrc :: rootPackage.replaceAll("\\.", "/") :: Nil mkString "/" | |
val webapp = cwd :: "src" :: "main" :: "webapp" :: Nil mkString "/" | |
val static = webapp :: "static" :: Nil mkString "/" | |
val webInf = webapp :: "WEB-INF" :: Nil mkString "/" | |
val templatesHidden = webInf :: "templates_hidden" :: Nil mkString "/" | |
val resources = cwd :: "src" :: "main" :: "resources" :: Nil mkString "/" | |
val props = resources :: "props" :: Nil mkString "/" | |
val project = cwd :: "project" :: Nil mkString "/" | |
val plugins = project :: "plugins" :: Nil mkString "/" | |
val dirs = scalaSrc :: bootstrapLiftweb :: packageRoot :: webapp :: static :: webInf :: templatesHidden :: resources :: props :: project :: plugins :: Nil | |
dirs foreach {new File(_).mkdirs} | |
// web.xml | |
val webXml = webInf :: "web.xml" :: Nil mkString "/" | |
val webXmlContent = """<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE web-app | |
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" | |
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> | |
<web-app> | |
<filter> | |
<filter-name>LiftFilter</filter-name> | |
<display-name>Lift Filter</display-name> | |
<description>The Filter that intercepts lift calls</description> | |
<filter-class>net.liftweb.http.LiftFilter</filter-class> | |
</filter> | |
<filter-mapping> | |
<filter-name>LiftFilter</filter-name> | |
<url-pattern>/*</url-pattern> | |
</filter-mapping> | |
</web-app> | |
""" | |
writeStringToFile(webXmlContent, webXml) | |
// build.properties | |
val buildPropertiesContent = "sbt.version=%s".format(sbtVersion) | |
val buildProperties = project :: "build.properties" :: Nil mkString "/" | |
writeStringToFile(buildPropertiesContent, buildProperties) | |
// build.sbt | |
val buildSbt = cwd :: "build.sbt" :: Nil mkString "/" | |
val buildSbtContent = """ | |
organization := """"+rootPackage+"""" | |
name := """"+projectName+"""" | |
version := "1.0" | |
scalaVersion := """"+scalaVersion+"""" | |
seq(webSettings: _*) | |
fork := true | |
libraryDependencies ++= { | |
val liftVersion = """"+liftVersion+"""" | |
Seq( | |
"net.liftweb" %% "lift-webkit" % liftVersion % "compile->default", | |
"net.liftweb" %% "lift-mapper" % liftVersion % "compile->default", | |
"net.liftweb" %% "lift-wizard" % liftVersion % "compile->default" | |
) | |
} | |
libraryDependencies ++= Seq( | |
"junit" % "junit" % "4.5" % "test->default", | |
"org.eclipse.jetty" % "jetty-webapp" % "7.4.4.v20110707" % "jetty", | |
//"javax.servlet" % "servlet-api" % "2.5" % "provided->default", | |
//"com.h2database" % "h2" % "1.2.138", | |
"ch.qos.logback" % "logback-classic" % "0.9.26" % "compile->default" | |
) | |
""" | |
writeStringToFile(buildSbtContent, buildSbt) | |
// plugins/build.sbt | |
val pluginsBuildSbt = plugins :: "build.sbt" :: Nil mkString "/" | |
val pluginsBuildSbtContent = """ | |
resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/" | |
resolvers ++= Seq( | |
"Web plugin repo" at "http://siasia.github.com/maven2", | |
Resolver.url("Typesafe repository", new java.net.URL("http://typesafe.artifactoryonline.com/typesafe/ivy-releases/"))(Resolver.defaultIvyPatterns) | |
) | |
libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-web-plugin" % ("0.1.0-"+v)) | |
libraryDependencies += "com.github.mpeltonen" %% "sbt-idea" % "0.10.0" | |
""" | |
writeStringToFile(pluginsBuildSbtContent, pluginsBuildSbt) | |
// props/default.props | |
val defaultProps = props :: "default.props" :: Nil mkString "/" | |
val defaultPropsContent = "" | |
writeStringToFile(defaultPropsContent, defaultProps) | |
// default.html | |
val defaultHtml = templatesHidden :: "default.html" :: Nil mkString "/" | |
val defaultHtmlContent = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div id="container"> | |
<div id="menu"> | |
</div> | |
<div id="content"> | |
There must be something here. | |
</div> | |
<div id="footer"> | |
</div> | |
</div> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> | |
</body> | |
</html>""" | |
writeStringToFile(defaultHtmlContent, defaultHtml) | |
// Boot.scala | |
val bootScala = bootstrapLiftweb :: "Boot.scala" :: Nil mkString "/" | |
val bootScalaContent = """ | |
package bootstrap.liftweb | |
import net.liftweb._ | |
import util._ | |
import Helpers._ | |
import common._ | |
import http._ | |
import sitemap._ | |
import Loc._ | |
/** | |
* A class that's instantiated early and run. It allows the application | |
* to modify lift's environment | |
*/ | |
class Boot { | |
def boot { | |
// where to search snippet | |
LiftRules.addToPackages(""""+rootPackage+"""") | |
// Build SiteMap | |
val entries = List( | |
Menu.i("Home") / "index", // the simple way to declare a menu | |
// more complex because this menu allows anything in the | |
// /static path to be visible | |
Menu(Loc("Static", Link(List("static"), true, "/static/index"), | |
"Static Content"))) | |
// set the sitemap. Note if you don't want access control for | |
// each page, just comment this line out. | |
LiftRules.setSiteMap(SiteMap(entries:_*)) | |
//Show the spinny image when an Ajax call starts | |
LiftRules.ajaxStart = | |
Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd) | |
// Make the spinny image go away when it ends | |
LiftRules.ajaxEnd = | |
Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd) | |
// Use jQuery 1.4 | |
LiftRules.jsArtifacts = net.liftweb.http.js.jquery.JQuery14Artifacts | |
// Force the request to be UTF-8 | |
LiftRules.early.append(_.setCharacterEncoding("UTF-8")) | |
// Use HTML5 for rendering | |
LiftRules.htmlProperties.default.set((r: Req) => | |
new Html5Properties(r.userAgent)) | |
} | |
} | |
""" | |
writeStringToFile(bootScalaContent, bootScala) | |
//index.html | |
val indexHtml = webapp :: "index.html" :: Nil mkString "/" | |
val indexHtmlContent = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>hellolift</title> | |
</head> | |
<body> | |
Hello, Lift! | |
<div class="lift:Menu.builder /> | |
</body> | |
</html> | |
""" | |
writeStringToFile(indexHtmlContent, indexHtml) | |
//static/index.html | |
val staticIndexHtml = static :: "index.html" :: Nil mkString "/" | |
val staticIndexHtmlContent = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>static index</title> | |
</head> | |
<body> | |
static content | |
<div class="lift:Menu.builder /> | |
</body> | |
</html> | |
""" | |
writeStringToFile(staticIndexHtmlContent, staticIndexHtml) | |
dirs foreach { dir => | |
if(new File(dir).list.length == 0) { | |
new File(dir + "/.keep").createNewFile | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment