From a57376f40b539260f1d13d3278f10e16a4a2fbe1 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Wed, 24 Sep 2014 20:19:19 -0700 Subject: wip --- src/main/scala/workbench/Plugin.scala | 26 +++++++++++++++++++++++++- src/main/scala/workbench/Server.scala | 4 +++- 2 files changed, 28 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/scala/workbench/Plugin.scala b/src/main/scala/workbench/Plugin.scala index 8e122fe..85b8f39 100644 --- a/src/main/scala/workbench/Plugin.scala +++ b/src/main/scala/workbench/Plugin.scala @@ -7,7 +7,8 @@ import scala.concurrent.ExecutionContext.Implicits.global object Plugin extends sbt.Plugin { val refreshBrowsers = taskKey[Unit]("Sends a message to all connected web pages asking them to refresh the page") - val updateBrowsers = taskKey[Unit]("partially resets some of the stuff in the browser") + val updateBrowsers = taskKey[Unit]("Partially resets some of the stuff in the browser") + val spliceBrowsers = taskKey[Unit]("Attempts to do a live update of the code running in the browser while maintaining state") val localUrl = settingKey[(String, Int)]("localUrl") private[this] val server = settingKey[Server]("local websocket server") @@ -65,6 +66,29 @@ object Plugin extends sbt.Plugin { } } }, + spliceBrowsers := { + val changed = updatedJS.value + // There is no point in clearing the browser if no js files have changed. + if (changed.length > 0) { + for{ + path <- changed + if !path.endsWith(".js.js") + }{ + + streams.value.log.info("workbench: Splicing " + path) + val prefix = "http://localhost:12345/" + var s = IO.read(new sbt.File(path.drop(prefix.length))) + + s = s.replace("\nvar ScalaJS = ", "\nvar ScalaJS = ScalaJS || ") + s = s.replaceAll("\n(ScalaJS\\.c\\.[a-zA-Z_$0-9]+\\.prototype) = ", "/*X*/\n$1 = $1 || ") + for(char <- Seq("d", "c", "h", "i", "n", "m")){ + s = s.replaceAll("\n(ScalaJS\\." + char + "\\.[a-zA-Z_$0-9]+) = ", "\n$1 = $1 || ") + } + IO.write(new sbt.File(path.drop(prefix.length) + ".js"), s.getBytes) + server.value.Wire[Api].run(path + ".js", None).call() + } + } + }, server := new Server(localUrl.value._1, localUrl.value._2, bootSnippet.value), (onUnload in Global) := { (onUnload in Global).value.compose{ state => server.value.kill() diff --git a/src/main/scala/workbench/Server.scala b/src/main/scala/workbench/Server.scala index 4d37073..8d05037 100644 --- a/src/main/scala/workbench/Server.scala +++ b/src/main/scala/workbench/Server.scala @@ -3,11 +3,12 @@ package com.lihaoyi.workbench import akka.actor.{ActorRef, Actor, ActorSystem} import com.typesafe.config.ConfigFactory import sbt.IO +import spray.httpx.encoding.Gzip import spray.routing.SimpleRoutingApp import akka.actor.ActorDSL._ import upickle.{Reader, Writer, Js} -import spray.http.{AllOrigins, HttpResponse} +import spray.http.{HttpEntity, AllOrigins, HttpResponse} import spray.http.HttpHeaders.`Access-Control-Allow-Origin` import concurrent.duration._ import scala.concurrent.Future @@ -94,6 +95,7 @@ class Server(url: String, port: Int, bootSnippet: String) extends SimpleRoutingA } } ~ getFromDirectory(".") + } ~ post { path("notifications") { ctx => -- cgit v1.2.3