Fork me on GitHub
#babashka
<
2023-10-14
>
nitin09:10:08

I made this command as alias to cljdoc in windows: Paste directly in cmd: for /f %i in ('bb -Sdeps "{:deps {io.github.lispyclouds/bblgum {:git/sha \"b1b939ae5ae522a55499a8260b450e8898f77781\"}}}" -e "(require '[bblgum.core :as b]) (->> (apropos \"\") (clojure.string/join \"\n\") (b/gum :filter :in) :result first symbol)"') do @bb -e "(doc %i)" Batch file escaped version: doskey cljdoc=FOR /F %%i in ('bb -Sdeps "{:deps {io.github.lispyclouds/bblgum {:git/sha \"b1b939ae5ae522a55499a8260b450e8898f77781\"}}}" -e "(require '[bblgum.core :as b]) (->> (apropos \"\") (clojure.string/join \"\n\") (b/gum :filter :in) :result first symbol)"') do @bb -e "(doc %%i)" The simpler version without using gum inside babashka: Paste directly in cmd: FOR /F %i in ('bb -e "(doseq [item (apropos \"\")] (println item))" ^| gum filter') do @bb -e "(doc %i)" Batch file escaped version (not working): doskey cljdoc=FOR /F %%i in ('bb -e "(doseq [item (apropos \"\")] (println item))" ^| gum filter') do @bb -e "(doc %%i)" In linux, none of them are working, I tried: bb -e "(doseq [item (apropos \"\")] (println item))" | gum filter | bb "(doc *input*)" bb -Sdeps "{:deps {io.github.lispyclouds/bblgum {:git/sha \"b1b939ae5ae522a55499a8260b450e8898f77781\"}}}" -e "(require '[bblgum.core :as b]) (->> (apropos \"\") (clojure.string/join \"\n\") (b/gum :filter :in) :result first symbol doc)" I saw source code of doc it's a macro, is it why it is unable to read input, find-doc works in above commands as its a function. Also, in windows too, linux like piped version wasn't working specifically for doc but was working for find-doc. What is the correct way to pipe data specifically for macros like doc? And correct command to make it work (more likely the gum outside babashka version) on both platforms?

borkdude09:10:40

It's easier if you leave all the shell stuff out of the equation and try to repro in pure Clojure

borkdude09:10:13

and also repro the string input separately. So solve one problem at a time

borkdude09:10:50

doc indeed is a macro and macros expect static input, in the case of doc it is a symbol, like (doc whatever) . (doc (slurp *in*)) doesn't work, as you say find-doc would work since that is the function equivalent

borkdude09:10:51

but you can work around this using another macro, like here: https://github.com/babashka/babashka/blob/master/examples/random_doc.clj

❤️ 1
nitin09:10:42

ok, i'll try using another macro as shown in random-doc and also inside a script instead of putting everything in string on shell 😅

nitin13:10:04

I couldn't make use of random-doc macro, to make get-doc, everytime it just returned java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to clojure.lang.Named, so ended up doing it manually:

(require '[bblgum.core :refer [gum]]
         '[clojure.string :as str]
         '[clojure.repl :as repl])

(defn filter-menu [input placeholder] (first (:result (gum :filter :in input :placeholder placeholder :fuzzy false))))

(defn print-doc [item-meta]
  (println "-------------------------")
  (let [namespace-value (str (:ns item-meta))
        function-name (:name item-meta)
        arg-list (:arglists item-meta)
        doc-string (:doc item-meta)]
    (println (str namespace-value "/" function-name))
    (println arg-list)
    (println doc-string)))

(let [item (filter-menu (str/join "\n" (repl/apropos "")) "Search documentation...")]
  (if (nil? item)
    (println "No documentation available.")
    (print-doc (meta (resolve (symbol item))))))

nitin13:10:41

This was get-doc (I tried):

(require '[clojure.repl])

(defmacro get-doc [sym]
  (if (:doc (meta (resolve sym)))
    `(clojure.repl/doc ~sym)
    `(println "No documentation available for" '~sym)))

(get-doc (symbol "clojure.core/diff")) ;; Error
Result from gum filter is a String, and passing that to get-doc gives this ClassCastException (clojure.lang.PersistentList cannot be cast to clojure.lang.Named) which I was unable to fix, hence, made manual print-doc function using metadata.

borkdude15:10:20

You are passing a list to get-doc . Macros don't evaluate their arguments

borkdude15:10:06

if you want to pass a string then write (resolve (symbol the-string)) in the macro instead

nitin15:10:47

I'm not passing a list to get-doc I am passing string only, also I tried (resolve (symbol the-string)) too before and again right now, it prints exactly the same exception error. I'm also scratching my head on this: `(defmacro get-doc [sym] (if (:doc (meta (resolve (symbol sym)))) (clojure.repl/doc (resolve (symbol ~sym))) (println "No documentation available for" '~sym)))`

borkdude15:10:32

(get-doc (symbol "clojure.core/diff"))
The argument here is a list

nitin15:10:37

how its a list?

picard-facepalm 1
borkdude16:10:12

macros do not evaluate their arguments.

borkdude16:10:35

(defmacro foo [x] (prn (type x)))
(foo (symbol "clojure.core/diff"))

nitin16:10:09

Ok got it, it is passing as (symbol "clojure.core/diff"), which is a list, sorry 😅, so I should pass a static input to a macro?

borkdude16:10:35

macros are compile time functions, typically used for code transformation

borkdude16:10:45

they only operate on what literally pass to them

nitin16:10:27

I gotta learn more about macros, I gotta try it, sorry about dumb questions.

Felix Dorner22:10:32

If I want to keep my library babashka compatible, what’s the best technique for that?

nate23:10:48

You could run your tests with babashka. I do that in my https://github.com/justone/bb-scripts project. I can run:

bb test
to run under Babashka or this:
clojure -M:clj:test
to run it under Clojure. I have it set up this way so I can develop my babashka scripts in Clojure, but it should be applicable to your library.

1
escherize02:10:24

I think there is also neil add test which does a similar (maybe the same) thing

Felix Dorner17:10:05

And then you have to keep the babashka included versions in sync manually in your deps, e.g.: https://github.com/justone/bb-scripts/blob/5f54bcec441622097b88539ddb809784925f6c67/deps.edn#L7 ? There’s no such thing as a maven parent with babashka libs, so that I could extend from that and then pull in libraries (e.g. babashka/fs) without specifying the version, as it’s provided by the parent?

borkdude18:10:16

@U05KWT468F8 re "parent", you can just make one dependency without any source which depends on other libraries.

bb print-deps
will print all libraries currently available in bb so you can generate a bb.edn from that, but I'd remove any libraries you're not using to reduce the amount of deps in your lib

👍 1
nate20:10:02

I’m sure someone could write a bb script to take the output of bb print-deps and then use rewrite-edn to update the corresponding ones in deps.edn.