PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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