Skip to content

Instantly share code, notes, and snippets.

@noisesmith
Created June 13, 2019 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noisesmith/301713f0c7a4e3c98149ecf97556dcff to your computer and use it in GitHub Desktop.
Save noisesmith/301713f0c7a4e3c98149ecf97556dcff to your computer and use it in GitHub Desktop.
custom user.clj, including a bunch of stuff I don't often use...
(ns user
(:require [clojure.test :as test]))
(defmacro lf
[f]
(load-file (str "/tmp/" (str f) ".clj")))
(defmacro l
[f]
`(lf ~(str "/clj-tmp-files/" f)))
(def src-dir (str (System/getenv "HOME")
"/sprinklr"))
(defmacro ll
[project & package]
`(load-file (str (clojure.string/join
"/"
[src-dir
~(str project)
"src"
~@(map str package)])
".clj")))
(defmacro le
[project & rest-package]
(list* `ll project project rest-package))
(defn exercise-coverage
([the-namespace]
(exercise-coverage (fnil inc 0) the-namespace))
([tracker the-namespace]
(apply exercise-coverage
tracker
the-namespace
(keys (ns-publics the-namespace))))
([tracker the-namespace & symbols]
(println "exercising " (pr-str symbols))
(let [state (atom {})]
(doseq [a-symbol symbols
:let [f-var (ns-resolve the-namespace a-symbol)
f @f-var]
:when (and (ifn? f)
(not (:macro (meta f-var))))]
(intern the-namespace a-symbol
(fn [& args]
;; old clojure version compat
(swap! state update-in [a-symbol] tracker)
(apply f args))))
state)))
(defn non-macros
[target-ns]
(map key
(remove #(:macro (meta (val %)))
(ns-publics target-ns))))
(defmacro with-use
[target-ns & body]
`(let ~(into []
(mapcat (juxt identity (fn [s] (symbol (name target-ns) (name s))))
(non-macros target-ns)))
~@body))
(defn set-debug
[target-var]
(let [metadata (meta target-var)
target (::original metadata @target-var)
debug-data (:debug-trace metadata (atom []))]
;; only one layer of debugging, if we were already debugging,
;; just replace the previous layer
(when-not (and (:debug-trace metadata)
(::original metadata))
(alter-meta! target-var assoc
::original target
:debug-trace debug-data))
(alter-var-root
target-var
(constantly
(fn replaced-debugging-function
[& args]
(let [result (try (apply target args)
(catch Exception e
{::error e}))]
(swap! debug-data conj {:at (java.util.Date.)
:args args
:result result})
(if-let [error (::error result)]
(throw error)
result)))))))
(defn unset-debug
[target-var]
(let [metadata (meta target-var)
debug-data (:debug-trace metadata)]
(when-let [original (::original metadata)]
(alter-meta! target-var dissoc ::original :debug-trace)
(alter-var-root target-var (constantly original)))
@debug-data))
(defn all-debugged
[]
(->> (all-ns)
(mapcat (comp vals ns-publics))
(filter (comp :debug-trace meta))))
(defmacro keyed
[& binds]
(into {}
(map (juxt (comp keyword name)
identity))
binds))
(defmacro locals
[]
`(keyed ~@(keys &env)))
#_
(defn all
"for validating that clj files under a path actually compile"
[path-string]
(let [path (java.io.File. path-string)]
(doseq [file (file-seq path)
:let [file-name (.getName file)]
:when (and (.endsWith file-name ".clj")
(not (= file-name "project.clj")))]
(try (load-file (str file))))))
(defn test-reload
[ns-sym other-namespaces other-files]
(doseq [n-s other-namespaces]
(require n-s :reload))
(require ns-sym :reload)
(doseq [fname other-files]
(load-file fname))
(in-ns ns-sym))
(defn test-reload-ns
[ns-sym & [other-namespaces other-files]]
(test-reload ns-sym other-namespaces other-files)
(test/run-tests ns-sym))
(defn test-reload-t
[t-sym & [other-namespaces other-files]]
(let [test-ns (symbol (namespace t-sym))]
(test-reload test-ns other-namespaces other-files)
(test/test-var (resolve t-sym))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment