From 02db94c4bc7e15ea8f35a2936900741f1a417912 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 21 Oct 2020 12:54:03 +0300 Subject: [PATCH] v test-fmt: reuse v.util.should_recompile_tool/2, extracted from v.util.launch_tool/3 --- cmd/tools/vtest-fmt.v | 18 ++++++----- vlib/v/util/util.v | 70 +++++++++++++++++++++++-------------------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/cmd/tools/vtest-fmt.v b/cmd/tools/vtest-fmt.v index 3b86438b70..ec96abbf9d 100644 --- a/cmd/tools/vtest-fmt.v +++ b/cmd/tools/vtest-fmt.v @@ -2,6 +2,7 @@ module main import os import testing +import v.util // os.v - // embeded comments, mib := [1/* CTL_KERN */, 14/* KERN_PROC */, 12/* KERN_PROC_PATHNAME */, -1] => comment the rest of the line const ( @@ -31,7 +32,7 @@ fn main() { fn v_test_formatting(vargs string) { all_v_files := v_files() - prepare_vfmt_when_needed() + prepare_vfmt_when_needed() testing.eheader('Run "v fmt" over all .v files') mut vfmt_test_session := testing.new_test_session('$vargs fmt -worker') vfmt_test_session.files << all_v_files @@ -60,11 +61,14 @@ fn prepare_vfmt_when_needed() { vexe := os.getenv('VEXE') vroot := os.dir(vexe) vfmtv := os.join_path(vroot, 'cmd', 'tools', 'vfmt.v') - if os.file_last_mod_unix(vexe) <= os.file_last_mod_unix(vfmtv) { - recompile_result := os.system('$vexe ' + os.join_path(vroot, 'cmd', 'tools', 'vfmt.v')) - if recompile_result != 0 { - eprintln('could not recompile cmd/tools/vfmt.v') - exit(2) - } + if util.should_recompile_tool(vexe, vfmtv) { + recompile_file(vexe, vfmtv) + } +} + +fn recompile_file(vexe string, file string) { + if recompile_result != 0 { + eprintln('could not recompile $file') + exit(2) } } diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index 2c7c9caba7..78e2906adb 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -126,8 +126,9 @@ pub fn launch_tool(is_verbose bool, tool_name string, args []string) { vroot := os.dir(vexe) set_vroot_folder(vroot) tool_args := args_quote_paths_with_spaces(args) - tool_exe := path_of_executable(os.real_path('$vroot/cmd/tools/$tool_name')) - tool_source := os.real_path('$vroot/cmd/tools/${tool_name}.v') + tool_basename := os.real_path(os.join_path(vroot, 'cmd', 'tools', tool_name)) + tool_exe := path_of_executable(tool_basename) + tool_source := tool_basename + '.v' tool_command := '"$tool_exe" $tool_args' if is_verbose { println('launch_tool vexe : $vroot') @@ -135,6 +136,40 @@ pub fn launch_tool(is_verbose bool, tool_name string, args []string) { println('launch_tool tool_args : $tool_args') println('launch_tool tool_command: $tool_command') } + should_compile := should_recompile_tool(vexe, tool_source) + if is_verbose { + println('launch_tool should_compile: $should_compile') + } + if should_compile { + emodules := external_module_dependencies_for_tool[tool_name] + for emodule in emodules { + check_module_is_installed(emodule, is_verbose) or { + panic(err) + } + } + mut compilation_command := '"$vexe" ' + compilation_command += '"$tool_source"' + if is_verbose { + println('Compiling $tool_name with: "$compilation_command"') + } + tool_compilation := os.exec(compilation_command) or { + panic(err) + } + if tool_compilation.exit_code != 0 { + eprintln('cannot compile `$tool_source`: \n$tool_compilation.output') + exit(1) + } + } + if is_verbose { + println('launch_tool running tool command: $tool_command ...') + } + exit(os.system(tool_command)) +} + +pub fn should_recompile_tool(vexe string, tool_source string) bool { + sfolder := os.base(tool_source) + tool_name := os.file_name(tool_source).replace('.v', '') + tool_exe := os.join_path(sfolder, path_of_executable(tool_name)) // TODO Caching should be done on the `vlib/v` level. mut should_compile := false if !os.exists(tool_exe) { @@ -158,37 +193,6 @@ pub fn launch_tool(is_verbose bool, tool_name string, args []string) { should_compile = true } } - if is_verbose { - println('launch_tool should_compile: $should_compile') - } - if should_compile { - emodules := external_module_dependencies_for_tool[tool_name] - for emodule in emodules { - check_module_is_installed(emodule, is_verbose) or { - panic(err) - } - } - mut compilation_command := '"$vexe" ' - compilation_command += '"$tool_source"' - if is_verbose { - println('Compiling $tool_name with: "$compilation_command"') - } - tool_compilation := os.exec(compilation_command) or { - panic(err) - } - if tool_compilation.exit_code != 0 { - mut err := 'Permission denied' - if !tool_compilation.output.contains(err) { - err = '\n$tool_compilation.output' - } - eprintln('cannot compile `$tool_source`: $err') - exit(1) - } - } - if is_verbose { - println('launch_tool running tool command: $tool_command ...') - } - exit(os.system(tool_command)) } pub fn quote_path_with_spaces(s string) string {