Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run jars without process/exec #47

Merged
merged 7 commits into from Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 26 additions & 6 deletions bbin
Expand Up @@ -163,6 +163,7 @@ WARNING: - Set the BABASHKA_BBIN_BIN_DIR env variable to \"$HOME/.babashka/bbi
[babashka.fs :as fs]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.main]
[clojure.pprint :as pprint]
[clojure.string :as str]
[rads.deps-info.infer :as deps-info-infer]
Expand Down Expand Up @@ -300,14 +301,14 @@ WARNING: - Set the BABASHKA_BBIN_BIN_DIR env variable to \"$HOME/.babashka/bbi
;
; :bbin/end

(require '[babashka.process :as process])
(require '[babashka.classpath :refer [add-classpath]])

(def script-jar {{script/jar|pr-str}})

(def base-command
[\"bb\" script-jar])
(add-classpath script-jar)

(process/exec (into base-command *command-line-args*))
(require '[{{script/main-ns}}])
(apply {{script/main-ns}}/-main *command-line-args*)
"))

(def ^:private local-dir-template-str
Expand Down Expand Up @@ -450,32 +451,50 @@ WARNING: - Set the BABASHKA_BBIN_BIN_DIR env variable to \"$HOME/.babashka/bbi
{:nofollow-links true})]
(install-script script-file script-contents (:dry-run cli-opts))))

(defn jar->main-ns [jar-path]
(with-open [jar-file (java.util.jar.JarFile. (fs/file jar-path))]
(or (some-> (.getManifest jar-file)
(.getMainAttributes)
;; TODO After July 17th 2023: Remove workaround below and start using (.getValue "Main-Class") instead
;; (see https://github.com/babashka/bbin/pull/47#discussion_r1071348344)
(->> (some (fn [[k v]]
(when (clojure.string/includes? k "Main-Class")
v))))
(clojure.main/demunge))
(throw (ex-info "jar has no Main-Class" {:babashka/exit 1})))))

(defn- install-http-jar [cli-opts]
(fs/create-dirs (util/jars-dir cli-opts))
(let [http-url (:script/lib cli-opts)
script-deps {:bbin/url http-url}
header {:coords script-deps}
_ (pprint header cli-opts)
script-name (or (:as cli-opts) (http-url->script-name http-url))
tmp-jar-path (doto (fs/file (fs/temp-dir) (str script-name ".jar"))
(fs/delete-on-exit))
_ (io/copy (:body @(http/get http-url {:as :byte-array})) tmp-jar-path)
main-ns (jar->main-ns tmp-jar-path)
cached-jar-path (fs/file (util/jars-dir cli-opts) (str script-name ".jar"))
_ (fs/move tmp-jar-path cached-jar-path)
_ (pprint header cli-opts)
script-edn-out (with-out-str
(binding [*print-namespace-maps* false]
(clojure.pprint/pprint header)))
template-opts {:script/meta (->> script-edn-out
str/split-lines
(map #(str comment-char " " %))
(str/join "\n"))
:script/main-ns main-ns
:script/jar cached-jar-path}
script-contents (selmer-util/without-escaping
(selmer/render local-jar-template-str template-opts))
script-file (fs/canonicalize (fs/file (util/bin-dir cli-opts) script-name)
{:nofollow-links true})]
(io/copy (:body @(http/get http-url {:as :byte-array})) cached-jar-path)
(install-script script-file script-contents (:dry-run cli-opts))))

(defn- install-local-jar [cli-opts]
(fs/create-dirs (util/jars-dir cli-opts))
(let [file-path (str (fs/canonicalize (:script/lib cli-opts) {:nofollow-links true}))
main-ns (jar->main-ns file-path)
script-deps {:bbin/url (str "file://" file-path)}
header {:coords script-deps}
_ (pprint header cli-opts)
Expand All @@ -488,6 +507,7 @@ WARNING: - Set the BABASHKA_BBIN_BIN_DIR env variable to \"$HOME/.babashka/bbi
str/split-lines
(map #(str comment-char " " %))
(str/join "\n"))
:script/main-ns main-ns
:script/jar cached-jar-path}
script-contents (selmer-util/without-escaping
(selmer/render local-jar-template-str template-opts))
Expand Down
32 changes: 26 additions & 6 deletions src/babashka/bbin/scripts.clj
Expand Up @@ -4,6 +4,7 @@
[babashka.fs :as fs]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.main]
[clojure.pprint :as pprint]
[clojure.string :as str]
[rads.deps-info.infer :as deps-info-infer]
Expand Down Expand Up @@ -141,14 +142,14 @@
;
; :bbin/end

(require '[babashka.process :as process])
(require '[babashka.classpath :refer [add-classpath]])

(def script-jar {{script/jar|pr-str}})

(def base-command
[\"bb\" script-jar])
(add-classpath script-jar)

(process/exec (into base-command *command-line-args*))
(require '[{{script/main-ns}}])
(apply {{script/main-ns}}/-main *command-line-args*)
"))

(def ^:private local-dir-template-str
Expand Down Expand Up @@ -291,32 +292,50 @@
{:nofollow-links true})]
(install-script script-file script-contents (:dry-run cli-opts))))

(defn jar->main-ns [jar-path]
(with-open [jar-file (java.util.jar.JarFile. (fs/file jar-path))]
(or (some-> (.getManifest jar-file)
(.getMainAttributes)
;; TODO After July 17th 2023: Remove workaround below and start using (.getValue "Main-Class") instead
;; (see https://github.com/babashka/bbin/pull/47#discussion_r1071348344)
(->> (some (fn [[k v]]
(when (clojure.string/includes? k "Main-Class")
v))))
(clojure.main/demunge))
(throw (ex-info "jar has no Main-Class" {:babashka/exit 1})))))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation is causing some test failures in Windows. I was able to fix this by simplifying to the following code:

(defn jar->main-ns [jar-path]
  (with-open [jar-file (JarFile. (fs/file jar-path))]
    (or (some-> (.getManifest jar-file)
                (.getMainAttributes)
                ;; FIXME .getValue is missing in Babashka? Create issue.
                ;; See https://docs.oracle.com/javase/7/docs/api/java/util/jar/Attributes.html
                #_(.getValue "Main-Class")
                (->> (into {}) (some (fn [[k v]]
                                       (when (clojure.string/includes? k "Main-Class") v))))
                (clojure.main/demunge))
        (throw (ex-info "jar has no Main-Class" {:babashka/exit 1})))))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the Attributes class was merged to master, but hasn't been released yet. This workaround seems good though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@borkdude: Would you prefer to leave in the workaround for now or bump the minimum Babashka version for bbin?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workaround + note and then remove workaround in 6 months or so

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeroenvandijk: Let's update the comment here based on @borkdude's feedback. ^


(defn- install-http-jar [cli-opts]
(fs/create-dirs (util/jars-dir cli-opts))
(let [http-url (:script/lib cli-opts)
script-deps {:bbin/url http-url}
header {:coords script-deps}
_ (pprint header cli-opts)
script-name (or (:as cli-opts) (http-url->script-name http-url))
tmp-jar-path (doto (fs/file (fs/temp-dir) (str script-name ".jar"))
(fs/delete-on-exit))
_ (io/copy (:body @(http/get http-url {:as :byte-array})) tmp-jar-path)
main-ns (jar->main-ns tmp-jar-path)
cached-jar-path (fs/file (util/jars-dir cli-opts) (str script-name ".jar"))
_ (fs/move tmp-jar-path cached-jar-path)
_ (pprint header cli-opts)
script-edn-out (with-out-str
(binding [*print-namespace-maps* false]
(clojure.pprint/pprint header)))
template-opts {:script/meta (->> script-edn-out
str/split-lines
(map #(str comment-char " " %))
(str/join "\n"))
:script/main-ns main-ns
:script/jar cached-jar-path}
script-contents (selmer-util/without-escaping
(selmer/render local-jar-template-str template-opts))
script-file (fs/canonicalize (fs/file (util/bin-dir cli-opts) script-name)
{:nofollow-links true})]
(io/copy (:body @(http/get http-url {:as :byte-array})) cached-jar-path)
(install-script script-file script-contents (:dry-run cli-opts))))

(defn- install-local-jar [cli-opts]
(fs/create-dirs (util/jars-dir cli-opts))
(let [file-path (str (fs/canonicalize (:script/lib cli-opts) {:nofollow-links true}))
main-ns (jar->main-ns file-path)
script-deps {:bbin/url (str "file://" file-path)}
header {:coords script-deps}
_ (pprint header cli-opts)
Expand All @@ -329,6 +348,7 @@
str/split-lines
(map #(str comment-char " " %))
(str/join "\n"))
:script/main-ns main-ns
:script/jar cached-jar-path}
script-contents (selmer-util/without-escaping
(selmer/render local-jar-template-str template-opts))
Expand Down