From 916858e016d52927bfb6205731f678b9d5bb591e Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 16 Aug 2020 20:25:58 +0200 Subject: [PATCH] Add -jar option to run uberjar --- src/babashka/impl/classpath.clj | 11 +++++++++-- src/babashka/main.clj | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/babashka/impl/classpath.clj b/src/babashka/impl/classpath.clj index d1cd286b..b3820b46 100644 --- a/src/babashka/impl/classpath.clj +++ b/src/babashka/impl/classpath.clj @@ -1,8 +1,9 @@ (ns babashka.impl.classpath {:no-doc true} - (:require [clojure.java.io :as io] + (:require [babashka.impl.clojure.main :refer [demunge]] + [clojure.java.io :as io] [clojure.string :as str]) - (:import [java.util.jar JarFile])) + (:import [java.util.jar JarFile Manifest])) (set! *warn-on-reflection* true) @@ -68,6 +69,12 @@ resource-paths (mapv #(str base-path %) [".bb" ".clj" ".cljc"])] (getResource loader resource-paths opts))) +(defn main-ns [manifest-resource] + (some-> (Manifest. (io/input-stream manifest-resource)) + (.getMainAttributes) + (.getValue "Main-Class") + (demunge))) + ;;;; Scratch (comment diff --git a/src/babashka/main.clj b/src/babashka/main.clj index ea6b2e16..581257b1 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -148,6 +148,11 @@ (recur (next options) (assoc opts-map :file (first options)))) + ("--jar" "-jar") + (let [options (next options)] + (recur (next options) + (assoc opts-map + :jar (first options)))) ("--repl") (let [options (next options)] (recur (next options) @@ -456,7 +461,8 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that :expressions :stream? :repl :socket-repl :nrepl :verbose? :classpath - :main :uberscript :describe?] :as _opts} + :main :uberscript :describe? + :jar] :as _opts} (parse-opts args) _ (do ;; set properties (when main (System/setProperty "babashka.main" main)) @@ -480,6 +486,8 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that (System/getenv "BABASHKA_CLASSPATH")) _ (when classpath (add-classpath* classpath)) + _ (when jar + (add-classpath* jar)) load-fn (fn [{:keys [:namespace :reload]}] (when-let [{:keys [:loader]} @cp-state] @@ -496,6 +504,12 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that (let [abs-path (.getAbsolutePath (io/file file))] (vars/bindRoot sci/file abs-path) (System/setProperty "babashka.file" abs-path))) + main (if (and jar (not main)) + (when-let [res (cp/getResource + (:loader @cp-state) + ["META-INF/MANIFEST.MF"] {:url? true})] + (cp/main-ns res)) + main) ;; TODO: pull more of these values to compile time opts {:aliases aliases :namespaces (-> namespaces