-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
trampoline.clj
67 lines (56 loc) · 2.53 KB
/
trampoline.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(ns leiningen.trampoline
"Run a task without nesting the project's JVM inside Leiningen's."
(:refer-clojure :exclude [trampoline])
(:require [clojure.string :as string]
[leiningen.core.eval :as eval]
[leiningen.core.main :as main]
[leiningen.core.utils :as utils]
[leiningen.core.project :as project]
[clojure.java.io :as io]
[clojure.pprint :as pprint]))
(def ^:dynamic *trampoline?* false)
(defn- trampoline-file []
(System/getenv "TRAMPOLINE_FILE"))
(defn- win-batch? []
(if-let [t (trampoline-file)]
(.endsWith t ".bat")))
(defn- quote-arg [arg]
(if (win-batch?)
(format "\"%s\"" arg)
(format "'%s'" arg)))
(defn trampoline-command-string [project forms profiles]
;; each form is (do init & body)
(let [forms (map rest forms) ;; strip off do
inits (map first forms)
rests (mapcat rest forms)
command (eval/shell-command project (concat '(do) inits rests))]
(string/join " " (map quote-arg command))))
(defn write-trampoline [project forms profiles]
(let [command (trampoline-command-string project forms profiles)
trampoline (trampoline-file)]
(main/debug "Trampoline command:" command)
(utils/mkdirs (.getParentFile (io/file trampoline)))
(spit trampoline command)))
(defn ^:higher-order trampoline
"Run a task without nesting the project's JVM inside Leiningen's.
Calculates the Clojure code to run in the project's process for the
given task and allows Leiningen's own JVM process to exit before
running it rather than launching a subprocess of Leiningen's JVM.
Use this to save memory or to work around stdin issues.
Note that this can be unpredictable on account of collapsing all
eval-in-project calls into one run. For example, tasks chained
together under different profiles end up all running together."
[project task-name & args]
(when (= :leiningen (:eval-in project))
(main/info "Warning: trampoline has no effect with :eval-in-leiningen."))
(binding [*trampoline?* true]
(main/apply-task (main/lookup-alias task-name project)
(-> (assoc project :eval-in :trampoline)
(vary-meta update-in [:without-profiles] assoc
:eval-in :trampoline))
args))
(if (seq @eval/trampoline-forms)
(write-trampoline @eval/trampoline-project
@eval/trampoline-forms
@eval/trampoline-profiles)
(main/abort task-name "did not run any project code for trampolining.")))