babashka

https://github.com/babashka/babashka. Also see #sci, #nbb and #babashka-circleci-builds .
roklenarcic 2020-12-04T14:05:45.067100Z

If anyone is using Cursive, how do you connect to bb nrepl or socket repl?

borkdude 2020-12-04T14:54:45.067400Z

@roklenarcic https://book.babashka.org/#repl and then ctrl-f for Cursive

borkdude 2020-12-04T14:55:01.067900Z

(sorry, that part of the docs was not included, I added it back now)

roklenarcic 2020-12-04T14:57:34.068100Z

thanks

kwrooijen 2020-12-04T14:59:11.068700Z

is there any type of "lein" tool for BB? Dependency management / configuration / environment management etc

borkdude 2020-12-04T15:03:51.069900Z

@kevin.van.rooijen Not yet. We're brainstorming about this here: https://github.com/borkdude/babashka/issues/473 For dependency management you currently use clojure and set the printed classpath using the -Spath option to the BABASHKA_CLASSPATH environment variable or you pass the string directly to the --classpath option.

borkdude 2020-12-04T15:04:45.070600Z

so typically: bb --classpath $(clojure -Spath) -m foo.main

kwrooijen 2020-12-04T15:05:49.070800Z

Ah cool. I'll read up on that

kwrooijen 2020-12-04T15:06:20.071500Z

Also, unrelated; Any plans / ideas / is it even possible for bytecode compiled BB? Basically to improve require speeds, similar to pyc files in python

kwrooijen 2020-12-04T15:09:44.071700Z

Or elc files in Elisp

borkdude 2020-12-04T15:10:43.072600Z

No plans yet, but it's a cool idea.

kwrooijen 2020-12-04T15:11:00.073Z

Do you think that's possible? I haven't looked at the implementation of sci

kwrooijen 2020-12-04T15:11:04.073300Z

But that would be amazing

kwrooijen 2020-12-04T15:11:30.073900Z

Also for obfuscation purposes

borkdude 2020-12-04T15:12:28.074900Z

I think the output from the analyzer namespace could be made printable so the interpreter could load that instead of parsing and analyzing the source from scratch. But this would need benchmarking to see if loading the analyzed output is actually faster (it should be but you never know unless you measure).

borkdude 2020-12-04T15:13:27.075200Z

If you want to know more about sci internals, I recommend watching my talk on it: https://youtu.be/pgNp4Lk3gf0

๐Ÿ‘€ 1
kwrooijen 2020-12-04T15:13:47.075500Z

Will do

borkdude 2020-12-04T15:15:29.076300Z

@kevin.van.rooijen In this area, running the uberscript + carve over the namespaces will likely also help. https://book.babashka.org/#_uberscript

kwrooijen 2020-12-04T15:23:36.076700Z

Yeah that's a nice flag

kwrooijen 2020-12-04T20:17:05.077700Z

Is there any way to get the classpaths in BB? There is a cp-state atom that gets modified when you run add-classpath but it's not accessible AFAIK https://github.com/borkdude/babashka/blob/67c33b227009b47131fb145bed42cf4742aa4586/src/babashka/main.clj#L356 In clj you would use clojure.java.classpath I think, but that doesn't work in bb

kwrooijen 2020-12-04T20:17:27.078200Z

My goal is to get all clj files that are available to be required

borkdude 2020-12-04T20:18:50.078800Z

We could make this available under a system property, but a function in babashka.classpath could also work

kwrooijen 2020-12-04T20:19:02.079Z

I can make a PR for that

borkdude 2020-12-04T20:19:17.079400Z

let's first decide on the solution then :)

kwrooijen 2020-12-04T20:19:43.080100Z

I think a function is babashka.classpath would be nice

borkdude 2020-12-04T20:19:54.080500Z

There are already these: https://book.babashka.org/#_system_properties But maybe a function is better, since the value can change. Right.

kwrooijen 2020-12-04T20:20:48.081200Z

It's also a "logical" place to look, since the namespace is already called classpath

borkdude 2020-12-04T20:20:58.081500Z

get-classpath? or just classpath ?

kwrooijen 2020-12-04T20:21:35.082200Z

Multiple directories is still a single classpath, right?

