Fork me on GitHub
#babashka
<
2020-06-07
>
Ian Fernandez04:06:56

anyone having problems with clj-http lite?

Ian Fernandez04:06:14

I couldn't require the client ns

Ian Fernandez04:06:19

using babashka

Ian Fernandez16:06:54

deps.edn =>

:paths   ["src"]
 :deps    {medley                          {:mvn/version "1.3.0"}
           org.clojure/data.xml            {:mvn/version "0.0.8"}
           com.amazonaws/aws-java-sdk-s3   {:mvn/version "1.11.796"}
           org.martinklepsch/clj-http-lite {:mvn/version "0.4.3"}
           clj-http                        {:mvn/version "3.10.1"}
           ;;potemkin                        {:mvn/version "0.4.5"}
           }
 :aliases {:test {:extra-paths ["test"]}}}

Ian Fernandez16:06:17

repl script =>

#!/bin/bash
export BABASHKA_CLASSPATH=$(clojure -Spath)
#export BABASHKA_PRELOADS="(require '[new.namespace])"

bb --classpath src:test --nrepl-server 1666

Crispin05:06:49

@d.ian.b try setting your classpath

borkdude07:06:07

If you don’t want a library there’s also babashka.curl @d.ian.b

Ian Fernandez16:06:44

I want a library that I can use both in clj and bb

borkdude16:06:42

babashka.curl also works in clojure

borkdude16:06:12

but so does clj-http-lite.

Ian Fernandez16:06:58

I'll try to use babashka.fcurl in clj then

Ian Fernandez16:06:12

I'm trying to migrate ancient-clj to bb / clj

borkdude16:06:26

You can also use reader conditionals with :bb for babashka specific code

Ian Fernandez17:06:14

yup but It's not working on cider =(

borkdude18:06:26

what do you mean?

borkdude18:06:05

CIDER should support this, else it's a bug

borkdude19:06:29

Btw, I just tried from CIDER and it does seem to work?

Ian Fernandez20:06:53

I was saying about the less opaque part of the #?()

Ian Fernandez20:06:08

that bb has to be lighter than clj

Ian Fernandez20:06:28

but thanks 😃

borkdude12:06:52

Invoke vim in the middle of a script, discovered by @marques.goncalves.fel :

#!/usr/bin/env bb

