PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
18package org.fusesource.scalate.sbt
19
20import _root_.sbt._
21
22import java.io.File
23import java.net.{URL, URLClassLoader}
24import java.{util => ju}
25import scala.collection.jcl
26import scala.collection.jcl.Conversions._
27
28/**
29 * Base trait for Scalate tool support.
30 */
31trait ScalateProject extends BasicScalaProject with MavenStyleScalaPaths {
32  /**
33   * The name of the bootstrap class.  If None, the tool will attempt to load
34   * a default class.
35   */
36  def scalateBootClassName: Option[String] = None
37
38  /**
39   * The directories to search for Scalate templates.
40   */
41  def scalateSources: List[Path] = List(mainResourcesPath)
42
43  /**
44   * Runs a block of code with the Scalate classpath as the context class
45   * loader.  The Scalate classpath is the [[runClassPath]] plus the
46   * [[buildScalaInstance]]'s jars.
47   */
48  protected def withScalateClassLoader[A](f: ClassLoader => A): A = {
49    val oldLoader = Thread.currentThread.getContextClassLoader
50    val sitegenPath = buildScalaInstance.jars.foldLeft(runClasspath) { 
51       (cp, jar) => cp +++ Path.fromFile(jar)
52    }
53    val loader = ClasspathUtilities.toLoader(sitegenPath)
54    Thread.currentThread.setContextClassLoader(loader)
55    try {
56      f(loader)
57    } finally {
58      Thread.currentThread.setContextClassLoader(oldLoader)
59    }
60  }
61}
62
63/**
64 * Base trait for Scalate tool support in a web project.
65 */
66trait ScalateWebProject extends ScalateProject with MavenStyleWebScalaPaths {
67  override def scalateSources = webappPath :: super.scalateSources
68}