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
([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)))
(defn unload-pod

View file

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

View file

@ -87,70 +87,26 @@
(.connect conn)
(io/make-parents dest)
(with-open [is (.getInputStream conn)]
(io/copy is dest))
(when verbose? (warn "Download complete."))))
(io/copy is dest))))
(def pod-meta-dir
(def pod-manifests-dir
;; wrapped in delay for GraalVM native-image
(delay (io/file (System/getProperty "user.home")
".babashka" "pods" "meta")))
(delay (io/file (or (System/getenv "XDG_DATA_HOME")
(System/getProperty "user.home"))
".babashka" "pods" "repository")))
(defn pod-meta
([qsym] (pod-meta qsym nil))
([qsym version]
(let [version (or version "latest")
f (io/file @pod-meta-dir (str qsym) (str version ".edn"))]
(if (.exists f) (edn/read-string (slurp f))
;; TODO: download from github?
(case qsym
org.babashka/pod-babashka-hsqldb
'{:pod/name org.babashka/pod-babashka-hsqldb
: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-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 github-url [qsym version]
(format
"https://raw.githubusercontent.com/babashka/pod-manifests/master/%s/%s.edn"
qsym version))
(defn pod-manifest
[qsym version]
(let [f (io/file @pod-manifests-dir (str qsym) (str version ".edn"))]
(if (.exists f)
(edn/read-string (slurp f))
(do (download (github-url qsym version) f false)
(edn/read-string (slurp f))))))
(defn cache-dir
^java.io.File
@ -191,37 +147,39 @@
(.digest digest))
(String. "UTF-8"))))
(defn resolve [qsym]
(when-let [package (pod-meta qsym)]
(let [artifacts (match-artifacts package)
cdir (cache-dir package)
ddir (data-dir package)
(defn resolve [qsym version force?]
(assert (string? version) "Version must be provided!")
(when-let [manifest (pod-manifest qsym version)]
(let [artifacts (match-artifacts manifest)
cdir (cache-dir manifest)
ddir (data-dir manifest)
execs (mapv (fn [artifact]
(let [url (:artifact/url artifact)
file-name (last (str/split url #"/"))
cache-file (io/file cdir file-name)
executable (io/file ddir (:artifact/executable artifact))]
(if (.exists executable)
nil #_(when verbose?
(warn "Package" (pkg-name package) "already installed"))
(do (download url cache-file false)
(when-let [expected-sha (:artifact/hash artifact)]
(let [sha (sha256 cache-file)]
(when-not (= (str/replace expected-sha #"^sha256:" "")
sha)
(throw (ex-info (str "Wrong SHA-256 for file" (str cache-file))
{:sha sha
:expected-sha expected-sha})))))
(let [filename (.getName cache-file)]
(cond (str/ends-with? filename ".zip")
(unzip {:zip-file cache-file
:destination-dir ddir
:verbose false})
(or (str/ends-with? filename ".tgz")
(str/ends-with? filename ".tar.gz"))
(un-tgz cache-file ddir
false)))
(make-executable ddir [(:artifact/executable artifact)] false)
(io/file ddir (:artifact/executable artifact))) )
(when (or force? (not (.exists executable)))
(warn (format "Downloading pod %s (%s)" qsym version))
(download url cache-file false)
(when-let [expected-sha (:artifact/hash artifact)]
(let [sha (sha256 cache-file)]
(when-not (= (str/replace expected-sha #"^sha256:" "")
sha)
(throw (ex-info (str "Wrong SHA-256 for file" (str cache-file))
{:sha sha
:expected-sha expected-sha})))))
(let [filename (.getName cache-file)]
(cond (str/ends-with? filename ".zip")
(unzip {:zip-file cache-file
:destination-dir ddir
:verbose false})
(or (str/ends-with? filename ".tgz")
(str/ends-with? filename ".tar.gz"))
(un-tgz cache-file ddir
false))
(.delete cache-file))
(make-executable ddir [(:artifact/executable artifact)] false)
(warn (format "Successfully installed pod %s (%s)" qsym version))
(io/file ddir (:artifact/executable artifact)))
(io/file ddir (:artifact/executable artifact)))) artifacts)]
[(.getAbsolutePath ^java.io.File (first execs))])))

View file

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

View file

@ -21,8 +21,12 @@
(defn load-pod
([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]
(let [env (:env ctx)
(let [opts (if (string? opts)
{:version opts}
opts)
env (:env ctx)
pod (binding [*out* @sci/out
*err* @sci/err]
(impl/load-pod