Skip to content

Commit

Permalink
tools.vpm: cleanup install part1 (#19718)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Nov 1, 2023
1 parent 7eaf21e commit be487b3
Showing 1 changed file with 68 additions and 105 deletions.
173 changes: 68 additions & 105 deletions cmd/tools/vpm/vpm.v
Expand Up @@ -76,7 +76,7 @@ fn main() {
vpm_search(requested_modules)
}
'install' {
vpm_install_(requested_modules, options)
vpm_install(requested_modules, options)
}
'update' {
vpm_update(requested_modules)
Expand Down Expand Up @@ -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 {
help.print_and_exit('vpm')
}
Expand Down Expand Up @@ -178,11 +178,11 @@ fn vpm_install_(requested_modules []string, opts []string) {
}

if vpm_modules.len > 0 {
vpm_install(vpm_modules, Source.vpm)
vpm_install_from_vpm(vpm_modules)
}
if external_modules.len > 0 {
source := if '--hg' in opts { Source.hg } else { Source.git }
vpm_install(external_modules, source)
vpm_install_from_vcs(external_modules, source)
}
}

Expand Down Expand Up @@ -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) {
mut errors := 0
for n in module_names {
Expand All @@ -257,31 +268,24 @@ fn vpm_install_from_vpm(module_names []string) {
eprintln('Skipping module "${name}", since it uses an unsupported VCS {${vcs}} .')
continue
}
if !ensure_vcs_is_installed(vcs) {
ensure_vcs_is_installed(vcs) or {
errors++
eprintln('VPM needs `${vcs}` to be installed.')
eprintln(err)
continue
}
//
minfo := mod_name_info(mod.name)
if os.exists(minfo.final_module_path) {
vpm_update([name])
continue
}
println('Installing module "${name}" from "${mod.url}" to "${minfo.final_module_path}" ...')
increment_module_download_count(name) or {
install_module(vcs, name, mod.url, minfo.final_module_path) or {
errors++
eprintln('Errors while incrementing the download count for ${name}:')
eprintln(err)
continue
}
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 {
increment_module_download_count(name) or {
errors++
eprintln('Failed installing module "${name}" to "${minfo.final_module_path}" .')
print_failed_cmd(cmd, cmdres)
continue
eprintln('Errors while incrementing the download count for ${name}:')
}
resolve_dependencies(name, minfo.final_module_path, module_names)
}
Expand All @@ -290,31 +294,25 @@ fn vpm_install_from_vpm(module_names []string) {
}
}

fn print_failed_cmd(cmd string, cmdres os.Result) {
verbose_println('Failed command: ${cmd}')
verbose_println('Failed command output:\n${cmdres.output}')
}

fn ensure_vcs_is_installed(vcs string) bool {
mut res := true
fn ensure_vcs_is_installed(vcs string) ! {
cmd := '${vcs} ${supported_vcs_version_cmds[vcs]}'
cmdres := os.execute(cmd)
if cmdres.exit_code != 0 {
print_failed_cmd(cmd, cmdres)
res = false
verbose_println(' command: ${cmd}')
res := os.execute(cmd)
if res.exit_code != 0 {
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
for raw_url in modules {
url := urllib.parse(raw_url) or {
errors++
eprintln('Errors while parsing module url "${raw_url}" : ${err}')
continue
}

// Module identifier based on URL.
// E.g.: `https://github.com/owner/awesome-v-project` -> `owner/awesome_v_project`
mut ident := url.path#[1..].replace('-', '_')
Expand All @@ -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(),
repo_name.to_lower()))

if os.exists(final_module_path) {
vpm_update([ident])
continue
}

if !ensure_vcs_is_installed(vcs_key) {
ensure_vcs_is_installed(vcs) or {
errors++
eprintln('VPM needs `${vcs_key}` to be installed.')
eprintln(err)
continue
}

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 {
install_module(vcs, repo_name, url.str(), final_module_path) or {
errors++
eprintln('Failed installing module "${repo_name}" to "${final_module_path}" .')
print_failed_cmd(cmd, cmdres)
eprintln(err)
continue
}
vmod_path := os.join_path(final_module_path, 'v.mod')
Expand Down Expand Up @@ -380,7 +369,7 @@ fn vpm_install_from_vcs(modules []string, vcs_key string) {
continue
}
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) {
os.rmdir(publisher_dir) or {
errors++
Expand All @@ -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 {
mut:
name string
Expand All @@ -434,27 +402,26 @@ 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 }
if !ensure_vcs_is_installed(vcs[0]) {
vcs := vcs_used_in_dir(result.final_path) or { return result }[0]
ensure_vcs_is_installed(vcs) or {
result.has_err = true
println('VPM needs `${vcs}` to be installed.')
eprintln(err)
return result
}
path_flag := if vcs[0] == 'hg' { '-R' } else { '-C' }
vcs_cmd := '${vcs[0]} ${path_flag} "${result.final_path}" ${supported_vcs_update_cmds[vcs[0]]}'
verbose_println(' command: ${vcs_cmd}')
vcs_res := os.execute('${vcs_cmd}')
if vcs_res.exit_code != 0 {
path_flag := if vcs == 'hg' { '-R' } else { '-C' }
cmd := '${vcs} ${path_flag} "${result.final_path}" ${supported_vcs_update_cmds[vcs]}'
verbose_println(' command: ${cmd}')
res := os.execute('${cmd}')
if res.exit_code != 0 {
result.has_err = true
println('Failed updating module "${zname}" in "${result.final_path}".')
print_failed_cmd(vcs_cmd, vcs_res)
verbose_println(' command output: ${res.output}')
return result
} else {
verbose_println(' ${vcs_res.output.trim_space()}')
increment_module_download_count(zname) or {
result.has_err = true
eprintln('Errors while incrementing the download count for ${zname}:')
}
}
verbose_println(' ${res.output.trim_space()}')
increment_module_download_count(zname) or {
result.has_err = true
eprintln('Errors while incrementing the download count for ${zname}:')
}
return result
}
Expand Down Expand Up @@ -487,27 +454,26 @@ 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 }
if !ensure_vcs_is_installed(vcs[0]) {
vcs := vcs_used_in_dir(final_module_path) or { continue }[0]
ensure_vcs_is_installed(vcs) or {
errors++
println('VPM needs `${vcs}` to be installed.')
eprintln(err)
continue
}
path_flag := if vcs[0] == 'hg' { '-R' } else { '-C' }
vcs_cmd := '${vcs[0]} ${path_flag} "${final_module_path}" ${supported_vcs_update_cmds[vcs[0]]}'
verbose_println(' command: ${vcs_cmd}')
vcs_res := os.execute('${vcs_cmd}')
if vcs_res.exit_code != 0 {
path_flag := if vcs == 'hg' { '-R' } else { '-C' }
cmd := '${vcs} ${path_flag} "${final_module_path}" ${supported_vcs_update_cmds[vcs]}'
verbose_println(' command: ${cmd}')
res := os.execute('${cmd}')
if res.exit_code != 0 {
errors++
println('Failed updating module "${zname}" in "${final_module_path}" .')
print_failed_cmd(vcs_cmd, vcs_res)
verbose_println(' command output: ${res.output}')
continue
} else {
verbose_println(' ${vcs_res.output.trim_space()}')
increment_module_download_count(zname) or {
errors++
eprintln('Errors while incrementing the download count for ${zname}:')
}
}
verbose_println(' ${res.output.trim_space()}')
increment_module_download_count(zname) or {
errors++
eprintln('Errors while incrementing the download count for ${zname}:')
}
resolve_dependencies(name, final_module_path, module_names)
}
Expand Down Expand Up @@ -542,14 +508,11 @@ fn get_mod_date_info(mut pp pool.PoolProcessor, idx int, wid int) &ModDateInfo {
result.exec_err = true
return result
}
if is_hg {
if res.exit_code == 1 {
result.outdated = true
return result
}
} else {
outputs << res.output
if is_hg && res.exit_code == 1 {
result.outdated = true
return result
}
outputs << res.output
}
// vcs[0] == 'git'
if !is_hg && outputs[1] != outputs[2] {
Expand Down Expand Up @@ -778,7 +741,7 @@ fn resolve_dependencies(name string, module_path string, module_names []string)
if deps.len > 0 {
println('Resolving ${deps.len} dependencies for module "${name}" ...')
verbose_println('Found dependencies: ${deps}')
vpm_install(deps, Source.vpm)
vpm_install_from_vpm(deps)
}
}

Expand Down

0 comments on commit be487b3

Please sign in to comment.