asdf
/
asdf.elv
243 строки · 7.0 Кб
1use path
2use re
3use str
4
5var asdf_dir = ~'/.asdf'
6if (and (has-env ASDF_DIR) (!=s $E:ASDF_DIR '')) {
7set asdf_dir = $E:ASDF_DIR
8} else {
9set-env ASDF_DIR $asdf_dir
10}
11
12var asdf_data_dir = $asdf_dir
13if (and (has-env ASDF_DATA_DIR) (!=s $E:ASDF_DATA_DIR '')) {
14set asdf_data_dir = $E:ASDF_DATA_DIR
15}
16
17# Add function wrapper so we can export variables
18fn asdf {|command @args|
19if (==s $command 'shell') {
20# set environment variables
21var parts = [($asdf_dir'/bin/asdf' export-shell-version elvish $@args)]
22if (==s $parts[0] 'set-env') {
23set-env $parts[1] $parts[2]
24} elif (==s $parts[0] 'unset-env') {
25unset-env $parts[1]
26}
27} else {
28# forward other commands to asdf script
29$asdf_dir'/bin/asdf' $command $@args
30}
31}
32
33fn match {|argz @pats|
34var matched = $true
35if (!= (count $argz) (count $pats)) {
36set matched = $false
37} else {
38for i [(range (count $pats))] {
39var pat = '^'$pats[$i]'$'
40var arg = $argz[$i]
41if (not (re:match $pat $arg)) {
42set matched = $false
43break
44}
45}
46}
47put $matched
48}
49
50fn ls-shims {
51ls $asdf_data_dir'/shims'
52}
53
54fn ls-executables {
55# Print all executable files and links in path
56try {
57find $@paths '(' -type f -o -type l ')' -print 2>/dev/null | each {|p|
58try {
59if (test -x $p) {
60path:base $p
61}
62} catch {
63# don't fail if permission denied
64}
65}
66} catch {
67# silence default non-zero exit status
68}
69}
70
71fn ls-installed-versions {|plugin_name|
72asdf list $plugin_name | each {|version|
73put (re:replace '\s*(.*)\s*' '${1}' $version)
74}
75}
76
77fn ls-all-versions {|plugin_name|
78asdf list-all $plugin_name | each {|version|
79put (re:replace '\s*(.*)\s*' '${1}' $version)
80}
81}
82
83# Append ~/.asdf/bin and ~/.asdf/shims to PATH
84for path [
85$asdf_dir'/bin'
86$asdf_data_dir'/shims'
87] {
88if (not (has-value $paths $path)) {
89set paths = [
90$path
91$@paths
92]
93}
94}
95
96# Setup argument completions
97fn arg-completer {|@argz|
98set argz = $argz[1..-1] # strip 'asdf' and trailing empty string
99var num = (count $argz)
100if (== $num 0) {
101# list all subcommands
102find $asdf_dir'/lib/commands' -name 'command-*' | each {|cmd|
103put (re:replace '.*/command-(.*)\.bash' '${1}' $cmd)
104}
105put 'plugin'
106} else {
107if (match $argz 'current') {
108# asdf current <name>
109asdf plugin-list
110} elif (match $argz 'env') {
111# asdf env <command>
112ls-shims
113} elif (match $argz 'env' '.*') {
114# asdf env <command> [util]
115ls-executables
116} elif (match $argz 'exec') {
117# asdf exec <command>
118ls-shims
119} elif (match $argz 'global') {
120# asdf global <name>
121asdf plugin-list
122} elif (match $argz 'global' '.*') {
123# asdf global <name> <version>
124ls-installed-versions $argz[-1]
125} elif (match $argz 'install') {
126# asdf install <name>
127asdf plugin-list
128} elif (match $argz 'install' '.*') {
129# asdf install <name> <version>
130ls-all-versions $argz[-1]
131} elif (match $argz 'install' '.*' '.*') {
132# asdf install <name> <version> [--keep-download]
133put '--keep-download'
134} elif (match $argz 'latest') {
135# asdf latest <name>
136asdf plugin-list
137} elif (match $argz 'latest' '.*') {
138# asdf latest <name> [<version>]
139ls-all-versions $argz[-1]
140} elif (match $argz 'list-all') {
141# asdf list all <name>
142asdf plugin-list
143} elif (match $argz 'list-all' '.*') {
144# asdf list all <name> [<version>]
145ls-all-versions $argz[-1]
146} elif (match $argz 'list') {
147# asdf list <name>
148asdf plugin-list
149} elif (match $argz 'list' '.*') {
150# asdf list <name> [<version>]
151ls-installed-versions $argz[-1]
152} elif (match $argz 'local') {
153# asdf local <name> [-p|--parent]
154asdf plugin-list
155put '-p'
156put '--parent'
157} elif (match $argz 'local' '(-p|(--parent))') {
158# asdf local <name> [-p|--parent] <version>
159asdf plugin-list
160} elif (match $argz 'local' '.*') {
161# asdf local <name> [-p|--parent]
162# asdf local <name> <version>
163ls-installed-versions $argz[-1]
164put '-p'
165put '--parent'
166} elif (match $argz 'local' '(-p|(--parent))' '.*') {
167# asdf local [-p|--parent] <name> <version>
168ls-installed-versions $argz[-1]
169} elif (match $argz 'local' '.*' '(-p|(--parent))') {
170# asdf local <name> [-p|--parent] <version>
171ls-installed-versions $argz[-2]
172} elif (match $argz 'local' '.*' '.*') {
173# asdf local <name> <version> [-p|--parent]
174put '-p'
175put '--parent'
176} elif (or (match $argz 'plugin-add') (match $argz 'plugin' 'add')) {
177# asdf plugin add <name>
178asdf plugin-list-all | each {|line|
179put (re:replace '([^\s]+)\s+.*' '${1}' $line)
180}
181} elif (or (match $argz 'plugin-list') (match $argz 'plugin' 'list')) {
182# asdf plugin list
183put '--urls'
184put '--refs'
185put 'all'
186} elif (or (match $argz 'plugin-push') (match $argz 'plugin' 'push')) {
187# asdf plugin push <name>
188asdf plugin-list
189} elif (or (match $argz 'plugin-remove') (match $argz 'plugin' 'remove')) {
190# asdf plugin remove <name>
191asdf plugin-list
192} elif (and (>= (count $argz) 3) (match $argz[..3] 'plugin-test' '.*' '.*')) {
193# asdf plugin-test <plugin-name> <plugin-url> [--asdf-tool-version <version>] [--asdf-plugin-gitref <git-ref>] [test-command*]
194put '--asdf-plugin-gitref'
195put '--asdf-tool-version'
196ls-executables
197ls-shims
198} elif (and (>= (count $argz) 4) (match $argz[..4] 'plugin' 'test' '.*' '.*')) {
199# asdf plugin test <plugin-name> <plugin-url> [--asdf-tool-version <version>] [--asdf-plugin-gitref <git-ref>] [test-command*]
200put '--asdf-plugin-gitref'
201put '--asdf-tool-version'
202ls-executables
203ls-shims
204} elif (or (match $argz 'plugin-update') (match $argz 'plugin' 'update')) {
205# asdf plugin update <name>
206asdf plugin-list
207put '--all'
208} elif (match $argz 'plugin') {
209# list plugin-* subcommands
210find $asdf_dir'/lib/commands' -name 'command-plugin-*' | each {|cmd|
211put (re:replace '.*/command-plugin-(.*)\.bash' '${1}' $cmd)
212}
213} elif (match $argz 'reshim') {
214# asdf reshim <name>
215asdf plugin-list
216} elif (match $argz 'reshim' '.*') {
217# asdf reshim <name> <version>
218ls-installed-versions $argz[-1]
219} elif (match $argz 'shim-versions') {
220# asdf shim-versions <command>
221ls-shims
222} elif (match $argz 'uninstall') {
223# asdf uninstall <name>
224asdf plugin-list
225} elif (match $argz 'uninstall' '.*') {
226# asdf uninstall <name> <version>
227ls-installed-versions $argz[-1]
228} elif (match $argz 'update') {
229if (== $num 1) {
230# asdf update
231put '--head'
232}
233} elif (match $argz 'where') {
234# asdf where <name>
235asdf plugin-list
236} elif (match $argz 'where' '.*') {
237# asdf where <name> [<version>]
238ls-installed-versions $argz[-1]
239} elif (match $argz 'which') {
240ls-shims
241}
242}
243}
244