vpm.tools: cleanup and make handling of the different VCS systems more robust (#19740)

This commit is contained in:
Turiiya 2023-11-03 19:25:46 +01:00 committed by GitHub
parent ceb191d317
commit be89cbf26f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 64 deletions

View File

@ -36,12 +36,11 @@ fn get_mod_date_info(mut pp pool.PoolProcessor, idx int, wid int) &ModDateInfo {
}
final_module_path := valid_final_path_of_existing_module(result.name) or { return result }
vcs := vcs_used_in_dir(final_module_path) or { return result }
is_hg := vcs[0] == 'hg'
vcs_cmd_steps := supported_vcs_outdated_steps[vcs[0]]
is_hg := vcs.cmd == 'hg'
mut outputs := []string{}
for step in vcs_cmd_steps {
for step in vcs.outdated_steps {
path_flag := if is_hg { '-R' } else { '-C' }
cmd := '${vcs[0]} ${path_flag} "${final_module_path}" ${step}'
cmd := '${vcs.cmd} ${path_flag} "${final_module_path}" ${step}'
res := os.execute('${cmd}')
if res.exit_code < 0 {
verbose_println('Error command: ${cmd}')
@ -55,7 +54,7 @@ fn get_mod_date_info(mut pp pool.PoolProcessor, idx int, wid int) &ModDateInfo {
}
outputs << res.output
}
// vcs[0] == 'git'
// vcs.cmd == 'git'
if !is_hg && outputs[1] != outputs[2] {
result.outdated = true
}
@ -229,8 +228,8 @@ fn ensure_vmodules_dir_exist() {
}
}
fn ensure_vcs_is_installed(vcs string) ! {
cmd := '${vcs} ${supported_vcs_version_cmds[vcs]}'
fn ensure_vcs_is_installed(vcs &VCS) ! {
cmd := '${vcs.cmd} --version'
verbose_println(' command: ${cmd}')
res := os.execute(cmd)
if res.exit_code != 0 {
@ -284,18 +283,13 @@ fn url_to_module_name(modulename string) string {
return res
}
fn vcs_used_in_dir(dir string) ?[]string {
mut vcs := []string{}
for repo_subfolder in supported_vcs_folders {
checked_folder := os.real_path(os.join_path(dir, repo_subfolder))
if os.is_dir(checked_folder) {
vcs << repo_subfolder.replace('.', '')
fn vcs_used_in_dir(dir string) ?VCS {
for vcs in supported_vcs.values() {
if os.is_dir(os.real_path(os.join_path(dir, vcs.dir))) {
return vcs
}
}
if vcs.len == 0 {
return none
}
return vcs
return none
}
fn valid_final_path_of_existing_module(modulename string) ?string {

View File

@ -79,13 +79,13 @@ fn vpm_install(requested_modules []string, opts []string) {
vpm_install_from_vpm(vpm_modules)
}
if external_modules.len > 0 {
source := if '--hg' in opts { Source.hg } else { Source.git }
vpm_install_from_vcs(external_modules, source)
vcs := if '--hg' in opts { supported_vcs['hd'] } else { supported_vcs['git'] }
vpm_install_from_vcs(external_modules, vcs)
}
}
fn install_module(vcs string, name string, url string, final_module_path string) ! {
cmd := '${vcs} ${supported_vcs_install_cmds[vcs]} "${url}" "${final_module_path}"'
fn install_module(vcs &VCS, name string, url string, final_module_path string) ! {
cmd := '${vcs.cmd} ${vcs.install_arg} "${url}" "${final_module_path}"'
verbose_println(' command: ${cmd}')
println('Installing module "${name}" from "${url}" to "${final_module_path}" ...')
res := os.execute(cmd)
@ -105,14 +105,14 @@ fn vpm_install_from_vpm(module_names []string) {
eprintln(err)
continue
}
mut vcs := mod.vcs
if vcs == '' {
vcs = supported_vcs_systems[0]
}
if vcs !in supported_vcs_systems {
errors++
eprintln('Skipping module "${name}", since it uses an unsupported VCS {${vcs}} .')
continue
vcs := if mod.vcs != '' {
supported_vcs[mod.vcs] or {
errors++
eprintln('Skipping module "${name}", since it uses an unsupported VCS {${mod.vcs}} .')
continue
}
} else {
supported_vcs['git']
}
ensure_vcs_is_installed(vcs) or {
errors++
@ -140,8 +140,7 @@ fn vpm_install_from_vpm(module_names []string) {
}
}
fn vpm_install_from_vcs(modules []string, vcs_key Source) {
vcs := vcs_key.str()
fn vpm_install_from_vcs(modules []string, vcs &VCS) {
mut errors := 0
for raw_url in modules {
url := urllib.parse(raw_url) or {

View File

@ -18,14 +18,14 @@ fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &ModUpdateInfo {
zname := url_to_module_name(result.name)
result.final_path = valid_final_path_of_existing_module(result.name) or { return result }
println('Updating module "${zname}" in "${result.final_path}" ...')
vcs := vcs_used_in_dir(result.final_path) or { return result }[0]
vcs := vcs_used_in_dir(result.final_path) or { return result }
ensure_vcs_is_installed(vcs) or {
result.has_err = true
eprintln(err)
return result
}
path_flag := if vcs == 'hg' { '-R' } else { '-C' }
cmd := '${vcs} ${path_flag} "${result.final_path}" ${supported_vcs_update_cmds[vcs]}'
path_flag := if vcs.cmd == 'hg' { '-R' } else { '-C' }
cmd := '${vcs.cmd} ${path_flag} "${result.final_path}" ${vcs.update_arg}'
verbose_println(' command: ${cmd}')
res := os.execute('${cmd}')
if res.exit_code != 0 {
@ -70,14 +70,14 @@ fn vpm_update_verbose(module_names []string) {
zname := url_to_module_name(name)
final_module_path := valid_final_path_of_existing_module(name) or { continue }
println('Updating module "${zname}" in "${final_module_path}" ...')
vcs := vcs_used_in_dir(final_module_path) or { continue }[0]
vcs := vcs_used_in_dir(final_module_path) or { continue }
ensure_vcs_is_installed(vcs) or {
errors++
eprintln(err)
continue
}
path_flag := if vcs == 'hg' { '-R' } else { '-C' }
cmd := '${vcs} ${path_flag} "${final_module_path}" ${supported_vcs_update_cmds[vcs]}'
path_flag := if vcs.cmd == 'hg' { '-R' } else { '-C' }
cmd := '${vcs.cmd} ${path_flag} "${final_module_path}" ${vcs.update_arg}'
verbose_println(' command: ${cmd}')
res := os.execute('${cmd}')
if res.exit_code != 0 {

View File

@ -17,36 +17,36 @@ mut:
vmodules_path string
}
enum Source {
git
hg
vpm
struct VCS {
cmd string
dir string
update_arg string
install_arg string
outdated_steps []string
}
const (
settings = &VpmSettings{}
default_vpm_server_urls = ['https://vpm.vlang.io', 'https://vpm.url4e.com']
vpm_server_urls = rand.shuffle_clone(default_vpm_server_urls) or { [] } // ensure that all queries are distributed fairly
valid_vpm_commands = ['help', 'search', 'install', 'update', 'upgrade', 'outdated',
'list', 'remove', 'show']
excluded_dirs = ['cache', 'vlib']
supported_vcs_systems = ['git', 'hg']
supported_vcs_folders = ['.git', '.hg']
supported_vcs_update_cmds = {
'git': 'pull --recurse-submodules' // pulling with `--depth=1` leads to conflicts, when the upstream is more than 1 commit newer
'hg': 'pull --update'
}
supported_vcs_install_cmds = {
'git': 'clone --depth=1 --recursive --shallow-submodules'
'hg': 'clone'
}
supported_vcs_outdated_steps = {
'git': ['fetch', 'rev-parse @', 'rev-parse @{u}']
'hg': ['incoming']
}
supported_vcs_version_cmds = {
'git': 'version'
'hg': 'version'
settings = &VpmSettings{}
default_vpm_server_urls = ['https://vpm.vlang.io', 'https://vpm.url4e.com']
vpm_server_urls = rand.shuffle_clone(default_vpm_server_urls) or { [] } // ensure that all queries are distributed fairly
valid_vpm_commands = ['help', 'search', 'install', 'update', 'upgrade', 'outdated', 'list',
'remove', 'show']
excluded_dirs = ['cache', 'vlib']
supported_vcs = {
'git': VCS{
cmd: 'git'
dir: '.git'
update_arg: 'pull --recurse-submodules' // pulling with `--depth=1` leads to conflicts, when the upstream is more than 1 commit newer
install_arg: 'clone --depth=1 --recursive --shallow-submodules'
outdated_steps: ['fetch', 'rev-parse @', 'rev-parse @{u}']
}
'hg': VCS{
cmd: 'hg'
dir: '.hg'
update_arg: 'pull --update'
install_arg: 'clone'
outdated_steps: ['incoming']
}
}
)