|
34 | 34 | [cljs.reader :as edn]))
|
35 | 35 | #?(:clj (:import [java.io File Reader PushbackReader]
|
36 | 36 | [java.net URL]
|
| 37 | + [java.lang Throwable] |
37 | 38 | [clojure.lang Namespace Var LazySeq ArityException]
|
38 | 39 | [cljs.tagged_literals JSValue])))
|
39 | 40 |
|
|
2840 | 2841 | compiler options, if :cache-analysis true will cache analysis to
|
2841 | 2842 | \":output-dir/some/ns/foo.cljs.cache.edn\". This function does not return a
|
2842 | 2843 | meaningful value."
|
2843 |
| - ([f] (analyze-file f nil)) |
| 2844 | + ([f] |
| 2845 | + (analyze-file f nil)) |
2844 | 2846 | ([f opts]
|
| 2847 | + (analyze-file f false opts)) |
| 2848 | + ([f skip-cache opts] |
2845 | 2849 | (binding [*file-defs* (atom #{})]
|
2846 | 2850 | (let [output-dir (util/output-directory opts)
|
2847 |
| - res (cond |
2848 |
| - (instance? File f) f |
2849 |
| - (instance? URL f) f |
2850 |
| - (re-find #"^file://" f) (URL. f) |
2851 |
| - :else (io/resource f))] |
| 2851 | + res (cond |
| 2852 | + (instance? File f) f |
| 2853 | + (instance? URL f) f |
| 2854 | + (re-find #"^file://" f) (URL. f) |
| 2855 | + :else (io/resource f))] |
2852 | 2856 | (assert res (str "Can't find " f " in classpath"))
|
2853 | 2857 | (ensure
|
2854 | 2858 | (let [ns-info (parse-ns res)
|
2855 |
| - path (if (instance? File res) |
2856 |
| - (.getPath ^File res) |
2857 |
| - (.getPath ^URL res)) |
2858 |
| - cache (when (:cache-analysis opts) |
2859 |
| - (cache-file res ns-info output-dir))] |
| 2859 | + path (if (instance? File res) |
| 2860 | + (.getPath ^File res) |
| 2861 | + (.getPath ^URL res)) |
| 2862 | + cache (when (:cache-analysis opts) |
| 2863 | + (cache-file res ns-info output-dir))] |
2860 | 2864 | (when-not (get-in @env/*compiler* [::namespaces (:ns ns-info) :defs])
|
2861 |
| - (if (or (not cache) (requires-analysis? res output-dir)) |
| 2865 | + (if (or skip-cache (not cache) (requires-analysis? res output-dir)) |
2862 | 2866 | (binding [*cljs-ns* 'cljs.user
|
2863 | 2867 | *cljs-file* path
|
2864 | 2868 | reader/*alias-map* (or reader/*alias-map* {})]
|
2865 | 2869 | (when (or *verbose* (:verbose opts))
|
2866 | 2870 | (util/debug-prn "Analyzing" (str res)))
|
2867 | 2871 | (let [env (assoc (empty-env) :build-options opts)
|
2868 |
| - ns (with-open [rdr (io/reader res)] |
2869 |
| - (loop [ns nil forms (seq (forms-seq* rdr (util/path res)))] |
2870 |
| - (if forms |
2871 |
| - (let [form (first forms) |
2872 |
| - env (assoc env :ns (get-namespace *cljs-ns*)) |
2873 |
| - ast (analyze env form nil opts)] |
2874 |
| - (if (= (:op ast) :ns) |
2875 |
| - (recur (:name ast) (next forms)) |
2876 |
| - (recur ns (next forms)))) |
2877 |
| - ns)))] |
| 2872 | + ns (with-open [rdr (io/reader res)] |
| 2873 | + (loop [ns nil forms (seq (forms-seq* rdr (util/path res)))] |
| 2874 | + (if forms |
| 2875 | + (let [form (first forms) |
| 2876 | + env (assoc env :ns (get-namespace *cljs-ns*)) |
| 2877 | + ast (analyze env form nil opts)] |
| 2878 | + (if (= (:op ast) :ns) |
| 2879 | + (recur (:name ast) (next forms)) |
| 2880 | + (recur ns (next forms)))) |
| 2881 | + ns)))] |
2878 | 2882 | (when (and cache (true? (:cache-analysis opts)))
|
2879 | 2883 | (write-analysis-cache ns cache res))))
|
2880 |
| - ;; we want want to keep dependency analysis information |
2881 |
| - ;; don't revert the environment - David |
2882 |
| - (let [{:keys [ns]} |
2883 |
| - (parse-ns res |
2884 |
| - (merge opts |
2885 |
| - {:restore false |
2886 |
| - :analyze-deps true |
2887 |
| - :load-macros true}))] |
2888 |
| - (when (or *verbose* (:verbose opts)) |
2889 |
| - (util/debug-prn "Reading analysis cache for" (str res))) |
2890 |
| - (swap! env/*compiler* |
2891 |
| - (fn [cenv] |
2892 |
| - (let [cached-ns (edn/read-string (slurp cache))] |
2893 |
| - (doseq [x (get-in cached-ns [::constants :order])] |
2894 |
| - (register-constant! x)) |
2895 |
| - (-> cenv |
2896 |
| - (assoc-in [::namespaces ns] cached-ns))))))))))))))) |
| 2884 | + (try |
| 2885 | + ;; we want want to keep dependency analysis information |
| 2886 | + ;; don't revert the environment - David |
| 2887 | + (let [{:keys [ns]} (parse-ns res |
| 2888 | + (merge opts |
| 2889 | + {:restore false |
| 2890 | + :analyze-deps true |
| 2891 | + :load-macros true})) |
| 2892 | + cached-ns (edn/read-string (slurp cache))] |
| 2893 | + (when (or *verbose* (:verbose opts)) |
| 2894 | + (util/debug-prn "Reading analysis cache for" (str res))) |
| 2895 | + (swap! env/*compiler* |
| 2896 | + (fn [cenv] |
| 2897 | + (let [] |
| 2898 | + (doseq [x (get-in cached-ns [::constants :order])] |
| 2899 | + (register-constant! x)) |
| 2900 | + (-> cenv |
| 2901 | + (assoc-in [::namespaces ns] cached-ns)))))) |
| 2902 | + (catch Throwable e |
| 2903 | + (analyze-file f true opts)))))))))))) |
0 commit comments