<commit_before>(ns figwheel-sidecar.build-middleware.stamp-and-clean
  (:require
   [figwheel-sidecar.utils :as utils]
   [clojure.java.io :as io]))

;; some minimal protection against corrupt builds
;; this is really only intended to run once at the start
;; of an autobuild session but its fast enough to run every time

(def user-dir-hash  (.hashCode (System/getProperty "user.dir")))

(def classpath-hash (.hashCode (System/getProperty "java.class.path")))

(defn stamp-file [build-config]
  (io/file
   (System/getProperty "java.io.tmpdir")
   (str
    (hash
     {:build-id  (.hashCode (str (:id build-config)))
      :root-path user-dir-hash})
    ".txt")))

;; this provides a simple map and stable order for hashing
(defn options-that-affect-build-cache [build-config]
  (-> build-config
      (select-keys [:static-fns :optimize-constants
                    :elide-asserts :target])
      (assoc
       :build-id     (:id build-config)
       :source-paths (:source-paths build-config)
       :classpath    classpath-hash)))

(defn current-stamp-signature [build-config]
  (->> build-config
       options-that-affect-build-cache
       (into (sorted-map))
       pr-str
       (.hashCode)
       str))

#_(count (into #{}  (take 100000 (repeatedly #(current-stamp-signature t)))))

(defn stamp! [build-config]
  (println "Stamping!")
  (println "Stamp file" (stamp-file build-config))
  (println "Stamp" (current-stamp-signature build-config))
  (spit (stamp-file build-config)
        (current-stamp-signature build-config)))

(defn stamp-value [build-state]
  (let [sf (stamp-file build-state)]
    (when (.exists sf) (slurp sf))))

(defn stamp-matches? [build-config]
  (println "Current Stamp " (current-stamp-signature build-config))
  (println "Previous Stamp" (stamp-value build-config))<commit_msg>Protect against corrupt builds using a stamp and clean system<commit_after>(ns figwheel-sidecar.build-middleware.stamp-and-clean
  (:require
   [figwheel-sidecar.utils :as utils]
   [clojure.java.io :as io]))

;; Minimal protection against corrupt builds

;; While minimal this will prevent plenty of headaches caused by
;; corrupted build :output-dir directories

;; This is a temporary solution until the ClojureScript compiler supports
;; something like a :signature key

(def user-dir-hash  (.hashCode (System/getProperty "user.dir")))

;; could hash all jar file names??
(def classpath-hash (.hashCode (System/getProperty "java.class.path")))

(defn stamp-file [build-config]
  (io/file (or (System/getProperty "user.dir")
               (io/file "."))
           (or (-> build-config :build-options :output-dir) "out")
           ".figwheel-compile-stamp"))

;; this provides a simple map and stable order for hashing
(defn options-that-affect-build-cache [build-config]
  (-> build-config
      (select-keys [:static-fns :optimize-constants
                    :elide-asserts :target])
      (assoc
       :build-id     (:id build-config)
       :source-paths (:source-paths build-config)
       :classpath    classpath-hash)))

(defn current-stamp-signature [build-config]
  (->> build-config
       options-that-affect-build-cache
       (into (sorted-map))
       pr-str
       (.hashCode)
       str))

(defn stamp! [build-config]
  #_(println "Stamping!")
  #_(println "Stamp file" (stamp-file build-config))
  #_(println "Stamp" (current-stamp-signature build-config))
  (let [sfile (stamp-file build-config)]
    (.mkdirs (.getParentFile sfile))
    (spit sfile
          (current-stamp-signature build-config))))

(defn stamp-value [build-state]
  (let [sf (stamp-file build-state)]
    (when (.exists sf) (slurp sf))))

(defn stamp-matches? [build-config]
  #_(println "Current Stamp " (current-stamp-signature build-config))
  #_(println "Previous Stamp" (stamp-value build-config))
{'fraction_non_alphanumeric': 0.14252030390358922, 'fraction_numerical': 0.0015719151165837044, 'mean_word_length': 3.314124293785311, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2496630', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1234, 'n_words': 322}