Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Transit sources #87

Merged
merged 4 commits into from Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,5 +11,6 @@ out
.lein-failures
.cljs_nashorn_repl
.repl
.bin
nashorn_code_cache
.planck_cache
14 changes: 0 additions & 14 deletions .travis.yml
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions project.clj
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion scripts/compile-builtin-sources
Expand Up @@ -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
22 changes: 22 additions & 0 deletions scripts/wish-compiler
@@ -0,0 +1,22 @@
#!/bin/bash

# auto-download latest release bin
if ! [ -d ".bin" ] || ! [ -f ".bin/wish-compiler" ]; then
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)

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

.bin/wish-compiler "$@"
4 changes: 3 additions & 1 deletion src/cljs/wish/providers/wish.cljs
Expand Up @@ -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
Expand Down
30 changes: 23 additions & 7 deletions src/cljs/wish/sources.cljs
Expand Up @@ -6,6 +6,7 @@
(:require [clojure.core.async :as async :refer [alts! <!]]
[clojure.tools.reader.reader-types :refer [string-push-back-reader]]
[cljs.reader :as edn]
[cognitect.transit :as t]
[wish.providers :as providers]
[wish.sheets :as sheets]
[wish.sources.compiler :refer [compile-directives]]
Expand All @@ -16,19 +17,34 @@
; cache of *compiled* sources by id
(defonce ^:private loaded-sources (atom {}))

(defn- compile-raw-source
[{:keys [kind] :as sheet} id raw]
(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))

(->DataSource
id
(->> directives
(compile-directives)
(sheets/post-compile kind))))))
; done!
directives)))

(defn- compile-raw-source
[{:keys [kind] :as sheet} id raw]
(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"
Expand Down