v.util: extend launch_tool with auto recompilation of a folder with several .v files too

pull/7930/head
Delyan Angelov 2021-01-06 23:48:27 +02:00
parent ffd753abdc
commit 9593ad20f9
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 30 additions and 8 deletions

View File

@ -129,7 +129,7 @@ pub fn launch_tool(is_verbose bool, tool_name string, args []string) {
mut tool_source := '' mut tool_source := ''
if os.is_dir(tool_basename) { if os.is_dir(tool_basename) {
tool_exe = path_of_executable(os.join_path(tool_basename, tool_name)) tool_exe = path_of_executable(os.join_path(tool_basename, tool_name))
tool_source = os.join_path(tool_basename, tool_name + '.v') tool_source = tool_basename
} else { } else {
tool_exe = path_of_executable(tool_basename) tool_exe = path_of_executable(tool_basename)
tool_source = tool_basename + '.v' tool_source = tool_basename + '.v'
@ -139,9 +139,10 @@ pub fn launch_tool(is_verbose bool, tool_name string, args []string) {
println('launch_tool vexe : $vroot') println('launch_tool vexe : $vroot')
println('launch_tool vroot : $vroot') println('launch_tool vroot : $vroot')
println('launch_tool tool_args : $tool_args') println('launch_tool tool_args : $tool_args')
println('launch_tool tool_source : $tool_source')
println('launch_tool tool_command: $tool_command') println('launch_tool tool_command: $tool_command')
} }
should_compile := should_recompile_tool(vexe, tool_source) should_compile := should_recompile_tool(vexe, tool_source, tool_name, tool_exe)
if is_verbose { if is_verbose {
println('launch_tool should_compile: $should_compile') println('launch_tool should_compile: $should_compile')
} }
@ -167,13 +168,26 @@ pub fn launch_tool(is_verbose bool, tool_name string, args []string) {
exit(os.system(tool_command)) exit(os.system(tool_command))
} }
// NB: should_recompile_tool/2 compares unix timestamps that have 1 second resolution // NB: should_recompile_tool/4 compares unix timestamps that have 1 second resolution
// That means that a tool can get recompiled twice, if called in short succession. // That means that a tool can get recompiled twice, if called in short succession.
// TODO: use a nanosecond mtime timestamp, if available. // TODO: use a nanosecond mtime timestamp, if available.
pub fn should_recompile_tool(vexe string, tool_source string) bool { pub fn should_recompile_tool(vexe string, tool_source string, tool_name string, tool_exe string) bool {
sfolder := os.dir(tool_source) if os.is_dir(tool_source) {
tool_name := os.base(tool_source).replace('.v', '') source_files := os.walk_ext(tool_source, '.v')
tool_exe := os.join_path(sfolder, path_of_executable(tool_name)) mut newest_sfile := ''
mut newest_sfile_mtime := 0
for sfile in source_files {
mtime := os.file_last_mod_unix(sfile)
if mtime > newest_sfile_mtime {
newest_sfile_mtime = mtime
newest_sfile = sfile
}
}
single_file_recompile := should_recompile_tool(vexe, newest_sfile, tool_name,
tool_exe)
// eprintln('>>> should_recompile_tool: tool_source: $tool_source | $single_file_recompile | $newest_sfile')
return single_file_recompile
}
// TODO Caching should be done on the `vlib/v` level. // TODO Caching should be done on the `vlib/v` level.
mut should_compile := false mut should_compile := false
if !os.exists(tool_exe) { if !os.exists(tool_exe) {
@ -212,6 +226,13 @@ pub fn should_recompile_tool(vexe string, tool_source string) bool {
return should_compile return should_compile
} }
fn tool_source2name_and_exe(tool_source string) (string, string) {
sfolder := os.dir(tool_source)
tool_name := os.base(tool_source).replace('.v', '')
tool_exe := os.join_path(sfolder, path_of_executable(tool_name))
return tool_name, tool_exe
}
pub fn quote_path(s string) string { pub fn quote_path(s string) string {
mut qs := s mut qs := s
if qs.contains('&') { if qs.contains('&') {
@ -424,7 +445,8 @@ pub fn prepare_tool_when_needed(source_name string) {
vexe := os.getenv('VEXE') vexe := os.getenv('VEXE')
vroot := os.dir(vexe) vroot := os.dir(vexe)
stool := os.join_path(vroot, 'cmd', 'tools', source_name) stool := os.join_path(vroot, 'cmd', 'tools', source_name)
if should_recompile_tool(vexe, stool) { tool_name, tool_exe := tool_source2name_and_exe(stool)
if should_recompile_tool(vexe, stool, tool_name, tool_exe) {
time.sleep_ms(1001) // TODO: remove this when we can get mtime with a better resolution time.sleep_ms(1001) // TODO: remove this when we can get mtime with a better resolution
recompile_file(vexe, stool) recompile_file(vexe, stool)
} }