2019-12-08 20:21:17 +03:00
|
|
|
module scripting
|
|
|
|
|
|
|
|
import os
|
2020-08-04 11:22:37 +03:00
|
|
|
import term
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
os.unsetenv('VERBOSE')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:22:37 +03:00
|
|
|
pub fn cprintln(message string) {
|
|
|
|
mut omessage := message
|
|
|
|
omessage = if term_colors { term.green(omessage) } else { omessage }
|
|
|
|
println(omessage)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
slabel := 'scripting.${label}'
|
2020-08-04 11:22:37 +03:00
|
|
|
cprintln('# ${slabel:-25s} : $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:')
|
|
|
|
cprintln('# ----------------------------------- #')
|
2019-12-08 20:21:17 +03:00
|
|
|
mut lnum := 1
|
|
|
|
lines := x.output.split_into_lines()
|
|
|
|
for line in lines {
|
2020-08-04 11:22:37 +03:00
|
|
|
cprintln('# ${lnum:3d}: $line')
|
2019-12-08 20:21:17 +03:00
|
|
|
lnum++
|
|
|
|
}
|
2020-08-04 11:22:37 +03:00
|
|
|
cprintln('# ----------------------------------- #')
|
2020-01-08 23:45:47 +03:00
|
|
|
}
|
2019-12-08 20:21:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chdir(path string) {
|
|
|
|
verbose_trace(@FN, 'cd $path')
|
2020-01-08 23:45:47 +03:00
|
|
|
os.chdir(path)
|
2019-12-08 20:21:17 +03:00
|
|
|
}
|
|
|
|
|
2020-01-27 18:56:32 +03:00
|
|
|
pub fn rmrf(path string) {
|
|
|
|
verbose_trace(@FN, 'rm -rf $path')
|
|
|
|
if os.exists(path) {
|
|
|
|
if os.is_dir(path) {
|
2020-03-03 17:02:50 +03:00
|
|
|
os.rmdir_all(path)
|
2020-01-27 18:56:32 +03:00
|
|
|
}else{
|
|
|
|
os.rm(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 11:06:00 +03:00
|
|
|
pub fn exec(cmd string) ?os.Result {
|
|
|
|
verbose_trace(@FN, cmd)
|
|
|
|
x := os.exec(cmd) or {
|
|
|
|
verbose_trace(@FN, '## failed.')
|
|
|
|
return error(err)
|
|
|
|
}
|
|
|
|
verbose_trace_exec_result(x)
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2019-12-08 20:21:17 +03:00
|
|
|
pub fn run(cmd string) string {
|
|
|
|
verbose_trace(@FN, cmd)
|
2020-01-08 23:45:47 +03:00
|
|
|
x := os.exec(cmd) or {
|
|
|
|
verbose_trace(@FN, '## failed.')
|
|
|
|
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 {
|
2019-12-08 20:21:17 +03:00
|
|
|
verbose_trace(@FN, cmd)
|
2020-01-08 23:45:47 +03: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 20:21:17 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-08 23:45:47 +03:00
|
|
|
pub fn tool_must_exist (toolcmd string) {
|
2019-12-08 20:21:17 +03:00
|
|
|
verbose_trace(@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)
|
|
|
|
println('${size:10d} $f')
|
|
|
|
}
|
2019-12-08 20:21:17 +03:00
|
|
|
}
|