tools.vpm: cleanup install part1 (#19718)

This commit is contained in:
Turiiya 2023-11-01 11:46:07 +01:00 committed by GitHub
parent 7eaf21ec27
commit be487b3c36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -76,7 +76,7 @@ fn main() {
vpm_search(requested_modules) vpm_search(requested_modules)
} }
'install' { 'install' {
vpm_install_(requested_modules, options) vpm_install(requested_modules, options)
} }
'update' { 'update' {
vpm_update(requested_modules) vpm_update(requested_modules)
@ -107,7 +107,7 @@ fn main() {
} }
} }
fn vpm_install_(requested_modules []string, opts []string) { fn vpm_install(requested_modules []string, opts []string) {
if settings.is_help { if settings.is_help {
help.print_and_exit('vpm') help.print_and_exit('vpm')
} }
@ -178,11 +178,11 @@ fn vpm_install_(requested_modules []string, opts []string) {
} }
if vpm_modules.len > 0 { if vpm_modules.len > 0 {
vpm_install(vpm_modules, Source.vpm) vpm_install_from_vpm(vpm_modules)
} }
if external_modules.len > 0 { if external_modules.len > 0 {
source := if '--hg' in opts { Source.hg } else { Source.git } source := if '--hg' in opts { Source.hg } else { Source.git }
vpm_install(external_modules, source) vpm_install_from_vcs(external_modules, source)
} }
} }
@ -238,6 +238,17 @@ fn vpm_search(keywords []string) {
} }
} }
fn install_module(vcs string, name string, url string, final_module_path string) ! {
cmd := '${vcs} ${supported_vcs_install_cmds[vcs]} "${url}" "${final_module_path}"'
verbose_println(' command: ${cmd}')
println('Installing module "${name}" from "${url}" to "${final_module_path}" ...')
res := os.execute(cmd)
if res.exit_code != 0 {
verbose_println(' command output: ${res.output}')
return error('Failed installing module "${name}" to "${final_module_path}" .')
}
}
fn vpm_install_from_vpm(module_names []string) { fn vpm_install_from_vpm(module_names []string) {
mut errors := 0 mut errors := 0
for n in module_names { for n in module_names {
@ -257,32 +268,25 @@ fn vpm_install_from_vpm(module_names []string) {
eprintln('Skipping module "${name}", since it uses an unsupported VCS {${vcs}} .') eprintln('Skipping module "${name}", since it uses an unsupported VCS {${vcs}} .')
continue continue
} }
if !ensure_vcs_is_installed(vcs) { ensure_vcs_is_installed(vcs) or {
errors++ errors++
eprintln('VPM needs `${vcs}` to be installed.') eprintln(err)
continue continue
} }
//
minfo := mod_name_info(mod.name) minfo := mod_name_info(mod.name)
if os.exists(minfo.final_module_path) { if os.exists(minfo.final_module_path) {
vpm_update([name]) vpm_update([name])
continue continue
} }
println('Installing module "${name}" from "${mod.url}" to "${minfo.final_module_path}" ...') install_module(vcs, name, mod.url, minfo.final_module_path) or {
errors++
eprintln(err)
continue
}
increment_module_download_count(name) or { increment_module_download_count(name) or {
errors++ errors++
eprintln('Errors while incrementing the download count for ${name}:') eprintln('Errors while incrementing the download count for ${name}:')
} }
vcs_install_cmd := '${vcs} ${supported_vcs_install_cmds[vcs]}'
cmd := '${vcs_install_cmd} "${mod.url}" "${minfo.final_module_path}"'
verbose_println(' command: ${cmd}')
cmdres := os.execute(cmd)
if cmdres.exit_code != 0 {
errors++
eprintln('Failed installing module "${name}" to "${minfo.final_module_path}" .')
print_failed_cmd(cmd, cmdres)
continue
}
resolve_dependencies(name, minfo.final_module_path, module_names) resolve_dependencies(name, minfo.final_module_path, module_names)
} }
if errors > 0 { if errors > 0 {
@ -290,23 +294,18 @@ fn vpm_install_from_vpm(module_names []string) {
} }
} }
fn print_failed_cmd(cmd string, cmdres os.Result) { fn ensure_vcs_is_installed(vcs string) ! {
verbose_println('Failed command: ${cmd}')
verbose_println('Failed command output:\n${cmdres.output}')
}
fn ensure_vcs_is_installed(vcs string) bool {
mut res := true
cmd := '${vcs} ${supported_vcs_version_cmds[vcs]}' cmd := '${vcs} ${supported_vcs_version_cmds[vcs]}'
cmdres := os.execute(cmd) verbose_println(' command: ${cmd}')
if cmdres.exit_code != 0 { res := os.execute(cmd)
print_failed_cmd(cmd, cmdres) if res.exit_code != 0 {
res = false verbose_println(' command output: ${res.output}')
return error('VPM needs `${vcs}` to be installed.')
} }
return res
} }
fn vpm_install_from_vcs(modules []string, vcs_key string) { fn vpm_install_from_vcs(modules []string, vcs_key Source) {
vcs := vcs_key.str()
mut errors := 0 mut errors := 0
for raw_url in modules { for raw_url in modules {
url := urllib.parse(raw_url) or { url := urllib.parse(raw_url) or {
@ -314,7 +313,6 @@ fn vpm_install_from_vcs(modules []string, vcs_key string) {
eprintln('Errors while parsing module url "${raw_url}" : ${err}') eprintln('Errors while parsing module url "${raw_url}" : ${err}')
continue continue
} }
// Module identifier based on URL. // Module identifier based on URL.
// E.g.: `https://github.com/owner/awesome-v-project` -> `owner/awesome_v_project` // E.g.: `https://github.com/owner/awesome-v-project` -> `owner/awesome_v_project`
mut ident := url.path#[1..].replace('-', '_') mut ident := url.path#[1..].replace('-', '_')
@ -325,27 +323,18 @@ fn vpm_install_from_vcs(modules []string, vcs_key string) {
} }
mut final_module_path := os.real_path(os.join_path(settings.vmodules_path, owner.to_lower(), mut final_module_path := os.real_path(os.join_path(settings.vmodules_path, owner.to_lower(),
repo_name.to_lower())) repo_name.to_lower()))
if os.exists(final_module_path) { if os.exists(final_module_path) {
vpm_update([ident]) vpm_update([ident])
continue continue
} }
ensure_vcs_is_installed(vcs) or {
if !ensure_vcs_is_installed(vcs_key) {
errors++ errors++
eprintln('VPM needs `${vcs_key}` to be installed.') eprintln(err)
continue continue
} }
install_module(vcs, repo_name, url.str(), final_module_path) or {
println('Installing module "${repo_name}" from "${url}" to "${final_module_path}" ...')
vcs_install_cmd := '${vcs_key} ${supported_vcs_install_cmds[vcs_key]}'
cmd := '${vcs_install_cmd} "${url}" "${final_module_path}"'
verbose_println(' command: ${cmd}')
cmdres := os.execute(cmd)
if cmdres.exit_code != 0 {
errors++ errors++
eprintln('Failed installing module "${repo_name}" to "${final_module_path}" .') eprintln(err)
print_failed_cmd(cmd, cmdres)
continue continue
} }
vmod_path := os.join_path(final_module_path, 'v.mod') vmod_path := os.join_path(final_module_path, 'v.mod')
@ -380,7 +369,7 @@ fn vpm_install_from_vcs(modules []string, vcs_key string) {
continue continue
} }
println('Module "${repo_name}" relocated to "${manifest.name}" successfully.') println('Module "${repo_name}" relocated to "${manifest.name}" successfully.')
publisher_dir := final_module_path.all_before_last(os.path_separator) publisher_dir := os.dir(final_module_path)
if os.is_dir_empty(publisher_dir) { if os.is_dir_empty(publisher_dir) {
os.rmdir(publisher_dir) or { os.rmdir(publisher_dir) or {
errors++ errors++
@ -399,27 +388,6 @@ fn vpm_install_from_vcs(modules []string, vcs_key string) {
} }
} }
fn vpm_install(module_names []string, source Source) {
if settings.is_help {
help.print_and_exit('install')
}
if module_names.len == 0 {
eprintln('´v install´ requires *at least one* module name.')
exit(2)
}
match source {
.vpm {
vpm_install_from_vpm(module_names)
}
.git {
vpm_install_from_vcs(module_names, 'git')
}
.hg {
vpm_install_from_vcs(module_names, 'hg')
}
}
}
pub struct ModUpdateInfo { pub struct ModUpdateInfo {
mut: mut:
name string name string
@ -434,27 +402,26 @@ fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &ModUpdateInfo {
zname := url_to_module_name(result.name) zname := url_to_module_name(result.name)
result.final_path = valid_final_path_of_existing_module(result.name) or { return result } result.final_path = valid_final_path_of_existing_module(result.name) or { return result }
println('Updating module "${zname}" in "${result.final_path}" ...') println('Updating module "${zname}" in "${result.final_path}" ...')
vcs := vcs_used_in_dir(result.final_path) or { return result } vcs := vcs_used_in_dir(result.final_path) or { return result }[0]
if !ensure_vcs_is_installed(vcs[0]) { ensure_vcs_is_installed(vcs) or {
result.has_err = true result.has_err = true
println('VPM needs `${vcs}` to be installed.') eprintln(err)
return result return result
} }
path_flag := if vcs[0] == 'hg' { '-R' } else { '-C' } path_flag := if vcs == 'hg' { '-R' } else { '-C' }
vcs_cmd := '${vcs[0]} ${path_flag} "${result.final_path}" ${supported_vcs_update_cmds[vcs[0]]}' cmd := '${vcs} ${path_flag} "${result.final_path}" ${supported_vcs_update_cmds[vcs]}'
verbose_println(' command: ${vcs_cmd}') verbose_println(' command: ${cmd}')
vcs_res := os.execute('${vcs_cmd}') res := os.execute('${cmd}')
if vcs_res.exit_code != 0 { if res.exit_code != 0 {
result.has_err = true result.has_err = true
println('Failed updating module "${zname}" in "${result.final_path}".') println('Failed updating module "${zname}" in "${result.final_path}".')
print_failed_cmd(vcs_cmd, vcs_res) verbose_println(' command output: ${res.output}')
return result return result
} else { }
verbose_println(' ${vcs_res.output.trim_space()}') verbose_println(' ${res.output.trim_space()}')
increment_module_download_count(zname) or { increment_module_download_count(zname) or {
result.has_err = true result.has_err = true
eprintln('Errors while incrementing the download count for ${zname}:') eprintln('Errors while incrementing the download count for ${zname}:')
}
} }
return result return result
} }
@ -487,27 +454,26 @@ fn vpm_update_verbose(module_names []string) {
zname := url_to_module_name(name) zname := url_to_module_name(name)
final_module_path := valid_final_path_of_existing_module(name) or { continue } final_module_path := valid_final_path_of_existing_module(name) or { continue }
println('Updating module "${zname}" in "${final_module_path}" ...') println('Updating module "${zname}" in "${final_module_path}" ...')
vcs := vcs_used_in_dir(final_module_path) or { continue } vcs := vcs_used_in_dir(final_module_path) or { continue }[0]
if !ensure_vcs_is_installed(vcs[0]) { ensure_vcs_is_installed(vcs) or {
errors++ errors++
println('VPM needs `${vcs}` to be installed.') eprintln(err)
continue continue
} }
path_flag := if vcs[0] == 'hg' { '-R' } else { '-C' } path_flag := if vcs == 'hg' { '-R' } else { '-C' }
vcs_cmd := '${vcs[0]} ${path_flag} "${final_module_path}" ${supported_vcs_update_cmds[vcs[0]]}' cmd := '${vcs} ${path_flag} "${final_module_path}" ${supported_vcs_update_cmds[vcs]}'
verbose_println(' command: ${vcs_cmd}') verbose_println(' command: ${cmd}')
vcs_res := os.execute('${vcs_cmd}') res := os.execute('${cmd}')
if vcs_res.exit_code != 0 { if res.exit_code != 0 {
errors++ errors++
println('Failed updating module "${zname}" in "${final_module_path}" .') println('Failed updating module "${zname}" in "${final_module_path}" .')
print_failed_cmd(vcs_cmd, vcs_res) verbose_println(' command output: ${res.output}')
continue continue
} else { }
verbose_println(' ${vcs_res.output.trim_space()}') verbose_println(' ${res.output.trim_space()}')
increment_module_download_count(zname) or { increment_module_download_count(zname) or {
errors++ errors++
eprintln('Errors while incrementing the download count for ${zname}:') eprintln('Errors while incrementing the download count for ${zname}:')
}
} }
resolve_dependencies(name, final_module_path, module_names) resolve_dependencies(name, final_module_path, module_names)
} }
@ -542,14 +508,11 @@ fn get_mod_date_info(mut pp pool.PoolProcessor, idx int, wid int) &ModDateInfo {
result.exec_err = true result.exec_err = true
return result return result
} }
if is_hg { if is_hg && res.exit_code == 1 {
if res.exit_code == 1 { result.outdated = true
result.outdated = true return result
return result
}
} else {
outputs << res.output
} }
outputs << res.output
} }
// vcs[0] == 'git' // vcs[0] == 'git'
if !is_hg && outputs[1] != outputs[2] { if !is_hg && outputs[1] != outputs[2] {
@ -778,7 +741,7 @@ fn resolve_dependencies(name string, module_path string, module_names []string)
if deps.len > 0 { if deps.len > 0 {
println('Resolving ${deps.len} dependencies for module "${name}" ...') println('Resolving ${deps.len} dependencies for module "${name}" ...')
verbose_println('Found dependencies: ${deps}') verbose_println('Found dependencies: ${deps}')
vpm_install(deps, Source.vpm) vpm_install_from_vpm(deps)
} }
} }