tools: use shared contexts in vpm's update, to resolve dependencies during threaded module updates (#19878)

This commit is contained in:
Turiiya 2023-11-15 08:13:24 +01:00 committed by GitHub
parent 757929392e
commit 107d466fad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,10 +4,12 @@ import os
import sync.pool import sync.pool
import v.help import v.help
pub struct ModUpdateInfo { struct UpdateSession {
idents []string
}
pub struct UpdateResult {
mut: mut:
name string
final_path string
has_err bool has_err bool
} }
@ -15,42 +17,42 @@ fn vpm_update(query []string) {
if settings.is_help { if settings.is_help {
help.print_and_exit('update') help.print_and_exit('update')
} }
modules := if query.len == 0 { idents := if query.len == 0 {
get_installed_modules() get_installed_modules()
} else { } else {
query.clone() query.clone()
} }
if settings.is_verbose { if settings.is_verbose {
vpm_update_verbose(modules) vpm_update_verbose(idents)
return return
} }
mut pp := pool.new_pool_processor(callback: update_module) mut pp := pool.new_pool_processor(callback: update_module)
pp.work_on_items(modules) ctx := UpdateSession{idents}
pp.set_shared_context(ctx)
pp.work_on_items(idents)
mut errors := 0 mut errors := 0
for res in pp.get_results[ModUpdateInfo]() { for res in pp.get_results[UpdateResult]() {
if res.has_err { if res.has_err {
errors++ errors++
continue continue
} }
resolve_dependencies(get_manifest(res.final_path), modules)
} }
if errors > 0 { if errors > 0 {
exit(1) exit(1)
} }
} }
fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &ModUpdateInfo { fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &UpdateResult {
mut result := &ModUpdateInfo{ mut result := &UpdateResult{}
name: pp.get_item[string](idx) ident := pp.get_item[string](idx)
} name := get_name_from_url(ident) or { ident }
name := get_name_from_url(result.name) or { result.name } install_path := get_path_of_existing_module(ident) or {
result.final_path = get_path_of_existing_module(result.name) or {
vpm_error('failed to find path for `${name}`.', verbose: true) vpm_error('failed to find path for `${name}`.', verbose: true)
result.has_err = true result.has_err = true
return result return result
} }
println('Updating module `${name}` in `${fmt_mod_path(result.final_path)}` ...') println('Updating module `${name}` in `${fmt_mod_path(install_path)}` ...')
vcs := vcs_used_in_dir(result.final_path) or { vcs := vcs_used_in_dir(install_path) or {
vpm_error('failed to find version control system for `${name}`.', verbose: true) vpm_error('failed to find version control system for `${name}`.', verbose: true)
result.has_err = true result.has_err = true
return result return result
@ -60,24 +62,24 @@ fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &ModUpdateInfo {
vpm_error(err.msg()) vpm_error(err.msg())
return result return result
} }
cmd := '${vcs.cmd} ${vcs.args.path} "${result.final_path}" ${vcs.args.update}' cmd := '${vcs.cmd} ${vcs.args.path} "${install_path}" ${vcs.args.update}'
vpm_log(@FILE_LINE, @FN, 'cmd: ${cmd}') vpm_log(@FILE_LINE, @FN, 'cmd: ${cmd}')
res := os.execute_opt(cmd) or { res := os.execute_opt(cmd) or {
result.has_err = true result.has_err = true
vpm_error('failed to update module `${name}` in `${result.final_path}`.', details: err.msg()) vpm_error('failed to update module `${name}` in `${install_path}`.', details: err.msg())
return result return result
} }
vpm_log(@FILE_LINE, @FN, 'cmd output: ${res.output.trim_space()}') vpm_log(@FILE_LINE, @FN, 'cmd output: ${res.output.trim_space()}')
increment_module_download_count(name) or { // Don't bail if the download count increment has failed.
result.has_err = true increment_module_download_count(name) or { vpm_error(err.msg(), verbose: true) }
vpm_error(err.msg(), verbose: true) ctx := unsafe { &UpdateSession(pp.get_shared_context()) }
} resolve_dependencies(get_manifest(install_path), ctx.idents)
return result return result
} }
fn vpm_update_verbose(modules []string) { fn vpm_update_verbose(idents []string) {
mut errors := 0 mut errors := 0
for mod in modules { for mod in idents {
name := get_name_from_url(mod) or { mod } name := get_name_from_url(mod) or { mod }
install_path := get_path_of_existing_module(mod) or { continue } install_path := get_path_of_existing_module(mod) or { continue }
println('Updating module `${name}` in `${fmt_mod_path(install_path)}` ...') println('Updating module `${name}` in `${fmt_mod_path(install_path)}` ...')
@ -101,7 +103,7 @@ fn vpm_update_verbose(modules []string) {
errors++ errors++
vpm_error(err.msg(), verbose: true) vpm_error(err.msg(), verbose: true)
} }
resolve_dependencies(get_manifest(install_path), modules) resolve_dependencies(get_manifest(install_path), idents)
} }
if errors > 0 { if errors > 0 {
exit(1) exit(1)