2019-12-08 20:21:17 +03:00
|
|
|
module scripting
|
|
|
|
|
|
|
|
import os
|
2020-08-04 11:22:37 +03:00
|
|
|
import term
|
2021-04-06 11:43:46 +03:00
|
|
|
import time
|
2020-08-04 11:22:37 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
term_colors = term.can_show_color_on_stdout()
|
|
|
|
)
|
2019-12-08 20:21:17 +03:00
|
|
|
|
2020-01-08 23:45:47 +03:00
|
|
|
pub fn set_verbose(on bool) {
|
|
|
|
// setting a global here would be the obvious solution,
|
|
|
|
// but V does not have globals normally.
|
|
|
|
if on {
|
|
|
|
os.setenv('VERBOSE', '1', true)
|
2021-01-26 17:43:10 +03:00
|
|
|
} else {
|
2020-01-08 23:45:47 +03:00
|
|
|
os.unsetenv('VERBOSE')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:43:46 +03:00
|
|
|
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('')
|
2020-08-04 11:22:37 +03:00
|
|
|
}
|
|
|
|
|
2020-01-08 23:45:47 +03:00
|
|
|
pub fn verbose_trace(label string, message string) {
|
2019-12-08 20:21:17 +03:00
|
|
|
if os.getenv('VERBOSE').len > 0 {
|
2021-04-07 12:22:51 +03:00
|
|
|
slabel := '$time.now().format_ss_milli() $label'
|
|
|
|
cprintln('# ${slabel:-43s} : $message')
|
2021-04-06 11:43:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verbose_trace_strong(label string, omessage string) {
|
|
|
|
if os.getenv('VERBOSE').len > 0 {
|
2021-04-07 12:22:51 +03:00
|
|
|
slabel := '$time.now().format_ss_milli() $label'
|
2021-04-06 11:43:46 +03:00
|
|
|
mut message := omessage
|
|
|
|
if scripting.term_colors {
|
|
|
|
message = term.bright_green(message)
|
|
|
|
}
|
2021-04-07 12:22:51 +03:00
|
|
|
cprintln('# ${slabel:-43s} : $message')
|
2019-12-08 20:21:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verbose_trace_exec_result(x os.Result) {
|
|
|
|
if os.getenv('VERBOSE').len > 0 {
|
2020-08-04 11:22:37 +03:00
|
|
|
cprintln('# cmd.exit_code : ${x.exit_code.str():-4s} cmd.output:')
|
2019-12-08 20:21:17 +03:00
|
|
|
mut lnum := 1
|
|
|
|
lines := x.output.split_into_lines()
|
2021-04-06 11:43:46 +03:00
|
|
|
for oline in lines {
|
|
|
|
mut line := oline
|
|
|
|
if scripting.term_colors {
|
|
|
|
line = term.bright_green(line)
|
|
|
|
}
|
2020-08-04 11:22:37 +03:00
|
|
|
cprintln('# ${lnum:3d}: $line')
|
2019-12-08 20:21:17 +03:00
|
|
|
lnum++
|
|
|
|
}
|
2021-04-07 12:22:51 +03:00
|
|
|
cprintln('# ----------------------------------------------------------------------')
|
2020-01-08 23:45:47 +03:00
|
|
|
}
|
2019-12-08 20:21:17 +03:00
|
|
|
}
|
|
|
|
|
2021-04-07 12:22:51 +03:00
|
|
|
fn modfn(mname string, fname string) string {
|
|
|
|
return '${mname}.$fname'
|
|
|
|
}
|
|
|
|
|
2019-12-08 20:21:17 +03:00
|
|
|
pub fn chdir(path string) {
|
2021-04-07 12:22:51 +03:00
|
|
|
verbose_trace_strong(modfn(@MOD, @FN), 'cd $path')
|
2021-08-28 12:44:03 +03:00
|
|
|
os.chdir(path) or {
|
|
|
|
verbose_trace(modfn(@MOD, @FN), '## failed.')
|
|
|
|
return
|
|
|
|
}
|
2019-12-08 20:21:17 +03:00
|
|
|
}
|
|
|
|
|
2021-04-07 12:22:51 +03:00
|
|
|
pub fn mkdir(path string) ? {
|
|
|
|
verbose_trace_strong(modfn(@MOD, @FN), 'mkdir $path')
|
|
|
|
os.mkdir(path) or {
|
|
|
|
verbose_trace(modfn(@MOD, @FN), '## failed.')
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mkdir_all(path string) ? {
|
|
|
|
verbose_trace_strong(modfn(@MOD, @FN), 'mkdir -p $path')
|
|
|
|
os.mkdir_all(path) or {
|
|
|
|
verbose_trace(modfn(@MOD, @FN), '## failed.')
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-27 18:56:32 +03:00
|
|
|
pub fn rmrf(path string) {
|
2021-04-07 12:22:51 +03:00
|
|
|
verbose_trace_strong(modfn(@MOD, @FN), 'rm -rf $path')
|
2020-01-27 18:56:32 +03:00
|
|
|
if os.exists(path) {
|
|
|
|
if os.is_dir(path) {
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rmdir_all(path) or { panic(err) }
|
2021-01-26 17:43:10 +03:00
|
|
|
} else {
|
2021-03-01 02:18:14 +03:00
|
|
|
os.rm(path) or { panic(err) }
|
2020-01-27 18:56:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:52:13 +03:00
|
|
|
// execute a command, and return a result, or an error, if it failed in any way.
|
2020-10-09 11:06:00 +03:00
|
|
|
pub fn exec(cmd string) ?os.Result {
|
2021-04-07 12:22:51 +03:00
|
|
|
verbose_trace_strong(modfn(@MOD, @FN), cmd)
|
2021-03-08 21:52:13 +03:00
|
|
|
x := os.execute(cmd)
|
|
|
|
if x.exit_code != 0 {
|
2021-04-07 12:22:51 +03:00
|
|
|
verbose_trace(modfn(@MOD, @FN), '## failed.')
|
2021-03-08 21:52:13 +03:00
|
|
|
return error(x.output)
|
2020-10-09 11:06:00 +03:00
|
|
|
}
|
|
|
|
verbose_trace_exec_result(x)
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:52:13 +03:00
|
|
|
// run a command, tracing its results, and returning ONLY its output
|
2019-12-08 20:21:17 +03:00
|
|
|
pub fn run(cmd string) string {
|
2021-04-07 12:22:51 +03:00
|
|
|
verbose_trace_strong(modfn(@MOD, @FN), cmd)
|
2021-03-08 21:52:13 +03:00
|
|
|
x := os.execute(cmd)
|
|
|
|
if x.exit_code < 0 {
|
2021-04-07 12:22:51 +03:00
|
|
|
verbose_trace(modfn(@MOD, @FN), '## failed.')
|
2020-01-08 23:45:47 +03:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
verbose_trace_exec_result(x)
|
|
|
|
if x.exit_code == 0 {
|
2020-06-15 15:16:37 +03:00
|
|
|
return x.output.trim_right('\r\n')
|
2020-01-08 23:45:47 +03:00
|
|
|
}
|
2019-12-08 20:21:17 +03:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2020-01-08 23:45:47 +03:00
|
|
|
pub fn exit_0_status(cmd string) bool {
|
2021-04-07 12:22:51 +03:00
|
|
|
verbose_trace_strong(modfn(@MOD, @FN), cmd)
|
2021-03-08 21:52:13 +03:00
|
|
|
x := os.execute(cmd)
|
|
|
|
if x.exit_code < 0 {
|
2021-04-07 12:22:51 +03:00
|
|
|
verbose_trace(modfn(@MOD, @FN), '## failed.')
|
2020-01-08 23:45:47 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
verbose_trace_exec_result(x)
|
|
|
|
if x.exit_code == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2019-12-08 20:21:17 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
pub fn tool_must_exist(toolcmd string) {
|
2021-04-07 12:22:51 +03:00
|
|
|
verbose_trace(modfn(@MOD, @FN), toolcmd)
|
2020-01-08 23:45:47 +03:00
|
|
|
if exit_0_status('type $toolcmd') {
|
|
|
|
return
|
|
|
|
}
|
2019-12-08 20:21:17 +03:00
|
|
|
eprintln('Missing tool: $toolcmd')
|
|
|
|
eprintln('Please try again after you install it.')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn used_tools_must_exist(tools []string) {
|
|
|
|
for t in tools {
|
|
|
|
tool_must_exist(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 23:45:47 +03:00
|
|
|
pub fn show_sizes_of_files(files []string) {
|
|
|
|
for f in files {
|
|
|
|
size := os.file_size(f)
|
2021-03-26 09:51:55 +03:00
|
|
|
println('$size $f') // println('${size:10d} $f')
|
2020-01-08 23:45:47 +03:00
|
|
|
}
|
2019-12-08 20:21:17 +03:00
|
|
|
}
|