2019-12-08 18:21:17 +01:00
|
|
|
module scripting
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
os.unsetenv('VERBOSE')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verbose_trace(label string, message string) {
|
2019-12-08 18:21:17 +01:00
|
|
|
if os.getenv('VERBOSE').len > 0 {
|
|
|
|
slabel := 'scripting.${label}'
|
2020-01-08 21:45:47 +01:00
|
|
|
println('# ${slabel:-25s} : $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-01-08 21:45:47 +01:00
|
|
|
println('# cmd.exit_code : ${x.exit_code.str():-4s} cmd.output:')
|
2019-12-08 18:21:17 +01:00
|
|
|
println('# ----------------------------------- #')
|
|
|
|
mut lnum := 1
|
|
|
|
lines := x.output.split_into_lines()
|
|
|
|
for line in lines {
|
|
|
|
println('# ${lnum:3d}: $line')
|
|
|
|
lnum++
|
|
|
|
}
|
|
|
|
println('# ----------------------------------- #')
|
2020-01-08 21:45:47 +01:00
|
|
|
}
|
2019-12-08 18:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chdir(path string) {
|
|
|
|
verbose_trace(@FN, 'cd $path')
|
2020-01-08 21:45:47 +01:00
|
|
|
os.chdir(path)
|
2019-12-08 18:21:17 +01:00
|
|
|
}
|
|
|
|
|
2020-01-27 16:56:32 +01:00
|
|
|
pub fn rmrf(path string) {
|
|
|
|
verbose_trace(@FN, 'rm -rf $path')
|
|
|
|
if os.exists(path) {
|
|
|
|
if os.is_dir(path) {
|
|
|
|
os.rmdir_recursive(path)
|
|
|
|
}else{
|
|
|
|
os.rm(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 18:21:17 +01:00
|
|
|
pub fn run(cmd string) string {
|
|
|
|
verbose_trace(@FN, cmd)
|
2020-01-08 21:45:47 +01:00
|
|
|
x := os.exec(cmd) or {
|
|
|
|
verbose_trace(@FN, '## failed.')
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
verbose_trace_exec_result(x)
|
|
|
|
if x.exit_code == 0 {
|
|
|
|
return x.output
|
|
|
|
}
|
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 {
|
2019-12-08 18:21:17 +01:00
|
|
|
verbose_trace(@FN, cmd)
|
2020-01-08 21:45:47 +01:00
|
|
|
x := os.exec(cmd) or {
|
|
|
|
verbose_trace(@FN, '## failed.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
verbose_trace_exec_result(x)
|
|
|
|
if x.exit_code == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2019-12-08 18:21:17 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-08 21:45:47 +01:00
|
|
|
pub fn tool_must_exist (toolcmd string) {
|
2019-12-08 18:21:17 +01:00
|
|
|
verbose_trace(@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)
|
|
|
|
println('${size:10d} $f')
|
|
|
|
}
|
2019-12-08 18:21:17 +01:00
|
|
|
}
|