PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/src/sbt-plugin/src/main/scala/PlaySettings.scala

http://github.com/playframework/Play20
Scala | 306 lines | 184 code | 94 blank | 28 comment | 2 complexity | 11ddb0d5e38721e081446c6d3a7b5e67 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
  3. */
  4. package play
  5. import sbt.{ Project => SbtProject, _ }
  6. import sbt.Keys._
  7. import Keys._
  8. import com.typesafe.sbt.SbtNativePackager._
  9. import com.typesafe.sbt.packager.Keys._
  10. import java.io.{ Writer, PrintWriter }
  11. import play.console.Colors
  12. trait Settings {
  13. this: PlayCommands with PlayPositionMapper with PlayRun with PlaySourceGenerators =>
  14. lazy val defaultJavaSettings = Seq[Setting[_]](
  15. templatesImport ++= defaultJavaTemplatesImport,
  16. routesImport ++= Seq(
  17. "play.libs.F"
  18. ),
  19. ebeanEnabled := true
  20. )
  21. lazy val defaultScalaSettings = Seq[Setting[_]](
  22. templatesImport ++= defaultScalaTemplatesImport
  23. )
  24. def closureCompilerSettings(optionCompilerOptions: com.google.javascript.jscomp.CompilerOptions) = Seq[Setting[_]](
  25. resourceGenerators in Compile <<= JavascriptCompiler(Some(optionCompilerOptions))(Seq(_)),
  26. resourceGenerators in Compile <+= LessCompiler,
  27. resourceGenerators in Compile <+= CoffeescriptCompiler
  28. )
  29. /** Ask SBT to manage the classpath for the given configuration. */
  30. def manageClasspath(config: Configuration) = managedClasspath in config <<= (classpathTypes in config, update) map { (ct, report) =>
  31. Classpaths.managedJars(config, ct, report)
  32. }
  33. lazy val defaultSettings = Seq[Setting[_]](
  34. scalaVersion := play.core.PlayVersion.scalaVersion,
  35. playPlugin := false,
  36. resolvers ++= Seq(
  37. "Typesafe Releases Repository" at "http://repo.typesafe.com/typesafe/releases/"
  38. ),
  39. target <<= baseDirectory / "target",
  40. sourceDirectory in Compile <<= baseDirectory / "app",
  41. sourceDirectory in Test <<= baseDirectory / "test",
  42. confDirectory <<= baseDirectory / "conf",
  43. resourceDirectory in Compile <<= baseDirectory / "conf",
  44. scalaSource in Compile <<= baseDirectory / "app",
  45. scalaSource in Test <<= baseDirectory / "test",
  46. javaSource in Compile <<= baseDirectory / "app",
  47. javaSource in Test <<= baseDirectory / "test",
  48. javacOptions in (Compile, doc) := List("-encoding", "utf8"),
  49. libraryDependencies <+= (playPlugin) { isPlugin =>
  50. val d = "com.typesafe.play" %% "play" % play.core.PlayVersion.current
  51. if (isPlugin)
  52. d % "provided"
  53. else
  54. d
  55. },
  56. libraryDependencies += "com.typesafe.play" %% "play-test" % play.core.PlayVersion.current % "test",
  57. ivyConfigurations += DocsApplication,
  58. libraryDependencies += "com.typesafe.play" %% "play-docs" % play.core.PlayVersion.current % DocsApplication.name,
  59. manageClasspath(DocsApplication),
  60. parallelExecution in Test := false,
  61. fork in Test := true,
  62. testOptions in Test += Tests.Argument(TestFrameworks.Specs2, "sequential", "true", "junitxml", "console"),
  63. testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "--ignore-runners=org.specs2.runner.JUnitRunner"),
  64. // Make sure Specs2 is at the end of the list of test frameworks, so that it gets priority over
  65. // JUnit. This is a hack/workaround to prevent Specs2 tests with @RunsWith annotations being
  66. // picked up by JUnit. We don't want JUnit to run the tests since JUnit ignores the Specs2
  67. // runnner, which means the tests run but their results are ignored by SBT.
  68. testFrameworks ~= { tf => tf.filter(_ != TestFrameworks.Specs2).:+(TestFrameworks.Specs2) },
  69. testListeners <<= (target, streams).map((t, s) => Seq(new eu.henkelmann.sbt.JUnitXmlTestsListener(t.getAbsolutePath, s.log))),
  70. testResultReporter <<= testResultReporterTask,
  71. testResultReporterReset <<= testResultReporterResetTask,
  72. generateReverseRouter := true,
  73. namespaceReverseRouter := false,
  74. sourceGenerators in Compile <+= (state, confDirectory, sourceManaged in Compile, routesImport, generateReverseRouter, namespaceReverseRouter) map { (s, cd, sm, ri, grr, nrr) =>
  75. RouteFiles(s, Seq(cd), sm, ri, grr, nrr)
  76. },
  77. // Adds config directory's source files to continuous hot reloading
  78. watchSources <+= confDirectory map { all => all },
  79. sourceGenerators in Compile <+= (state, unmanagedSourceDirectories in Compile, sourceManaged in Compile, templatesTypes, templatesImport, excludeFilter in unmanagedSources) map ScalaTemplates,
  80. // Adds app directory's source files to continuous hot reloading
  81. watchSources <++= baseDirectory map { path => ((path / "app") ** "*" --- (path / "app/assets") ** "*").get },
  82. commands ++= Seq(shCommand, playCommand, playStartCommand, h2Command, classpathCommand, licenseCommand, computeDependenciesCommand),
  83. // THE `in Compile` IS IMPORTANT!
  84. run in Compile <<= playRunSetting,
  85. shellPrompt := playPrompt,
  86. copyResources in Compile <<= (copyResources in Compile, playCopyAssets) map { (r, pr) => r ++ pr },
  87. mainClass in (Compile, run) := Some("play.core.server.NettyServer"),
  88. compile in (Compile) <<= PostCompile(scope = Compile),
  89. compile in Test <<= PostCompile(Test),
  90. computeDependencies <<= computeDependenciesTask,
  91. playVersion := play.core.PlayVersion.current,
  92. // all dependencies from outside the project (all dependency jars)
  93. playDependencyClasspath <<= externalDependencyClasspath in Runtime,
  94. // all user classes, in this project and any other subprojects that it depends on
  95. playReloaderClasspath <<= Classpaths.concatDistinct(exportedProducts in Runtime, internalDependencyClasspath in Runtime),
  96. playCommonClassloader <<= playCommonClassloaderTask,
  97. playDependencyClassLoader := createURLClassLoader,
  98. playReloaderClassLoader := createDelegatedResourcesClassLoader,
  99. playCopyAssets <<= playCopyAssetsTask,
  100. playCompileEverything <<= playCompileEverythingTask,
  101. playReload <<= playReloadTask,
  102. sourcePositionMappers += playPositionMapper,
  103. ivyLoggingLevel := UpdateLogging.DownloadOnly,
  104. routesImport := Seq.empty[String],
  105. playMonitoredFiles <<= playMonitoredFilesTask,
  106. playDefaultPort := 9000,
  107. // Default hooks
  108. playOnStarted := Nil,
  109. playOnStopped := Nil,
  110. playRunHooks := Nil,
  111. playRunHooks <++= playOnStarted map { funcs =>
  112. funcs map play.PlayRunHook.makeRunHookFromOnStarted
  113. },
  114. playRunHooks <++= playOnStopped map { funcs =>
  115. funcs map play.PlayRunHook.makeRunHookFromOnStopped
  116. },
  117. playInteractionMode := play.PlayConsoleInteractionMode,
  118. // Assets
  119. playAssetsDirectories := Seq.empty[File],
  120. playExternalAssets := Seq.empty[(File, File => PathFinder, String)],
  121. playAssetsDirectories <+= baseDirectory / "public",
  122. requireJs := Nil,
  123. requireJsFolder := "",
  124. requireJsShim := "",
  125. requireNativePath := None,
  126. buildRequire <<= buildRequireTask,
  127. packageBin in Compile <<= (packageBin in Compile).dependsOn(buildRequire),
  128. resourceGenerators in Compile <+= LessCompiler,
  129. resourceGenerators in Compile <+= CoffeescriptCompiler,
  130. resourceGenerators in Compile <+= JavascriptCompiler(fullCompilerOptions = None),
  131. lessEntryPoints <<= (sourceDirectory in Compile)(base => ((base / "assets" ** "*.less") --- base / "assets" ** "_*")),
  132. coffeescriptEntryPoints <<= (sourceDirectory in Compile)(base => base / "assets" ** "*.coffee"),
  133. javascriptEntryPoints <<= (sourceDirectory in Compile)(base => ((base / "assets" ** "*.js") --- (base / "assets" ** "_*"))),
  134. lessOptions := Seq.empty[String],
  135. coffeescriptOptions := Seq.empty[String],
  136. closureCompilerOptions := Seq.empty[String],
  137. // Settings
  138. devSettings := Nil,
  139. // Templates
  140. templatesImport := defaultTemplatesImport,
  141. templatesTypes := Map(
  142. "html" -> "play.api.templates.HtmlFormat",
  143. "txt" -> "play.api.templates.TxtFormat",
  144. "xml" -> "play.api.templates.XmlFormat",
  145. "js" -> "play.api.templates.JavaScriptFormat"
  146. ),
  147. // Native packaging
  148. sourceDirectory in Universal <<= baseDirectory(_ / "dist"),
  149. mainClass in Compile := Some("play.core.server.NettyServer"),
  150. mappings in Universal <++= (confDirectory) map {
  151. confDirectory: File =>
  152. val confDirectoryLen = confDirectory.getCanonicalPath.length
  153. val pathFinder = confDirectory ** ("*" -- "routes")
  154. pathFinder.get map {
  155. confFile: File =>
  156. confFile -> ("conf/" + confFile.getCanonicalPath.substring(confDirectoryLen))
  157. }
  158. },
  159. mappings in Universal <++= (doc in Compile) map {
  160. docDirectory: File =>
  161. val docDirectoryLen = docDirectory.getCanonicalPath.length
  162. val pathFinder = docDirectory ** "*"
  163. pathFinder.get map {
  164. docFile: File =>
  165. docFile -> ("share/doc/api/" + docFile.getCanonicalPath.substring(docDirectoryLen))
  166. }
  167. },
  168. mappings in Universal <++= (baseDirectory) map {
  169. baseDirectory: File =>
  170. val pathFinder = baseDirectory * "README*"
  171. pathFinder.get map {
  172. readmeFile: File =>
  173. readmeFile -> readmeFile.getName
  174. }
  175. },
  176. // Adds the Play application directory to the command line args passed to Play
  177. bashScriptExtraDefines += "addJava \"-Duser.dir=$(cd \"${app_home}/..\"; pwd -P)\"\n"
  178. )
  179. /**
  180. * Add this to your build.sbt, eg:
  181. *
  182. * {{{
  183. * play.Project.emojiLogs
  184. * }}}
  185. *
  186. * Note that this setting is not supported and may break or be removed or changed at any time.
  187. */
  188. lazy val emojiLogs = logManager ~= { lm =>
  189. new LogManager {
  190. def apply(data: sbt.Settings[Scope], state: State, task: Def.ScopedKey[_], writer: java.io.PrintWriter) = {
  191. val l = lm.apply(data, state, task, writer)
  192. val FailuresErrors = "(?s).*(\\d+) failures?, (\\d+) errors?.*".r
  193. new Logger {
  194. def filter(s: String) = {
  195. val filtered = s.replace("\033[32m+\033[0m", "\u2705 ")
  196. .replace("\033[33mx\033[0m", "\u274C ")
  197. .replace("\033[31m!\033[0m", "\uD83D\uDCA5 ")
  198. filtered match {
  199. case FailuresErrors("0", "0") => filtered + " \uD83D\uDE04"
  200. case FailuresErrors(_, _) => filtered + " \uD83D\uDE22"
  201. case _ => filtered
  202. }
  203. }
  204. def log(level: Level.Value, message: => String) = l.log(level, filter(message))
  205. def success(message: => String) = l.success(message)
  206. def trace(t: => Throwable) = l.trace(t)
  207. override def ansiCodesSupported = l.ansiCodesSupported
  208. }
  209. }
  210. }
  211. }
  212. }