<commit_before>(ns tailrecursion.boot.core
  (:require [cemerick.pomegranate           :as pom]
            [clojure.java.io                :as io]
            [clojure.set                    :refer [difference]]
            [clojure.string                 :refer [join]]
            [clojure.pprint                 :refer [pprint]]
            [tailrecursion.boot.tmpregistry :as tmp])
  (:import [java.net URLClassLoader URL]))

;; INTERNAL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defmacro guard [expr & [default]]
  `(try ~expr (catch Throwable _# ~default)))

(defn load-sym [sym]
  (when-let [ns (and sym (namespace sym))] (require (symbol ns)))
  (or (resolve sym) (assert false (format "Can't resolve #'%s." sym))))

(defn index-of [v val]
  (ffirst (filter (comp #{val} second) (map vector (range) v))))

(defn exclude [syms coordinate]
  (if-let [idx (index-of coordinate :exclusions)]
    (let [exclusions (get coordinate (inc idx))]
      (assoc coordinate (inc idx) (into exclusions syms)))
    (into coordinate [:exclusions syms])))

(defn add-dependencies! [deps repos]
  (let [deps (mapv (partial exclude ['org.clojure/clojure]) deps)]
    (pom/add-dependencies :coordinates deps :repositories (zipmap repos repos))))

(defn add-directories! [dirs]
  (when (seq dirs)
    (let [meth (doto (.getDeclaredMethod URLClassLoader "addURL" (into-array Class [URL])) (.setAccessible true))]
      (.invoke meth (ClassLoader/getSystemClassLoader) (object-array (map #(..  (io/file %) toURI toURL) dirs))))))

(defn configure! [old new]
  (let [[nd od] [(:dependencies new) (:dependencies old)] 
        [ns os] [(:src-paths new) (:src-paths old)]]
    (when-not (= nd od) (add-dependencies! nd (:repositories new)))
    (when-not (= ns os) (add-directories! (difference ns os)))))

(defn require-task [tasks [ns & {:keys [refer as]}]]
  {:pre [(symbol? ns)
         (or as refer)
         (or (nil? as) (symbol? as))
         (or (nil? refer) (= :all refer) (vector? refer))]}
  (require ns)
  (let [pub (-> (->> (ns-publics ns) (map (fn [[k v]] [k (meta v)]))) 
                (->> (filter (comp ::task second)) (map first))) 
        mk  (fn [sym] {:main [(symbol (str ns) (str sym))]})
        r   (when refer
              (->> pub
                (filter #(or (= :all refer) (contains? (set refer) %)))
                (map #((juxt keyword mk) %)))) 
        a   (when as
              (map #((juxt (comp (partial keyword (str as)) str) mk) %) pub))] 
    (merge (reduce into {} [r a]) tasks)))

(defn require-tasks [env]
  (assoc env :tasks (reduce require-task (:tasks env) (:require-tasks env))))

(defn prep-task [env task args]
  (let [m     (or (:main task) '[tailrecursion.boot.core.task/nop]) <commit_msg>fix issue with adding URLs to classpath<commit_after>(ns tailrecursion.boot.core
  (:require [cemerick.pomegranate           :as pom]
            [clojure.java.io                :as io]
            [clojure.set                    :refer [difference]]
            [clojure.string                 :refer [join]]
            [clojure.pprint                 :refer [pprint]]
            [tailrecursion.boot.tmpregistry :as tmp])
  (:import [java.net URLClassLoader URL]))

;; INTERNAL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defmacro guard [expr & [default]]
  `(try ~expr (catch Throwable _# ~default)))

(defn load-sym [sym]
  (when-let [ns (and sym (namespace sym))] (require (symbol ns)))
  (or (resolve sym) (assert false (format "Can't resolve #'%s." sym))))

(defn index-of [v val]
  (ffirst (filter (comp #{val} second) (map vector (range) v))))

(defn exclude [syms coordinate]
  (if-let [idx (index-of coordinate :exclusions)]
    (let [exclusions (get coordinate (inc idx))]
      (assoc coordinate (inc idx) (into exclusions syms)))
    (into coordinate [:exclusions syms])))

(defn add-dependencies! [deps repos]
  (let [deps (mapv (partial exclude ['org.clojure/clojure]) deps)]
    (pom/add-dependencies :coordinates deps :repositories (zipmap repos repos))))

(defn add-directories! [dirs]
  (when (seq dirs)
    (let [meth  (doto (.getDeclaredMethod URLClassLoader "addURL" (into-array Class [URL]))
                  (.setAccessible true))
          cldr  (ClassLoader/getSystemClassLoader)
          urls  (->> dirs (map io/file) (filter #(.exists %)) (map #(.. % toURI toURL)))]
      (doseq [url urls] (.invoke meth cldr (object-array [url]))))))

(defn configure! [old new]
  (let [[nd od] [(:dependencies new) (:dependencies old)] 
        [ns os] [(:src-paths new) (:src-paths old)]]
    (when-not (= nd od) (add-dependencies! nd (:repositories new)))
    (when-not (= ns os) (add-directories! (difference ns os)))))

(defn require-task [tasks [ns & {:keys [refer as]}]]
  {:pre [(symbol? ns)
         (or as refer)
         (or (nil? as) (symbol? as))
         (or (nil? refer) (= :all refer) (vector? refer))]}
  (require ns)
  (let [pub (-> (->> (ns-publics ns) (map (fn [[k v]] [k (meta v)]))) 
                (->> (filter (comp ::task second)) (map first))) 
        mk  (fn [sym] {:main [(symbol (str ns) (str sym))]})
        r   (when refer
              (->> pub
                (filter #(or (= :all refer) (contains? (set refer) %)))
                (map #((juxt keyword mk) %)))) 
        a   (when as
              (map #((juxt (comp (partial keyword (str as)) str) mk) %) pub))] 
    (merge (reduce into {} [r a]) tasks)))

(defn require-tasks [env]
  (assoc env :tasks (reduce require-task (:tasks env) (:require-tasks env))))

(defn prep-task [env task args]
  (let [m     (or (:main task) '[tailrecursion.boot.core.task/nop]) 
{'fraction_non_alphanumeric': 0.1936968838526912, 'fraction_numerical': 0.0, 'mean_word_length': 2.7685123415610406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '7368396', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1867, 'n_words': 578}