Skip to content

Commit e231080

Browse files
mfikesdnolen
authored andcommitted
CLJS-2651: Shared AOT cache: Support git deps
1 parent adeaa9b commit e231080

File tree

1 file changed

+60
-15
lines changed

1 file changed

+60
-15
lines changed

src/main/clojure/cljs/closure.clj

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@
4646
[com.sun.nio.file SensitivityWatchEventModifier]
4747
[com.google.common.base Throwables]))
4848

49+
;; Copied from clojure.tools.gitlibs
50+
51+
(def ^:private GITLIBS-CACHE-DIR
52+
(delay
53+
(.getCanonicalPath
54+
(let [env (System/getenv "GITLIBS")]
55+
(if (string/blank? env)
56+
(io/file (System/getProperty "user.home") ".gitlibs")
57+
(io/file env))))))
58+
59+
(defn- gitlibs-cache-dir
60+
"Returns the gitlibs cache dir, a string."
61+
[]
62+
@GITLIBS-CACHE-DIR)
63+
64+
(defn- gitlibs-src?
65+
"Returns true if the file comes from the gitlibs cache."
66+
[file]
67+
(string/starts-with? (util/path file) (gitlibs-cache-dir)))
68+
4969
(def name-chars (map char (concat (range 48 57) (range 65 90) (range 97 122))))
5070

5171
(defn random-char []
@@ -556,6 +576,29 @@
556576
[compilable opts]
557577
(-compile compilable opts))
558578

579+
(def ^:private USER-HOME-WRITABLE
580+
(delay (.canWrite (io/file (System/getProperty "user.home")))))
581+
582+
(defn- aot-cache? [opts]
583+
"Returns true if compilation artifacts shuold be placed in the
584+
shared AOT cache."
585+
(and (:aot-cache opts)
586+
@USER-HOME-WRITABLE))
587+
588+
(defn- copy-from-cache
589+
[cache-path cacheable source-file opts]
590+
(doseq [[k ^File f] cacheable]
591+
(when (.exists f)
592+
(let [target (io/file (util/output-directory opts)
593+
(-> (.getAbsolutePath f)
594+
(string/replace (.getAbsolutePath cache-path) "")
595+
(subs 1)))]
596+
(when (and (or ana/*verbose* (:verbose opts)) (= :output-file k))
597+
(util/debug-prn (str "Copying cached " f " to " target)))
598+
(util/mkdirs target)
599+
(spit target (slurp f))
600+
(.setLastModified target (util/last-modified source-file))))))
601+
559602
(defn find-sources
560603
"Given a Compilable, find sources and return a sequence of IJavaScript."
561604
[compilable opts]
@@ -569,8 +612,21 @@
569612
IJavaScript."
570613
[^File file {:keys [output-file] :as opts}]
571614
(if output-file
572-
(let [out-file (.toString (io/file (util/output-directory opts) output-file))]
573-
(compiled-file (comp/compile-file file out-file opts)))
615+
(let [out-file (io/file (util/output-directory opts) output-file)]
616+
(if (and (aot-cache? opts)
617+
(gitlibs-src? file))
618+
(let [cacheable (ana/cacheable-files file (util/ext file) opts)
619+
cache-path (ana/cache-base-path (util/path file) opts)]
620+
(if (not (.exists (:output-file cacheable)))
621+
(let [ret (compiled-file (comp/compile-file file (:output-file cacheable)
622+
(assoc opts :output-dir (util/path cache-path))))]
623+
(copy-from-cache cache-path cacheable file opts)
624+
ret)
625+
(do
626+
(when-not (.exists out-file)
627+
(copy-from-cache cache-path cacheable file opts))
628+
(compiled-file (comp/compile-file file (.toString out-file) opts)))))
629+
(compiled-file (comp/compile-file file (.toString out-file) opts))))
574630
(let [path (.getPath ^File file)]
575631
(binding [ana/*cljs-file* path]
576632
(with-open [rdr (io/reader file)]
@@ -616,24 +672,13 @@
616672
(when (or (nil? out-file)
617673
(comp/requires-compilation? jar-file out-file opts))
618674
;; actually compile from JAR
619-
(if (or (not (:aot-cache opts))
620-
(not (.canWrite (io/file (System/getProperty "user.home")))))
675+
(if (not (aot-cache? opts))
621676
(-compile (jar-file-to-disk jar-file (util/output-directory opts) opts) opts)
622677
(let [cache-path (ana/cache-base-path (util/path jar-file) opts)]
623678
(when (comp/requires-compilation? jar-file (:output-file cacheable) opts)
624679
(-compile (jar-file-to-disk jar-file cache-path opts)
625680
(assoc opts :output-dir (util/path cache-path))))
626-
(doseq [[k ^File f] cacheable]
627-
(when (.exists f)
628-
(let [target (io/file (util/output-directory opts)
629-
(-> (.getAbsolutePath f)
630-
(string/replace (.getAbsolutePath cache-path) "")
631-
(subs 1)))]
632-
(when (and (or ana/*verbose* (:verbose opts)) (= :output-file k))
633-
(util/debug-prn (str "Copying cached " f " to " target)))
634-
(util/mkdirs target)
635-
(spit target (slurp f))
636-
(.setLastModified target (util/last-modified jar-file))))))))
681+
(copy-from-cache cache-path cacheable jar-file opts))))
637682
;; Files that don't require compilation (cljs.loader for example)
638683
;; need to be copied from JAR to disk.
639684
(when-not (.exists out-file)

0 commit comments

Comments
 (0)