Skip to content

Commit

Permalink
tools: use expansion character/string to format home paths in vpm out…
Browse files Browse the repository at this point in the history
…put (vlang#19847)
  • Loading branch information
ttytm authored Nov 12, 2023
1 parent 7c5d439 commit fbb43f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
30 changes: 24 additions & 6 deletions cmd/tools/vpm/common.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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, '~')
}
}
8 changes: 4 additions & 4 deletions cmd/tools/vpm/install.v
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ')
Expand Down Expand Up @@ -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
Expand All @@ -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' {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/tools/vpm/update.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++
Expand Down

0 comments on commit fbb43f5

Please sign in to comment.