PageRenderTime 65ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

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

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