summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlihaoyi <haoyi.sg@gmail.com>2013-11-13 04:14:07 -0800
committerlihaoyi <haoyi.sg@gmail.com>2013-11-13 04:14:07 -0800
commit12d6290b225dbec02bc04b13d14a2a29132e7da2 (patch)
tree07aff99746a75fd8b69c2b244dae33a6511a98c5
parent332e7cde8d103b791e5017b202cd325bcd7d7a1f (diff)
downloadworkbench-12d6290b225dbec02bc04b13d14a2a29132e7da2.tar.gz
workbench-12d6290b225dbec02bc04b13d14a2a29132e7da2.tar.bz2
workbench-12d6290b225dbec02bc04b13d14a2a29132e7da2.zip
added readme, caching to prevent unnecessary work, copy-js-to-target behavior
-rw-r--r--plugin/src/main/scala/scala/js/resource/Plugin.scala35
-rw-r--r--plugin/src/main/scala/scala/scalajs/js/resource/Plugin.scala54
-rw-r--r--readme.md33
-rw-r--r--runtime/src/main/scala/scala/scalajs/js/Resource.scala (renamed from runtime/src/main/scala/scala/js/Resource.scala)0
4 files changed, 87 insertions, 35 deletions
diff --git a/plugin/src/main/scala/scala/js/resource/Plugin.scala b/plugin/src/main/scala/scala/js/resource/Plugin.scala
deleted file mode 100644
index f7af5e7..0000000
--- a/plugin/src/main/scala/scala/js/resource/Plugin.scala
+++ /dev/null
@@ -1,35 +0,0 @@
-package scala.js.resource
-
-import sbt._
-import Keys._
-import scala.scalajs.sbtplugin.ScalaJSPlugin.ScalaJSKeys._
-import org.apache.commons.codec.binary.Base64
-
-
-object Plugin extends sbt.Plugin {
-
- val resourceSettings = Seq(
- watchSources := {
- watchSources.value ++ (resources in Compile).value
- },
- packageJS := {
-
- val fileData = for{
- resourceRoot <- (resources in Compile).value
- (file, path) <- Path.allSubpaths(resourceRoot)
- } yield {
- val b64 = Base64.encodeBase64String(IO.readBytes(file))
- path -> b64
- }
-
- val bundle = crossTarget.value / "resources.js"
- val fileLines = for((path, data) <- fileData) yield {
- " \"" + path + "\": \"" + data + "\""
- }
-
- IO.write(bundle, "\nScalaJS.resources = {\n" + fileLines.mkString(",\n") + "\n}" )
-
- (packageJS in Compile).value :+ bundle
- }
- )
-}
diff --git a/plugin/src/main/scala/scala/scalajs/js/resource/Plugin.scala b/plugin/src/main/scala/scala/scalajs/js/resource/Plugin.scala
new file mode 100644
index 0000000..c8960c8
--- /dev/null
+++ b/plugin/src/main/scala/scala/scalajs/js/resource/Plugin.scala
@@ -0,0 +1,54 @@
+package scala.scalajs.js.resource
+
+import sbt._
+import Keys._
+import scala.scalajs.sbtplugin.ScalaJSPlugin.ScalaJSKeys._
+import org.apache.commons.codec.binary.Base64
+
+
+object Plugin extends sbt.Plugin {
+
+ val resourceSettings = Seq(
+ watchSources := {
+ watchSources.value ++ (resources in Compile).value ++ Path.allSubpaths((sourceDirectory in Compile).value / "js").map(_._1).toSeq
+ },
+ packageJS := {
+ val bundledJSResults: Set[sbt.File] = FileFunction.cached(
+ cacheDirectory.value,
+ FilesInfo.lastModified,
+ FilesInfo.exists
+ ){(inFiles: Set[File]) =>
+ val pathMap = Path.relativeTo((resources in Compile).value)
+ val bundle = crossTarget.value / "resources.js"
+ val fileLines = for(file <- inFiles) yield {
+ val b64 = Base64.encodeBase64String(IO.readBytes(file))
+ " \"" + pathMap(file).get + "\": \"" + b64 + "\""
+ }
+ IO.write(bundle, "\nScalaJS.resources = {\n" + fileLines.mkString(",\n") + "\n}" )
+ Set(bundle)
+ }(
+ for{
+ (resourceRoot: File) <- (resources in Compile).value.toSet
+ (file, path) <- Path.allSubpaths(resourceRoot)
+ } yield file
+ )
+
+ val copiedJSFiles = FileFunction.cached(
+ cacheDirectory.value,
+ FilesInfo.lastModified,
+ FilesInfo.exists
+ ){(inFiles: Set[File]) =>
+ val pathMap = Path.relativeTo((sourceDirectory in Compile).value / "js")
+ IO.copy(inFiles.map{f => f -> crossTarget.value / pathMap(f).get})
+ }(
+ for{
+ (file, path) <- Path.allSubpaths((sourceDirectory in Compile).value / "js").toSet
+ } yield file
+
+ )
+ val normalResults: Seq[sbt.File] = (packageJS in Compile).value
+
+ normalResults ++ bundledJSResults ++ copiedJSFiles
+ }
+ )
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..e78c040
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,33 @@
+scala-js-resource
+-----------------
+
+Provides common functionality when working with [scala-js](https://github.com/lampepfl/scala-js) applications. Enhances the sbt `packageJS` command to:
+
+- Copies files from your `/src/js` folder directly into the output path
+- Bundles up files from your `/src/resource` folder into a single `resource.js` file, and makes them available through the `scala.scalajs.js.Resource` object. For example, calling `Resource("cow.txt").string` will provide you the string contents of `/src/resource/cow.txt`.
+
+Currently really rough around the edges, but it already does much of what I want it to do. Pull requests welcome!
+
+License
+-------
+The MIT License (MIT)
+
+Copyright (c) 2013 Li Haoyi
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/runtime/src/main/scala/scala/js/Resource.scala b/runtime/src/main/scala/scala/scalajs/js/Resource.scala
index 632ebb9..632ebb9 100644
--- a/runtime/src/main/scala/scala/js/Resource.scala
+++ b/runtime/src/main/scala/scala/scalajs/js/Resource.scala