-
Notifications
You must be signed in to change notification settings - Fork 9
/
http_jar.clj
67 lines (58 loc) · 2.54 KB
/
http_jar.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 babashka.bbin.scripts.http-jar
(:require [babashka.bbin.protocols :as p]
[babashka.bbin.scripts.common :as common]
[babashka.bbin.dirs :as dirs]
[babashka.bbin.util :as util]
[babashka.fs :as fs]
[babashka.http-client :as http]
[clojure.java.io :as io]
[clojure.string :as str]
[selmer.parser :as selmer]
[selmer.util :as selmer-util]))
(def ^:private local-jar-template-str
(str/trim "
#!/usr/bin/env bb
; :bbin/start
;
{{script/meta}}
;
; :bbin/end
(require '[babashka.classpath :refer [add-classpath]])
(def script-jar {{script/jar|pr-str}})
(add-classpath script-jar)
(require '[{{script/main-ns}}])
(apply {{script/main-ns}}/-main *command-line-args*)
"))
(defrecord HttpJar [cli-opts coords]
p/Script
(install [_]
(fs/create-dirs (dirs/jars-dir cli-opts))
(let [http-url (:script/lib cli-opts)
script-deps {:bbin/url http-url}
header {:coords script-deps}
script-name (or (:as cli-opts) (common/http-url->script-name http-url))
tmp-jar-path (doto (fs/file (fs/temp-dir) (str script-name ".jar"))
(fs/delete-on-exit))
_ (io/copy (:body (http/get http-url {:as :bytes})) tmp-jar-path)
main-ns (common/jar->main-ns tmp-jar-path)
cached-jar-path (fs/file (dirs/jars-dir cli-opts) (str script-name ".jar"))
_ (fs/move tmp-jar-path cached-jar-path {:replace-existing true})
script-edn-out (with-out-str
(binding [*print-namespace-maps* false]
(util/pprint header)))
template-opts {:script/meta (->> script-edn-out
str/split-lines
(map #(str common/comment-char " " %))
(str/join "\n"))
:script/main-ns main-ns
:script/jar cached-jar-path}
script-contents (selmer-util/without-escaping
(selmer/render local-jar-template-str template-opts))
script-file (fs/canonicalize (fs/file (dirs/bin-dir cli-opts) script-name)
{:nofollow-links true})]
(common/install-script script-name header script-file script-contents cli-opts)))
(upgrade [_]
(p/install (map->HttpJar {:cli-opts {:script/lib (:bbin/url coords)}
:coords coords})))
(uninstall [_]
(common/delete-files cli-opts)))