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