forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
epm.elv.go
353 lines (315 loc) · 8.76 KB
/
epm.elv.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package bundled
const epmElv = `use re
# Verbosity configuration
debug-mode = $false
# Configuration for common domains
-default-domain-config = [
&"github.com"= [
&method= git
&protocol= https
&levels= 2
]
&"bitbucket.org"= [
&method= git
&protocol= https
&levels= 2
]
&"gitlab.com"= [
&method= git
&protocol= https
&levels= 2
]
]
# Internal configuration
-data-dir = ~/.elvish
-lib-dir = $-data-dir/lib
# General utility functions
fn -debug [text]{
if $debug-mode {
print (edit:styled '=> ' blue)
echo $text
}
}
fn -info [text]{
print (edit:styled '=> ' green)
echo $text
}
fn -warn [text]{
print (edit:styled '=> ' yellow)
echo $text
}
fn -error [text]{
print (edit:styled '=> ' red)
echo $text
}
fn dest [pkg]{
put $-lib-dir/$pkg
}
fn is-installed [pkg]{
bool ?(test -e (dest $pkg))
}
fn -package-domain [pkg]{
splits &max=2 / $pkg | take 1
}
fn -package-without-domain [pkg]{
splits &max=2 / $pkg | drop 1 | joins ''
}
# Merge two maps
fn -merge [a b]{
keys $b | each [k]{ a[$k] = $b[$k] }
put $a
}
# Uppercase first letter of a string
fn -first-upper [s]{
put (echo $s[0] | tr '[:lower:]' '[:upper:]')$s[(count $s[0]):]
}
# Expand tilde at the beginning of a string to the home dir
fn -tilde-expand [p]{
re:replace "^~" $E:HOME $p
}
# Known method handlers. Each entry is indexed by method name (the
# value of the "method" key in the domain configs), and must contain
# two keys: install and upgrade, each one must be a closure that
# received two arguments: package name and the domain config entry
#
# - Method 'git' requires the key 'protocol' in the domain config,
# which has to be 'http' or 'https'
# - Method 'rsync' requires the key 'location' in the domain config,
# which has to contain the directory where the domain files are
# stored. It can be any source location understood by the rsync
# command.
-method-handler = [
&git= [
&src= [pkg dom-cfg]{
put $dom-cfg[protocol]"://"$pkg
}
&install= [pkg dom-cfg]{
dest = (dest $pkg)
-info "Installing "$pkg
mkdir -p $dest
git clone ($-method-handler[git][src] $pkg $dom-cfg) $dest
}
&upgrade= [pkg dom-cfg]{
dest = (dest $pkg)
-info "Updating "$pkg
try {
git -C $dest pull
} except _ {
-error "Something failed, please check error above and retry."
}
}
]
&rsync= [
&src= [pkg dom-cfg]{
put (-tilde-expand $dom-cfg[location])/(-package-without-domain $pkg)/
}
&install= [pkg dom-cfg]{
dest = (dest $pkg)
pkgd = (-package-without-domain $pkg)
-info "Installing "$pkg
rsync -av ($-method-handler[rsync][src] $pkg $dom-cfg) $dest
}
&upgrade= [pkg dom-cfg]{
dest = (dest $pkg)
pkgd = (-package-without-domain $pkg)
if (not (is-installed $pkg)) {
-error "Package "$pkg" is not installed."
return
}
-info "Updating "$pkg
rsync -av ($-method-handler[rsync][src] $pkg $dom-cfg) $dest
}
]
]
# Return the filename of the domain config file for the given domain
# (regardless of whether it exists)
fn -domain-config-file [dom]{
put $-lib-dir/$dom/epm-domain.cfg
}
# Return the filename of the metadata file for the given package
# (regardless of whether it exists)
fn -package-metadata-file [pkg]{
put (dest $pkg)/metadata.json
}
fn -write-domain-config [dom]{
cfgfile = (-domain-config-file $dom)
mkdir -p (dirname $cfgfile)
if (has-key $-default-domain-config $dom) {
put $-default-domain-config[$dom] | to-json > $cfgfile
} else {
-error "No default config exists for domain "$dom"."
}
}
# Returns the domain config for a given domain, as a JSON data
# structure. If the file does not exist but we have a built-in
# definition, then we return the default. Otherwise we return $false,
# so the result can always be checked with 'if'.
fn -domain-config [dom]{
cfgfile = (-domain-config-file $dom)
cfg = $false
if ?(test -f $cfgfile) {
# If the config file exists, read it...
cfg = (cat $cfgfile | from-json)
-debug "Read domain config for "$dom": "(to-string $cfg)
} else {
# ...otherwise check if we have a default config for the domain, and save it
if (has-key $-default-domain-config $dom) {
cfg = $-default-domain-config[$dom]
-debug "No existing config for "$dom", using the default: "(to-string $cfg)
} else {
-debug "No existing config for "$dom" and no default available."
}
}
put $cfg
}
# Return the method by which a package is installed
fn -package-method [pkg]{
dom = (-package-domain $pkg)
cfg = (-domain-config $dom)
if $cfg {
put $cfg[method]
} else {
put $false
}
}
# Invoke package operations defined in $-method-handler above
fn -package-op [pkg what]{
dom = (-package-domain $pkg)
cfg = (-domain-config $dom)
if $cfg {
method = $cfg[method]
if (has-key $-method-handler $method) {
if (has-key $-method-handler[$method] $what) {
$-method-handler[$method][$what] $pkg $cfg
} else {
fail "Unknown operation '"$what"' for package "$pkg
}
} else {
fail "Unknown method '"$method"', specified in in config file "(-domain-config-file $dom)
}
} else {
-error "No config for domain '"$dom"'."
}
}
# Uninstall a single package by removing its directory
fn -uninstall-package [pkg]{
if (not (is-installed $pkg)) {
-error "Package "$pkg" is not installed."
return
}
dest = (dest $pkg)
-info "Removing package "$pkg
rm -rf $dest
}
######################################################################
# Main user-facing functions
# Read and parse the package metadata, if it exists
fn metadata [pkg]{
# Base metadata attributes
res = [
&name= $pkg
&method= (-package-method $pkg)
&src= (-package-op $pkg src)
&dst= (dest $pkg)
&installed= (is-installed $pkg)
]
# Merge with package-specified attributes, if any
file = (-package-metadata-file $pkg)
if (and (is-installed $pkg) ?(test -f $file)) {
res = (-merge (cat $file | from-json) $res)
}
put $res
}
# Print out information about a package
fn query [pkg]{
data = (metadata $pkg)
special-keys = [name method installed src dst]
echo (edit:styled "Package "$data[name] cyan)
if $data[installed] {
echo (edit:styled "Installed at "$data[dst] green)
} else {
echo (edit:styled "Not installed" red)
}
echo (edit:styled "Source:" blue) $data[method] $data[src]
keys $data | each [key]{
if (not (has-value $special-keys $key)) {
val = $data[$key]
if (eq (kind-of $val) list) {
val = (joins ", " $val)
}
echo (edit:styled (-first-upper $key)":" blue) $val
}
}
}
# List installed packages
fn installed {
put $-lib-dir/*[nomatch-ok] | each [dir]{
dom = (re:replace &literal $-lib-dir/ '' $dir)
cfg = (-domain-config $dom)
# Only list domains for which we know the config, so that the user
# can have his own non-package directories under ~/.elvish/lib
# without conflicts.
if $cfg {
lvl = $cfg[levels]
pat = '^\Q'$-lib-dir'/\E('(repeat (+ $lvl 1) '[^/]+' | joins '/')')/$'
put (each [d]{ re:find $pat $d } [ $-lib-dir/$dom/**[nomatch-ok]/ ] )[groups][1][text]
}
}
}
# epm:list is an alias for epm:installed
fn list { installed }
# Install and upgrade are method-specific, so we call the
# corresponding functions using -package-op
fn install [&silent-if-installed=$false @pkgs]{
if (eq $pkgs []) {
-error "You must specify at least one package."
return
}
for pkg $pkgs {
if (is-installed $pkg) {
if (not $silent-if-installed) {
-info "Package "$pkg" is already installed."
}
} else {
-package-op $pkg install
# Check if there are any dependencies to install
metadata = (metadata $pkg)
if (has-key $metadata dependencies) {
deps = $metadata[dependencies]
-info "Installing dependencies: "(joins " " $deps)
# If the installation of dependencies fails, uninstall the
# target package (leave any already-installed dependencies in
# place)
try {
install $@deps
} except e {
-error "Dependency installation failed. Uninstalling "$pkg", please check the errors above and try again."
-uninstall-package $pkg
}
}
}
}
}
fn upgrade [@pkgs]{
if (eq $pkgs []) {
pkgs = [(installed)]
-info 'Upgrading all installed packages'
}
for pkg $pkgs {
if (not (is-installed $pkg)) {
-error "Package "$pkg" is not installed."
} else {
-package-op $pkg upgrade
}
}
}
# Uninstall is the same for everyone, just remove the directory
fn uninstall [@pkgs]{
if (eq $pkgs []) {
-error 'You must specify at least one package.'
return
}
for pkg $pkgs {
-uninstall-package $pkg
}
}`