From 74073a0d3b69056e06ea936c9bfbef882b0f8516 Mon Sep 17 00:00:00 2001 From: Alexandru Tache Date: Sun, 6 Apr 2014 18:15:18 +0300 Subject: Added more settings keys: reloadPrefix - Specify the address from which the javascript files should be reloaded javascriptUrlGenerator - Specify a function that transforms the file path of a js file into an url --- Plugin.scala | 9 ++++++++- workbench_template.js | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Plugin.scala b/Plugin.scala index c7d3bf3..d56090f 100644 --- a/Plugin.scala +++ b/Plugin.scala @@ -25,6 +25,8 @@ 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 pubSub = actor(new Actor{ var waitingActor: Option[ActorRef] = None @@ -61,6 +63,8 @@ object Plugin extends sbt.Plugin with SimpleRoutingApp{ val workbenchSettings = Seq( localUrl := ("localhost", 12345), + reloadPrefix := None, + javascriptUrlGenerator := {s => s}, extraLoggers := { val clientLogger = FullLogger{ @@ -86,9 +90,11 @@ object Plugin extends sbt.Plugin with SimpleRoutingApp{ 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) + pubSub ! Json.arr( "run", - "/" + f.head.getAbsolutePath.drop(new File("").getAbsolutePath.length), + "/" + javascriptUrlGenerator.value(fsPath), bootSnippet.value ) f @@ -105,6 +111,7 @@ object Plugin extends sbt.Plugin with SimpleRoutingApp{ ).replace("", localUrl.value._1) .replace("", localUrl.value._2.toString) .replace("", bootSnippet.value) + .replace("", reloadPrefix.value.getOrElse("")) } } ~ getFromDirectory(".") diff --git a/workbench_template.js b/workbench_template.js index f9c016c..ff4c3a4 100644 --- a/workbench_template.js +++ b/workbench_template.js @@ -1,6 +1,7 @@ (function(){ var shadowBody = null var bootSnippet = "" + var reloadPrefix = "" window.onload = function(){ shadowBody = document.body.cloneNode(true) } @@ -41,7 +42,14 @@ if (data[0] == "run"){ var tag = document.createElement("script") var loaded = false - tag.setAttribute("src", "http://:" + data[1]) + var serverPrefix + + if (reloadPrefix === "") + serverPrefix = "http://:" + else + serverPrefix = reloadPrefix + tag.setAttribute("src", serverPrefix + data[1]) + var bootSnippet = data[2] if (bootSnippet){ tag.onreadystatechange = tag.onload = function() { -- cgit v1.2.3 From 51e543105d60537219a7083db4adf2412cf93547 Mon Sep 17 00:00:00 2001 From: Alexandru Tache Date: Mon, 7 Apr 2014 10:51:12 +0300 Subject: Refactored the two setting keys into a task key --- Plugin.scala | 53 ++++++++++++++++++++++++++++++++------------------- 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("", localUrl.value._1) .replace("", localUrl.value._2.toString) .replace("", bootSnippet.value) - .replace("", 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 = "" - var 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://:" - else - serverPrefix = reloadPrefix - tag.setAttribute("src", serverPrefix + data[1]) + tag.setAttribute("src", data[1]) var bootSnippet = data[2] if (bootSnippet){ -- cgit v1.2.3 From 5d68363ac8f7dfe308e638cb7d1bbb610c67dee0 Mon Sep 17 00:00:00 2001 From: Alexandru Tache Date: Tue, 8 Apr 2014 20:00:47 +0300 Subject: Replaced map with foreach --- Plugin.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugin.scala b/Plugin.scala index 9d7012a..a363b99 100644 --- a/Plugin.scala +++ b/Plugin.scala @@ -64,7 +64,7 @@ object Plugin extends sbt.Plugin with SimpleRoutingApp{ localUrl := ("localhost", 12345), updatedJS := { var files: List[String] = Nil - ((crossTarget in Compile).value * "*.js").get.map { + ((crossTarget in Compile).value * "*.js").get.foreach { (x: File) => streams.value.log.info("workbench: Checking " + x.getName) FileFunction.cached(streams.value.cacheDirectory / x.getName, FilesInfo.lastModified, FilesInfo.lastModified) { @@ -104,7 +104,7 @@ object Plugin extends sbt.Plugin with SimpleRoutingApp{ if (changed.length > 0) { pubSub ! Json.arr("clear") - changed.map { + changed.foreach { path => streams.value.log.info("workbench: Refreshing " + path) pubSub ! Json.arr( -- cgit v1.2.3