tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
module main
|
|
|
|
|
2020-04-26 08:32:05 +02:00
|
|
|
import os
|
|
|
|
import os.cmdline
|
|
|
|
import net.http
|
|
|
|
import json
|
|
|
|
import vhelp
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
|
|
|
|
const (
|
2020-03-16 12:55:49 +01:00
|
|
|
default_vpm_server_urls = ['https://vpm.best', 'https://vpm.vlang.io']
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
valid_vpm_commands = ['help', 'search', 'install', 'update', 'remove']
|
2020-01-03 22:07:58 +01:00
|
|
|
excluded_dirs = ['cache', 'vlib']
|
|
|
|
supported_vcs_systems = ['git', 'hg']
|
|
|
|
supported_vcs_folders = ['.git', '.hg']
|
|
|
|
supported_vcs_update_cmds = {
|
2020-05-13 07:19:12 +02:00
|
|
|
'git': 'git pull'
|
2020-01-03 22:07:58 +01:00
|
|
|
'hg': 'hg pull --update'
|
|
|
|
}
|
|
|
|
supported_vcs_install_cmds = {
|
|
|
|
'git': 'git clone --depth=1'
|
|
|
|
'hg': 'hg clone'
|
|
|
|
}
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Mod {
|
2019-12-23 11:02:50 +01:00
|
|
|
id int
|
|
|
|
name string
|
|
|
|
url string
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
nr_downloads int
|
2020-01-03 22:07:58 +01:00
|
|
|
vcs string
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 05:22:28 +01:00
|
|
|
struct Vmod {
|
|
|
|
mut:
|
|
|
|
name string
|
|
|
|
version string
|
|
|
|
deps []string
|
|
|
|
}
|
|
|
|
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
fn main() {
|
2020-01-03 22:07:58 +01:00
|
|
|
init_settings()
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
// This tool is intended to be launched by the v frontend,
|
2019-12-23 11:02:50 +01:00
|
|
|
// which provides the path to V inside os.getenv('VEXE')
|
2020-01-03 22:07:58 +01:00
|
|
|
args := os.args // args are: vpm [options] SUBCOMMAND module names
|
|
|
|
params := cmdline.only_non_options(args[1..])
|
|
|
|
verbose_println('cli params: $params')
|
|
|
|
if params.len < 1 {
|
2020-03-13 20:52:49 +01:00
|
|
|
vpm_help()
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
exit(5)
|
|
|
|
}
|
2020-01-03 22:07:58 +01:00
|
|
|
vpm_command := params[0]
|
|
|
|
module_names := params[1..]
|
|
|
|
ensure_vmodules_dir_exist()
|
2019-12-23 11:02:50 +01:00
|
|
|
// println('module names: ') println(module_names)
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
match vpm_command {
|
2019-12-23 11:02:50 +01:00
|
|
|
'help' {
|
2020-03-13 20:52:49 +01:00
|
|
|
vpm_help()
|
2019-12-23 11:02:50 +01:00
|
|
|
}
|
|
|
|
'search' {
|
|
|
|
vpm_search(module_names)
|
|
|
|
}
|
|
|
|
'install' {
|
|
|
|
vpm_install(module_names)
|
|
|
|
}
|
|
|
|
'update' {
|
|
|
|
vpm_update(module_names)
|
|
|
|
}
|
|
|
|
'remove' {
|
|
|
|
vpm_remove(module_names)
|
|
|
|
}
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
else {
|
|
|
|
println('Error: you tried to run "v $vpm_command"')
|
|
|
|
println('... but the v package management tool vpm only knows about these commands:')
|
|
|
|
for validcmd in valid_vpm_commands {
|
|
|
|
println(' v $validcmd')
|
|
|
|
}
|
|
|
|
exit(3)
|
2019-12-30 05:22:28 +01:00
|
|
|
}
|
|
|
|
}
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 05:22:28 +01:00
|
|
|
fn vpm_search(keywords []string) {
|
2020-01-03 22:07:58 +01:00
|
|
|
if settings.is_help {
|
2020-03-13 20:52:49 +01:00
|
|
|
vhelp.show_topic('search')
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
exit(0)
|
|
|
|
}
|
2019-12-30 05:22:28 +01:00
|
|
|
if keywords.len == 0 {
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
println(' v search requires *at least one* keyword')
|
|
|
|
exit(2)
|
|
|
|
}
|
2019-12-30 05:22:28 +01:00
|
|
|
modules := get_all_modules()
|
|
|
|
joined := keywords.join(', ')
|
|
|
|
mut index := 0
|
|
|
|
for mod in modules {
|
|
|
|
// TODO for some reason .filter results in substr error, so do it manually
|
|
|
|
for k in keywords {
|
|
|
|
if !mod.contains(k) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if index == 0 {
|
|
|
|
println('Search results for "$joined":\n')
|
|
|
|
}
|
|
|
|
index++
|
|
|
|
mut parts := mod.split('.')
|
|
|
|
// in case the author isn't present
|
|
|
|
if parts.len == 1 {
|
|
|
|
parts << parts[0]
|
|
|
|
parts[0] = ''
|
|
|
|
}
|
|
|
|
println('${index}. ${parts[1]} by ${parts[0]} [$mod]')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println('\nUse "v install author.module_name" to install the module')
|
|
|
|
if index == 0 {
|
|
|
|
println('No module(s) found for "$joined"')
|
|
|
|
}
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 11:02:50 +01:00
|
|
|
fn vpm_install(module_names []string) {
|
2020-01-03 22:07:58 +01:00
|
|
|
if settings.is_help {
|
2020-03-13 20:52:49 +01:00
|
|
|
vhelp.show_topic('install')
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
if module_names.len == 0 {
|
|
|
|
println(' v install requires *at least one* module name')
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
mut errors := 0
|
2019-12-30 05:22:28 +01:00
|
|
|
for n in module_names {
|
|
|
|
name := n.trim_space()
|
2020-03-16 14:30:22 +01:00
|
|
|
mod := get_module_meta_info(name) or {
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
errors++
|
2020-03-16 14:30:22 +01:00
|
|
|
println('Errors while retrieving meta data for module ${name}:')
|
|
|
|
println(err)
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-01-03 22:07:58 +01:00
|
|
|
mut vcs := mod.vcs
|
|
|
|
if vcs == '' {
|
|
|
|
vcs = supported_vcs_systems[0]
|
|
|
|
}
|
2020-04-13 19:59:57 +02:00
|
|
|
if vcs !in supported_vcs_systems {
|
2020-01-03 22:07:58 +01:00
|
|
|
errors++
|
|
|
|
println('Skipping module "$name", since it uses an unsupported VCS {$vcs} .')
|
|
|
|
continue
|
|
|
|
}
|
2020-03-20 16:41:18 +01:00
|
|
|
final_module_path := os.real_path(os.join_path(settings.vmodules_path,mod.name.replace('.', os.path_separator)))
|
2019-12-30 05:22:28 +01:00
|
|
|
if os.exists(final_module_path) {
|
|
|
|
vpm_update([name])
|
|
|
|
continue
|
|
|
|
}
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
println('Installing module "$name" from $mod.url to $final_module_path ...')
|
2020-01-03 22:07:58 +01:00
|
|
|
vcs_install_cmd := supported_vcs_install_cmds[vcs]
|
|
|
|
cmd := '${vcs_install_cmd} "${mod.url}" "${final_module_path}"'
|
|
|
|
verbose_println(' command: $cmd')
|
|
|
|
cmdres := os.exec(cmd) or {
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
errors++
|
|
|
|
println('Could not install module "$name" to "$final_module_path" .')
|
2020-01-03 22:07:58 +01:00
|
|
|
verbose_println('Error command: $cmd')
|
|
|
|
verbose_println('Error details: $err')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cmdres.exit_code != 0 {
|
|
|
|
errors++
|
|
|
|
println('Failed installing module "$name" to "$final_module_path" .')
|
|
|
|
verbose_println('Failed command: ${cmd}')
|
|
|
|
verbose_println('Failed command output:\n${cmdres.output}')
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-12-30 05:22:28 +01:00
|
|
|
resolve_dependencies(name, final_module_path, module_names)
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
}
|
|
|
|
if errors > 0 {
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 05:22:28 +01:00
|
|
|
fn vpm_update(m []string) {
|
|
|
|
mut module_names := m
|
2020-01-03 22:07:58 +01:00
|
|
|
if settings.is_help {
|
2020-03-13 20:52:49 +01:00
|
|
|
vhelp.show_topic('update')
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
exit(0)
|
|
|
|
}
|
2019-12-30 05:22:28 +01:00
|
|
|
if module_names.len == 0 {
|
|
|
|
module_names = get_installed_modules()
|
|
|
|
}
|
|
|
|
mut errors := 0
|
|
|
|
for name in module_names {
|
2020-01-03 22:07:58 +01:00
|
|
|
final_module_path := valid_final_path_of_existing_module(name) or {
|
2019-12-30 05:22:28 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
os.chdir(final_module_path)
|
|
|
|
println('Updating module "$name"...')
|
2020-01-03 22:07:58 +01:00
|
|
|
verbose_println(' work folder: $final_module_path')
|
|
|
|
vcs := vcs_used_in_dir(final_module_path) or {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vcs_cmd := supported_vcs_update_cmds[vcs[0]]
|
|
|
|
verbose_println(' command: $vcs_cmd')
|
|
|
|
vcs_res := os.exec('${vcs_cmd}') or {
|
2019-12-30 05:22:28 +01:00
|
|
|
errors++
|
|
|
|
println('Could not update module "$name".')
|
2020-01-03 22:07:58 +01:00
|
|
|
verbose_println('Error command: ${vcs_cmd}')
|
|
|
|
verbose_println('Error details:\n$err')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if vcs_res.exit_code != 0 {
|
|
|
|
errors++
|
|
|
|
println('Failed updating module "${name}".')
|
|
|
|
verbose_println('Failed command: ${vcs_cmd}')
|
|
|
|
verbose_println('Failed details:\n${vcs_res.output}')
|
2019-12-30 05:22:28 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
resolve_dependencies(name, final_module_path, module_names)
|
|
|
|
}
|
|
|
|
if errors > 0 {
|
|
|
|
exit(1)
|
|
|
|
}
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 11:02:50 +01:00
|
|
|
fn vpm_remove(module_names []string) {
|
2020-01-03 22:07:58 +01:00
|
|
|
if settings.is_help {
|
2020-03-13 20:52:49 +01:00
|
|
|
vhelp.show_topic('remove')
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
exit(0)
|
|
|
|
}
|
2019-12-30 05:22:28 +01:00
|
|
|
if module_names.len == 0 {
|
|
|
|
println(' v update requires *at least one* module name')
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
for name in module_names {
|
2020-01-03 22:07:58 +01:00
|
|
|
final_module_path := valid_final_path_of_existing_module(name) or {
|
2019-12-30 05:22:28 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
println('Removing module "$name"...')
|
2020-01-03 22:07:58 +01:00
|
|
|
verbose_println('removing folder $final_module_path')
|
2020-03-03 15:02:50 +01:00
|
|
|
os.rmdir_all(final_module_path)
|
2020-01-03 22:07:58 +01:00
|
|
|
// delete author directory if it is empty
|
2019-12-30 05:22:28 +01:00
|
|
|
author := name.split('.')[0]
|
2020-03-20 16:41:18 +01:00
|
|
|
author_dir := os.real_path(os.join_path(settings.vmodules_path,author))
|
2019-12-30 05:22:28 +01:00
|
|
|
if os.is_dir_empty(author_dir) {
|
2020-01-03 22:07:58 +01:00
|
|
|
verbose_println('removing author folder $author_dir')
|
2019-12-30 05:22:28 +01:00
|
|
|
os.rmdir(author_dir)
|
|
|
|
}
|
|
|
|
}
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
}
|
|
|
|
|
2020-01-03 22:07:58 +01:00
|
|
|
fn valid_final_path_of_existing_module(name string) ?string {
|
2020-03-09 02:23:34 +01:00
|
|
|
name_of_vmodules_folder := os.join_path(settings.vmodules_path,name.replace('.', os.path_separator))
|
2020-03-20 16:41:18 +01:00
|
|
|
final_module_path := os.real_path(name_of_vmodules_folder)
|
2020-01-03 22:07:58 +01:00
|
|
|
if !os.exists(final_module_path) {
|
|
|
|
println('No module with name "$name" exists at $name_of_vmodules_folder')
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
if !os.is_dir(final_module_path) {
|
|
|
|
println('Skipping "$name_of_vmodules_folder", since it is not a folder.')
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
vcs_used_in_dir(final_module_path) or {
|
|
|
|
println('Skipping "$name_of_vmodules_folder", since it does not use a supported vcs.')
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
return final_module_path
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_vmodules_dir_exist() {
|
2020-01-03 22:07:58 +01:00
|
|
|
if !os.is_dir(settings.vmodules_path) {
|
|
|
|
println('Creating $settings.vmodules_path/ ...')
|
|
|
|
os.mkdir(settings.vmodules_path) or {
|
2019-12-23 11:02:50 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 20:52:49 +01:00
|
|
|
fn vpm_help() {
|
|
|
|
vhelp.show_topic('vpm')
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 13:19:04 +01:00
|
|
|
}
|
2019-12-23 11:02:50 +01:00
|
|
|
|
2020-01-03 22:07:58 +01:00
|
|
|
fn vcs_used_in_dir(dir string) ?[]string {
|
2020-04-26 13:49:31 +02:00
|
|
|
mut vcs := []string{}
|
2020-01-03 22:07:58 +01:00
|
|
|
for repo_subfolder in supported_vcs_folders {
|
2020-03-20 16:41:18 +01:00
|
|
|
checked_folder := os.real_path(os.join_path(dir,repo_subfolder))
|
2020-01-03 22:07:58 +01:00
|
|
|
if os.is_dir(checked_folder) {
|
|
|
|
vcs << repo_subfolder.replace('.', '')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vcs.len == 0 {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
return vcs
|
|
|
|
}
|
|
|
|
|
2019-12-30 05:22:28 +01:00
|
|
|
fn get_installed_modules() []string {
|
2020-01-03 22:07:58 +01:00
|
|
|
dirs := os.ls(settings.vmodules_path) or {
|
2019-12-30 05:22:28 +01:00
|
|
|
return []
|
|
|
|
}
|
2020-04-26 13:49:31 +02:00
|
|
|
mut modules := []string{}
|
2019-12-30 05:22:28 +01:00
|
|
|
for dir in dirs {
|
2020-03-13 20:52:49 +01:00
|
|
|
adir := os.join_path(settings.vmodules_path,dir)
|
2020-01-03 22:07:58 +01:00
|
|
|
if dir in excluded_dirs || !os.is_dir(adir) {
|
2019-12-30 05:22:28 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
author := dir
|
2020-01-03 22:07:58 +01:00
|
|
|
mods := os.ls(adir) or {
|
2019-12-30 05:22:28 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for m in mods {
|
2020-03-13 20:52:49 +01:00
|
|
|
vcs_used_in_dir(os.join_path(adir,m)) or {
|
2020-01-03 22:07:58 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-12-30 05:22:28 +01:00
|
|
|
modules << '${author}.$m'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modules
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_all_modules() []string {
|
2020-01-03 22:07:58 +01:00
|
|
|
url := get_working_server_url()
|
2019-12-30 05:22:28 +01:00
|
|
|
r := http.get(url) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if r.status_code != 200 {
|
2020-03-16 10:53:28 +01:00
|
|
|
println('Failed to search vpm.vlang.io. Status code: $r.status_code')
|
2019-12-30 05:22:28 +01:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
s := r.text
|
|
|
|
mut read_len := 0
|
2020-04-26 13:49:31 +02:00
|
|
|
mut modules := []string{}
|
2019-12-30 05:22:28 +01:00
|
|
|
for read_len < s.len {
|
|
|
|
mut start_token := '<a href="/mod'
|
|
|
|
end_token := '</a>'
|
|
|
|
// get the start index of the module entry
|
|
|
|
mut start_index := s.index_after(start_token, read_len)
|
|
|
|
if start_index == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// get the index of the end of anchor (a) opening tag
|
|
|
|
// we use the previous start_index to make sure we are getting a module and not just a random 'a' tag
|
|
|
|
start_token = '">'
|
|
|
|
start_index = s.index_after(start_token, start_index) + start_token.len
|
|
|
|
// get the index of the end of module entry
|
|
|
|
end_index := s.index_after(end_token, start_index)
|
|
|
|
if end_index == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
modules << s[start_index..end_index]
|
|
|
|
read_len = end_index
|
|
|
|
if read_len >= s.len {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modules
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_dependencies(name, module_path string, module_names []string) {
|
2020-03-13 20:52:49 +01:00
|
|
|
vmod_path := os.join_path(module_path,'v.mod')
|
2019-12-30 05:22:28 +01:00
|
|
|
if !os.exists(vmod_path) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data := os.read_file(vmod_path) or {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vmod := parse_vmod(data)
|
2020-04-26 13:49:31 +02:00
|
|
|
mut deps := []string{}
|
2019-12-30 05:22:28 +01:00
|
|
|
// filter out dependencies that were already specified by the user
|
|
|
|
for d in vmod.deps {
|
2020-04-26 06:39:23 +02:00
|
|
|
if d !in module_names {
|
2019-12-30 05:22:28 +01:00
|
|
|
deps << d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deps.len > 0 {
|
|
|
|
println('Resolving ${deps.len} dependencies for module "$name"...')
|
2020-01-03 22:07:58 +01:00
|
|
|
verbose_println('Found dependencies: $deps')
|
2019-12-30 05:22:28 +01:00
|
|
|
vpm_install(deps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_vmod(data string) Vmod {
|
|
|
|
keys := ['name', 'version', 'deps']
|
|
|
|
mut m := {
|
|
|
|
'name': '',
|
|
|
|
'version': '',
|
|
|
|
'deps': ''
|
|
|
|
}
|
|
|
|
for key in keys {
|
|
|
|
mut key_index := data.index('$key:') or {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key_index += key.len + 1
|
|
|
|
m[key] = data[key_index..data.index_after('\n', key_index)].trim_space().replace("'", '').replace('[', '').replace(']', '')
|
|
|
|
}
|
|
|
|
mut vmod := Vmod{}
|
|
|
|
vmod.name = m['name']
|
|
|
|
vmod.version = m['version']
|
|
|
|
if m['deps'].len > 0 {
|
|
|
|
vmod.deps = m['deps'].split(',')
|
|
|
|
}
|
|
|
|
return vmod
|
|
|
|
}
|
2020-01-03 22:07:58 +01:00
|
|
|
|
|
|
|
fn get_working_server_url() string {
|
|
|
|
server_urls := if settings.server_urls.len > 0 { settings.server_urls } else { default_vpm_server_urls }
|
|
|
|
for url in server_urls {
|
|
|
|
verbose_println('Trying server url: $url')
|
|
|
|
http.head(url) or {
|
|
|
|
verbose_println(' $url failed.')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
panic('No responding vpm server found. Please check your network connectivity and try again later.')
|
|
|
|
}
|
|
|
|
|
|
|
|
// settings context:
|
|
|
|
struct VpmSettings {
|
|
|
|
mut:
|
|
|
|
is_help bool
|
|
|
|
is_verbose bool
|
|
|
|
server_urls []string
|
|
|
|
vmodules_path string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
settings = &VpmSettings{}
|
|
|
|
)
|
|
|
|
|
|
|
|
fn init_settings() {
|
|
|
|
mut s := &VpmSettings(0)
|
|
|
|
unsafe{
|
|
|
|
s = settings
|
|
|
|
}
|
|
|
|
s.is_help = '-h' in os.args || '--help' in os.args || 'help' in os.args
|
2020-05-21 07:30:38 +02:00
|
|
|
s.is_verbose = '-verbose' in os.args || '--verbose' in os.args || '-v' in os.args
|
2020-02-16 12:42:28 +01:00
|
|
|
s.server_urls = cmdline.options(os.args, '-server-url')
|
2020-01-03 22:07:58 +01:00
|
|
|
s.vmodules_path = os.home_dir() + '.vmodules'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verbose_println(s string) {
|
|
|
|
if settings.is_verbose {
|
|
|
|
println(s)
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 14:30:22 +01:00
|
|
|
|
|
|
|
fn get_module_meta_info(name string) ?Mod {
|
2020-04-26 13:49:31 +02:00
|
|
|
mut errors := []string{}
|
2020-03-16 14:30:22 +01:00
|
|
|
for server_url in default_vpm_server_urls {
|
|
|
|
modurl := server_url + '/jsmod/$name'
|
|
|
|
verbose_println('Retrieving module metadata from: $modurl ...')
|
|
|
|
r := http.get(modurl) or {
|
|
|
|
errors << 'Http server did not respond to our request for ${modurl}.'
|
|
|
|
errors << 'Error details: $err'
|
|
|
|
continue
|
|
|
|
}
|
2020-05-08 22:50:36 +02:00
|
|
|
if r.status_code == 404 || r.text.contains('404') {
|
2020-03-16 14:30:22 +01:00
|
|
|
errors << 'Skipping module "$name", since $server_url reported that "$name" does not exist.'
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if r.status_code != 200 {
|
|
|
|
errors << 'Skipping module "$name", since $server_url responded with $r.status_code http status code. Please try again later.'
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s := r.text
|
|
|
|
if s.len > 0 && s[0] != `{` {
|
|
|
|
errors << 'Invalid json data'
|
|
|
|
errors << s.trim_space().limit(100) + '...'
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mod := json.decode(Mod,s) or {
|
|
|
|
errors << 'Skipping module "$name", since its information is not in json format.'
|
|
|
|
continue
|
|
|
|
}
|
2020-03-29 10:08:42 +02:00
|
|
|
if '' == mod.url || '' == mod.name {
|
2020-03-16 14:30:22 +01:00
|
|
|
errors << 'Skipping module "$name", since it is missing name or url information.'
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
return error(errors.join_lines())
|
|
|
|
}
|