Fork me on GitHub
#emacs
<
2022-10-28
>
dev-hartmann11:10:32

I am a total noob when it comes to emacs lisp, but I want to read a projects deps.edn and extract its aliases and present to ciders jack in

motform11:10:53

I'm not sure if cider already have the capacity to do something like that, but you might want to look at https://github.com/clojure-emacs/parseedn to get data out of deps.edn

dev-hartmann11:10:25

Awesome, you preceded my question 👌:skin-tone-4:

👍 1
teodorlu13:10:58

You could also shell out to babashka and write normal clojure :) I normally work with EDN like that.

dev-hartmann13:10:42

This was actually my first thought, call a script and reach for the result in emacs

dev-hartmann13:10:50

I‘m looking for the how to do that atm since my emacs foo is lacking 😅

teodorlu13:10:17

Mine too 😂 Way more comfortable in clojure.

dev-hartmann13:10:37

Haha, exactly… :rolling_on_the_floor_laughing:

teodorlu13:10:53

I can dig up some of my code when I'm in front of a computer in an hour or so

dev-hartmann13:10:15

That‘d be awesome!

👍 1
dpsutton13:10:08

Cider does this for shadow I think. You could just modify that

dev-hartmann13:10:37

Oh, didn’t know that… will have a look there

dev-hartmann13:10:34

Just found it in the docs. Cider jack in cljs does parse it from the shadow-Cluster file

dev-hartmann13:10:40

Should be similar to this

teodorlu14:10:54

as I'm reading my code, I'm realizing that my approach of writing Clojure forms and parsing them as quoted Elisp is probably a very very bad idea. But I promised to post the code, so I'm posting the code 🙂

(require 's)

(defmacro t-comment (&rest _) "Do nothing."
  nil)

(defun t-str (&rest xs)
  "Join XS into string."
  (apply #'s-concat
         (mapcar (lambda (s)
                   (format "%s" s))
                 xs)))

(defun t-bb-fun (form)
  "Convert quoted FORM to Babashka shell invocation."
  (t-str "bb -e " "'" form "'"))

;; Example:
;;
;;   (t-bb-fun '(assoc *input* :message "Hello!"))

(defmacro t-bb (form)
  "Convert FORM to babashka shell invocation."
  (list 't-bb-fun (list 'quote form)))

(t-comment
 ;; here's an example:

 (shell-command-to-string
  (t-bb (+ 1 2)))
 ;; => "3\n"

 (message
  (shell-command-to-string
   (t-bb (hash-map :x 1 :y 2))))
 ;; from *Messages"
 ;;
 ;; "{:y 2, :x 1}"
 )

dev-hartmann14:10:47

I already mimiced the cider function , but just parsing deps.edn

👍 1
dev-hartmann14:10:07

cider actually uses the same package you recommended for parsing edn 🙂

dev-hartmann15:10:58

wohoo…. got it working 🙂

dev-hartmann15:10:06

(defun cider--deps-parse-aliases (hash)
  (let* ((builds (when (hash-table-p hash)
                   (gethash :aliases hash)))
         (build-keys (when (hash-table-p builds)
                       (hash-table-keys builds))))
    (append build-keys '(:none))))

(defun cider--deps-get-aliases ()
  (let ((deps-edn (concat (clojure-project-dir) "deps.edn")))
    (when (file-exists-p deps-edn)
      (with-temp-buffer
        (insert-file-contents deps-edn)
        (let ((hash (car (parseedn-read '((map . identity))))))
          (cider--deps-parse-aliases  hash))))))

(defun cider-get-deps-aliases ()
  (interactive)
  (let ((options (completing-read "Select alias to run: "
                                      (cider--deps-get-aliases))))))

thanks2 1
dev-hartmann15:10:20

just in case someone cares 🙂 feedback welcome

Benjamin08:10:12

(cider--deps-parse-aliases  hash)
there is a space too many. Here is 1 way to put it into a command:
(defun cider--deps-parse-aliases (hash)
  (let* ((builds (when (hash-table-p hash)
                   (gethash :aliases hash)))
         (build-keys (when (hash-table-p builds)
                       (hash-table-keys builds))))
    (append build-keys '(:none))))

(defun cider--deps-get-aliases (deps-edn)
  (cider--deps-parse-aliases
   (car (with-temp-buffer
	  (insert-file-contents deps-edn)
	  (parseedn-read '((map . identity)))))))

(defun cider-jack-in-with-an-alias-from-deps ()
  (interactive)
  (if-let* ((deps-edn (concat (clojure-project-dir) "deps.edn"))
	    (_ (file-exists-p deps-edn)))
      (let* ((my-alias
	      (completing-read "Select alias to run: " (cider--deps-get-aliases deps-edn)))
	     (cider-clojure-cli-aliases
	      (concat
	       cider-clojure-cli-aliases ":" my-alias)))
	(call-interactively #'cider-jack-in-clj))
    (user-error "no deps.edn file in project")))
I updated cider--deps-get-aliases to be more inside out + and it takes the file as an arg. But your version is fine

Benjamin08:10:54

I made a version using dash pattern matching

(defun cider--deps-parse-aliases (edn)
  (-when-let*
      (((m) edn)
       ((&hash :aliases aliases) m))
    (append (hash-table-keys aliases) '(:none))))

(defun cider--deps-get-aliases (deps-edn)
  (cider--deps-parse-aliases
   (with-temp-buffer
     (insert-file-contents deps-edn)
     (parseedn-read '((map . identity))))))

dev-hartmann08:10:39

Nice! Looks really clean!

emacs 1
dev-hartmann08:10:07

Will try this evening to append the result to the jack-in command

dev-hartmann08:10:46

The plan is to check the preferred-build tool var, to either parse the project.clj profiles or deps.edn aliases. Check if there’s anything that overrides the default repl command and then do the jack in

dev-hartmann08:10:23

But first trying to get the happy path working 😅

dev-hartmann08:10:39

As my emacs lisp skills are lacking

Benjamin08:10:47

sounds like you can use cider-project-type

dev-hartmann10:10:21

Cool! Still haven’t explored the cider code enough 😅

caumond16:10:30

Hi guys, I have a multi repo, and my issue is that subprojects are recognized as projects. If I understand it well this is due to my projectile setting. So all searches are done in the subproject only. I would like to stick to the global mono repo and stop the auto discorvery thing, but I don't know how to do. Can you gave me a clue what's the best option to search, I tried many and failed many times already. If useful, the global repo I want to stick to is a git repo, the subrepo are recognized as clojure projects.

lukasz18:10:14

Setting this: https://github.com/lukaszkorecki/command-center/blob/master/settings/lk/navigation.el#L32-L38 made it workable for me - find-file works with in a sub-project and C-c p p shows a project selector so I can jump between different codebases within a single repo

Chris Clark21:10:48

That’s surprising that projectile is discovering the sub-projects first. Do you have .git directories in each of the subprojects or something? One thing you can do is put a .projectile file in the directory where you want projectile to consider the project.

caumond13:10:38

yes, I don't get the link between the doc I read and the behavior I end up with. Maybe a behavior of setup layers I use: doom + lsp clojure... In anycase, the .projectile solution seems to work. thx @U01ERKW6F18. I keep your solution @U0JEFEZH6 in case the other one is not actually not working. Thanks for the help!

🎉 1