-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnvenv.v
335 lines (277 loc) · 9.08 KB
/
nvenv.v
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
import os
import utils
import v.vmod
import cli { Command, Flag }
fn main() {
vm := vmod.decode(@VMOD_FILE) or { panic(err.msg) }
mut cmd := Command{
name: '$vm.name'
description: '$vm.description'
version: '$vm.version'
disable_flags: true
}
mut setup_cmd := Command{
name: 'setup'
description: 'Set up required files and directories, required at first usage.'
execute: setup
}
mut list_cmd := Command{
name: 'ls'
description: 'List your installed versions.'
pre_execute: utils.setup_exists
execute: list
}
mut list_remote_cmd := Command{
name: 'list-remote'
description: 'List the available versions.'
pre_execute: list_remote_pre
execute: list_remote
}
list_remote_cmd.add_flag(Flag{
flag: .int
name: 'versions'
abbrev: 'v'
default_value: ['6']
description: 'Remote versions to show.'
})
mut install_cmd := Command{
name: 'install'
description: 'Install a version.'
usage: '<version>'
required_args: 1
pre_execute: utils.setup_exists
execute: install
}
mut uninstall_cmd := Command{
name: 'uninstall'
description: 'Uninstall a version.'
usage: '<version>'
required_args: 1
pre_execute: utils.setup_exists
execute: uninstall
}
uninstall_cmd.add_flag(Flag{
flag: .bool
name: 'force'
abbrev: 'f'
default_value: ['false']
description: 'Force uninstallation.'
})
uninstall_cmd.add_flag(Flag{
flag: .bool
name: 'clean'
abbrev: 'c'
default_value: ['false']
description: 'Uninstall and clean the cache files of the version.'
})
mut update_nightly_cmd := Command{
name: 'update-nightly'
description: 'Update Neovim Nightly version.'
pre_execute: utils.setup_exists
execute: update_nightly
}
mut use_cmd := Command{
name: 'use'
description: 'Use a specific version.'
usage: '<version>'
required_args: 1
pre_execute: utils.setup_exists
execute: use
}
mut clean_cmd := Command{
name: 'clean'
description: 'Clean Nvenv cache files.'
pre_execute: utils.setup_exists
execute: clean
}
cmd.add_commands([setup_cmd, list_cmd, list_remote_cmd, install_cmd, uninstall_cmd,
update_nightly_cmd, use_cmd, clean_cmd])
cmd.setup()
cmd.parse(os.args)
}
fn setup(cmd Command) ? {
dependencies := ['curl', 'tar', 'jq']
if !os.exists(utils.nvenv_home) {
// Create required directories
// /home/user/.cache/nvenv => cache files for nvenv
// /home/user/.local/bin => where nvim will be symlinked
// /home/user/.local/share/nvenv => core files for nvenv
os.mkdir_all(utils.nvenv_cache) ?
os.mkdir_all('$os.home_dir()/.local/bin') ?
os.mkdir_all(utils.nvenv_versions) ?
// Check for missing dependencies
for dependency in dependencies {
utils.check_command(dependency)
}
} else {
utils.error_msg('Setup is already done.', 1)
}
}
fn list_remote_pre(_ Command) ? {
utils.log_msg('Fetching remote versions ...\n')
}
fn list_remote(cmd Command) ? {
versions_to_show := cmd.flags.get_int('versions') ?
releases := 'https://api.github.com/repos/neovim/neovim/releases'
// Filter the releases JSON array and exclude v0.5.0 because stable is an alias
// for this version
jq_cmd := 'jq \'[.[] | select(.tag_name!="v0.5.0") | .tag_name] | .[:$versions_to_show] | .[]\''
remote_versions := os.execute('curl -s $releases | $jq_cmd').output
if remote_versions.len == 0 {
utils.error_msg('Failed to get Neovim releases.', 3)
}
// Trim leading `v` and leading `"`
remote_versions_list := remote_versions.replace('v', '').replace('"', '').split('\n')
utils.print_versions(remote_versions_list, true)
}
fn list(cmd Command) ? {
versions := utils.nvenv_versions
mut installed_versions := utils.get_subdirs(versions)
if os.is_dir_empty(versions) {
utils.error_msg("You don't have any version installed.", 2)
}
installed_versions.sort_by_len()
utils.print_versions(installed_versions.reverse(), false)
}
fn install(cmd Command) ? {
version := cmd.args[0]
utils.check_version(version, 'install')
if os.exists(utils.version_path(version)) {
utils.error_msg('Version $version is already installed. If you want to update it, run `nvenv update $version`.',
1)
}
dl_path, filename := utils.download_path(version)
if !os.exists(dl_path) {
dl_url := utils.download_url(version)
utils.log_msg('Downloading version $version ...')
if os.system('curl --progress-bar -Lo $dl_path $dl_url') != 0 {
utils.error_msg('Failed to download version $version ($dl_url)', 2)
}
} else {
utils.log_msg('Using cached files for version $version ...')
}
mut tar_name := 'nvim-'
$if linux && x64 {
tar_name += 'linux64'
} $else $if macos {
tar_name += 'osx64'
}
os.rmdir('$utils.nvenv_versions/$tar_name') or {}
tar_path := '$utils.nvenv_cache/$filename'
utils.log_msg('Installing version $version ...')
if os.system('cd $utils.nvenv_cache && tar -xf $tar_path && mv $tar_name $utils.nvenv_versions/$version') != 0 {
utils.error_msg('Failed to install Nvim.', 3)
}
// If there is no current used version then use the new downloaded version as current
if !os.exists(utils.nvenv_current) {
use(cmd) ?
utils.log_msg('Version $version successfully installed.\n\tYou may need to add $os.home_dir()/.local/bin to your \$PATH.')
} else {
utils.log_msg('Version $version successfully installed.\n\tYou can now use it by doing `nvenv use $version`.')
}
}
fn uninstall(cmd Command) ? {
version := cmd.args[0]
force := cmd.flags.get_bool('force') ?
clean := cmd.flags.get_bool('clean') ?
current_version := utils.check_current()
utils.check_version(version, 'uninstall')
if !os.exists(utils.version_path(version)) {
utils.error_msg('Version $version is not installed.', 2)
}
if !force && version == current_version {
utils.error_msg('Version $version cannot be uninstalled because it\'s in use, maybe you want to use `--force`?',
1)
}
utils.log_msg('Uninstalling version $version ...')
os.rmdir_all(utils.version_path(version)) or {}
if !force {
utils.log_msg('Version $version successfully uninstalled.')
} else {
if version == current_version {
// Remove symlinks
utils.remove_symlink(utils.nvenv_current)
utils.remove_symlink(utils.nvim_current)
utils.warn_msg('Version $version was in use and was forcibly uninstalled, you must set a new version with `nvenv use <version>`.')
} else {
utils.log_msg('Version $version was forcibly uninstalled.')
}
}
if clean {
cache_file, _ := utils.download_path(version)
os.rm(cache_file) or { utils.warn_msg('Cache files for version $version were not found.') }
utils.log_msg('Cache files for version $version were successfully cleaned.')
}
}
fn update_nightly(cmd Command) ? {
version := 'nightly'
if !os.exists(utils.version_path(version)) {
utils.error_msg('Version $version is not installed.', 2)
}
// /home/user/.local/share/nvenv/versions/nightly
target_version := utils.version_path(version)
// /home/user/.cache/nvenv/nightly.tar.gz, nightly.tar.gz
dl_path, filename := utils.download_path(version)
// Delete the old cache file
dl_url := utils.download_url(version)
utils.log_msg('Downloading version $version update ...')
if os.system('curl --progress-bar -Lo $dl_path $dl_url') != 0 {
utils.error_msg('Failed to download version $version update ($dl_url)', 2)
}
mut tar_name := 'nvim-'
$if linux && x64 {
tar_name += 'linux64'
} $else $if macos {
tar_name += 'osx64'
}
os.rmdir('$utils.nvenv_versions/$tar_name') or {}
tar_path := '$utils.nvenv_cache/$filename'
utils.log_msg('Updating version $version ...')
// Extract the tarball, move all its content to the existing version directory and then delete the new version dir
if os.system('cd $utils.nvenv_cache && tar -xf $tar_path && cp -arf $tar_name/* $target_version && rm -r $tar_name') != 0 {
utils.error_msg('Failed to update Nvim.', 3)
}
utils.log_msg('Version $version successfully updated.')
}
fn use(cmd Command) ? {
version := cmd.args[0]
current_version := utils.check_current()
utils.check_version(version, 'use')
if !os.exists(utils.version_path(version)) {
utils.error_msg('Version $version is not installed, run `nvenv install $version`.',
2)
}
if version == current_version {
utils.error_msg('Version $version is already in use', 1)
}
target := utils.nvenv_current
nvim_current_version := utils.nvim_current
if os.is_link(target) && !utils.remove_symlink(target) {
utils.error_msg('Failed to use version $version, cannot remove previous symlink ($target)',
1)
}
// Create two symlinks,
// nvenv_home/current
// ~/.local/bin/nvim
os.symlink(utils.version_path(version), target) or {
utils.error_msg('Failed to use version $version, cannot create symlink ($target)',
1)
}
if !os.is_link(nvim_current_version) {
os.symlink('$target/bin/nvim', nvim_current_version) or {
utils.error_msg('Failed to use version $version, cannot create symlink ($nvim_current_version)',
1)
}
}
utils.log_msg('Using version $version')
}
fn clean(cmd Command) ? {
if os.is_dir_empty(utils.nvenv_cache) {
utils.error_msg("You don't have any version downloaded.", 2)
}
utils.log_msg('Cleaning cache files ...')
for cache_file in utils.get_files(utils.nvenv_cache) {
os.rm(cache_file) or {}
}
utils.log_msg('Cleaned all cache files successfully.')
}