Skip to content

Commit cc80ee0

Browse files
committed
tools: improve the default performance/behaviour of oldv
1 parent ff98373 commit cc80ee0

File tree

3 files changed

+115
-38
lines changed

3 files changed

+115
-38
lines changed

cmd/tools/modules/scripting/scripting.v

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,25 @@ pub fn cprintln_strong(omessage string) {
4646

4747
pub fn verbose_trace(label string, message string) {
4848
if os.getenv('VERBOSE').len > 0 {
49-
slabel := '$time.now().format_ss_milli() scripting.$label'
50-
cprintln('# ${slabel:-40s} : $message')
49+
slabel := '$time.now().format_ss_milli() $label'
50+
cprintln('# ${slabel:-43s} : $message')
5151
}
5252
}
5353

5454
pub fn verbose_trace_strong(label string, omessage string) {
5555
if os.getenv('VERBOSE').len > 0 {
56-
slabel := '$time.now().format_ss_milli() scripting.$label'
56+
slabel := '$time.now().format_ss_milli() $label'
5757
mut message := omessage
5858
if scripting.term_colors {
5959
message = term.bright_green(message)
6060
}
61-
cprintln('# ${slabel:-40s} : $message')
61+
cprintln('# ${slabel:-43s} : $message')
6262
}
6363
}
6464

6565
pub fn verbose_trace_exec_result(x os.Result) {
6666
if os.getenv('VERBOSE').len > 0 {
6767
cprintln('# cmd.exit_code : ${x.exit_code.str():-4s} cmd.output:')
68-
cprintln('# ----------------------------------- #')
6968
mut lnum := 1
7069
lines := x.output.split_into_lines()
7170
for oline in lines {
@@ -76,17 +75,37 @@ pub fn verbose_trace_exec_result(x os.Result) {
7675
cprintln('# ${lnum:3d}: $line')
7776
lnum++
7877
}
79-
cprintln('# ----------------------------------- #')
78+
cprintln('# ----------------------------------------------------------------------')
8079
}
8180
}
8281

82+
fn modfn(mname string, fname string) string {
83+
return '${mname}.$fname'
84+
}
85+
8386
pub fn chdir(path string) {
84-
verbose_trace_strong(@FN, 'cd $path')
87+
verbose_trace_strong(modfn(@MOD, @FN), 'cd $path')
8588
os.chdir(path)
8689
}
8790

91+
pub fn mkdir(path string) ? {
92+
verbose_trace_strong(modfn(@MOD, @FN), 'mkdir $path')
93+
os.mkdir(path) or {
94+
verbose_trace(modfn(@MOD, @FN), '## failed.')
95+
return err
96+
}
97+
}
98+
99+
pub fn mkdir_all(path string) ? {
100+
verbose_trace_strong(modfn(@MOD, @FN), 'mkdir -p $path')
101+
os.mkdir_all(path) or {
102+
verbose_trace(modfn(@MOD, @FN), '## failed.')
103+
return err
104+
}
105+
}
106+
88107
pub fn rmrf(path string) {
89-
verbose_trace_strong(@FN, 'rm -rf $path')
108+
verbose_trace_strong(modfn(@MOD, @FN), 'rm -rf $path')
90109
if os.exists(path) {
91110
if os.is_dir(path) {
92111
os.rmdir_all(path) or { panic(err) }
@@ -98,10 +117,10 @@ pub fn rmrf(path string) {
98117

99118
// execute a command, and return a result, or an error, if it failed in any way.
100119
pub fn exec(cmd string) ?os.Result {
101-
verbose_trace_strong(@FN, cmd)
120+
verbose_trace_strong(modfn(@MOD, @FN), cmd)
102121
x := os.execute(cmd)
103122
if x.exit_code != 0 {
104-
verbose_trace(@FN, '## failed.')
123+
verbose_trace(modfn(@MOD, @FN), '## failed.')
105124
return error(x.output)
106125
}
107126
verbose_trace_exec_result(x)
@@ -110,10 +129,10 @@ pub fn exec(cmd string) ?os.Result {
110129

111130
// run a command, tracing its results, and returning ONLY its output
112131
pub fn run(cmd string) string {
113-
verbose_trace_strong(@FN, cmd)
132+
verbose_trace_strong(modfn(@MOD, @FN), cmd)
114133
x := os.execute(cmd)
115134
if x.exit_code < 0 {
116-
verbose_trace(@FN, '## failed.')
135+
verbose_trace(modfn(@MOD, @FN), '## failed.')
117136
return ''
118137
}
119138
verbose_trace_exec_result(x)
@@ -124,10 +143,10 @@ pub fn run(cmd string) string {
124143
}
125144

126145
pub fn exit_0_status(cmd string) bool {
127-
verbose_trace_strong(@FN, cmd)
146+
verbose_trace_strong(modfn(@MOD, @FN), cmd)
128147
x := os.execute(cmd)
129148
if x.exit_code < 0 {
130-
verbose_trace(@FN, '## failed.')
149+
verbose_trace(modfn(@MOD, @FN), '## failed.')
131150
return false
132151
}
133152
verbose_trace_exec_result(x)
@@ -138,7 +157,7 @@ pub fn exit_0_status(cmd string) bool {
138157
}
139158

140159
pub fn tool_must_exist(toolcmd string) {
141-
verbose_trace(@FN, toolcmd)
160+
verbose_trace(modfn(@MOD, @FN), toolcmd)
142161
if exit_0_status('type $toolcmd') {
143162
return
144163
}

cmd/tools/modules/vgit/vgit.v

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import os
44
import flag
55
import scripting
66

7-
const (
8-
remote_v_repo_url = 'https://github.com/vlang/v'
9-
remote_vc_repo_url = 'https://github.com/vlang/vc'
10-
)
11-
127
pub fn check_v_commit_timestamp_before_self_rebuilding(v_timestamp int) {
138
if v_timestamp >= 1561805697 {
149
return
@@ -164,9 +159,9 @@ pub mut:
164159

165160
pub fn add_common_tool_options(mut context VGitOptions, mut fp flag.FlagParser) []string {
166161
tdir := os.temp_dir()
167-
context.workdir = os.real_path(fp.string('workdir', `w`, tdir, 'A writable base folder. Default: $tdir'))
168-
context.v_repo_url = fp.string('vrepo', 0, vgit.remote_v_repo_url, 'The url of the V repository. You can clone it locally too. See also --vcrepo below.')
169-
context.vc_repo_url = fp.string('vcrepo', 0, vgit.remote_vc_repo_url, 'The url of the vc repository. You can clone it
162+
context.workdir = os.real_path(fp.string('workdir', `w`, context.workdir, 'A writable base folder. Default: $tdir'))
163+
context.v_repo_url = fp.string('vrepo', 0, context.v_repo_url, 'The url of the V repository. You can clone it locally too. See also --vcrepo below.')
164+
context.vc_repo_url = fp.string('vcrepo', 0, context.vc_repo_url, 'The url of the vc repository. You can clone it
170165
${flag.space}beforehand, and then just give the local folder
171166
${flag.space}path here. That will eliminate the network ops
172167
${flag.space}done by this tool, which is useful, if you want

cmd/tools/oldv.v

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,20 @@ const (
2929

3030
struct Context {
3131
mut:
32-
vgo vgit.VGitOptions
33-
commit_v string = 'master'
34-
// the commit from which you want to produce a working v compiler (this may be a commit-ish too)
35-
commit_vc string = 'master'
36-
// this will be derived from commit_v
32+
vgo vgit.VGitOptions
33+
vgcontext vgit.VGitContext
34+
commit_v string = 'master' // the commit from which you want to produce a working v compiler (this may be a commit-ish too)
3735
commit_v_hash string // this will be filled from the commit-ish commit_v using rev-list. It IS a commit hash.
3836
path_v string // the full path to the v folder inside workdir.
3937
path_vc string // the full path to the vc folder inside workdir.
4038
cmd_to_run string // the command that you want to run *in* the oldv repo
41-
cc string = 'cc'
42-
// the C compiler to use for bootstrapping.
43-
cleanup bool // should the tool run a cleanup first
39+
cc string = 'cc' // the C compiler to use for bootstrapping.
40+
cleanup bool // should the tool run a cleanup first
41+
use_cache bool // use local cached copies for --vrepo and --vcrepo in
4442
}
4543

4644
fn (mut c Context) compile_oldv_if_needed() {
47-
mut vgit_context := vgit.VGitContext{
45+
c.vgcontext = vgit.VGitContext{
4846
workdir: c.vgo.workdir
4947
v_repo_url: c.vgo.v_repo_url
5048
vc_repo_url: c.vgo.vc_repo_url
@@ -53,28 +51,91 @@ fn (mut c Context) compile_oldv_if_needed() {
5351
path_v: c.path_v
5452
path_vc: c.path_vc
5553
}
56-
vgit_context.compile_oldv_if_needed()
57-
c.commit_v_hash = vgit_context.commit_v__hash
58-
if !os.exists(vgit_context.vexepath) && c.cmd_to_run.len > 0 {
54+
c.vgcontext.compile_oldv_if_needed()
55+
c.commit_v_hash = c.vgcontext.commit_v__hash
56+
if !os.exists(c.vgcontext.vexepath) && c.cmd_to_run.len > 0 {
5957
// NB: 125 is a special code, that git bisect understands as 'skip this commit'.
6058
// it is used to inform git bisect that the current commit leads to a build failure.
6159
exit(125)
6260
}
6361
}
6462

63+
const cache_oldv_folder = os.join_path(os.cache_dir(), 'oldv')
64+
65+
const cache_oldv_folder_v = os.join_path(cache_oldv_folder, 'v')
66+
67+
const cache_oldv_folder_vc = os.join_path(cache_oldv_folder, 'vc')
68+
69+
fn sync_cache() {
70+
scripting.verbose_trace(@FN, 'start')
71+
if !os.exists(cache_oldv_folder) {
72+
scripting.verbose_trace(@FN, 'creating $cache_oldv_folder')
73+
scripting.mkdir_all(cache_oldv_folder) or {
74+
scripting.verbose_trace(@FN, '## failed.')
75+
exit(1)
76+
}
77+
}
78+
scripting.chdir(cache_oldv_folder)
79+
for reponame in ['v', 'vc'] {
80+
repofolder := os.join_path(cache_oldv_folder, reponame)
81+
if !os.exists(repofolder) {
82+
scripting.verbose_trace(@FN, 'cloning to $repofolder')
83+
scripting.exec('git clone --quiet https://github.com/vlang/$reponame $repofolder') or {
84+
scripting.verbose_trace(@FN, '## error during clone: $err')
85+
exit(1)
86+
}
87+
}
88+
scripting.chdir(repofolder)
89+
scripting.exec('git pull --quiet') or {
90+
scripting.verbose_trace(@FN, 'pulling to $repofolder')
91+
scripting.verbose_trace(@FN, '## error during pull: $err')
92+
exit(1)
93+
}
94+
}
95+
scripting.verbose_trace(@FN, 'done')
96+
}
97+
6598
fn main() {
6699
scripting.used_tools_must_exist(['git', 'cc'])
100+
//
101+
// Resetting VEXE here allows for `v run cmd/tools/oldv.v'.
102+
// the parent V would have set VEXE, which later will
103+
// affect the V's run from the tool itself.
104+
os.setenv('VEXE', '', true)
105+
//
67106
mut context := Context{}
107+
context.vgo.workdir = cache_oldv_folder
68108
mut fp := flag.new_flag_parser(os.args)
69109
fp.application(os.file_name(os.executable()))
70110
fp.version(tool_version)
71111
fp.description(tool_description)
72112
fp.arguments_description('VCOMMIT')
73113
fp.skip_executable()
74-
fp.limit_free_args(1, 1)
75-
context.cleanup = fp.bool('clean', 0, true, 'Clean before running (slower).')
114+
context.use_cache = fp.bool('cache', `u`, true, 'Use a cache of local repositories for --vrepo and --vcrepo in \$HOME/.cache/oldv/')
115+
if context.use_cache {
116+
context.vgo.v_repo_url = cache_oldv_folder_v
117+
context.vgo.vc_repo_url = cache_oldv_folder_vc
118+
} else {
119+
context.vgo.v_repo_url = 'https://github.com/vlang/v'
120+
context.vgo.vc_repo_url = 'https://github.com/vlang/vc'
121+
}
122+
should_sync := fp.bool('cache-sync', `s`, false, 'Update the local cache')
123+
if !should_sync {
124+
fp.limit_free_args(1, 1)
125+
}
126+
////
127+
context.cleanup = fp.bool('clean', 0, false, 'Clean before running (slower).')
76128
context.cmd_to_run = fp.string('command', `c`, '', 'Command to run in the old V repo.\n')
77129
commits := vgit.add_common_tool_options(mut context.vgo, mut fp)
130+
if should_sync {
131+
sync_cache()
132+
exit(0)
133+
}
134+
if context.use_cache {
135+
if !os.is_dir(cache_oldv_folder_v) || !os.is_dir(cache_oldv_folder_vc) {
136+
sync_cache()
137+
}
138+
}
78139
if commits.len > 0 {
79140
context.commit_v = commits[0]
80141
} else {
@@ -102,7 +163,9 @@ fn main() {
102163
if context.cmd_to_run.len > 0 {
103164
scripting.cprintln_strong('# command: ${context.cmd_to_run:-34s}')
104165
cmdres := os.execute_or_panic(context.cmd_to_run)
105-
scripting.cprintln_strong('# exit code: ${cmdres.exit_code:-4d}')
166+
if cmdres.exit_code != 0 {
167+
scripting.cprintln_strong('# exit code: ${cmdres.exit_code:-4d}')
168+
}
106169
scripting.cprint_strong('# result: ')
107170
print(cmdres.output)
108171
exit(cmdres.exit_code)

0 commit comments

Comments
 (0)