kwrooijen 2020-12-04T20:21:41.082400Z

So it wouldn't be classpaths ?

borkdude 2020-12-04T20:21:59.082900Z

yes, I think it should return the classpath as is, so foo:bar:baz, that's one classpath

kwrooijen 2020-12-04T20:22:21.083300Z

Ok, I'd prefer classpath myself

borkdude 2020-12-04T20:22:30.083500Z

ok, go ahead then

kwrooijen 2020-12-04T20:22:39.083800Z

There aren't really any side effects, other than reading an atom

kwrooijen 2020-12-04T20:22:52.084Z

a seq of strings or vector?

kwrooijen 2020-12-04T20:23:16.084300Z

Or just the raw string with colon separator?

borkdude 2020-12-04T20:23:34.084600Z

the raw string

kwrooijen 2020-12-04T20:23:41.084800Z

๐Ÿ‘

borkdude 2020-12-04T20:24:24.085700Z

we could add a function parse or something, but I would do that as a separate function and only if there is a good reason to add it. generally it's just (split ... (re-pattern file-separator))

borkdude 2020-12-04T20:24:40.085900Z

for now let's only do classpath

kwrooijen 2020-12-04T20:25:40.086100Z

Works for me

2020-12-04T20:42:20.086600Z

#!/usr/bin/env bb
(require '[babashka.classpath :refer [add-classpath]]
         '[clojure.java.shell :refer [sh]]
         '[org.httpkit.server :as server])

(defn app [req]
  {:status  200
   :headers {"Content-Type" "text/html"}
   :body    "hello HTTP!"})

(defonce server (atom nil))

(defn stop-server []
  (when-not (nil? @server)
    ;; graceful shutdown: wait 100ms for existing requests to be finished
    ;; :timeout is optional, when no timeout, stop immediately
    (@server :timeout 100)
    (reset! server nil)))

(defn -main [& args]
  ;; The #' is useful when you want to hot-reload code
  ;; You may want to take a look: <https://github.com/clojure/tools.namespace>
  ;; and <https://http-kit.github.io/migration.html#reload>
  (reset! server (server/run-server #'app {:port 8080})))

(-main)
Why this is not running the web-server?

2020-12-04T20:42:29.086800Z

it's returning me

2020-12-04T20:42:41.087100Z

#object[clojure.lang.AFunction$1 0x4d572c3e "clojure.lang.AFunction$1@7fa00b1a1648"]

kwrooijen 2020-12-04T20:43:12.087500Z

I think because the main thread isn't blocking

kwrooijen 2020-12-04T20:43:27.087900Z

It starts the server on a new thread and then exits the main thread, exiting the program

dharrigan 2020-12-04T20:43:48.088100Z

yes

dharrigan 2020-12-04T20:43:49.088300Z

If you do this

dharrigan 2020-12-04T20:44:25.088800Z

(defn -main [&amp; args]
  ;; The #' is useful when you want to hot-reload code
  ;; You may want to take a look: <https://github.com/clojure/tools.namespace>
  ;; and <https://http-kit.github.io/migration.html#reload>
  (reset! server (server/run-server #'app {:port 8080}))
  @(promise))

(-main)

dharrigan 2020-12-04T20:44:44.089200Z

the promise will block (since it canโ€™t be derefโ€™ed)

dharrigan 2020-12-04T20:45:00.089400Z

โฏ http :8080
HTTP/1.1 200 OK
Content-Length: 11
Content-Type: text/html
Date: Fri, 04 Dec 2020 20:43:45 GMT
Server: http-kit

hello HTTP!

๐Ÿ‘ 1
2020-12-04T20:49:15.089800Z

aw thanks thanks

2020-12-04T20:49:16.090Z

๐Ÿ™‚

dharrigan 2020-12-04T20:50:09.090200Z

Youโ€™re most welcome ๐Ÿ˜‰

2020-12-04T20:50:17.090500Z

yey thanks!

nate 2020-12-04T22:26:41.093400Z

Neat, just tried interpolating a whole form into my $ usage (from babashka.process), and it worked well. I was writing something like this:

(let [arg (str "/home" "/nate")]
  ($ ls ~arg))
but now I write this:
($ ls ~(str "/home" "/nate"))

๐Ÿ˜Ž 3