PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/beschauer/playframework
Scala | 308 lines | 185 code | 95 blank | 28 comment | 2 complexity | 25bb512f43701a91a47d7318d0818225 MD5 | raw file
  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.sbtplugin.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. generateRefReverseRouter := true,
  74. namespaceReverseRouter := false,
  75. sourceGenerators in Compile <+= (state, confDirectory, sourceManaged in Compile, routesImport, generateReverseRouter, generateRefReverseRouter, namespaceReverseRouter) map {
  76. (s, cd, sm, ri, grr, grrr, nrr) => RouteFiles(s, Seq(cd), sm, ri, grr, grrr, nrr)
  77. },
  78. // Adds config directory's source files to continuous hot reloading
  79. watchSources <+= confDirectory map { all => all },
  80. sourceGenerators in Compile <+= (state, unmanagedSourceDirectories in Compile, sourceManaged in Compile, templatesTypes, templatesImport, excludeFilter in unmanagedSources) map ScalaTemplates,
  81. // Adds app directory's source files to continuous hot reloading
  82. watchSources <++= baseDirectory map { path => ((path / "app") ** "*" --- (path / "app/assets") ** "*").get },
  83. commands ++= Seq(shCommand, playStartCommand, h2Command, classpathCommand, licenseCommand, computeDependenciesCommand),
  84. // THE `in Compile` IS IMPORTANT!
  85. run in Compile <<= playRunSetting,
  86. shellPrompt := playPrompt,
  87. copyResources in Compile <<= (copyResources in Compile, playCopyAssets) map { (r, pr) => r ++ pr },
  88. mainClass in (Compile, run) := Some("play.core.server.NettyServer"),
  89. compile in (Compile) <<= PostCompile(scope = Compile),
  90. compile in Test <<= PostCompile(Test),
  91. computeDependencies <<= computeDependenciesTask,
  92. playVersion := play.core.PlayVersion.current,
  93. // all dependencies from outside the project (all dependency jars)
  94. playDependencyClasspath <<= externalDependencyClasspath in Runtime,
  95. // all user classes, in this project and any other subprojects that it depends on
  96. playReloaderClasspath <<= Classpaths.concatDistinct(exportedProducts in Runtime, internalDependencyClasspath in Runtime),
  97. playCommonClassloader <<= playCommonClassloaderTask,
  98. playDependencyClassLoader := createURLClassLoader,
  99. playReloaderClassLoader := createDelegatedResourcesClassLoader,
  100. playCopyAssets <<= playCopyAssetsTask,
  101. playCompileEverything <<= playCompileEverythingTask,
  102. playReload <<= playReloadTask,
  103. sourcePositionMappers += playPositionMapper,
  104. ivyLoggingLevel := UpdateLogging.DownloadOnly,
  105. routesImport := Seq.empty[String],
  106. playMonitoredFiles <<= playMonitoredFilesTask,
  107. playDefaultPort := 9000,
  108. // Default hooks
  109. playOnStarted := Nil,
  110. playOnStopped := Nil,
  111. playRunHooks := Nil,
  112. playRunHooks <++= playOnStarted map { funcs =>
  113. funcs map play.PlayRunHook.makeRunHookFromOnStarted
  114. },
  115. playRunHooks <++= playOnStopped map { funcs =>
  116. funcs map play.PlayRunHook.makeRunHookFromOnStopped
  117. },
  118. playInteractionMode := play.PlayConsoleInteractionMode,
  119. // Assets
  120. playAssetsDirectories := Seq.empty[File],
  121. playExternalAssets := Seq.empty[(File, File => PathFinder, String)],
  122. playAssetsDirectories <+= baseDirectory / "public",
  123. requireJs := Nil,
  124. requireJsFolder := "",
  125. requireJsShim := "",
  126. requireNativePath := None,
  127. buildRequire <<= buildRequireTask,
  128. packageBin in Compile <<= (packageBin in Compile).dependsOn(buildRequire),
  129. resourceGenerators in Compile <+= LessCompiler,
  130. resourceGenerators in Compile <+= CoffeescriptCompiler,
  131. resourceGenerators in Compile <+= JavascriptCompiler(fullCompilerOptions = None),
  132. lessEntryPoints <<= (sourceDirectory in Compile)(base => ((base / "assets" ** "*.less") --- base / "assets" ** "_*")),
  133. coffeescriptEntryPoints <<= (sourceDirectory in Compile)(base => base / "assets" ** "*.coffee"),
  134. javascriptEntryPoints <<= (sourceDirectory in Compile)(base => ((base / "assets" ** "*.js") --- (base / "assets" ** "_*"))),
  135. lessOptions := Seq.empty[String],
  136. coffeescriptOptions := Seq.empty[String],
  137. closureCompilerOptions := Seq.empty[String],
  138. // Settings
  139. devSettings := Nil,
  140. // Templates
  141. templatesImport := defaultTemplatesImport,
  142. templatesTypes := Map(
  143. "html" -> "play.api.templates.HtmlFormat",
  144. "txt" -> "play.api.templates.TxtFormat",
  145. "xml" -> "play.api.templates.XmlFormat",
  146. "js" -> "play.api.templates.JavaScriptFormat"
  147. ),
  148. // Native packaging
  149. sourceDirectory in Universal <<= baseDirectory(_ / "dist"),
  150. mainClass in Compile := Some("play.core.server.NettyServer"),
  151. mappings in Universal <++= (confDirectory) map {
  152. confDirectory: File =>
  153. val confDirectoryLen = confDirectory.getCanonicalPath.length
  154. val pathFinder = confDirectory ** ("*" -- "routes")
  155. pathFinder.get map {
  156. confFile: File =>
  157. confFile -> ("conf/" + confFile.getCanonicalPath.substring(confDirectoryLen))
  158. }
  159. },
  160. mappings in Universal <++= (doc in Compile) map {
  161. docDirectory: File =>
  162. val docDirectoryLen = docDirectory.getCanonicalPath.length
  163. val pathFinder = docDirectory ** "*"
  164. pathFinder.get map {
  165. docFile: File =>
  166. docFile -> ("share/doc/api/" + docFile.getCanonicalPath.substring(docDirectoryLen))
  167. }
  168. },
  169. mappings in Universal <++= (baseDirectory) map {
  170. baseDirectory: File =>
  171. val pathFinder = baseDirectory * "README*"
  172. pathFinder.get map {
  173. readmeFile: File =>
  174. readmeFile -> readmeFile.getName
  175. }
  176. },
  177. // Adds the Play application directory to the command line args passed to Play
  178. bashScriptExtraDefines += "addJava \"-Duser.dir=$(cd \"${app_home}/..\"; pwd -P)\"\n"
  179. )
  180. /**
  181. * Add this to your build.sbt, eg:
  182. *
  183. * {{{
  184. * play.Project.emojiLogs
  185. * }}}
  186. *
  187. * Note that this setting is not supported and may break or be removed or changed at any time.
  188. */
  189. lazy val emojiLogs = logManager ~= { lm =>
  190. new LogManager {
  191. def apply(data: sbt.Settings[Scope], state: State, task: Def.ScopedKey[_], writer: java.io.PrintWriter) = {
  192. val l = lm.apply(data, state, task, writer)
  193. val FailuresErrors = "(?s).*(\\d+) failures?, (\\d+) errors?.*".r
  194. new Logger {
  195. def filter(s: String) = {
  196. val filtered = s.replace("\033[32m+\033[0m", "\u2705 ")
  197. .replace("\033[33mx\033[0m", "\u274C ")
  198. .replace("\033[31m!\033[0m", "\uD83D\uDCA5 ")
  199. filtered match {
  200. case FailuresErrors("0", "0") => filtered + " \uD83D\uDE04"
  201. case FailuresErrors(_, _) => filtered + " \uD83D\uDE22"
  202. case _ => filtered
  203. }
  204. }
  205. def log(level: Level.Value, message: => String) = l.log(level, filter(message))
  206. def success(message: => String) = l.success(message)
  207. def trace(t: => Throwable) = l.trace(t)
  208. override def ansiCodesSupported = l.ansiCodesSupported
  209. }
  210. }
  211. }
  212. }
  213. }