forked from technomancy/leiningen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.clj
More file actions
54 lines (47 loc) · 2.24 KB
/
Copy pathrun.clj
File metadata and controls
54 lines (47 loc) · 2.24 KB
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
(ns leiningen.run
"Run a -main function with optional command-line arguments."
(:require [leiningen.core.eval :as eval]
[leiningen.core.main :as main])
(:import (java.io FileNotFoundException)
(clojure.lang Reflector)))
(defn- normalize-main [given]
(if (namespace (symbol given))
(symbol given)
(symbol (name given) "-main")))
(defn run-form
"Construct a form to run the given main defn or class with arguments."
[given args]
`(let [v# (resolve '~(normalize-main given))]
(if (ifn? v#)
(v# ~@args)
(Reflector/invokeStaticMethod
~(name given) "main" (into-array [(into-array String '~args)])))))
(defn- run-main
"Loads the project namespaces as well as all its dependencies and then calls
ns/f, passing it the args."
[project given & args]
(try (eval/eval-in-project project (run-form given args)
;; TODO: why are we using both init and resolve?
`(try (require '~(symbol (namespace
(normalize-main given))))
(catch FileNotFoundException _#)))
(catch clojure.lang.ExceptionInfo e
(main/abort))))
(defn ^{:help-arglists '([]) :no-project-needed true} run
"Run the project's -main function.
USAGE: lein run [--] [ARGS...]
Calls the -main function in the namespace specified as :main in project.clj.
You may use -- to escape the first argument in case it begins with `-' or `:'.
If your main function is not called \"-main\", you may use a namespaced symbol
like clojure.main/main.
USAGE: lein run -m NAMESPACE[/MAIN_FUNCTION] [ARGS...]
Calls the main function in the specified namespace.
See also \"lein help trampoline\" for a way to save memory using this task."
[project & [flag & args :as all-args]]
(let [all-args (if (= flag "--") args all-args)]
(cond (or (= flag ":main")
(= flag "-m")) (if (first args)
(apply run-main project args)
(main/abort "Option -m requires a namespace argument."))
(:main project) (apply run-main project (:main project) all-args)
:else (main/abort "No :main namespace specified in project.clj."))))