From 9bb3a5b3a3478196f295c752940e4b0c58cc08a3 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 6 Apr 2021 11:43:46 +0300 Subject: [PATCH] tools: update oldv to be faster for the most common cases --- cmd/tools/modules/scripting/scripting.v | 60 ++++++++++++++++++++----- cmd/tools/modules/vgit/vgit.v | 17 +++++-- cmd/tools/oldv.v | 10 +++-- 3 files changed, 68 insertions(+), 19 deletions(-) diff --git a/cmd/tools/modules/scripting/scripting.v b/cmd/tools/modules/scripting/scripting.v index 323aca47b0..55ddc22c14 100644 --- a/cmd/tools/modules/scripting/scripting.v +++ b/cmd/tools/modules/scripting/scripting.v @@ -2,6 +2,7 @@ module scripting import os import term +import time const ( term_colors = term.can_show_color_on_stdout() @@ -17,16 +18,47 @@ pub fn set_verbose(on bool) { } } -pub fn cprintln(message string) { - mut omessage := message - omessage = if scripting.term_colors { term.green(omessage) } else { omessage } - println(omessage) +pub fn cprint(omessage string) { + mut message := omessage + if scripting.term_colors { + message = term.cyan(message) + } + print(message) +} + +pub fn cprint_strong(omessage string) { + mut message := omessage + if scripting.term_colors { + message = term.bright_green(message) + } + print(message) +} + +pub fn cprintln(omessage string) { + cprint(omessage) + println('') +} + +pub fn cprintln_strong(omessage string) { + cprint_strong(omessage) + println('') } pub fn verbose_trace(label string, message string) { if os.getenv('VERBOSE').len > 0 { - slabel := 'scripting.$label' - cprintln('# ${slabel:-25s} : $message') + slabel := '$time.now().format_ss_milli() scripting.$label' + cprintln('# ${slabel:-40s} : $message') + } +} + +pub fn verbose_trace_strong(label string, omessage string) { + if os.getenv('VERBOSE').len > 0 { + slabel := '$time.now().format_ss_milli() scripting.$label' + mut message := omessage + if scripting.term_colors { + message = term.bright_green(message) + } + cprintln('# ${slabel:-40s} : $message') } } @@ -36,7 +68,11 @@ pub fn verbose_trace_exec_result(x os.Result) { cprintln('# ----------------------------------- #') mut lnum := 1 lines := x.output.split_into_lines() - for line in lines { + for oline in lines { + mut line := oline + if scripting.term_colors { + line = term.bright_green(line) + } cprintln('# ${lnum:3d}: $line') lnum++ } @@ -45,12 +81,12 @@ pub fn verbose_trace_exec_result(x os.Result) { } pub fn chdir(path string) { - verbose_trace(@FN, 'cd $path') + verbose_trace_strong(@FN, 'cd $path') os.chdir(path) } pub fn rmrf(path string) { - verbose_trace(@FN, 'rm -rf $path') + verbose_trace_strong(@FN, 'rm -rf $path') if os.exists(path) { if os.is_dir(path) { os.rmdir_all(path) or { panic(err) } @@ -62,7 +98,7 @@ pub fn rmrf(path string) { // execute a command, and return a result, or an error, if it failed in any way. pub fn exec(cmd string) ?os.Result { - verbose_trace(@FN, cmd) + verbose_trace_strong(@FN, cmd) x := os.execute(cmd) if x.exit_code != 0 { verbose_trace(@FN, '## failed.') @@ -74,7 +110,7 @@ pub fn exec(cmd string) ?os.Result { // run a command, tracing its results, and returning ONLY its output pub fn run(cmd string) string { - verbose_trace(@FN, cmd) + verbose_trace_strong(@FN, cmd) x := os.execute(cmd) if x.exit_code < 0 { verbose_trace(@FN, '## failed.') @@ -88,7 +124,7 @@ pub fn run(cmd string) string { } pub fn exit_0_status(cmd string) bool { - verbose_trace(@FN, cmd) + verbose_trace_strong(@FN, cmd) x := os.execute(cmd) if x.exit_code < 0 { verbose_trace(@FN, '## failed.') diff --git a/cmd/tools/modules/vgit/vgit.v b/cmd/tools/modules/vgit/vgit.v index bf4eb1f74c..59714b0844 100644 --- a/cmd/tools/modules/vgit/vgit.v +++ b/cmd/tools/modules/vgit/vgit.v @@ -44,6 +44,12 @@ pub fn normalized_workpath_for_commit(workdir string, commit string) string { return os.real_path(workdir + os.path_separator + nc) } +fn get_current_folder_commit_hash() string { + vline := scripting.run('git rev-list -n1 --timestamp HEAD') + _, v_commithash := line_to_timestamp_and_commit(vline) + return v_commithash +} + pub fn prepare_vc_source(vcdir string, cdir string, commit string) (string, string) { scripting.chdir(cdir) // Building a historic v with the latest vc is not always possible ... @@ -54,7 +60,7 @@ pub fn prepare_vc_source(vcdir string, cdir string, commit string) (string, stri scripting.verbose_trace(@FN, 'v_timestamp: $v_timestamp | v_commithash: $v_commithash') check_v_commit_timestamp_before_self_rebuilding(v_timestamp) scripting.chdir(vcdir) - scripting.run('git checkout master') + scripting.run('git checkout --quiet master') // mut vccommit := '' vcbefore_subject_match := scripting.run('git rev-list HEAD -n1 --timestamp --grep=${v_commithash[0..7]} ') @@ -67,7 +73,7 @@ pub fn prepare_vc_source(vcdir string, cdir string, commit string) (string, stri _, vccommit = line_to_timestamp_and_commit(vcbefore) } scripting.verbose_trace(@FN, 'vccommit: $vccommit') - scripting.run('git checkout "$vccommit" ') + scripting.run('git checkout --quiet "$vccommit" ') scripting.run('wc *.c') scripting.chdir(cdir) return v_commithash, vccommit @@ -119,7 +125,12 @@ pub fn (mut vgit_context VGitContext) compile_oldv_if_needed() { clone_or_pull(vgit_context.v_repo_url, vgit_context.path_v) clone_or_pull(vgit_context.vc_repo_url, vgit_context.path_vc) scripting.chdir(vgit_context.path_v) - scripting.run('git checkout $vgit_context.commit_v') + scripting.run('git checkout --quiet $vgit_context.commit_v') + if os.is_dir(vgit_context.path_v) && os.exists(vgit_context.vexepath) { + // already compiled, so no need to compile v again + vgit_context.commit_v__hash = get_current_folder_commit_hash() + return + } v_commithash, vccommit_before := prepare_vc_source(vgit_context.path_vc, vgit_context.path_v, 'HEAD') vgit_context.commit_v__hash = v_commithash diff --git a/cmd/tools/oldv.v b/cmd/tools/oldv.v index d9f99733bb..fc3c195c7c 100644 --- a/cmd/tools/oldv.v +++ b/cmd/tools/oldv.v @@ -97,12 +97,14 @@ fn main() { } context.compile_oldv_if_needed() scripting.chdir(context.path_v) - scripting.cprintln('# v commit hash: $context.commit_v_hash') - scripting.cprintln('# checkout folder: $context.path_v') + shorter_hash := context.commit_v_hash[0..10] + scripting.cprintln('# v commit hash: $shorter_hash | folder: $context.path_v') if context.cmd_to_run.len > 0 { + scripting.cprintln_strong('# command: ${context.cmd_to_run:-34s}') cmdres := os.execute_or_panic(context.cmd_to_run) - scripting.cprintln('# command: ${context.cmd_to_run:-34s} exit code: ${cmdres.exit_code:-4d} result:') - println(cmdres.output) + scripting.cprintln_strong('# exit code: ${cmdres.exit_code:-4d}') + scripting.cprint_strong('# result: ') + print(cmdres.output) exit(cmdres.exit_code) } }