diff options
author | Alexandru Tache <hadesgames@gmail.com> | 2014-04-07 10:51:12 +0300 |
---|---|---|
committer | Alexandru Tache <hadesgames@gmail.com> | 2014-04-07 10:51:12 +0300 |
commit | 51e543105d60537219a7083db4adf2412cf93547 (patch) | |
tree | 7fcecc64e8891d9d14f57a4fa49d730d6b6ace4f | |
parent | 74073a0d3b69056e06ea936c9bfbef882b0f8516 (diff) | |
download | workbench-51e543105d60537219a7083db4adf2412cf93547.tar.gz workbench-51e543105d60537219a7083db4adf2412cf93547.tar.bz2 workbench-51e543105d60537219a7083db4adf2412cf93547.zip |
Refactored the two setting keys into a task key
-rw-r--r-- | Plugin.scala | 53 | ||||
-rw-r--r-- | workbench_template.js | 8 |
2 files changed, 34 insertions, 27 deletions
diff --git a/Plugin.scala b/Plugin.scala index d56090f..9d7012a 100644 --- a/Plugin.scala +++ b/Plugin.scala @@ -25,8 +25,7 @@ object Plugin extends sbt.Plugin with SimpleRoutingApp{ val localUrl = settingKey[(String, Int)]("localUrl") private[this] val routes = settingKey[Unit]("local websocket server") val bootSnippet = settingKey[String]("piece of javascript to make things happen") - val reloadPrefix = settingKey[Option[String]]("Use this if you want the javascript to be reloaded from a specific address") - val javascriptUrlGenerator = settingKey[String => String]("Transform the javascript filepath to a server url") + val updatedJS = taskKey[List[String]]("Provides the addresses of the JS files that have changed") val pubSub = actor(new Actor{ var waitingActor: Option[ActorRef] = None @@ -63,9 +62,25 @@ object Plugin extends sbt.Plugin with SimpleRoutingApp{ val workbenchSettings = Seq( localUrl := ("localhost", 12345), - reloadPrefix := None, - javascriptUrlGenerator := {s => s}, - + updatedJS := { + var files: List[String] = Nil + ((crossTarget in Compile).value * "*.js").get.map { + (x: File) => + streams.value.log.info("workbench: Checking " + x.getName) + FileFunction.cached(streams.value.cacheDirectory / x.getName, FilesInfo.lastModified, FilesInfo.lastModified) { + (f: Set[File]) => + val fsPath = f.head.getAbsolutePath.drop(new File("").getAbsolutePath.length) + files = fsPath :: files + f + }(Set(x)) + } + files + }, + updatedJS <<= (updatedJS, localUrl) map { (paths, localUrl) => + paths.map { path => + s"http://${localUrl._1}:${localUrl._2}$path" + } + }, extraLoggers := { val clientLogger = FullLogger{ new Logger { @@ -84,21 +99,20 @@ object Plugin extends sbt.Plugin with SimpleRoutingApp{ pubSub ! Json.arr("reload") }, updateBrowsers := { - pubSub ! Json.arr("clear") - ((crossTarget in Compile).value * "*.js").get.map{ (x: File) => - streams.value.log.info("workbench: Checking " + x.getName) - FileFunction.cached(streams.value.cacheDirectory / x.getName, FilesInfo.lastModified, FilesInfo.lastModified){ (f: Set[File]) => - streams.value.log.info("workbench: Refreshing " + x.getName) - - val fsPath = f.head.getAbsolutePath.drop(new File("").getAbsolutePath.length) + val changed = updatedJS.value + // There is no point in clearing the browser if no js files have changed. + if (changed.length > 0) { + pubSub ! Json.arr("clear") - pubSub ! Json.arr( - "run", - "/" + javascriptUrlGenerator.value(fsPath), - bootSnippet.value - ) - f - }(Set(x)) + changed.map { + path => + streams.value.log.info("workbench: Refreshing " + path) + pubSub ! Json.arr( + "run", + path, + bootSnippet.value + ) + } } }, routes := startServer(localUrl.value._1, localUrl.value._2){ @@ -111,7 +125,6 @@ object Plugin extends sbt.Plugin with SimpleRoutingApp{ ).replace("<host>", localUrl.value._1) .replace("<port>", localUrl.value._2.toString) .replace("<bootSnippet>", bootSnippet.value) - .replace("<reloadPrefix>", reloadPrefix.value.getOrElse("")) } } ~ getFromDirectory(".") diff --git a/workbench_template.js b/workbench_template.js index ff4c3a4..0bbe8b9 100644 --- a/workbench_template.js +++ b/workbench_template.js @@ -1,7 +1,6 @@ (function(){ var shadowBody = null var bootSnippet = "<bootSnippet>" - var reloadPrefix = "<reloadPrefix>" window.onload = function(){ shadowBody = document.body.cloneNode(true) } @@ -42,13 +41,8 @@ if (data[0] == "run"){ var tag = document.createElement("script") var loaded = false - var serverPrefix - if (reloadPrefix === "") - serverPrefix = "http://<host>:<port>" - else - serverPrefix = reloadPrefix - tag.setAttribute("src", serverPrefix + data[1]) + tag.setAttribute("src", data[1]) var bootSnippet = data[2] if (bootSnippet){ |