(require '[ :as io])

(defn vim [file]
  (->
   (ProcessBuilder. ["vim" file])
   (.inheritIO)
   (.start)
   (.waitFor)))

(def readme
  (let [f (io/file "README.md")]
    (when (not (.exists f))
      (vim (.getPath f)))
    (slurp f)))

(println readme)

Burin17:06:54

I am publishing my very first Babashka pod that uses multigrep library. https://github.com/agilecreativity/pod-b12n-mgrep

borkdude18:06:49

@agilecreativity nice! any reason you're not just using it directly?

borkdude@MBP2019 ~ $ export BABASHKA_CLASSPATH=$(clojure -Sdeps '{:deps {clj-commons/multigrep {:mvn/version "0.5.0"}}}' -Spath)
borkdude@MBP2019 ~ $ rlwrap bb 
Babashka v0.1.1-SNAPSHOT REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (require '[multigrep.core :as mg])
nil
user=> (first (mg/grep #"babashka" "/Users/borkdude/Dropbox/dev/clojure/babashka/README.md"))
{:file "/Users/borkdude/Dropbox/dev/clojure/babashka/README.md", :line "<img src=\"logo/babashka.svg\" width=\"425px\">", :regex #"babashka", :re-seq ("babashka"), :line-number 1}

Burin18:06:56

Wow, forgot about -Spath , I want to added one more function to it to my use-case. e.g. greplace-all! in this case. And just want to learn how to leverage the pod system with it. I know it is very simple library (easy for me to start with). Thanks for creating Babashka and the pod system. It is really nice to be able to interact with many library via the cli.

Burin18:06:06

qq: I am getting the error when I remove the following pr-str in the calling of the function. e.g. https://github.com/agilecreativity/pod-b12n-mgrep/blob/master/src/pod/b12n/mgrep.clj#L68

Burin18:06:21

Must i return string all the time for the main function to work?

Burin18:06:20

e.g. if I remove the pr-str from the last line I am getting some errors:

(defn grep
  [pattern file]
  (-> (mgrep/grep (re-pattern pattern) file)
      pr-str))

borkdude18:06:32

what is the error

Burin18:06:42

Let me try to replicate it.

borkdude18:06:52

I'll be right back

Burin18:06:12

Here is the errors:

#error {
 :cause "No dispatch macro for: \""
 :via
 [{:type java.lang.RuntimeException
   :message "No dispatch macro for: \""
   :at [clojure.lang.Util runtimeException "Util.java" 221]}]
 :trace
 [[clojure.lang.Util runtimeException "Util.java" 221]
  [clojure.lang.EdnReader$DispatchReader invoke "EdnReader.java" 552]
  [clojure.lang.EdnReader readDelimitedList "EdnReader.java" 757]
  [clojure.lang.EdnReader$MapReader invoke "EdnReader.java" 680]
  [clojure.lang.EdnReader readDelimitedList "EdnReader.java" 757]
  [clojure.lang.EdnReader$ListReader invoke "EdnReader.java" 654]
  [clojure.lang.EdnReader read "EdnReader.java" 145]
  [clojure.lang.EdnReader read "EdnReader.java" 111]
  [clojure.lang.EdnReader readString "EdnReader.java" 67]
  [clojure.edn$read_string invokeStatic "edn.clj" 46]
  [babashka.pods.impl$processor$fn__15148 invoke "impl.clj" 82]
  [babashka.pods.impl$processor invokeStatic "impl.clj" 94]
  [babashka.pods.sci$fn__15240$fn__15273 invoke "sci.clj" 63]
  [sci.impl.vars$binding_conveyor_fn$fn__7581 invoke "vars.cljc" 142]
  [clojure.core$binding_conveyor_fn$fn__5758 invoke "core.clj" 2032]
  [clojure.lang.AFn call "AFn.java" 18]
  [java.util.concurrent.FutureTask run "FutureTask.java" 264]
  [java.util.concurrent.ThreadPoolExecutor runWorker "ThreadPoolExecutor.java" 1128]
  [java.util.concurrent.ThreadPoolExecutor$Worker run "ThreadPoolExecutor.java" 628]
  [java.lang.Thread run "Thread.java" 834]
  [com.oracle.svm.core.thread.JavaThreads threadStartRoutine "JavaThreads.java" 517]
  [com.oracle.svm.core.posix.thread.PosixJavaThreads pthreadStartRoutine "PosixJavaThreads.java" 193]]}

Burin18:06:32

I push my changes to develop branch: The diff was:

(defn grep
   [pattern file]
   (-> (mgrep/grep (re-pattern pattern) file)
-      pr-str))
+      #_pr-str
+      identity
+      ))

Burin18:06:55

It is failing around this call:

(mgrep/grep "defproject" "project.clj")

Burin18:06:15

The code to replicate the issue is in smoke-test.clj in the root of the project:

#!/usr/bin/env bb
(require '[babashka.pods :as pods])
(pods/load-pod ["pod-b12n-mgrep"])
(require '[pod.b12n.mgrep :as mgrep])

{:mgrep-project
 (mgrep/grep "defproject" "project.clj")

 :mgrep-deps
 (mgrep/grep "mvn" "deps.edn")

Burin18:06:00

When run directly I am getting the correct result:

(grep "defproject" "project.clj")

;;=>
({:file "project.clj", :line "(defproject net.b12n/pod-mgrep \"1.0.0\"", :line-number 1, :regex #"defproject", :re-seq ("defproject")})

borkdude18:06:41

your error seems to indicate you're trying to send a regex over the wire?

user=> (edn/read-string (pr-str #"foo"))
Execution error at user/eval5 (REPL:1).
No dispatch macro for: "

Burin18:06:33

I see, I noticed that also that anything that is not string will not work. Right?

borkdude18:06:19

The protocol uses EDN or JSON. So arguments serialized as EDN should be also readable as EDN

Burin18:06:20

Got it, so I can also send it using json by specify “format” “json” ?

borkdude18:06:54

You can also define "client side" functions to work around limitations.

Burin18:06:23

Can you explain what is client side function?

borkdude18:06:24

E.g. you can define (defn foo [x] (if (regex? x) {:regex (str x)}) and then decode :regex as a regex in your pod.

Burin18:06:11

Thank you. Now it all make sense to me.

borkdude18:06:19

I'm also using this in the sql pods to send connections between bb and the pod. connections are just represented as a map with an id in it

borkdude18:06:29

and I translate those maps back to a connection on the pod side

👍 4
borkdude18:06:34

one other tip if you weren't aware of it: you can test your pod with the pod library on the JVM before natively compiling it

borkdude18:06:44

this makes iterating on it faster

Burin18:06:16

I see, so just add that as dependency and try to see if that work first.

Burin18:06:42

I should do that going forward. Thanks for the tips

Burin18:06:08

In the case of regex above, I guess I can also use reader to handle it

{"readers" {"my/tag" "clojure.core/identity"}}
like the example from the doc?

borkdude18:06:34

I totally forgot about that, but I guess so 🙂

David Pham18:06:42

Just my two 2 cents for improvement for babashka. This is only a personal feedback, so I hope you do not take it personally, it is probably only me. One of my biggest issue is about adding library to Babashka. I know there is documentation, but somehow, it does not feel easy (as you have to export the classpath as a string and everything, and it might be harder to make transportable). For example, I wanted to use clj-http-lite and I find it convoluted. Is there a way babashka could leverage on deps.edn for building the cp?

borkdude19:06:50

@neo2551 Btw, any reason you're using clj-http-lite and not the built-in babashka.curl? Maybe besides the point of using libs as a general question

David Pham19:06:00

I tried it at work, and I just wanted to make a post request with some arguments and somehow with a similar code clj-http-lite managed to return me the correct response while babashka.curl could not.

David Pham19:06:21

It was about content length from the header request

David Pham19:06:41

I was a bit in a rush, so I did not took the time to perfectly found the cause, sorry.

borkdude19:06:42

please report any issues with babashka.curl at https://github.com/borkdude/babashka.curl

David Pham19:06:05

I would need to find the exact cause, otherwise I don't want to waste your time!

borkdude19:06:25

sure 🙂 so tl;dr: deps.clj is the answer I have to your question currently

David Pham19:06:31

I will try that 🙂 Thanks a lot

David Pham19:06:36

Does deps start faster than clojure?

borkdude19:06:25

similar. there are two flavors: one that runs as a babashka script and natively compiled binaries for linux, mac and windows.

borkdude19:06:45

the bb script runs a few ms slower than the bash equivalent, the binary might be slightly faster