From 3dc7c27b17ab4e8ac8a6fb78524eac27d987570f Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 26 Dec 2020 13:57:01 +0100 Subject: [PATCH] wip --- script/hsqldb.clj | 13 +++ src/babashka/pods.clj | 2 + src/babashka/pods/impl.clj | 11 ++- src/babashka/pods/impl/resolver.clj | 134 ++++++++++------------------ src/babashka/pods/jvm.clj | 6 +- src/babashka/pods/sci.clj | 6 +- 6 files changed, 79 insertions(+), 93 deletions(-) create mode 100644 script/hsqldb.clj diff --git a/script/hsqldb.clj b/script/hsqldb.clj new file mode 100644 index 0000000..5de23fb --- /dev/null +++ b/script/hsqldb.clj @@ -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;"]) diff --git a/src/babashka/pods.clj b/src/babashka/pods.clj index 964a6c9..82b35e8 100644 --- a/src/babashka/pods.clj +++ b/src/babashka/pods.clj @@ -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 diff --git a/src/babashka/pods/impl.clj b/src/babashka/pods/impl.clj index be55c4b..ed7f57c 100644 --- a/src/babashka/pods/impl.clj +++ b/src/babashka/pods/impl.clj @@ -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) diff --git a/src/babashka/pods/impl/resolver.clj b/src/babashka/pods/impl/resolver.clj index fed724e..41e51e3 100644 --- a/src/babashka/pods/impl/resolver.clj +++ b/src/babashka/pods/impl/resolver.clj @@ -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))]))) diff --git a/src/babashka/pods/jvm.clj b/src/babashka/pods/jvm.clj index 289f71e..94aa545 100644 --- a/src/babashka/pods/jvm.clj +++ b/src/babashka/pods/jvm.clj @@ -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] diff --git a/src/babashka/pods/sci.clj b/src/babashka/pods/sci.clj index 4a412de..2b3ae83 100644 --- a/src/babashka/pods/sci.clj +++ b/src/babashka/pods/sci.clj @@ -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