Fork me on GitHub
#babashka
<
2020-01-12
>
borkdude13:01:55

On master, clojure.repl/dir:

user=> (dir clojure.string)
blank?
capitalize
ends-with?
...

😍 4
borkdude17:01:26

Self-contained script with dependencies:

(ns script
  {:clj-kondo/config '{:linters {:unresolved-symbol {:exclude [add-classpath!]}}}}
  (:require [clojure.java.shell :refer [sh]]
            [ :as io]))

;; set up dependency tool
(when-not (.exists (io/file "/tmp/deps.clj"))
  (sh "curl" "" "-o" "/tmp/deps.clj"))

(def medley-dep '{:deps {medley {:git/url ""
                                 :sha "91adfb5da33f8d23f75f0894da1defe567a625c0"}}})

(def cp (:out (sh "bb" "/tmp/deps.clj"
                  "-Spath"
                  "-Sdeps" (str medley-dep))))

(add-classpath! cp)

(require '[medley.core :as m])
(m/index-by :id [{:id 1} {:id 2}])

sogaiu17:01:30

so this is addition and use of a dependency at runtime, right?

borkdude17:01:18

just pushed this to master. feel free to try it out.. not sure if this is the right API, so feedback welcome 🙂

sogaiu17:01:19

ok, will check it out. coincidentally, i was working on adding a jar to the classpath at runtime in an ordinary clojure project: https://github.com/sogaiu/alc.start-repl/blob/master/src/alc/start_repl/impl/attach.clj

sogaiu17:01:42

just tried -- here's what i got:

$ ./bb examples/add-classpath.bb 
{1 {:id 1}, 2 {:id 2}}

borkdude17:01:28

I made a small tweak to the script where it skips downloading if the file already exists

sogaiu17:01:41

is there a checksum check?

borkdude17:01:44

I get this in about 90ms on the second run

sogaiu17:01:52

i guess there couldn't be without the checksum data

borkdude17:01:25

you could add whatever checksums to your script, this is just an example

sogaiu17:01:08

makes me appreciate the checksums under .m2 🙂

borkdude17:01:41

updated the script example to include clj-kondo config to suppress warning for add-classpath!. should I maybe add this function to a namespace instead of clojure.core?

borkdude17:01:05

oh, I just discovered that *file* is also an actual thing in clojure. I thought it was invented by joker but now I understand why I didn't get a warning about that

sogaiu17:01:16

oops -- i guess grepping clojure source would have turned that up

sogaiu17:01:26

does it mean something different?

borkdude17:01:53

no, it's exactly the same feature

sogaiu17:01:54

"The path of the file being evaluated, as a String"

sogaiu17:01:59

ah, nice 🙂

sogaiu17:01:22

wow, i should use that

borkdude17:01:40

so I guess moving add-classpath! to another namespace makes sense. clojure does have add-classpath without the exclamation mark, but that's deprecated

borkdude17:01:52

so re-using that would give clj-kondo warnings 😛

sogaiu17:01:24

perhaps it would be less confusing (to some people) too

sogaiu17:01:40

meh...i get: NO_SOURCE_PATH for *file*

sogaiu17:01:48

that's in a clojure-based project

borkdude17:01:10

but if you do clojure file.clj it works

sogaiu17:01:43

i guess it's nice when it works

borkdude19:01:07

Updated version with change on bb master (see #babashka_circleci_builds for downloads)

(ns script
  (:require [clojure.java.shell :refer [sh]]
            [ :as io]
            [babashka.classpath :refer [add-classpath]]))

;; set up dependency tool
(when-not (.exists (io/file "/tmp/deps.clj"))
  (sh "curl" "" "-o" "/tmp/deps.clj"))

(def medley-dep '{:deps {medley {:git/url ""
                                 :sha "91adfb5da33f8d23f75f0894da1defe567a625c0"}}})

(def cp (:out (sh "bb" "/tmp/deps.clj"
                  "-Spath"
                  "-Sdeps" (str medley-dep))))

(add-classpath cp)

(require '[medley.core :as m])
(m/index-by :id [{:id 1} {:id 2}])

sogaiu19:01:25

tried with: 5a63d3748600948c572fe576e274bc4c6b45e74e

$ ./bb examples/add-classpath-2.bb 
{1 {:id 1}, 2 {:id 2}}
looks like that works here too 🙂

sogaiu20:01:08

:thumbsup: tried the add-classpath sample on 0.0.62 successfully

borkdude21:01:43

Manual deps management using git only:

sogaiu22:01:24

i suppose most things live under src

borkdude22:01:59

yeah probably the sha and src dir could be optional in a last opts arg