| 1 | module common |
| 2 | |
| 3 | import os |
| 4 | import log |
| 5 | import term |
| 6 | import time |
| 7 | |
| 8 | // exec is a helper function, to execute commands and exit early, if they fail. |
| 9 | pub fn exec(command string) { |
| 10 | cmd := resolve_v_command(command) |
| 11 | log.info('cmd: ${cmd}') |
| 12 | result := os.system(cmd) |
| 13 | if result != 0 { |
| 14 | exit(result) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | // resolve_v_command ensures that commands starting with `v ` use the V from @VEXEROOT, |
| 19 | // not a potentially different V found via PATH. |
| 20 | fn resolve_v_command(command string) string { |
| 21 | if command.starts_with('v ') { |
| 22 | return os.quoted_path(os.join_path_single(@VEXEROOT, 'v')) + command[1..] |
| 23 | } |
| 24 | return command |
| 25 | } |
| 26 | |
| 27 | // unset is a helper function to unset a specific env variable. |
| 28 | pub fn unset(evar string) { |
| 29 | log.info('unsetting env variable: ${evar}') |
| 30 | os.unsetenv(evar) |
| 31 | } |
| 32 | |
| 33 | // file_size_greater_than asserts that the given file exists, and is at least min_fsize bytes long. |
| 34 | pub fn file_size_greater_than(fpath string, min_fsize u64) { |
| 35 | log.info('path should exist `${fpath}` ...') |
| 36 | if !os.exists(fpath) { |
| 37 | exit(1) |
| 38 | } |
| 39 | log.info('path exists, and should be a file: `${fpath}` ...') |
| 40 | if !os.is_file(fpath) { |
| 41 | exit(2) |
| 42 | } |
| 43 | real_size := os.file_size(fpath) |
| 44 | log.info('actual file size of `${fpath}` is ${real_size}, wanted: ${min_fsize}, diff: ${real_size - min_fsize}.') |
| 45 | if real_size < min_fsize { |
| 46 | exit(3) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | const self_command = os.quoted_path(os.join_path_single(@VEXEROOT, 'v')) + ' ' + |
| 51 | os.real_path(os.executable()).replace_once(os.real_path(@VEXEROOT), '').trim_left('/\\') + |
| 52 | '.vsh' |
| 53 | |
| 54 | pub const is_github_job = os.getenv('GITHUB_JOB') != '' |
| 55 | |
| 56 | pub type Fn = fn () |
| 57 | |
| 58 | pub struct Task { |
| 59 | pub mut: |
| 60 | f Fn = unsafe { nil } |
| 61 | label string |
| 62 | } |
| 63 | |
| 64 | pub fn (t Task) run(tname string) { |
| 65 | cmd := '${self_command} ${tname}' |
| 66 | log.info('Start ${term.colorize(term.yellow, t.label)}, cmd: `${cmd}`') |
| 67 | start := time.now() |
| 68 | t.f() |
| 69 | dt := time.now() - start |
| 70 | log.info('Finished ${term.colorize(term.yellow, t.label)} in ${dt.milliseconds()} ms, cmd: `${cmd}`') |
| 71 | println('') |
| 72 | } |
| 73 | |
| 74 | pub fn run(all_tasks map[string]Task) { |
| 75 | unbuffer_stdout() |
| 76 | log.use_stdout() |
| 77 | if os.args.len < 2 { |
| 78 | println('Usage: v run macos_ci.vsh <task_name>') |
| 79 | println('Available tasks are: ${all_tasks.keys()}') |
| 80 | exit(0) |
| 81 | } |
| 82 | task_name := os.args[1] |
| 83 | if task_name == 'all' { |
| 84 | log.info(term.colorize(term.green, 'Run everything...')) |
| 85 | mut failed_tasks := []string{} |
| 86 | for tname, t in all_tasks { |
| 87 | cmd := '${self_command} ${tname}' |
| 88 | log.info('Start ${term.colorize(term.yellow, t.label)}, cmd: `${cmd}`') |
| 89 | start := time.now() |
| 90 | result := os.system(cmd) |
| 91 | dt := time.now() - start |
| 92 | if result != 0 { |
| 93 | log.error('FAILED ${term.colorize(term.red, t.label)} in ${dt.milliseconds()} ms, cmd: `${cmd}`') |
| 94 | failed_tasks << tname |
| 95 | } else { |
| 96 | log.info('Finished ${term.colorize(term.yellow, t.label)} in ${dt.milliseconds()} ms, cmd: `${cmd}`') |
| 97 | } |
| 98 | } |
| 99 | if failed_tasks.len > 0 { |
| 100 | log.error('${failed_tasks.len} task(s) failed: ${failed_tasks}') |
| 101 | exit(1) |
| 102 | } |
| 103 | exit(0) |
| 104 | } |
| 105 | t := all_tasks[task_name] or { |
| 106 | eprintln('Unknown task with name: `${task_name}`') |
| 107 | exit(1) |
| 108 | } |
| 109 | t.run(task_name) |
| 110 | } |
| 111 | |