|
46 | 46 | [com.sun.nio.file SensitivityWatchEventModifier] |
47 | 47 | [com.google.common.base Throwables])) |
48 | 48 |
|
| 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 | + |
49 | 69 | (def name-chars (map char (concat (range 48 57) (range 65 90) (range 97 122)))) |
50 | 70 |
|
51 | 71 | (defn random-char [] |
|
556 | 576 | [compilable opts] |
557 | 577 | (-compile compilable opts)) |
558 | 578 |
|
| 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 | + |
559 | 602 | (defn find-sources |
560 | 603 | "Given a Compilable, find sources and return a sequence of IJavaScript." |
561 | 604 | [compilable opts] |
|
569 | 612 | IJavaScript." |
570 | 613 | [^File file {:keys [output-file] :as opts}] |
571 | 614 | (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)))) |
574 | 630 | (let [path (.getPath ^File file)] |
575 | 631 | (binding [ana/*cljs-file* path] |
576 | 632 | (with-open [rdr (io/reader file)] |
|
616 | 672 | (when (or (nil? out-file) |
617 | 673 | (comp/requires-compilation? jar-file out-file opts)) |
618 | 674 | ;; 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)) |
621 | 676 | (-compile (jar-file-to-disk jar-file (util/output-directory opts) opts) opts) |
622 | 677 | (let [cache-path (ana/cache-base-path (util/path jar-file) opts)] |
623 | 678 | (when (comp/requires-compilation? jar-file (:output-file cacheable) opts) |
624 | 679 | (-compile (jar-file-to-disk jar-file cache-path opts) |
625 | 680 | (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)))) |
637 | 682 | ;; Files that don't require compilation (cljs.loader for example) |
638 | 683 | ;; need to be copied from JAR to disk. |
639 | 684 | (when-not (.exists out-file) |
|
0 commit comments