|
1 | 1 | (ns build
|
2 |
| - (:require [clojure.tools.build.api :as b])) |
| 2 | + (:require [clojure.tools.build.api :as b] |
| 3 | + [clojure.java.io :as io] |
| 4 | + [clojure.string :as str]) |
| 5 | + (:import [java.security MessageDigest] |
| 6 | + [java.math BigInteger] |
| 7 | + [java.nio.file Paths])) |
3 | 8 |
|
4 | 9 | (def ^:private TARGET-DIR "target")
|
5 | 10 | (def ^:private CLASS-DIR (format "%s/classes" TARGET-DIR))
|
|
25 | 30 | :basis @basis
|
26 | 31 | :main main-ns}))
|
27 | 32 |
|
| 33 | +(defn- md5 |
| 34 | + [^String s] |
| 35 | + (->> (.getBytes s) |
| 36 | + (.digest (MessageDigest/getInstance "MD5")) |
| 37 | + (BigInteger. 1) |
| 38 | + (format "%032x"))) |
| 39 | + |
| 40 | + |
| 41 | +(defn- join-path |
| 42 | + [p & ps] |
| 43 | + (str (.normalize (Paths/get p (into-array String ps))))) |
| 44 | + |
| 45 | + |
| 46 | +(defn- file-name-with-hash |
| 47 | + [content-hash file-name-in-html] |
| 48 | + (let [file-name-split (str/split file-name-in-html #"\.") |
| 49 | + new-file-name-split (-> file-name-split |
| 50 | + (butlast) |
| 51 | + (concat [content-hash (last file-name-split)]))] |
| 52 | + (str/join "." new-file-name-split))) |
| 53 | + |
| 54 | + |
| 55 | +(defn- hash-css |
| 56 | + "Rename css file in html with hash of the file." |
| 57 | + [{:keys [output-file-path file-name-in-html html-file-path] |
| 58 | + :or {html-file-path "resources/public/index.html"}}] |
| 59 | + (let [output-file (io/file output-file-path) |
| 60 | + new-file-name (-> (slurp output-file) |
| 61 | + (md5) |
| 62 | + (file-name-with-hash file-name-in-html)) |
| 63 | + new-file-path (join-path (.getParent output-file) new-file-name) |
| 64 | + html (slurp html-file-path)] |
| 65 | + (.renameTo output-file (io/file new-file-path)) |
| 66 | + (->> (str/replace html file-name-in-html new-file-name) |
| 67 | + (spit html-file-path)))) |
| 68 | + |
28 | 69 | (defn build
|
29 | 70 | "Build an uberjar."
|
30 | 71 | [_]
|
31 | 72 | (clean)
|
| 73 | + (hash-css {:output-file-path "resources/public/css/output-prod.css" |
| 74 | + :file-name-in-html "output.css"}) |
32 | 75 | (uber {:uber-file UBER-FILE
|
33 | 76 | :class-dir CLASS-DIR
|
34 | 77 | :main-ns MAIN-NS}))
|
0 commit comments