/sbt-scalate-plugin/src/main/scala/org/fusesource/scalate/sbt/ScalateProject.scala

http://github.com/scalate/scalate · Scala · 68 lines · 27 code · 6 blank · 35 comment · 0 complexity · 6495409d5b0cda2e24302318482b1ea6 MD5 · raw file

  1. /**
  2. * Copyright (C) 2009-2011 the original author or authors.
  3. * See the notice.md file distributed with this work for additional
  4. * information regarding copyright ownership.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.fusesource.scalate.sbt
  19. import _root_.sbt._
  20. import java.io.File
  21. import java.net.{URL, URLClassLoader}
  22. import java.{util => ju}
  23. import scala.collection.jcl
  24. import scala.collection.jcl.Conversions._
  25. /**
  26. * Base trait for Scalate tool support.
  27. */
  28. trait ScalateProject extends BasicScalaProject with MavenStyleScalaPaths {
  29. /**
  30. * The name of the bootstrap class. If None, the tool will attempt to load
  31. * a default class.
  32. */
  33. def scalateBootClassName: Option[String] = None
  34. /**
  35. * The directories to search for Scalate templates.
  36. */
  37. def scalateSources: List[Path] = List(mainResourcesPath)
  38. /**
  39. * Runs a block of code with the Scalate classpath as the context class
  40. * loader. The Scalate classpath is the [[runClassPath]] plus the
  41. * [[buildScalaInstance]]'s jars.
  42. */
  43. protected def withScalateClassLoader[A](f: ClassLoader => A): A = {
  44. val oldLoader = Thread.currentThread.getContextClassLoader
  45. val sitegenPath = buildScalaInstance.jars.foldLeft(runClasspath) {
  46. (cp, jar) => cp +++ Path.fromFile(jar)
  47. }
  48. val loader = ClasspathUtilities.toLoader(sitegenPath)
  49. Thread.currentThread.setContextClassLoader(loader)
  50. try {
  51. f(loader)
  52. } finally {
  53. Thread.currentThread.setContextClassLoader(oldLoader)
  54. }
  55. }
  56. }
  57. /**
  58. * Base trait for Scalate tool support in a web project.
  59. */
  60. trait ScalateWebProject extends ScalateProject with MavenStyleWebScalaPaths {
  61. override def scalateSources = webappPath :: super.scalateSources
  62. }