Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/net/lewisship/cli_tools/cache.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@
[clojure.string :as str]
[clj-commons.ansi :refer [perr]]
#?(:bb [babashka.classpath :as cp]))
(:import [java.io File]
[java.security MessageDigest]))
(:import (java.io File)
(java.nio ByteBuffer)
(java.security MessageDigest)))

(defn- get-classpath
[]
#?(
:bb (cp/get-classpath)
#?(:bb (cp/get-classpath)
:clj (System/getProperty "java.class.path")))

(defn- get-split-classpath
[]
(let [sep fs/path-separator
paths (-> (get-classpath)
(str/split (re-pattern sep)))]

(->> paths
sort
(mapv io/file))))
Expand All @@ -34,16 +33,19 @@
(let [bytes (.getBytes value "UTF-8")]
(.update digest bytes)))

(defn- update-digest-from-file-contents
(defn- update-digest-from-file
[^MessageDigest digest f]
(let [f' (fs/file f)]
;; Would be better to digest the 8 raw bytes, but this is easier.
(update-digest-from-string digest (Long/toHexString (.lastModified f')))))
(let [f' (fs/file f)
last-modified (.lastModified f')
b (ByteBuffer/allocate 8)]
(.putLong b last-modified)
(.flip b)
(.update digest b)))

(defn- update-digest-recursively
[digest ^File root]
(let [paths (fs/glob root "**")]
(run! #(update-digest-from-file-contents digest %) paths)))
(run! #(update-digest-from-file digest %) paths)))

(defn- update-digest
[digest source]
Expand All @@ -52,7 +54,8 @@
;; The assumption is that files are .jar files and the name will change if
;; the contents change.
(update-digest-from-string digest (.getCanonicalPath f))
;; But for a source directory, find all the sources (and digest their contents).
;; But for a source directory, find all the sources (and digest their file time stamps).
;; Adding or removing a file (even if no other files are touched) will change the digest.
(update-digest-recursively digest f))))

(defn- hex-string [^bytes input]
Expand Down