|
| 1 | +(ns buildversion-plugin.shell |
| 2 | + "A simple but flexible library for shelling out from Clojure. |
| 3 | + This code was copied from https://github.com/Raynes/conch" |
| 4 | + (:require [clojure.java.io :as io]) |
| 5 | + (:import (java.util.concurrent TimeUnit TimeoutException))) |
| 6 | + |
| 7 | +(defn proc |
| 8 | + "Spin off another process. Returns the process's input stream, |
| 9 | + output stream, and err stream as a map of :in, :out, and :err keys |
| 10 | + If passed the optional :dir and/or :env keyword options, the dir |
| 11 | + and enviroment will be set to what you specify. If you pass |
| 12 | + :verbose and it is true, commands will be printed. If it is set to |
| 13 | + :very, environment variables passed, dir, and the command will be |
| 14 | + printed. If passed the :clear-env keyword option, then the process |
| 15 | + will not inherit its environment from its parent process." |
| 16 | + [& args] |
| 17 | + (let [[cmd args] (split-with (complement keyword?) args) |
| 18 | + args (apply hash-map args) |
| 19 | + builder (ProcessBuilder. (into-array String cmd)) |
| 20 | + env (.environment builder)] |
| 21 | + (when (:clear-env args) |
| 22 | + (.clear env)) |
| 23 | + (doseq [[k v] (:env args)] |
| 24 | + (.put env k v)) |
| 25 | + (when-let [dir (:dir args)] |
| 26 | + (.directory builder (io/file dir))) |
| 27 | + (when (:verbose args) (apply println cmd)) |
| 28 | + (when (= :very (:verbose args)) |
| 29 | + (when-let [env (:env args)] (prn env)) |
| 30 | + (when-let [dir (:dir args)] (prn dir))) |
| 31 | + (when (:redirect-err args) |
| 32 | + (.redirectErrorStream builder true)) |
| 33 | + (let [process (.start builder)] |
| 34 | + {:out (.getInputStream process) |
| 35 | + :in (.getOutputStream process) |
| 36 | + :err (.getErrorStream process) |
| 37 | + :process process}))) |
| 38 | + |
| 39 | +(defn destroy |
| 40 | + "Destroy a process." |
| 41 | + [process] |
| 42 | + (.destroy (:process process))) |
| 43 | + |
| 44 | +;; .waitFor returns the exit code. This makes this function useful for |
| 45 | +;; both getting an exit code and stopping the thread until a process |
| 46 | +;; terminates. |
| 47 | +(defn exit-code |
| 48 | + "Waits for the process to terminate (blocking the thread) and returns |
| 49 | + the exit code. If timeout is passed, it is assumed to be milliseconds |
| 50 | + to wait for the process to exit. If it does not exit in time, it is |
| 51 | + killed (with or without fire)." |
| 52 | + ([process] (.waitFor (:process process))) |
| 53 | + ([process timeout] |
| 54 | + (try |
| 55 | + (.get (future (.waitFor (:process process))) timeout TimeUnit/MILLISECONDS) |
| 56 | + (catch Exception e |
| 57 | + (if (or (instance? TimeoutException e) |
| 58 | + (instance? TimeoutException (.getCause e))) |
| 59 | + (do (destroy process) |
| 60 | + :timeout) |
| 61 | + (throw e)))))) |
| 62 | + |
| 63 | +(defn stream-to |
| 64 | + "Stream :out or :err from a process to an ouput stream. |
| 65 | + Options passed are fed to clojure.java.io/copy. They are :encoding to |
| 66 | + set the encoding and :buffer-size to set the size of the buffer. |
| 67 | + :encoding defaults to UTF-8 and :buffer-size to 1024." |
| 68 | + [process from to & args] |
| 69 | + (apply io/copy (process from) to args)) |
| 70 | + |
| 71 | +(defn stream-to-string |
| 72 | + "Streams the output of the process to a string and returns it." |
| 73 | + [process from & args] |
| 74 | + (with-open [writer (java.io.StringWriter.)] |
| 75 | + (apply stream-to process from writer args) |
| 76 | + (str writer))) |
0 commit comments