From da0b89cc57f660f413f7b7453c2029bd8fb1b401 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 10 Jan 2022 15:45:28 +0200 Subject: [PATCH] tools: improve support for renamed v executables (`vlang self` now works too) --- cmd/tools/vself.v | 30 +++++++++++++++++------------- vlib/v/util/util.v | 14 +++++++++----- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/cmd/tools/vself.v b/cmd/tools/vself.v index 5c8d1d2a60..ea5f27cf54 100644 --- a/cmd/tools/vself.v +++ b/cmd/tools/vself.v @@ -8,9 +8,13 @@ import v.util.recompilation const is_debug = os.args.contains('-debug') fn main() { + // support a renamed `v` executable too: vexe := pref.vexe_path() vroot := os.dir(vexe) - recompilation.must_be_enabled(vroot, 'Please install V from source, to use `v self` .') + vexe_name := os.file_name(vexe) + short_v_name := vexe_name.all_before('.') + // + recompilation.must_be_enabled(vroot, 'Please install V from source, to use `$vexe_name self` .') os.chdir(vroot) ? os.setenv('VCOLORS', 'always', true) args := os.args[1..].filter(it != 'self') @@ -26,8 +30,8 @@ fn main() { // The user just wants an independent copy of v, and so we are done. return } - backup_old_version_and_rename_newer() or { panic(err.msg) } - println('V built successfully!') + backup_old_version_and_rename_newer(short_v_name) or { panic(err.msg) } + println('V built successfully as executable "$vexe_name".') } fn compile(vroot string, cmd string) { @@ -41,7 +45,7 @@ fn compile(vroot string, cmd string) { } } -fn list_folder(bmessage string, message string) { +fn list_folder(short_v_name string, bmessage string, message string) { if !is_debug { return } @@ -49,37 +53,37 @@ fn list_folder(bmessage string, message string) { println(bmessage) } if os.user_os() == 'windows' { - os.system('dir v*.exe') + os.system('dir $short_v_name*.exe') } else { - os.system('ls -lartd v*') + os.system('ls -lartd $short_v_name*') } println(message) } -fn backup_old_version_and_rename_newer() ?bool { +fn backup_old_version_and_rename_newer(short_v_name string) ?bool { mut errors := []string{} - short_v_file := if os.user_os() == 'windows' { 'v.exe' } else { 'v' } + short_v_file := if os.user_os() == 'windows' { '${short_v_name}.exe' } else { '$short_v_name' } short_v2_file := if os.user_os() == 'windows' { 'v2.exe' } else { 'v2' } short_bak_file := if os.user_os() == 'windows' { 'v_old.exe' } else { 'v_old' } v_file := os.real_path(short_v_file) v2_file := os.real_path(short_v2_file) bak_file := os.real_path(short_bak_file) - list_folder('before:', 'removing $bak_file ...') + list_folder(short_v_name, 'before:', 'removing $bak_file ...') if os.exists(bak_file) { os.rm(bak_file) or { errors << 'failed removing $bak_file: $err.msg' } } - list_folder('', 'moving $v_file to $bak_file ...') + list_folder(short_v_name, '', 'moving $v_file to $bak_file ...') os.mv(v_file, bak_file) or { errors << err.msg } - list_folder('', 'removing $v_file ...') + list_folder(short_v_name, '', 'removing $v_file ...') os.rm(v_file) or {} - list_folder('', 'moving $v2_file to $v_file ...') + list_folder(short_v_name, '', 'moving $v2_file to $v_file ...') os.mv_by_cp(v2_file, v_file) or { panic(err.msg) } - list_folder('after:', '') + list_folder(short_v_name, 'after:', '') if errors.len > 0 { eprintln('backup errors:\n >> ' + errors.join('\n >> ')) diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index 23b0fbcf88..997ad8ee6c 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -43,10 +43,14 @@ pub fn tabs(n int) string { // pub fn set_vroot_folder(vroot_path string) { // Preparation for the compiler module: - // VEXE env variable is needed so that compiler.vexe_path() - // can return it later to whoever needs it: - vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' } - os.setenv('VEXE', os.real_path(os.join_path_single(vroot_path, vname)), true) + // VEXE env variable is needed so that compiler.vexe_path() can return it + // later to whoever needs it. NB: guessing is a heuristic, so only try to + // guess the V executable name, if VEXE has not been set already. + vexe := os.getenv('VEXE') + if vexe == '' { + vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' } + os.setenv('VEXE', os.real_path(os.join_path_single(vroot_path, vname)), true) + } os.setenv('VCHILD', 'true', true) } @@ -133,7 +137,7 @@ pub fn launch_tool(is_verbose bool, tool_name string, args []string) { tool_source = tool_basename + '.v' } if is_verbose { - println('launch_tool vexe : $vroot') + println('launch_tool vexe : $vexe') println('launch_tool vroot : $vroot') println('launch_tool tool_source : $tool_source') println('launch_tool tool_exe : $tool_exe')