Fork me on GitHub
#babashka
<
2021-10-11
>
alexdavis12:10:57

What is the process for making a java lib work with bb? I want to extract text from a pdf so I’m trying to use https://github.com/dotemacs/pdfboxing but it seems to fail when importing a java lib…

----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  Unable to resolve classname: org.apache.pdfbox.pdmodel.PDDocument
Location: pdfboxing/common.clj:3:3

----- Context ------------------------------------------------------------------
1: (ns pdfboxing.common
2:   (:require [ :as io])
3:   (:import java.io.File
     ^--- Unable to resolve classname: org.apache.pdfbox.pdmodel.PDDocument
4:            org.apache.pdfbox.pdmodel.PDDocument
5:            org.apache.pdfbox.io.RandomAccessFile
6:            org.apache.pdfbox.pdfparser.PDFParser))
7: 
8: (defn try-get-as-pdf

----- Stack trace --------------------------------------------------------------
pdfboxing.common - pdfboxing/common.clj:3:3
pdfboxing.text   - pdfboxing/text.clj:2:3

borkdude12:10:20

@U7KPK060K Unfortunately Java libraries with custom classes don't work with bb, unless they are built into bb. This restriction comes from GraalVM native-image. Ways to work around this: 1. shell out to another program which can do the task you are after 2. write a babashka pod that wraps the Java library 3. use #nbb which is similar to #babashka but gives access to all Node.js libraries without any restrictions 4. Or of course, just use JVM Clojure

alexdavis12:10:04

Ok thanks, thought that might be the case

borkdude12:10:39

Let me try to make a quick nbb example (just for my own curiosity)

alexdavis12:10:17

Haha I just setup the same thing but with jvm clojure and it took over a minute to run (on a fast machine!) Your nbb demo takes like 3 seconds, it's a great use case for it I think

alexdavis12:10:25

Thanks for putting that together!!

alexdavis12:10:41

my bb test script is this if relevant

(require '[babashka.deps :refer [add-deps]])
(add-deps '{:deps {pdfboxing/pdfboxing {:mvn/version "0.1.14"}}})
(require '[pdfboxing.text :as text])
(text/extract "hello.pdf")

deas13:10:58

Hi! Appears babashka does not have javax.securityand hence, digestis not available. What would be a suggested approach to create a digest for a stream I have in memory?

borkdude13:10:20

does java.security.MessageDigest work for you?

👍 1
deas14:10:05

Whoops, its there? 😲 So appears only parts of javax.securityare missing. Thought it is the whole package due to.

user=> (require 'digest)   
java.lang.Exception: Unable to resolve classname: java.security.Provider [at digest.clj:6:3]
user=> 

borkdude14:10:23

@U06FLL69W Ah you mean, you want to load the Clojure library digest?

borkdude14:10:33

I didn't understand that :)

borkdude14:10:40

Let me see what I can do about this

👍 1
deas14:10:48

Yes, was just trying the library and hoping for the best. Already shelling out to sha1sum, but that's a bit ugly. 😂

borkdude14:10:24

@U06FLL69W you can just use interop on MessageDigest directly

borkdude14:10:35

but I forked the library and made it babashka compatible now: https://github.com/babashka/clj-digest

borkdude14:10:18

Both the Providers and Security class were missing but you don't actually need them

deas14:10:34

Awesome! Thanks!

borkdude14:10:23

I think it would be quite convenient to include this library in bb itself maybe

👍 1
deas15:10:06

I think its footprint does not hurt. 😉

borkdude15:10:41

I added the necessary classes to bb now so the original library can also be run from source in the future

deas15:10:11

Was about to ask whether adding deps.edn is worth the hassle. 😂

borkdude15:10:44

it doesn't have one?

borkdude15:10:12

I will take care of this once the library is migrated to clj-commons

borkdude15:10:58

I added it to the fork now

deas16:10:49

Awesome. Pulling it in via deps works now. deps should probably default to {}instead of complaining Manifest type not detected 😉 /cc @U064X3EF3

borkdude16:10:19

you can do this yourself btw by adding :deps/manifest :deps to your lib map

👍 1
Michaël Salihi14:10:10

Hi, I wonder to know what is the best option to run two bb parallel tasks but trigger the second one only if a file generated by the first task exists?

borkdude14:10:25

That kind of looks to me like it isn't parallel but sequential?

Michaël Salihi14:10:12

The thing is that this two tasks are blocking: 1. shadow-cljs watch app 2. webpack server

Michaël Salihi14:10:09

Without parallel the second task will be never started, right?

borkdude14:10:33

so what you want to do is babashka.wait/wait-for-file

borkdude14:10:38

and then start the second one

Michaël Salihi14:10:19

ok thanks, I try this!

borkdude14:10:29

so you can do that in a task as well. wait for the path and then start webpack

Michaël Salihi14:10:28

It works perfectly thanks!

{:tasks
 {:requires
  ([babashka.wait :refer [wait-for-path]])
  dev {:doc ""
       :task (run '-dev {:parallel true})}
  -dev {:depends [dev:cljs dev:webpack]}
  dev:cljs {:doc "Runs Shadow-cljs watch"
            :task (shell "npx shadow-cljs watch app")}
  dev:webpack {:doc "Runs Webpack dev server"
               :task (when (wait-for-path "target/index.js")
                       (shell "npx webpack server -c webpack.config.js"))}}}

borkdude15:10:23

Awesome! Go forth and spread the word :)

👍 1
mkarp16:10:59

Hey there! Can you please let me know if it's possible to have two projects sitting next to each other, with a bb.edn file in each of them, and one project's tasks to depend on tasks from the other project?

borkdude16:10:51

@U9ES37CSZ This is currently not yet supported (similar to how you cannot do this with Makefiles). But you can have one root bb.edn and use :dir to execute shell and clojure commands in subdirectories, if that helps

borkdude16:10:21

(shell {:dir "project1"} "do-something") (clojure {:dir "project2"} "-M:foobar")

borkdude16:10:29

The other option is to split out common code into a library and use :local/root to share that code between projects

borkdude16:10:53

and then hook that up to your project-specific bb.edn

mkarp16:10:51

Thank you for the ideas @U04V15CAJ 🙏