asdf

Форк
0
/
asdf.elv 
243 строки · 7.0 Кб
1
use path
2
use re
3
use str
4

5
var asdf_dir = ~'/.asdf'
6
if (and (has-env ASDF_DIR) (!=s $E:ASDF_DIR '')) {
7
  set asdf_dir = $E:ASDF_DIR
8
} else {
9
  set-env ASDF_DIR $asdf_dir
10
}
11

12
var asdf_data_dir = $asdf_dir
13
if (and (has-env ASDF_DATA_DIR) (!=s $E:ASDF_DATA_DIR '')) {
14
  set asdf_data_dir = $E:ASDF_DATA_DIR
15
}
16

17
# Add function wrapper so we can export variables
18
fn asdf {|command @args|
19
  if (==s $command 'shell') {
20
    # set environment variables
21
    var parts = [($asdf_dir'/bin/asdf' export-shell-version elvish $@args)]
22
    if (==s $parts[0] 'set-env') {
23
      set-env $parts[1] $parts[2]
24
    } elif (==s $parts[0] 'unset-env') {
25
      unset-env $parts[1]
26
    }
27
  } else {
28
    # forward other commands to asdf script
29
    $asdf_dir'/bin/asdf' $command $@args
30
  }
31
}
32

33
fn match {|argz @pats|
34
  var matched = $true
35
  if (!= (count $argz) (count $pats)) {
36
    set matched = $false
37
  } else {
38
    for i [(range (count $pats))] {
39
      var pat = '^'$pats[$i]'$'
40
      var arg = $argz[$i]
41
      if (not (re:match $pat $arg)) {
42
        set matched = $false
43
        break
44
      }
45
    }
46
  }
47
  put $matched
48
}
49

50
fn ls-shims {
51
  ls $asdf_data_dir'/shims'
52
}
53

