From 12d6290b225dbec02bc04b13d14a2a29132e7da2 Mon Sep 17 00:00:00 2001 From: lihaoyi Date: Wed, 13 Nov 2013 04:14:07 -0800 Subject: added readme, caching to prevent unnecessary work, copy-js-to-target behavior --- .../src/main/scala/scala/js/resource/Plugin.scala | 35 -------------- .../scala/scala/scalajs/js/resource/Plugin.scala | 54 ++++++++++++++++++++++ readme.md | 33 +++++++++++++ runtime/src/main/scala/scala/js/Resource.scala | 30 ------------ .../src/main/scala/scala/scalajs/js/Resource.scala | 30 ++++++++++++ 5 files changed, 117 insertions(+), 65 deletions(-) delete mode 100644 plugin/src/main/scala/scala/js/resource/Plugin.scala create mode 100644 plugin/src/main/scala/scala/scalajs/js/resource/Plugin.scala create mode 100644 readme.md delete mode 100644 runtime/src/main/scala/scala/js/Resource.scala create mode 100644 runtime/src/main/scala/scala/scalajs/js/Resource.scala 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/js/Resource.scala deleted file mode 100644 index 632ebb9..0000000 --- a/runtime/src/main/scala/scala/js/Resource.scala +++ /dev/null @@ -1,30 +0,0 @@ -package scala.scalajs -package js - - -object Resource { - println("Resource") - val fileDict = { - val fileDict = js.Dynamic.global.ScalaJS.resources.asInstanceOf[js.Object] - for(key <- js.Object.keys(fileDict)){ - val data = fileDict.asInstanceOf[js.Dictionary](key).asInstanceOf[String] - fileDict.asInstanceOf[js.Dictionary](key) = new Resource(data).asInstanceOf[js.Any] - } - fileDict - } - - println("Resource Initialized") - def apply(path: String) = { - js.Dynamic.global - .ScalaJS - .resources - .asInstanceOf[js.Dictionary] - .apply(path) - .asInstanceOf[Resource] - } - def create(value: String) = new Resource(value) -} - -class Resource(base64: String){ - lazy val string = js.Dynamic.global.atob(base64).asInstanceOf[js.String] -} \ No newline at end of file diff --git a/runtime/src/main/scala/scala/scalajs/js/Resource.scala b/runtime/src/main/scala/scala/scalajs/js/Resource.scala new file mode 100644 index 0000000..632ebb9 --- /dev/null +++ b/runtime/src/main/scala/scala/scalajs/js/Resource.scala @@ -0,0 +1,30 @@ +package scala.scalajs +package js + + +object Resource { + println("Resource") + val fileDict = { + val fileDict = js.Dynamic.global.ScalaJS.resources.asInstanceOf[js.Object] + for(key <- js.Object.keys(fileDict)){ + val data = fileDict.asInstanceOf[js.Dictionary](key).asInstanceOf[String] + fileDict.asInstanceOf[js.Dictionary](key) = new Resource(data).asInstanceOf[js.Any] + } + fileDict + } + + println("Resource Initialized") + def apply(path: String) = { + js.Dynamic.global + .ScalaJS + .resources + .asInstanceOf[js.Dictionary] + .apply(path) + .asInstanceOf[Resource] + } + def create(value: String) = new Resource(value) +} + +class Resource(base64: String){ + lazy val string = js.Dynamic.global.atob(base64).asInstanceOf[js.String] +} \ No newline at end of file -- cgit v1.2.3