Skip to content

Commit

Permalink
Factored out javascript settings
Browse files Browse the repository at this point in the history
Made JavaScript sources based on resources, and added jsSources to
resources.
  • Loading branch information
jroper committed Oct 28, 2013
1 parent d9bb9ed commit ed1c421
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/com/typesafe/webdriver/LocalBrowser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LocalBrowser(sessionProps: Props, maybeArgs: Option[Seq[String]]) extends

when(Started) {
case Event(CreateSession, _) =>
val session = context.actorOf(sessionProps, "session")
val session = context.actorOf(sessionProps)
session ! Session.Connect
sender ! session
stay()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.typesafe.webdriver.sbt

import sbt._
import sbt.Keys._

/**
* Adds JavaScript settings to SBT
*/
object JavaScriptSettings extends Plugin {

val JavaScript = config("js")
val JavaScriptTest = config("js-test")
val jsSource = SettingKey[File]("js-source", "The main source directory for JavaScript.")

private def filterSources(sources: Seq[File], includeFilter: FileFilter, excludeFilter: FileFilter): Seq[File] = {
val filter = includeFilter -- excludeFilter
sources.filter(filter.accept)
}

private def locateSources(sourceDirectory: File, includeFilter: FileFilter, excludeFilter: FileFilter): Seq[File] =
(sourceDirectory ** (includeFilter -- excludeFilter)).get

override def projectSettings = Seq(
includeFilter in JavaScript := GlobFilter("*.js"),
includeFilter in JavaScriptTest := GlobFilter("*Test.js") | GlobFilter("*Spec.js"),

// jsSource is just a directory that allows you to layout your project nicely, anything in it gets added to the
// resources task.
jsSource in Compile := (sourceDirectory in Compile).value / "js",
jsSource in Test := (sourceDirectory in Test).value / "js",
unmanagedResources in Compile <++= (jsSource in Compile, includeFilter in (Compile, unmanagedResources), excludeFilter in (Compile, unmanagedResources)) map locateSources,
unmanagedResources in Test <++= (jsSource in Test, includeFilter in (Test, unmanagedResources), excludeFilter in (Test, unmanagedResources)) map locateSources,

// The actual javascript sources come from whatever is in resources
unmanagedSources in JavaScript <<= (unmanagedResources in Compile, includeFilter in JavaScript, excludeFilter in JavaScript) map filterSources,
managedSources in JavaScript <<= (managedResources in Compile, includeFilter in JavaScript, excludeFilter in JavaScript) map filterSources,
unmanagedSources in JavaScriptTest <<= (unmanagedResources in Test, includeFilter in JavaScriptTest, excludeFilter in JavaScriptTest) map filterSources,
managedSources in JavaScriptTest <<= (managedResources in Test, includeFilter in JavaScriptTest, excludeFilter in JavaScriptTest) map filterSources,
sources in JavaScript <<= (unmanagedSources in JavaScript, managedSources in JavaScript) map(_ ++ _),
sources in JavaScriptTest <<= (unmanagedSources in JavaScriptTest, managedSources in JavaScriptTest) map(_ ++ _)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,22 @@ abstract class WebDriverPlugin extends sbt.Plugin {

object WebDriverKeys {
val webBrowser = TaskKey[ActorRef]("js-web-browser", "An actor representing the webdriver browser.")
val JavaScript = config("js")
val JavaScriptTest = config("js-test")
val jsSource = SettingKey[File]("js-source", "The main source directory for JavaScript.")
val parallelism = SettingKey[Int]("js-parallelism", "The number of parallel tasks for the webdriver host. Defaults to the # of available processors + 1 to keep things busy.")
val reporter = TaskKey[LoggerReporter]("js-reporter", "The reporter to use for conveying processing results.")
}

import WebDriverKeys._

def locateSources(sourceDirectory: File, includeFilter: FileFilter, excludeFilter: FileFilter): Seq[File] =
(sourceDirectory * (includeFilter -- excludeFilter)).get

def webDriverSettings = Seq(
webBrowser <<= (state) map (_.get(browserAttrKey).get),
jsSource in JavaScript := (sourceDirectory in Compile).value / "js",
jsSource in JavaScriptTest := (sourceDirectory in Test).value / "js",
parallelism := java.lang.Runtime.getRuntime.availableProcessors() + 1,
reporter := new LoggerReporter(5, streams.value.log),
includeFilter in JavaScript := GlobFilter("*.js"),
includeFilter in JavaScriptTest := GlobFilter("*Test.js") | GlobFilter("*Spec.js"),
sources in JavaScript <<= (jsSource in JavaScript, includeFilter in JavaScript, excludeFilter in JavaScript) map (locateSources),
sources in JavaScriptTest <<= (jsSource in JavaScriptTest, includeFilter in JavaScriptTest, excludeFilter in JavaScriptTest) map (locateSources)
)


implicit val system = withActorClassloader(ActorSystem("webdriver-system"))
implicit val timeout = Timeout(5.seconds)
implicit val webDriverSystem = withActorClassloader(ActorSystem("webdriver-system"))
implicit val webDriverTimeout = Timeout(5.seconds)

private val browserAttrKey = AttributeKey[ActorRef]("web-browser")
private val browserOwnerAttrKey = AttributeKey[WebDriverPlugin]("web-browser-owner")

private def load(state: State): State = {
state.get(browserOwnerAttrKey) match {
case None => {
val browser = system.actorOf(PhantomJs.props(), "localBrowser")
val browser = webDriverSystem.actorOf(PhantomJs.props(), "localBrowser")
browser ! LocalBrowser.Startup
val newState = state.put(browserAttrKey, browser).put(browserOwnerAttrKey, this)
newState.addExitHook(unload(newState))
Expand Down Expand Up @@ -78,7 +59,10 @@ abstract class WebDriverPlugin extends sbt.Plugin {

override val globalSettings: Seq[Setting[_]] = Seq(
onLoad in Global := (onLoad in Global).value andThen (load),
onUnload in Global := (onUnload in Global).value andThen (unload)
onUnload in Global := (onUnload in Global).value andThen (unload),
webBrowser <<= (state) map (_.get(browserAttrKey).get),
parallelism := java.lang.Runtime.getRuntime.availableProcessors() + 1,
reporter := new LoggerReporter(5, streams.value.log)
)

/*
Expand Down

0 comments on commit ed1c421

Please sign in to comment.