Skip to content

Commit 6d4a89d

Browse files
authored
Refactor conditional method invocation to more robust approach (#175)
1 parent fab8932 commit 6d4a89d

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ jobs:
4545
java -version
4646
bb --version
4747
48+
- name: 🔧 Install bb from dev build (until next release)
49+
shell: bash
50+
run: |
51+
curl -sLO https://raw.githubusercontent.com/babashka/babashka/master/install
52+
chmod +x ./install
53+
./install --dev-build --dir /tmp
54+
cp /tmp/bb "$(which bb)"
55+
4856
- name: Run bb tests
4957
run: |
5058
bb test:bb

deps.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{:deps {babashka/fs {:mvn/version "0.4.18"}}
2+
:paths ["src" "resources"]
23
:aliases {:test {:extra-paths ["test"]
34
:extra-deps {io.github.cognitect-labs/test-runner {:git/tag "v0.5.1" :git/sha "dfb30dd"}}
45
:main-opts ["-m" "cognitect.test-runner"]}

project.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
:url "https://www.eclipse.org/legal/epl-1.0/"}
88
:dependencies [[org.clojure/clojure "1.9.0"]
99
[babashka/fs "0.4.18"]]
10+
:source-paths ["src" "resources"]
1011
:deploy-repositories [["clojars" {:url "https://clojars.org/repo"
1112
:username :env/clojars_user
1213
:password :env/clojars_pass
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[ {
2+
"name" : "org.graalvm.nativeimage.ProcessProperties",
3+
"methods": [{"name": "exec"}]
4+
} ]

src/babashka/process.cljc

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -604,16 +604,10 @@
604604
:err :string} opts)]
605605
@(process* {:cmd cmd :opts opts :prev prev})))
606606

607-
(def ^:private has-exec?
608-
(boolean (try (.getMethod ^Class
609-
(resolve 'org.graalvm.nativeimage.ProcessProperties) "exec"
610-
(into-array [java.nio.file.Path (Class/forName "[Ljava.lang.String;") java.util.Map]))
611-
(catch Exception _ false))))
612-
613-
(defmacro ^:no-doc
614-
if-has-exec [then else]
615-
(if has-exec?
616-
then else))
607+
(def ^:private exec-meth
608+
(delay (try (.getMethod (Class/forName "org.graalvm.nativeimage.ProcessProperties") "exec"
609+
(into-array [java.nio.file.Path (Class/forName "[Ljava.lang.String;") java.util.Map]))
610+
(catch Exception _ nil))))
617611

618612
(defn exec
619613
"Replaces the current process image with the process image specified
@@ -649,10 +643,10 @@
649643
^java.util.Map env (into (or (as-string-map env)
650644
(into {} (System/getenv)))
651645
(as-string-map extra-env))]
652-
(if-has-exec
653-
(org.graalvm.nativeimage.ProcessProperties/exec (fs/path program)
654-
(into-array String args)
655-
env)
646+
(if-let [^java.lang.reflect.Method meth @exec-meth]
647+
(.invoke meth nil (into-array Object [(fs/path program)
648+
(into-array String args)
649+
env]))
656650
(throw (ex-info "exec is not supported in non-GraalVM environments" {:cmd cmd}))))))
657651

658652
(def ^:private default-shell-opts

0 commit comments

Comments
 (0)