Declarative pod support for uberscript (#1227)

This commit is contained in:
Michiel Borkent 2022-04-02 23:41:04 +02:00 committed by GitHub
parent 7af93adf15
commit e81ef4aac0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 18 deletions

View file

@ -52,11 +52,11 @@
[hf.depstar.uberjar :as uberjar] [hf.depstar.uberjar :as uberjar]
[sci.addons :as addons] [sci.addons :as addons]
[sci.core :as sci] [sci.core :as sci]
[sci.impl.io :as sio]
[sci.impl.namespaces :as sci-namespaces] [sci.impl.namespaces :as sci-namespaces]
[sci.impl.types :as sci-types] [sci.impl.types :as sci-types]
[sci.impl.unrestrict :refer [*unrestricted*]] [sci.impl.unrestrict :refer [*unrestricted*]]
[sci.impl.vars :as vars] [sci.impl.vars :as vars])
[sci.impl.io :as sio])
(:gen-class)) (:gen-class))
(def windows? (def windows?
@ -669,8 +669,8 @@ Use bb run --help to show this help output.
opts-key (if (str/ends-with? opt ".jar") opts-key (if (str/ends-with? opt ".jar")
:jar :file)] :jar :file)]
(assoc opts-map (assoc opts-map
opts-key opt opts-key opt
:command-line-args (next options)))) :command-line-args (next options))))
(defn parse-opts (defn parse-opts
([options] (parse-opts options nil)) ([options] (parse-opts options nil))
@ -765,16 +765,25 @@ Use bb run --help to show this help output.
:expressions [(:source res)]}) :expressions [(:source res)]})
{}) {})
res)))) res))))
(when-let [pod (get @pod-namespaces namespace)] (if-let [pod (get @pod-namespaces namespace)]
(pods/load-pod (:pod-spec pod) (:opts pod))) (if uberscript
(case namespace (do
clojure.spec.alpha (swap! uberscript-sources conj
(binding [*out* *err*] (format
(println "[babashka] WARNING: Use the babashka-compatible version of clojure.spec.alpha, available here: https://github.com/babashka/spec.alpha")) "(babashka.pods/load-pod '%s \"%s\" '%s)\n"
clojure.core.specs.alpha (:pod-spec pod) (:version (:opts pod))
(binding [*out* *err*] (dissoc (:opts pod)
(println "[babashka] WARNING: clojure.core.specs.alpha is removed from the classpath, unless you explicitly add the dependency.")) :version :metadata)))
nil))) {})
(pods/load-pod (:pod-spec pod) (:opts pod)))
(case namespace
clojure.spec.alpha
(binding [*out* *err*]
(println "[babashka] WARNING: Use the babashka-compatible version of clojure.spec.alpha, available here: https://github.com/babashka/spec.alpha"))
clojure.core.specs.alpha
(binding [*out* *err*]
(println "[babashka] WARNING: clojure.core.specs.alpha is removed from the classpath, unless you explicitly add the dependency."))
nil))))
main (if (and jar (not main)) main (if (and jar (not main))
(when-let [res (cp/getResource (when-let [res (cp/getResource
(cp/loader jar) (cp/loader jar)
@ -939,7 +948,7 @@ Use bb run --help to show this help output.
(->> {:pods bb-edn-pods} pr-str (spit bb-edn-resource)) (->> {:pods bb-edn-pods} pr-str (spit bb-edn-resource))
(let [cp-with-bb-edn (str cp cp/path-sep bb-edn-dir)] (let [cp-with-bb-edn (str cp cp/path-sep bb-edn-dir)]
(uberjar/run (assoc uber-params (uberjar/run (assoc uber-params
:classpath cp-with-bb-edn))))) :classpath cp-with-bb-edn)))))
(uberjar/run uber-params))) (uberjar/run uber-params)))
(throw (Exception. "The uberjar task needs a classpath.")))) (throw (Exception. "The uberjar task needs a classpath."))))
exit-code)))) exit-code))))
@ -968,8 +977,8 @@ Use bb run --help to show this help output.
(let [raw-string (slurp bb-edn-file) (let [raw-string (slurp bb-edn-file)
edn (edn/read-string raw-string) edn (edn/read-string raw-string)
edn (assoc edn edn (assoc edn
:raw raw-string :raw raw-string
:file bb-edn-file) :file bb-edn-file)
edn (if-let [deps-root (or (:deps-root global-opts) edn (if-let [deps-root (or (:deps-root global-opts)
(some-> config fs/parent))] (some-> config fs/parent))]
(assoc edn :deps-root deps-root) (assoc edn :deps-root deps-root)

View file

@ -0,0 +1,6 @@
(ns my.main-pod
(:require [my.other-ns-with-pod]
[pod.babashka.go-sqlite3 :as sqlite]))
(defn -main [& _args]
(sqlite/query ":memory:" ["SELECT 1 + 2 as sum"]))

View file

@ -0,0 +1,2 @@
(ns my.other-ns-with-pod
(:require [pod.babashka.go-sqlite3]))

View file

@ -1,7 +1,8 @@
(ns babashka.uberscript-test (ns babashka.uberscript-test
(:require (:require
[babashka.test-utils :as tu] [babashka.test-utils :as tu]
[clojure.test :as t :refer [deftest is]])) [clojure.test :as t :refer [deftest is]]
[clojure.string :as str]))
(deftest basic-test (deftest basic-test
(let [tmp-file (java.io.File/createTempFile "uberscript" ".clj")] (let [tmp-file (java.io.File/createTempFile "uberscript" ".clj")]
@ -23,3 +24,12 @@
(is (= ":clojure.string/foo\ntrue\n(\"1\" \"2\" \"3\" \"4\")\n" (is (= ":clojure.string/foo\ntrue\n(\"1\" \"2\" \"3\" \"4\")\n"
(tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4")))))) (tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4"))))))
(deftest pods-test
(let [tmp-file (java.io.File/createTempFile "uberscript" ".clj")]
(.deleteOnExit tmp-file)
(tu/with-config (pr-str '{:paths ["test-resources/babashka/uberscript/src"]
:pods {org.babashka/go-sqlite3 {:version "0.1.0"}}})
(is (empty? (tu/bb nil "uberscript" (.getPath tmp-file) "-m" "my.main-pod")))
(is (= 1 (count (re-seq #"load-pod 'org.babashka/go-sqlite3"
(str/join (str/split-lines (slurp tmp-file))))))))
(is (str/includes? (tu/bb nil "--file" (.getPath tmp-file)) "3"))))