Skip to content

Commit

Permalink
Asset :asset-path option, fixes #18
Browse files Browse the repository at this point in the history
Removes specified path from the start of reloaded URL
  • Loading branch information
Deraen committed Jun 13, 2015
1 parent e61aef9 commit 1a452ee
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,3 @@
## 0.3.1

- Added `:asset-path` option
1 change: 1 addition & 0 deletions build.boot
@@ -1,4 +1,5 @@
(set-env!
:source-paths #{"src" "test"}
:dependencies '[[org.clojure/clojure "1.6.0" :scope "provided"]
[boot/core "2.0.0" :scope "provided"]
[adzerk/bootlaces "0.1.10" :scope "test"]
Expand Down
12 changes: 8 additions & 4 deletions src/adzerk/boot_reload.clj
Expand Up @@ -39,10 +39,13 @@
(client/connect ~url {:on-jsload #(~(or on-jsload '+))}))))
(map pr-str) (interpose "\n") (apply str) (spit f)))

(defn- send-changed! [pod changed]
(defn- send-changed! [pod asset-path changed]
(when-not (empty? changed)
(pod/with-call-in pod
(adzerk.boot-reload.server/send-changed! ~(get-env :target-path) ~changed))))
(adzerk.boot-reload.server/send-changed!
~(get-env :target-path)
~asset-path
~changed))))

(defn- add-init!
[in-file out-file]
Expand All @@ -64,7 +67,8 @@

[i ip ADDR str "The (optional) IP address for the websocket server to listen on."
p port PORT int "The (optional) port the websocket server listens on."
j on-jsload SYM sym "The (optional) callback to call when JS files are reloaded."]
j on-jsload SYM sym "The (optional) callback to call when JS files are reloaded."
a asset-path PATH str "The (optional) asset-path. This is removed from the start of reloaded urls."]

(let [pod (make-pod)
src (tmp-dir!)
Expand All @@ -82,6 +86,6 @@
(add-init! in-file out-file)))
(-> fileset (add-resource tmp) commit!))
(with-post-wrap fileset
(send-changed! @pod (changed @prev fileset))
(send-changed! @pod asset-path (changed @prev fileset))
(reset! prev fileset)))))

16 changes: 10 additions & 6 deletions src/adzerk/boot_reload/server.clj
Expand Up @@ -2,21 +2,25 @@
(:require
[clojure.java.io :as io]
[boot.util :as util]
[org.httpkit.server :as http])
[org.httpkit.server :as http]
[clojure.string :as string])
(:import
[java.io IOException]))
[java.io IOException]
[java.net URI]))

(def state (atom {}))

(defn web-path [proto rel-path tgt-path]
(defn web-path [proto rel-path tgt-path asset-path]
(if-not (= "file:" proto)
(str "/" rel-path)
(if asset-path
(string/replace rel-path (re-pattern (str "^" asset-path "/")) "")
(str "/" rel-path))
(.getCanonicalPath (io/file tgt-path rel-path))))

(defn send-changed! [tgt-path changed]
(defn send-changed! [tgt-path asset-path changed]
(doseq [[id {:keys [proto channel]}] @state]
(http/send! channel
(pr-str (into [] (map #(web-path proto % tgt-path) changed))))))
(pr-str (into [] (map #(web-path proto % tgt-path asset-path) changed))))))

(defn set-proto! [channel proto]
(doseq [[id {c :channel}] @state]
Expand Down
16 changes: 16 additions & 0 deletions test/adzerk/boot_reload/server_test.clj
@@ -0,0 +1,16 @@
(ns adzerk.boot-reload.server-test
(:require [clojure.test :refer :all]
[adzerk.boot-reload.server :refer :all]))

(deftest web-path-test
(testing "Basic"
(is (= "/js/out/saapas/core.js"
(web-path "http:" "js/out/saapas/core.js" "target" nil))))

(testing "Asset-path"
(is (= "js/out/saapas/core.js"
(web-path "http:" "public/js/out/saapas/core.js" nil "public")))

(is (= "public/js/out/saapas/core.js"
(web-path "http:" "public/js/out/saapas/core.js" nil "foobar")))
))

0 comments on commit 1a452ee

Please sign in to comment.