<commit_before>(ns asset-builder.core
  (:require [clojure.java.io :as io]
            [asset-minifier.core :as assets]) )

;; ## Reporting

(defn- print-colorized
  [color-code args]
  (printf "\u001b[%s%s\u001b[0m%n"
          color-code
          (clojure.string/join " " args))
  (flush))

(defn- print-heading
  [& args]
  (print-colorized "1m" args))

(defn- print-success
  [start-time & args]
  (let [delta-str (format "[%.3fs]" (/ (- (System/nanoTime) start-time) 1e9))]
    (->> (concat args [delta-str])
         (print-colorized "32m"))))

(defn- location
  [exception]
  (if-let [s (first (.getStackTrace exception))]
    (format "(%s:%d)" (.getFileName s) (.getLineNumber s))
    "(unknown)"))

(defn- format-exception
  [exception]
  (str (.getMessage exception) " " (location exception)))

(defn- print-exception
  [start-time exception]
  (let [delta-str (format "[%.3fs]" (/ (- (System/nanoTime) start-time) 1e9))]
    (.printStackTrace exception *out*)
    (flush)
    (->> (vector "!"
                 (->> (iterate #(.getCause %) exception)
                      (take-while identity)
                      (rest)
                      (map format-exception)
                      (map #(str "+-- " %))
                      (cons (str (format-exception exception) " " delta-str))
                      (clojure.string/join "\n")))
         (print-colorized "31m"))))

(defmacro ^:private with-reporting
  [[heading success-msg error-msg] & body]
  `(let [_#     (print-heading ~heading)
         start# (System/nanoTime)]
     (try
       (let [result# (do ~@body)]
         (print-success start# ~success-msg)
         result#)
       (catch Throwable t#
         (print-exception start# t#)
         ::error))))

;; ## CLJS

(defonce cljs-build!
  (or
    (try
      (require 'cljs.build.api)
      (some-> (resolve 'cljs.build.api/build) deref)
      (catch Throwable _))
    (fn [& _]
      (throw
        (IllegalStateException.
          "dependency 'org.clojure/clojurescript' missing (or incompatible).")))))

(defn- build-cljs
  [{:keys [cljs-path cljs]}]
  (with-reporting ["Compiling ClojureScript ..."
                   "ClojureScript has been compiled."]
    (->> {:verbose true}
         (merge cljs)
         (cljs-build! cljs-path))))

;; ## Assets

(defn- ->kb
  [v]
  (format "%.2fKB" (/ v 1024.0)))

(defn- minify-asset
  [source-path target-path]<commit_msg>add CLJS default value for ':output-dir'.<commit_after>(ns asset-builder.core
  (:require [clojure.java.io :as io]
            [asset-minifier.core :as assets]) )

;; ## Reporting

(defn- print-colorized
  [color-code args]
  (printf "\u001b[%s%s\u001b[0m%n"
          color-code
          (clojure.string/join " " args))
  (flush))

(defn- print-heading
  [& args]
  (print-colorized "1m" args))

(defn- print-success
  [start-time & args]
  (let [delta-str (format "[%.3fs]" (/ (- (System/nanoTime) start-time) 1e9))]
    (->> (concat args [delta-str])
         (print-colorized "32m"))))

(defn- location
  [exception]
  (if-let [s (first (.getStackTrace exception))]
    (format "(%s:%d)" (.getFileName s) (.getLineNumber s))
    "(unknown)"))

(defn- format-exception
  [exception]
  (str (.getMessage exception) " " (location exception)))

(defn- print-exception
  [start-time exception]
  (let [delta-str (format "[%.3fs]" (/ (- (System/nanoTime) start-time) 1e9))]
    (.printStackTrace exception *out*)
    (flush)
    (->> (vector "!"
                 (->> (iterate #(.getCause %) exception)
                      (take-while identity)
                      (rest)
                      (map format-exception)
                      (map #(str "+-- " %))
                      (cons (str (format-exception exception) " " delta-str))
                      (clojure.string/join "\n")))
         (print-colorized "31m"))))

(defmacro ^:private with-reporting
  [[heading success-msg error-msg] & body]
  `(let [_#     (print-heading ~heading)
         start# (System/nanoTime)]
     (try
       (let [result# (do ~@body)]
         (print-success start# ~success-msg)
         result#)
       (catch Throwable t#
         (print-exception start# t#)
         ::error))))

;; ## CLJS

(defonce cljs-build!
  (or
    (try
      (require 'cljs.build.api)
      (some-> (resolve 'cljs.build.api/build) deref)
      (catch Throwable _))
    (fn [& _]
      (throw
        (IllegalStateException.
          "dependency 'org.clojure/clojurescript' missing (or incompatible).")))))

(defn- build-cljs
  [{:keys [cljs-path cljs]}]
  (with-reporting ["Compiling ClojureScript ..."
                   "ClojureScript has been compiled."]
    (->> (merge
           {:output-dir "target/out"}
           cljs
           {:verbose true})
         (cljs-build! cljs-path))))

;; ## Assets

(defn- ->kb
  [v]
  (format "%.2fKB" (/ v 1024.0)))

(defn- minify-asset
  [source-path target-path]
{'fraction_non_alphanumeric': 0.1778324113911084, 'fraction_numerical': 0.009834050399508297, 'mean_word_length': 2.17012987012987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1974668', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1653, 'n_words': 364}