From ebab11e226b2f281967515e00ff8a83560f2719f Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Wed, 3 Oct 2018 10:27:50 -0400 Subject: [PATCH 1/4] Experiment with using transit to load WISH builtin sources Refs #9 --- project.clj | 1 + src/cljs/wish/providers/wish.cljs | 4 +++- src/cljs/wish/sources.cljs | 27 +++++++++++++++++++-------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/project.clj b/project.clj index 8ba1074d..2c2b4674 100644 --- a/project.clj +++ b/project.clj @@ -12,6 +12,7 @@ [cljs-ajax "0.7.4"] [com.cemerick/url "0.1.1"] [alandipert/storage-atom "2.0.1"] + [com.cognitect/transit-cljs "0.8.256"] [cljsjs/react-virtualized "9.18.5-1"] [cljsjs/react-swipeable-views "0.12.18-0"] diff --git a/src/cljs/wish/providers/wish.cljs b/src/cljs/wish/providers/wish.cljs index 6591f7e9..7d8109d0 100644 --- a/src/cljs/wish/providers/wish.cljs +++ b/src/cljs/wish/providers/wish.cljs @@ -14,7 +14,9 @@ (def ^:private builtin-sources {"dnd5e-srd" {:name "D&D 5e System Reference Document" - :path "/dnd5e.edn.json"}}) + ;; :path "/dnd5e.edn.json" + :path "/dnd5e.transit.json" + }}) (deftype WishProvider [] IProvider diff --git a/src/cljs/wish/sources.cljs b/src/cljs/wish/sources.cljs index e16d8589..58515501 100644 --- a/src/cljs/wish/sources.cljs +++ b/src/cljs/wish/sources.cljs @@ -6,6 +6,7 @@ (:require [clojure.core.async :as async :refer [alts! DataSource id - (->> directives + (->> raw + (t/read (t/reader :json)) (compile-directives) - (sheets/post-compile kind)))))) + (sheets/post-compile kind)))) + + (loop [reader (string-push-back-reader raw) + directives []] + (if-let [d (edn/read reader)] + ; keep loading directives + (recur reader (conj directives d)) + + (->DataSource + id + (->> directives + (compile-directives) + (sheets/post-compile kind))))))) (defn- load-source! "Returns a channel that signals with [err] or [nil source] when done" From 729414fa4bf390b699656f064d37a76fdb59c34f Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Mon, 8 Oct 2018 17:55:03 -0400 Subject: [PATCH 2/4] Use wish-compiler to compile transit Data Sources Refs #9 Also removes all the cruft from the travis config to build and use planck for this task --- .gitignore | 1 + .travis.yml | 14 -------------- scripts/compile-builtin-sources | 2 +- scripts/wish-compiler | 13 +++++++++++++ 4 files changed, 15 insertions(+), 15 deletions(-) create mode 100755 scripts/wish-compiler diff --git a/.gitignore b/.gitignore index f7f041cc..5a17dd2c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,6 @@ out .lein-failures .cljs_nashorn_repl .repl +.bin nashorn_code_cache .planck_cache diff --git a/.travis.yml b/.travis.yml index 76dfd08a..7a3e99d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,20 +7,6 @@ addons: chrome: stable before_script: - # plk depends on clojure - - curl -O https://download.clojure.org/install/linux-install-1.9.0.381.sh - - chmod +x linux-install-1.9.0.381.sh - - sudo ./linux-install-1.9.0.381.sh - - # build planck from scratch since we need the latest version - - sudo apt-get install javascriptcoregtk-3.0 libglib2.0-dev libzip-dev libcurl4-gnutls-dev libicu-dev - - git clone --depth 1 https://github.com/planck-repl/planck.git - - (cd planck && script/build --fast && sudo script/install) - - planck --version - - # set up the plk tool - - export PATH=$PATH:$PWD/planck/planck-sh/ - # set the version variable - export WISH_VERSION=$(git rev-parse --short HEAD) diff --git a/scripts/compile-builtin-sources b/scripts/compile-builtin-sources index b71f627b..ae21d120 100755 --- a/scripts/compile-builtin-sources +++ b/scripts/compile-builtin-sources @@ -17,5 +17,5 @@ fi for s in "${sources[@]}"; do echo "Compiling $s" - scripts/compile-source.cljs "$s" + scripts/wish-compiler "resources/sources/$s" "resources/public/sources/$s.transit.json" done diff --git a/scripts/wish-compiler b/scripts/wish-compiler new file mode 100755 index 00000000..75c62e8c --- /dev/null +++ b/scripts/wish-compiler @@ -0,0 +1,13 @@ +#!/bin/bash + +# auto-download latest release bin +if ! [ -d ".bin" ] || ! [ -f ".bin/wish-compiler" ]; then + mkdir .bin + url=$(curl -s https://api.github.com/repos/dhleong/wish-compiler/releases/latest \ + | grep "browser_download_url" \ + | cut -d '"' -f 4) + curl $url -Lo .bin/wish-compiler + chmod +x .bin/wish-compiler +fi + +.bin/wish-compiler "$@" From c2a90ca8e250bc9062aaa0653f24d7d936eaa239 Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Mon, 8 Oct 2018 19:34:35 -0400 Subject: [PATCH 3/4] Use a very simple heuristic to choose transit vs edn for data sources Refs #9 --- src/cljs/wish/sources.cljs | 47 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/cljs/wish/sources.cljs b/src/cljs/wish/sources.cljs index 58515501..74d67c10 100644 --- a/src/cljs/wish/sources.cljs +++ b/src/cljs/wish/sources.cljs @@ -17,29 +17,34 @@ ; cache of *compiled* sources by id (defonce ^:private loaded-sources (atom {})) +(defn- read-transit-directives [raw] + (t/read (t/reader :json) raw)) + +(defn- read-edn-directives [raw] + (loop [reader (string-push-back-reader raw) + directives []] + (if-let [d (edn/read reader)] + ; keep loading directives + (recur reader (conj directives d)) + + ; done! + directives))) + (defn- compile-raw-source [{:keys [kind] :as sheet} id raw] - (if (= "wish" (namespace id)) - (do - (log/info "Compile transit!") - (->DataSource - id - (->> raw - (t/read (t/reader :json)) - (compile-directives) - (sheets/post-compile kind)))) - - (loop [reader (string-push-back-reader raw) - directives []] - (if-let [d (edn/read reader)] - ; keep loading directives - (recur reader (conj directives d)) - - (->DataSource - id - (->> directives - (compile-directives) - (sheets/post-compile kind))))))) + (let [directives (if (= (subs raw 0 2) "[[") + (do + (log "Read transit for " id) + (read-transit-directives raw)) + + (do + (log "Read edn for " id) + (read-edn-directives raw)))] + (->DataSource + id + (->> directives + (compile-directives) + (sheets/post-compile kind))))) (defn- load-source! "Returns a channel that signals with [err] or [nil source] when done" From cb6434a84c07e88bec60500f705b42c1259c92c9 Mon Sep 17 00:00:00 2001 From: Daniel Leong Date: Mon, 8 Oct 2018 21:44:02 -0400 Subject: [PATCH 4/4] Add more logging to scripts/wish-compiler --- scripts/wish-compiler | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scripts/wish-compiler b/scripts/wish-compiler index 75c62e8c..288fd034 100755 --- a/scripts/wish-compiler +++ b/scripts/wish-compiler @@ -2,11 +2,20 @@ # auto-download latest release bin if ! [ -d ".bin" ] || ! [ -f ".bin/wish-compiler" ]; then - mkdir .bin - url=$(curl -s https://api.github.com/repos/dhleong/wish-compiler/releases/latest \ + mkdir .bin 2> /dev/null + response=$(curl -s https://api.github.com/repos/dhleong/wish-compiler/releases/latest) + url=$(echo "$response" \ | grep "browser_download_url" \ | cut -d '"' -f 4) - curl $url -Lo .bin/wish-compiler + + echo "Fetching wish-compiler: $url" + curl $url -Lo .bin/wish-compiler || echo "Error getting latest compiler version: $response" + + if ! [ -f ".bin/wish-compiler" ]; then + echo "Failed to download wish-compiler" + exit 1 + fi + chmod +x .bin/wish-compiler fi