<commit_before>(ns shadow.cljs.devtools.server.util
  (:require
    [clojure.core.async :as async :refer (go <! >! alt!! alts!!)]
    [clojure.string :as str]
    [clojure.java.io :as io]
    [shadow.jvm-log :as log]
    [shadow.build.log :as build-log]
    [shadow.build.api :as cljs]
    [shadow.build.warnings :as warnings]
    [shadow.build.api :as build-api]
    [shadow.cljs.devtools.errors :as errors]
    [shadow.cljs.devtools.config :as config]
    [shadow.cljs.devtools.server.runtime :as runtime]
    [clojure.set :as set]
    [clojure.core.async.impl.protocols :as async-prot]
    [shadow.build.npm :as npm])
  (:import (java.io Writer InputStreamReader BufferedReader IOException ByteArrayOutputStream ByteArrayInputStream PrintWriter File)
           [java.net SocketException]
           [java.util List]))

(defn chan? [x]
  (satisfies? async-prot/ReadPort x))

(defn find-version-from-pom [pom-xml]
  (let [[_ version :as m]
        (->> (slurp pom-xml)
             (re-find #"<version>([^<]+)</version>"))]
    (when m
      version)))

(defn find-version []
  (let [pom-xml (io/resource "META-INF/maven/thheller/shadow-cljs/pom.xml")]

    (if (nil? pom-xml)
      "<snapshot>"
      (find-version-from-pom pom-xml))))

(defn async-logger [ch]
  (reify
    build-log/BuildLog
    (log*
      [_ state event]
      (async/offer! ch {:type :build-log
                        :event event}))))

(def null-log
  (reify
    build-log/BuildLog
    (log* [_ state msg])))

(def silent-log
  (reify build-log/BuildLog
    (log* [_ state {::build-log/keys [level] :as evt}]
      (when (not= level :info)
        (build-log/log* build-log/stdout state evt)
        ))))

;; .getCanonicalFile also resolves symlinks which we want to keep
;; and File itself doesn't have a way to only remove .. or . placeholders otherwise
;; so going to path and back immediately to avoid having to deal with non-normalized
;; paths later on
(defn normalize-file [^File file]
  (-> file
      (.toPath)
      (.normalize)
      (.toFile)))

(defn new-build
  [{:keys [build-id] :or {build-id :custom} :as build-config} mode opts]
  (let [{:keys [classpath cache-root build-executor babel config] :as runtime}
        (runtime/get-instance!)

        {:keys [cache-blockers]}
        config

        cache-dir
        (config/make-cache-dir cache-root build-id mode)

        npm-config
        (select-keys (:js-options build-config) [:js-package-dirs :node-modules-dir :entry-keys :extensions])

        ;; don't use shared npm instance since lookups are cached and
        ;; js-package-dirs may affect what things resolve to
        npm
        (npm/start npm-config)]

    (-> (build-api/init)<commit_msg>allow global :js-package-dirs<commit_after>(ns shadow.cljs.devtools.server.util
  (:require
    [clojure.core.async :as async :refer (go <! >! alt!! alts!!)]
    [clojure.string :as str]
    [clojure.java.io :as io]
    [shadow.jvm-log :as log]
    [shadow.build.log :as build-log]
    [shadow.build.api :as cljs]
    [shadow.build.warnings :as warnings]
    [shadow.build.api :as build-api]
    [shadow.cljs.devtools.errors :as errors]
    [shadow.cljs.devtools.config :as config]
    [shadow.cljs.devtools.server.runtime :as runtime]
    [clojure.set :as set]
    [clojure.core.async.impl.protocols :as async-prot]
    [shadow.build.npm :as npm])
  (:import (java.io Writer InputStreamReader BufferedReader IOException ByteArrayOutputStream ByteArrayInputStream PrintWriter File)
           [java.net SocketException]
           [java.util List]))

(defn chan? [x]
  (satisfies? async-prot/ReadPort x))

(defn find-version-from-pom [pom-xml]
  (let [[_ version :as m]
        (->> (slurp pom-xml)
             (re-find #"<version>([^<]+)</version>"))]
    (when m
      version)))

(defn find-version []
  (let [pom-xml (io/resource "META-INF/maven/thheller/shadow-cljs/pom.xml")]

    (if (nil? pom-xml)
      "<snapshot>"
      (find-version-from-pom pom-xml))))

(defn async-logger [ch]
  (reify
    build-log/BuildLog
    (log*
      [_ state event]
      (async/offer! ch {:type :build-log
                        :event event}))))

(def null-log
  (reify
    build-log/BuildLog
    (log* [_ state msg])))

(def silent-log
  (reify build-log/BuildLog
    (log* [_ state {::build-log/keys [level] :as evt}]
      (when (not= level :info)
        (build-log/log* build-log/stdout state evt)
        ))))

;; .getCanonicalFile also resolves symlinks which we want to keep
;; and File itself doesn't have a way to only remove .. or . placeholders otherwise
;; so going to path and back immediately to avoid having to deal with non-normalized
;; paths later on
(defn normalize-file [^File file]
  (-> file
      (.toPath)
      (.normalize)
      (.toFile)))

(defn new-build
  [{:keys [build-id] :or {build-id :custom} :as build-config} mode opts]
  (let [{:keys [classpath cache-root build-executor babel config] :as runtime}
        (runtime/get-instance!)

        {:keys [cache-blockers]}
        config

        cache-dir
        (config/make-cache-dir cache-root build-id mode)

        npm-config
        (merge
          ;; global config so it doesn't have to be configured per build
          (select-keys (:js-options config) [:js-package-dirs :node-modules-dir :entry-keys :extensions])
          ;; build config supersedes global
          (select-keys (:js-options build-config) [:js-package-dirs :node-modules-dir :entry-keys :extensions]))

        ;; don't use shared npm instance since lookups are cached and
        ;; js-package-dirs may affect what things resolve to
        npm
        (npm/start npm-config)]

    (-> (build-api/init)
{'fraction_non_alphanumeric': 0.14308426073131955, 'fraction_numerical': 0.0, 'mean_word_length': 2.6790123456790123, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '4730859', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1889, 'n_words': 502}