Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
73 lines (55 sloc) 2.08 KB
(ns shadow.resource
[ :as io]
[shadow.cljs.util :as util]
[cljs.env :as env]
[cljs.analyzer :as ana]
[clojure.string :as str]
[ :as rc])
(:import [java.nio.file Paths]))
(defn slurp-resource
"API function to let other macros read resources while also recording that they did so"
[env path]
(let [current-ns (-> env :ns :name)
(str/starts-with? path "/")
(subs path 1)
(str/starts-with? path ".")
(let [resource-name
(util/ns->cljs-filename current-ns)
(-> (Paths/get resource-name (into-array String []))
(when-not parent
(throw (ana/error env (str "Could not resolve " path " from " current-ns))))
(-> parent
(.resolve path)
rc (io/resource path)]
(when-not rc
(throw (ana/error env (str "Resource not found: " path))))
;; when used as part of a compilation record which namespace did so
(when env/*compiler*
(let [last-mod (util/url-last-modified rc)]
(swap! env/*compiler* assoc-in [::ana/namespaces current-ns ::resource-refs path] last-mod)))
(slurp rc)))
(defmacro inline
"Inlines the given resource path as a string value
will throw if the path is not found on the classpath.
relative paths will be resolved relative to the current namespace
(:require [shadow.resource :as rc]))
(def x (rc/inline \"./\"))
this includes demo/ from the classpath and ends as
(def x \"contents of\")
Cannot be used dynamically and should be limited to small files.
Larger files should be loaded dynamically at runtime."
(when-not (string? path)
(throw (ana/error &env (str "shadow.resource/inline must be called with a literal string argument"))))
(slurp-resource &env path))