|
1 | 1 | (ns lambdaisland.shellutils
|
2 |
| - "Globbing and other shell-like filename handling |
| 2 | + "Globbing and other shell-like filename handling, and subshell handling |
3 | 3 |
|
4 | 4 | Extracted from https://github.com/lambdaisland/open-source and further improved"
|
5 |
| - (:require [clojure.java.io :as io]) |
6 |
| - (:import (java.io File) |
7 |
| - (java.nio.file Path Paths))) |
| 5 | + (:require |
| 6 | + [clojure.java.io :as io] |
| 7 | + [clojure.string :as str]) |
| 8 | + (:import |
| 9 | + (java.io File) |
| 10 | + (java.nio.file Path Paths))) |
| 11 | + |
| 12 | +(defn cli-opts |
| 13 | + "Options map from lambdaisland.cli, if it is used. We auto-detect certain flags, |
| 14 | + like --dry-run." |
| 15 | + [] |
| 16 | + (some-> (try (requiring-resolve 'lambdaisland.cli/*opts*) (catch Exception _)) deref)) |
8 | 17 |
|
9 | 18 | (def ^:dynamic *cwd*
|
10 | 19 | "Current working directory
|
11 | 20 |
|
12 |
| - Relative paths are resolved starting from this location. Defaults to the CWD |
13 |
| - of the JVM, as exposed through the 'user.dir' property." |
| 21 | + Relative paths are resolved starting from this location, and it is used for |
| 22 | + subshells. Defaults to the CWD of the JVM, as exposed through the 'user.dir' |
| 23 | + property." |
14 | 24 | (System/getProperty "user.dir"))
|
15 | 25 |
|
| 26 | +(defmacro with-cwd [cwd & body] |
| 27 | + `(let [prev# *cwd* |
| 28 | + cwd# (File. *cwd* ~cwd)] |
| 29 | + (binding [*cwd* cwd#] |
| 30 | + (try |
| 31 | + (System/setProperty "user.dir" (str cwd#)) |
| 32 | + ~@body |
| 33 | + (finally |
| 34 | + (System/setProperty "user.dir" prev#)))))) |
| 35 | + |
| 36 | +(defmacro with-temp-cwd |
| 37 | + "Same as with-cwd except that it creates a temp dir |
| 38 | + in *cwd* and evals the body inside it. |
| 39 | +
|
| 40 | + It cleans up the temp dir afterwards also removing |
| 41 | + any temp files created within it." |
| 42 | + [& body] |
| 43 | + ;; FIXME: use Files/createTempDirectory |
| 44 | + `(let [cwd# ".temp" |
| 45 | + dir# (File. *cwd* cwd#)] |
| 46 | + (.mkdirs dir#) |
| 47 | + (with-cwd cwd# ~@body) |
| 48 | + (delete-recursively dir#))) |
| 49 | + |
| 50 | +(defn slurp-cwd [f] |
| 51 | + (slurp (File. *cwd* f))) |
| 52 | + |
| 53 | +(defn spit-cwd [f c] |
| 54 | + (spit (File. *cwd* f) c)) |
| 55 | + |
| 56 | +(defn fatal [& msg] |
| 57 | + (apply println "[\033[0;31mFATAL\033[0m] " msg) |
| 58 | + (System/exit -1)) |
| 59 | + |
| 60 | +(defn process-builder [args] |
| 61 | + (doto (ProcessBuilder. args) |
| 62 | + (.inheritIO))) |
| 63 | + |
| 64 | +;; (def windows? (str/starts-with? (System/getProperty "os.name") "Windows")) |
| 65 | + |
| 66 | +(defn- cmd->str [args] |
| 67 | + (str/join " " (map #(if (str/includes? % " ") (pr-str %) %) args))) |
| 68 | + |
| 69 | +(defn spawn |
| 70 | + "Like [[clojure.java.shell/sh]], but inherit IO stream from the parent process, |
| 71 | + and prints out the invocation. By default terminates when a command |
| 72 | + fails (non-zero exit code). |
| 73 | +
|
| 74 | + If the last argument is a map, it is used for options |
| 75 | + - `:dir` Directory to execute in |
| 76 | + - `:continue-on-error?` Should a non-zero exit code be ignored. |
| 77 | + - `:fail-message` Error message to show when the command returns a non-zero exit code" |
| 78 | + [& args] |
| 79 | + (let [[opts args] (if (map? (last args)) |
| 80 | + [(last args) (butlast args)] |
| 81 | + [{} args]) |
| 82 | + dir (:dir opts *cwd*) |
| 83 | + ;; args (if windows? (cons "winpty" args) args) |
| 84 | + ] |
| 85 | + (println "=>" (cmd->str args) (str "(in " dir ")") "") |
| 86 | + (when-not (:dry-run (cli-opts)) |
| 87 | + (let [res (-> (process-builder args) |
| 88 | + (cond-> dir |
| 89 | + (.directory (io/file dir))) |
| 90 | + .start |
| 91 | + .waitFor)] |
| 92 | + (when (and (not= 0 res) (not (:continue-on-error? opts))) |
| 93 | + (fatal (:fail-message opts "command failed") res)) |
| 94 | + res)))) |
| 95 | + |
| 96 | +(defn bash |
| 97 | + "Interpret the command as a bash script, allows using redirects, pipes and other |
| 98 | + bash features." |
| 99 | + [& args] |
| 100 | + (spawn "bash" "-c" (str/join " " args))) |
| 101 | + |
16 | 102 | (defmacro with-cwd
|
17 | 103 | "Execute `body` with [[*cwd*]] set to `cwd`."
|
18 | 104 | [cwd & body]
|
|
0 commit comments