54
fn ls-executables {
55
  # Print all executable files and links in path
56
  try {
57
    find $@paths '(' -type f -o -type l ')' -print 2>/dev/null | each {|p|
58
      try {
59
        if (test -x $p) {
60
          path: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

71
fn ls-installed-versions {|plugin_name|
72
  asdf list $plugin_name | each {|version|
73
    put (re:replace '\s*(.*)\s*' '${1}' $version)
74
  }
75
}
76

77
fn ls-all-versions {|plugin_name|
78
  asdf list-all $plugin_name | each {|version|
79
    put (re:replace '\s*(.*)\s*' '${1}' $version)
80
  }
81
}
82

83
# Append ~/.asdf/bin and ~/.asdf/shims to PATH
84
for path [
85
  $asdf_dir'/bin'
86
  $asdf_data_dir'/shims'
87
] {
88
  if (not (has-value $paths $path)) {
89
    set paths = [
90
      $path
91
      $@paths
92
    ]
93
  }
94
}
95

96
# Setup argument completions
97
fn arg-completer {|@argz|
98
  set argz = $argz[1..-1]  # strip 'asdf' and trailing empty string
99
  var num = (count $argz)
100
  if (== $num 0) {
101
    # list all subcommands
102
    find $asdf_dir'/lib/commands' -name 'command-*' | each {|cmd|
103
      put (re:replace '.*/command-(.*)\.bash' '${1}' $cmd)
104
    }
105
    put 'plugin'
106
  } else {
107
    if (match $argz 'current') {
108
      # asdf current <name>
109
      asdf plugin-list
110
    } elif (match $argz 'env') {
111
      # asdf env <command>
112
      ls-shims
113
    } elif (match $argz 'env' '.*') {
114
      # asdf env <command> [util]
115
      ls-executables
116
    } elif (match $argz 'exec') {
117
      # asdf exec <command>
118
      ls-shims
119
    } elif (match $argz 'global') {
120
      # asdf global <name>
121
      asdf plugin-list
122
    } elif (match $argz 'global' '.*') {
123
      # asdf global <name> <version>
124
      ls-installed-versions $argz[-1]
125
    } elif (match $argz 'install') {
126
      # asdf install <name>
127
      asdf plugin-list
128
    } elif (match $argz 'install' '.*') {
129
      # asdf install <name> <version>
130
      ls-all-versions $argz[-1]
131
    } elif (match $argz 'install' '.*' '.*') {
132
      # asdf install <name> <version> [--keep-download]
133
      put '--keep-download'
134
    } elif (match $argz 'latest') {
135
      # asdf latest <name>
136
      asdf plugin-list
137
    } elif (match $argz 'latest' '.*') {
138
      # asdf latest <name> [<version>]
139
      ls-all-versions $argz[-1]
140
    } elif (match $argz 'list-all') {
141
      # asdf list all <name>
142
      asdf plugin-list
143
    } elif (match $argz 'list-all' '.*') {
144
      # asdf list all <name> [<version>]
145
      ls-all-versions $argz[-1]
146
    } elif (match $argz 'list') {
147
      # asdf list <name>
148
      asdf plugin-list
149
    } elif (match $argz 'list' '.*') {
150
      # asdf list <name> [<version>]
151
      ls-installed-versions $argz[-1]
152
    } elif (match $argz 'local') {
153
      # asdf local <name> [-p|--parent]
154
      asdf plugin-list
155
      put '-p'
156
      put '--parent'
157
    } elif (match $argz 'local' '(-p|(--parent))') {
158
      # asdf local <name> [-p|--parent] <version>
159
      asdf plugin-list
160
    } elif (match $argz 'local' '.*') {
161
      # asdf local <name> [-p|--parent]
162
      # asdf local <name> <version>
163
      ls-installed-versions $argz[-1]
164
      put '-p'
165
      put '--parent'
166
    } elif (match $argz 'local' '(-p|(--parent))' '.*') {
167
      # asdf local [-p|--parent] <name> <version>
168
      ls-installed-versions $argz[-1]
169
    } elif (match $argz 'local' '.*' '(-p|(--parent))') {
170
      # asdf local <name> [-p|--parent] <version>
171
      ls-installed-versions $argz[-2]
172
    } elif (match $argz 'local' '.*' '.*') {
173
      # asdf local <name> <version> [-p|--parent]
174
      put '-p'
175
      put '--parent'
176
    } elif (or (match $argz 'plugin-add') (match $argz 'plugin' 'add')) {
177
      # asdf plugin add <name>
178
      asdf plugin-list-all | each {|line|
179
        put (re:replace '([^\s]+)\s+.*' '${1}' $line)
180
      }
181
    } elif (or (match $argz 'plugin-list') (match $argz 'plugin' 'list')) {
182
      # asdf plugin list
183
      put '--urls'
184
      put '--refs'
185
      put 'all'
186
    } elif (or (match $argz 'plugin-push') (match $argz 'plugin' 'push')) {
187
      # asdf plugin push <name>
188
      asdf plugin-list
189
    } elif (or (match $argz 'plugin-remove') (match $argz 'plugin' 'remove')) {
190
      # asdf plugin remove <name>
191
      asdf 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*]
194
      put '--asdf-plugin-gitref'
195
      put '--asdf-tool-version'
196
      ls-executables
197
      ls-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*]
200
      put '--asdf-plugin-gitref'
201
      put '--asdf-tool-version'
202
      ls-executables
203
      ls-shims
204
    } elif (or (match $argz 'plugin-update') (match $argz 'plugin' 'update')) {
205
      # asdf plugin update <name>
206
      asdf plugin-list
207
      put '--all'
208
    } elif (match $argz 'plugin') {
209
      # list plugin-* subcommands
210
      find $asdf_dir'/lib/commands' -name 'command-plugin-*' | each {|cmd|
211
        put (re:replace '.*/command-plugin-(.*)\.bash' '${1}' $cmd)
212
      }
213
    } elif (match $argz 'reshim') {
214
      # asdf reshim <name>
215
      asdf plugin-list
216
    } elif (match $argz 'reshim' '.*') {
217
      # asdf reshim <name> <version>
218
      ls-installed-versions $argz[-1]
219
    } elif (match $argz 'shim-versions') {
220
      # asdf shim-versions <command>
221
      ls-shims
222
    } elif (match $argz 'uninstall') {
223
      # asdf uninstall <name>
224
      asdf plugin-list
225
    } elif (match $argz 'uninstall' '.*') {
226
      # asdf uninstall <name> <version>
227
      ls-installed-versions $argz[-1]
228
    } elif (match $argz 'update') {
229
      if (== $num 1) {
230
        # asdf update
231
        put '--head'
232
      }
233
    } elif (match $argz 'where') {
234
      # asdf where <name>
235
      asdf plugin-list
236
    } elif (match $argz 'where' '.*') {
237
      # asdf where <name> [<version>]
238
      ls-installed-versions $argz[-1]
239
    } elif (match $argz 'which') {
240
      ls-shims
241
    }
242
  }
243
}
244

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.