From 1a452ee14b43eae9be8eac04a27ff2e742f0683e Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Sat, 13 Jun 2015 19:55:17 +0300 Subject: [PATCH] Asset :asset-path option, fixes #18 Removes specified path from the start of reloaded URL --- CHANGELOG.md | 3 +++ build.boot | 1 + src/adzerk/boot_reload.clj | 12 ++++++++---- src/adzerk/boot_reload/server.clj | 16 ++++++++++------ test/adzerk/boot_reload/server_test.clj | 16 ++++++++++++++++ 5 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 test/adzerk/boot_reload/server_test.clj diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..116d10d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.3.1 + +- Added `:asset-path` option diff --git a/build.boot b/build.boot index e2648b1..2dc3d63 100644 --- a/build.boot +++ b/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"] diff --git a/src/adzerk/boot_reload.clj b/src/adzerk/boot_reload.clj index 314fdc2..934036e 100644 --- a/src/adzerk/boot_reload.clj +++ b/src/adzerk/boot_reload.clj @@ -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] @@ -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!) @@ -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))))) diff --git a/src/adzerk/boot_reload/server.clj b/src/adzerk/boot_reload/server.clj index 165bd67..e817503 100644 --- a/src/adzerk/boot_reload/server.clj +++ b/src/adzerk/boot_reload/server.clj @@ -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] diff --git a/test/adzerk/boot_reload/server_test.clj b/test/adzerk/boot_reload/server_test.clj new file mode 100644 index 0000000..3976e74 --- /dev/null +++ b/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"))) + ))