Fork me on GitHub
#babashka
<
2023-04-15
>
anovick11:04:27

Hey guys 🙂 Got a few questions on bb hope somebody could help out by answering 🙏 1. Is it possible to work on babashka code with a REPL? i.e. live-coding and sending code to the REPL for inspection? I am developing with Cursive atm so if theres an easy integration for that I would like to know (probably need to configure a new REPL I suppose) 2. Also, what is the design philosophy behind babashka/fs? does it aim to have full coverage over filesystem APIs, or is it just some useful subset of those? I guess it has the things that the author deems most useful to have for scripting, is that the case?

borkdude11:04:03

@UDHL22ZFE 1. Absolutely! https://cursive-ide.com/userguide/babashka.html 2. The use cases in babashka.fs are driven by whatever comes up in the area of file system manipulation. What you're seeing there is what has come up so far

anovick11:04:16

@U04V15CAJ Awesome! thanks :hugging_face:

anovick12:04:06

@U04V15CAJ I got around to trying this out! I followed the guide: installed deps and configured the BB repl It just works! 🥳 All I did with it this time was just use fs/list-dir to show me the files on my home directory and then call count on it to let me know how many files/dirs there are. It's important for me to set this up ahead of time for when I would need to write something quickly and won't have time to bother with tooling Thanks for your help! :hugging_face:

👍 2
Stuart11:04:26

Hey, probably a stupid question or I'm doing something dumb. I have a little personal project that I run against a webserver at home. The web server runs all the time on port 7229, but when I'm running it in debug mode it runs on 7145 (so I can have both at the same time). So in my front end I have a

(def port 7145)
Which I use when building the API call URLS. I'm writing a release bash script which will build my backend, build my front end and copy it over to the correct folder on my machine. I'd like to first as a step in though to update my settings.cljs file and change (def port 7145) to (def port 7229) . Is their an easy way to do this in babashka, or any other tool I might be missing that will let me easy change some clojure source? I can read the whole file in, find that text and replace it and write it back out again, but I'm sure there must be an easier way / this must be a solved problem already 😄

borkdude11:04:58

You can also use a macro to capture some environment variable at compile time

borkdude11:04:34

If you want to do it with rewriting, you can simply use str/replace and put some recognizable comments around it which you could use in a regex, I've done this in several libraries

borkdude11:04:47

Another way is to use #CHB5Q2XUJ which is available in bb

Stuart11:04:59

fantastic, thank you. I'll have a look at each of those options.

Stuart11:04:19

oooh rewrite-clj looks cool. ANd zippers is on my backlog to learn, so that's an excuse (prod) to get tothat!

Stuart14:04:49

Thanks again for your help on this. I wrote a little script that finds my (def port ) and rewrites it to the release port using babashka and re-write-clj, works perfectly. rewrite-clj is so neat!

🎉 2
borkdude14:04:24

Agreed, rewrite rocks!

Schmoho14:04:38

I am a newbie trying to write a script that reads a bunch of lines via pipe from stdin and for each line requires additional input from the user, but I cannot figure it out ... When I slurp the entire input to iterate over it later, the stream gets closed. When I try to do both things in the same lineseq the read-line call reads from the piped stdin. So I assume that if there is no call to interject a read-line specifically from tty, I would need some way to read the input and not close stdin, although that of course would be somewhat more inefficient. I would really appreciate some help ...

borkdude14:04:07

AFAIK this is not possible: you either read from stdin or from the user's input

Schmoho14:04:47

Isn't there some way to prevent a stream from closing then?

nate17:04:02

I remember using /dev/tty as a read source when I need to read all of stdin and then prompt the user for something in a go program I wrote a while ago.

nate17:04:12

Here's a very rough babashka script that does the same:

#!/usr/bin/env bb

(ns stdin
  (:require [ :as io]
            [clojure.string :as string]))

(when (= *file* (System/getProperty "babashka.file"))
  (let [in-lines (map string/trim (string/split-lines (slurp *in*)))
        tty (io/reader (io/file "/dev/tty"))]
    (binding [*in* tty]
      (doseq [line in-lines]
        (println line)
        (println "Keep this line?")
        (if (string/includes? (string/lower-case (read-line)) "y")
          (println " keeping")
          (println " discarding"))
        (println "--------------------------------------")))))

borkdude17:04:02

Here is a short demo of reading stdin + user input in a similar way:

$ echo 'hello' | bb -e '(prn (read-line)) (binding [*in* (io/reader "/dev/tty")] (prn (read-line)))'
"hello"
123
"123"