Skip to content

Instantly share code, notes, and snippets.

@plexus
Created September 2, 2021 08:31
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 plexus/720798947e999a2142390a702a9ebe0b to your computer and use it in GitHub Desktop.
Save plexus/720798947e999a2142390a702a9ebe0b to your computer and use it in GitHub Desktop.
(require 'arc-mode)
(setq cider-nrepl-jar-url "https://repo.clojars.org/cider/cider-nrepl/0.26.0/cider-nrepl-0.26.0.jar")
(setq cider-cache-dir (expand-file-name "cider-cache" user-emacs-directory))
(setq cider-jar-temp-dir
(make-temp-name
(expand-file-name "cider-jar"
temporary-file-directory)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Shell out to a clojure tool approach
(defun cider-deps-executable ()
"Find an executable that can compute a classpath"
(cond
((executable-find "clojure")
'("clojure"))
((executable-find "deps")
'("deps"))
((executable-find "bb")
'("bb" "clojure"))))
(defmacro with-binary-buffer (&rest body)
`(with-temp-buffer
(set-buffer-multibyte nil)
(setq buffer-file-coding-system 'binary)
,@body))
(defun cider-exec-to-string (args)
(with-binary-buffer
(apply #'call-process (car args) nil (current-buffer) nil (cdr args))
(buffer-substring-no-properties (point-min) (point-max))))
(defun cider-nrepl-classpath ()
"Compute a classpath that contains cider-nrepl."
(let ((command (cider-deps-executable)))
(split-string
(string-trim
(cider-exec-to-string
(seq-concatenate 'list
command
'("-Srepro" "-Spath" "-Sdeps" "{:deps {cider/cider-nrepl {:mvn/version \"RELEASE\"}}}"))))
":")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Just grab the jar approach
(defun cider-nrepl-jar-path ()
"Download cider-nrepl.jar and cache it.
Returns the path to the jar"
(let ((path (expand-file-name (file-name-nondirectory cider-nrepl-jar-url) cider-cache-dir)))
(if (file-exists-p path)
path
(make-directory cider-cache-dir t)
(url-copy-file cider-nrepl-jar-url path)
path)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; "classpath" lookup using arc-mode functionality
(defun cider-jar-contents (jarfile)
"Get the list of filenames in a jar (or zip) file."
(with-binary-buffer
(insert-file-contents-literally jarfile nil)
(seq-map #'archive--file-desc-ext-file-name
(archive-zip-summarize))))
(defun cider-jar-contains-p (jarfile name)
"Does the jar contain a file with the given name?"
(seq-find (lambda (p) (equal name p))
(cider-jar-contents jarfile)))
(defun cider-find-resource-in-jar (jarfile name)
"Return a file in a jar as a string."
(make-directory cider-jar-temp-dir)
(when (cider-jar-contains-p jarfile name)
(let ((default-directory cider-jar-temp-dir))
(with-binary-buffer
(archive-zip-extract jarfile name)
(buffer-substring-no-properties (point-min) (point-max))))))
(defun cider-find-resource (paths name)
"Find a resource in a list of directories or jar files.
Similar to a classpath lookup. Returns the file contents as a string."
(let ((path (car paths)))
(when path
(or
(cond
((file-directory-p path)
(let ((expanded (expand-file-name name path)))
(when (file-exists-p expanded)
(with-binary-buffer
(insert-file-contents-literally expanded nil)
(buffer-substring-no-properties (point-min) (point-max))))))
((and (file-exists-p path) (string-suffix-p ".jar" path))
(cider-find-resource-in-jar path name)))
(cider-find-resource (cdr paths) name)))))
(setq cp (cider-nrepl-classpath))
;; ("src" "/home/arne/.m2/repository/cider/cider-nrepl/0.26.0/cider-nrepl-0.26.0.jar" "/home/arne/.m2/repository/org/clojure/clojure/1.10.3/clojure-1.10.3.jar" "/home/arne/.m2/repository/nrepl/nrepl/0.8.3/nrepl-0.8.3.jar" "/home/arne/.m2/repository/org/clojure/core.specs.alpha/0.2.56/core.specs.alpha-0.2.56.jar" "/home/arne/.m2/repository/org/clojure/spec.alpha/0.2.194/spec.alpha-0.2.194.jar")
(cider-find-resource-in-jar
"/home/arne/.m2/repository/cider/cider-nrepl/0.26.0/cider-nrepl-0.26.0.jar"
"cider/nrepl.clj")
;; "(ns cider.nrepl ..."
(cider-find-resource
classpath
"cider/nrepl.clj")
(cider-find-resource-in-jar
(cider-nrepl-jar-path)
"cider/nrepl.clj")
(defmacro measure-time (&rest body)
"Measure the time it takes to evaluate BODY."
`(let ((time (current-time))
(res (progn ,@body)))
(message "%.06f" (float-time (time-since time)))
res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment