Skip to content

Commit

Permalink
wip [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed May 11, 2023
1 parent 4dacdda commit 98249c4
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions src/borkdude/deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,51 @@
pb)

(defn- shell-command
"Executes shell command.
"Shell out. Options:
Accepts the following options:
- `:args`: the command line arguments
- ....
`:to-string?`: instead of writing to stdoud, write to a string and
return it."
([args] (shell-command args nil))
([args {:keys [:to-string?]}]
(let [args (mapv str args)
args (if (and windows? (not (System/getenv "DEPS_CLJ_NO_WINDOWS_FIXES")))
(mapv #(str/replace % "\"" "\\\"") args)
args)
pb (cond-> (ProcessBuilder. ^java.util.List args)
true (.redirectError ProcessBuilder$Redirect/INHERIT)
(not to-string?) (.redirectOutput ProcessBuilder$Redirect/INHERIT)
true (.redirectInput ProcessBuilder$Redirect/INHERIT))
_ (when-let [dir *dir*]
(.directory pb (io/file dir)))
_ (when-let [env *env*]
(set-env pb env))
_ (when-let [extra-env *extra-env*]
(add-env pb extra-env))
proc (.start pb)
string-out
(when to-string?
(let [sw (java.io.StringWriter.)]
(with-open [w (io/reader (.getInputStream proc))]
(io/copy w sw))
(str sw)))
exit-code (.waitFor proc)]
(when (not (zero? exit-code))
(*exit-fn* {:exit exit-code}))
string-out)))
[{:keys [args env extra-env in out err]}]
;;([args] (shell-command args nil))
(let [args (mapv str args)
args (if (and windows? (not (System/getenv "DEPS_CLJ_NO_WINDOWS_FIXES")))
(mapv #(str/replace % "\"" "\\\"") args)
args)
pb (cond-> (ProcessBuilder. ^java.util.List args)
(not err) (.redirectError ProcessBuilder$Redirect/INHERIT)
(not out) (.redirectOutput ProcessBuilder$Redirect/INHERIT)
(not in) (.redirectInput ProcessBuilder$Redirect/INHERIT))
_ (when-let [dir *dir*]
(.directory pb (io/file dir)))
_ (when-let [env env]
(set-env pb env))
_ (when-let [extra-env extra-env]
(add-env pb extra-env))
proc (.start pb)
out
(if (= out :string)
(let [sw (java.io.StringWriter.)]
(with-open [w (io/reader (.getInputStream proc))]
(io/copy w sw))
(str sw))
(.getInputStream proc))
err
(if (= err :string)
(let [sw (java.io.StringWriter.)]
(with-open [w (io/reader (.getErrorStream proc))]
(io/copy w sw))
(str sw))
(.getErrorStream proc))
exit-code (.waitFor proc)
res {:exit exit-code
:out out
:err err}]
(when (not (zero? exit-code))
(*exit-fn* res))
res))

(def ^:private ^:dynamic *process-fn* shell-command)

Expand Down

0 comments on commit 98249c4

Please sign in to comment.