This commit is contained in:
Michiel Borkent 2020-12-26 13:57:01 +01:00
parent 3e59d1462d
commit 3dc7c27b17
6 changed files with 79 additions and 93 deletions

13
script/hsqldb.clj Normal file
View file

@ -0,0 +1,13 @@
(require '[babashka.pods :as pods])
(pods/load-pod 'org.babashka/hsqldb "0.0.1" #_{:force true})
(require '[pod.babashka.hsqldb :as db])
(def db "jdbc:hsqldb:mem:testdb;sql.syntax_mys=true")
(db/execute! db ["create table foo ( foo int );"])
(db/execute! db ["insert into foo values (1, 2, 3);"])
(db/execute! db ["select * from foo;"])

View file

@ -3,6 +3,8 @@
(defn load-pod (defn load-pod
([pod-spec] (load-pod pod-spec nil)) ([pod-spec] (load-pod pod-spec nil))
([pod-spec version opts]
(load-pod pod-spec (assoc opts :version version)))
([pod-spec opts] (jvm/load-pod pod-spec opts))) ([pod-spec opts] (jvm/load-pod pod-spec opts)))
(defn unload-pod (defn unload-pod

View file

@ -278,9 +278,14 @@
(defn load-pod (defn load-pod
([pod-spec] (load-pod pod-spec nil)) ([pod-spec] (load-pod pod-spec nil))
([pod-spec {:keys [:remove-ns :resolve :transport]}] ([pod-spec opts]
(let [pod-spec (cond (string? pod-spec) [pod-spec] (let [opts (if (string? opts)
(qualified-symbol? pod-spec) (resolver/resolve pod-spec) {:version opts}
opts)
{:keys [:remove-ns :resolve :transport :version :force]} opts
version (if (string? opts) opts version)
pod-spec (cond (string? pod-spec) [pod-spec]
(qualified-symbol? pod-spec) (resolver/resolve pod-spec version force)
:else pod-spec) :else pod-spec)
pb (ProcessBuilder. ^java.util.List pod-spec) pb (ProcessBuilder. ^java.util.List pod-spec)
socket? (identical? :socket transport) socket? (identical? :socket transport)

View file

@ -87,70 +87,26 @@
(.connect conn) (.connect conn)
(io/make-parents dest) (io/make-parents dest)
(with-open [is (.getInputStream conn)] (with-open [is (.getInputStream conn)]
(io/copy is dest)) (io/copy is dest))))
(when verbose? (warn "Download complete."))))
(def pod-meta-dir (def pod-manifests-dir
;; wrapped in delay for GraalVM native-image ;; wrapped in delay for GraalVM native-image
(delay (io/file (System/getProperty "user.home") (delay (io/file (or (System/getenv "XDG_DATA_HOME")
".babashka" "pods" "meta"))) (System/getProperty "user.home"))
".babashka" "pods" "repository")))
(defn pod-meta (defn github-url [qsym version]
([qsym] (pod-meta qsym nil)) (format
([qsym version] "https://raw.githubusercontent.com/babashka/pod-manifests/master/%s/%s.edn"
(let [version (or version "latest") qsym version))
f (io/file @pod-meta-dir (str qsym) (str version ".edn"))]
(if (.exists f) (edn/read-string (slurp f)) (defn pod-manifest
;; TODO: download from github? [qsym version]
(case qsym (let [f (io/file @pod-manifests-dir (str qsym) (str version ".edn"))]
org.babashka/pod-babashka-hsqldb (if (.exists f)
'{:pod/name org.babashka/pod-babashka-hsqldb (edn/read-string (slurp f))
:pod/description "" (do (download (github-url qsym version) f false)
:pod/version "0.0.1" (edn/read-string (slurp f))))))
:pod/license ""
:pod/artifacts
[{:os/name "Mac.*"
:os/arch "x86_64"
:artifact/url "https://github.com/babashka/babashka-sql-pods/releases/download/v0.0.1/pod-babashka-hsqldb-0.0.1-macos-amd64.zip"
#_#_:artifact/hash "sha256:sfEkDVDKf/owDyW+hCj22N5eNgFNYk62fxpvKexwva0="
;; TODO: should this be a command vector rather?
:artifact/executable "pod-babashka-hsqldb"
;; or rather, give optional args here.
}
{:os/name "Linux.*"
:os/arch "amd64"
:artifact/url "https://github.com/babashka/babashka-sql-pods/releases/download/v0.0.1/pod-babashka-hsqldb-0.0.1-linux-amd64.zip"
#_#_:artifact/hash "sha256:NlCox8UXMq/y0dBGTjYkDSJEcJ8UZrkBQsK/OyRIQ6c="
:artifact/executable "pod-babashka-hsqldb"}
#_{:os/name "Windows.*"
:os/arch "amd64"
:artifact/url "https://github.com/borkdude/babashka/releases/download/v0.2.2/babashka-0.2.2-windows-amd64.zip"
:artifact/hash "sha256:AAMks+jCr5JbeU4jHwaGxPHG22jyfvB5lzVEaRTpcHE="
:artifact/executable "bb.exe"}]}
org.babashka/pod-babashka-postgresql
'{:pod/name org.babashka/pod-babashka-postgresql
:pod/description ""
:pod/version "0.0.1"
:pod/license ""
:pod/artifacts
[{:os/name "Mac.*"
:os/arch "x86_64"
:artifact/url "https://github.com/babashka/babashka-sql-pods/releases/download/v0.0.1/pod-babashka-postgresql-0.0.1-macos-amd64.zip"
#_#_:artifact/hash "sha256:sfEkDVDKf/owDyW+hCj22N5eNgFNYk62fxpvKexwva0="
;; TODO: should this be a command vector rather?
:artifact/executable "pod-babashka-postgresql"
;; or rather, give optional args here.
}
{:os/name "Linux.*"
:os/arch "amd64"
:artifact/url "https://github.com/babashka/babashka-sql-pods/releases/download/v0.0.1/pod-babashka-postgresql-0.0.1-linux-amd64.zip"
#_#_:artifact/hash "sha256:NlCox8UXMq/y0dBGTjYkDSJEcJ8UZrkBQsK/OyRIQ6c="
:artifact/executable "pod-babashka-postgresql"}
#_{:os/name "Windows.*"
:os/arch "amd64"
:artifact/url "https://github.com/borkdude/babashka/releases/download/v0.2.2/babashka-0.2.2-windows-amd64.zip"
:artifact/hash "sha256:AAMks+jCr5JbeU4jHwaGxPHG22jyfvB5lzVEaRTpcHE="
:artifact/executable "bb.exe"}]})))))
(defn cache-dir (defn cache-dir
^java.io.File ^java.io.File
@ -191,37 +147,39 @@
(.digest digest)) (.digest digest))
(String. "UTF-8")))) (String. "UTF-8"))))
(defn resolve [qsym] (defn resolve [qsym version force?]
(when-let [package (pod-meta qsym)] (assert (string? version) "Version must be provided!")
(let [artifacts (match-artifacts package) (when-let [manifest (pod-manifest qsym version)]
cdir (cache-dir package) (let [artifacts (match-artifacts manifest)
ddir (data-dir package) cdir (cache-dir manifest)
ddir (data-dir manifest)
execs (mapv (fn [artifact] execs (mapv (fn [artifact]
(let [url (:artifact/url artifact) (let [url (:artifact/url artifact)
file-name (last (str/split url #"/")) file-name (last (str/split url #"/"))
cache-file (io/file cdir file-name) cache-file (io/file cdir file-name)
executable (io/file ddir (:artifact/executable artifact))] executable (io/file ddir (:artifact/executable artifact))]
(if (.exists executable) (when (or force? (not (.exists executable)))
nil #_(when verbose? (warn (format "Downloading pod %s (%s)" qsym version))
(warn "Package" (pkg-name package) "already installed")) (download url cache-file false)
(do (download url cache-file false) (when-let [expected-sha (:artifact/hash artifact)]
(when-let [expected-sha (:artifact/hash artifact)] (let [sha (sha256 cache-file)]
(let [sha (sha256 cache-file)] (when-not (= (str/replace expected-sha #"^sha256:" "")
(when-not (= (str/replace expected-sha #"^sha256:" "") sha)
sha) (throw (ex-info (str "Wrong SHA-256 for file" (str cache-file))
(throw (ex-info (str "Wrong SHA-256 for file" (str cache-file)) {:sha sha
{:sha sha :expected-sha expected-sha})))))
:expected-sha expected-sha}))))) (let [filename (.getName cache-file)]
(let [filename (.getName cache-file)] (cond (str/ends-with? filename ".zip")
(cond (str/ends-with? filename ".zip") (unzip {:zip-file cache-file
(unzip {:zip-file cache-file :destination-dir ddir
:destination-dir ddir :verbose false})
:verbose false}) (or (str/ends-with? filename ".tgz")
(or (str/ends-with? filename ".tgz") (str/ends-with? filename ".tar.gz"))
(str/ends-with? filename ".tar.gz")) (un-tgz cache-file ddir
(un-tgz cache-file ddir false))
false))) (.delete cache-file))
(make-executable ddir [(:artifact/executable artifact)] false) (make-executable ddir [(:artifact/executable artifact)] false)
(io/file ddir (:artifact/executable artifact))) ) (warn (format "Successfully installed pod %s (%s)" qsym version))
(io/file ddir (:artifact/executable artifact)))
(io/file ddir (:artifact/executable artifact)))) artifacts)] (io/file ddir (:artifact/executable artifact)))) artifacts)]
[(.getAbsolutePath ^java.io.File (first execs))]))) [(.getAbsolutePath ^java.io.File (first execs))])))

View file

@ -33,8 +33,12 @@
(defn load-pod (defn load-pod
([pod-spec] (load-pod pod-spec nil)) ([pod-spec] (load-pod pod-spec nil))
([pod-spec version opts] (load-pod pod-spec (assoc opts :version version)))
([pod-spec opts] ([pod-spec opts]
(let [pod (impl/load-pod (let [opts (if (string? opts)
{:version opts}
opts)
pod (impl/load-pod
pod-spec pod-spec
(merge {:remove-ns remove-ns (merge {:remove-ns remove-ns
:resolve (fn [sym] :resolve (fn [sym]

View file

@ -21,8 +21,12 @@
(defn load-pod (defn load-pod
([ctx pod-spec] (load-pod ctx pod-spec nil)) ([ctx pod-spec] (load-pod ctx pod-spec nil))
([ctx pod-spec version opts] (load-pod ctx pod-spec (assoc opts :version version)))
([ctx pod-spec opts] ([ctx pod-spec opts]
(let [env (:env ctx) (let [opts (if (string? opts)
{:version opts}
opts)
env (:env ctx)
pod (binding [*out* @sci/out pod (binding [*out* @sci/out
*err* @sci/err] *err* @sci/err]
(impl/load-pod (impl/load-pod