tools: use expansion character/string to format home paths in vpm output (#19847)

This commit is contained in:
Turiiya 2023-11-12 15:09:04 +01:00 committed by GitHub
parent 7c5d439717
commit fbb43f5ae4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 12 deletions

View file

@ -16,11 +16,12 @@ mut:
url string
vcs string
// Fields based on preference / environment.
version string // specifies the requested version.
install_path string
is_installed bool
is_external bool
installed_version string
version string // specifies the requested version.
install_path string
install_path_fmted string
is_installed bool
is_external bool
installed_version string
}
struct ModuleVpmInfo {
@ -44,6 +45,8 @@ struct ErrorOptions {
verbose bool // is used to only output the error message if the verbose setting is enabled.
}
const home_dir = os.home_dir()
fn parse_query(query []string) ([]Module, []Module) {
mut vpm_modules, mut external_modules := []Module{}, []Module{}
mut errors := 0
@ -64,6 +67,7 @@ fn parse_query(query []string) ([]Module, []Module) {
name: name
url: ident
install_path: install_path
install_path_fmted: fmt_mod_path(install_path)
is_external: true
}
} else {
@ -74,11 +78,13 @@ fn parse_query(query []string) ([]Module, []Module) {
}
name_normalized := info.name.replace('-', '_').to_lower()
name_as_path := name_normalized.replace('.', os.path_separator)
install_path := os.real_path(os.join_path(settings.vmodules_path, name_as_path))
Module{
name: info.name
url: info.url
vcs: info.vcs
install_path: os.real_path(os.join_path(settings.vmodules_path, name_as_path))
install_path: install_path
install_path_fmted: fmt_mod_path(install_path)
}
}
mod.version = version
@ -414,3 +420,15 @@ fn vpm_error(msg string, opts ErrorOptions) {
}
}
}
// Formatted version of the vmodules install path. E.g. `/home/user/.vmodules` -> `~/.vmodules`
fn fmt_mod_path(path string) string {
if !path.contains(home_dir) {
return path
}
return $if windows {
path.replace(home_dir, '%USERPROFILE%')
} $else {
path.replace(home_dir, '~')
}
}

View file

@ -157,7 +157,7 @@ fn vpm_install_from_vcs(modules []Module) {
final_path := os.real_path(os.join_path(settings.vmodules_path, manifest.name.replace('-',
'_').to_lower()))
if m.install_path != final_path {
verbose_println('Relocating `${m.name} (${m.install_path})` to `${manifest.name} (${final_path})`...')
verbose_println('Relocating `${m.name} (${m.install_path_fmted})` to `${manifest.name} (${final_path})`...')
if os.exists(final_path) {
println('Target directory for `${m.name} (${final_path})` already exists.')
input := os.input('Replace it with the module directory? [Y/n]: ')
@ -234,7 +234,7 @@ fn (m Module) install(vcs &VCS) InstallResult {
cmd := '${vcs.cmd} ${install_arg} "${m.url}" "${m.install_path}"'
vpm_log(@FILE_LINE, @FN, 'command: ${cmd}')
println('Installing `${m.name}`...')
verbose_println(' cloning from `${m.url}` to `${m.install_path}`')
verbose_println(' cloning from `${m.url}` to `${m.install_path_fmted}`')
res := os.execute_opt(cmd) or {
vpm_error('failed to install `${m.name}`.', details: err.msg())
return .failed
@ -249,7 +249,7 @@ fn (m Module) confirm_install() bool {
return false
} else {
install_version := at_version(if m.version == '' { 'latest' } else { m.version })
println('Module `${m.name}${at_version(m.installed_version)}` is already installed at `${m.install_path}`.')
println('Module `${m.name}${at_version(m.installed_version)}` is already installed at `${m.install_path_fmted}`.')
input := os.input('Replace it with `${m.name}${install_version}`? [Y/n]: ')
match input.to_lower() {
'', 'y' {
@ -264,7 +264,7 @@ fn (m Module) confirm_install() bool {
}
fn (m Module) remove() ! {
verbose_println('Removing `${m.name}` from `${m.install_path}`...')
verbose_println('Removing `${m.name}` from `${m.install_path_fmted}`...')
$if windows {
os.execute_opt('rd /s /q ${m.install_path}')!
} $else {

View file

@ -40,7 +40,7 @@ fn update_module(mut pp pool.PoolProcessor, idx int, wid int) &ModUpdateInfo {
}
name := get_name_from_url(result.name) or { result.name }
result.final_path = get_path_of_existing_module(result.name) or { return result }
println('Updating module `${name}` in `${result.final_path}` ...')
println('Updating module `${name}` in `${fmt_mod_path(result.final_path)}` ...')
vcs := vcs_used_in_dir(result.final_path) or { return result }
vcs.is_executable() or {
result.has_err = true
@ -67,7 +67,7 @@ fn vpm_update_verbose(modules []string) {
for mod in modules {
name := get_name_from_url(mod) or { mod }
install_path := get_path_of_existing_module(mod) or { continue }
println('Updating module `${name}` in `${install_path}` ...')
println('Updating module `${name}` in `${fmt_mod_path(install_path)}` ...')
vcs := vcs_used_in_dir(install_path) or { continue }
vcs.is_executable() or {
errors++