aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/cmd/tools/modules/scripting/scripting.v
blob: edf6a0ade35567713ab5eb2b326ead90520871d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
module scripting

import os
import term
import time

const (
	term_colors = term.can_show_color_on_stdout()
)

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 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('')
}

pub fn verbose_trace(label string, message string) {
	if os.getenv('VERBOSE').len > 0 {
		slabel := '$time.now().format_ss_milli() $label'
		cprintln('# ${slabel:-43s} : $message')
	}
}

pub fn verbose_trace_strong(label string, omessage string) {
	if os.getenv('VERBOSE').len > 0 {
		slabel := '$time.now().format_ss_milli() $label'
		mut message := omessage
		if scripting.term_colors {
			message = term.bright_green(message)
		}
		cprintln('# ${slabel:-43s} : $message')
	}
}

pub fn verbose_trace_exec_result(x os.Result) {
	if os.getenv('VERBOSE').len > 0 {
		cprintln('#   cmd.exit_code : ${x.exit_code.str():-4s}  cmd.output:')
		mut lnum := 1
		lines := x.output.split_into_lines()
		for oline in lines {
			mut line := oline
			if scripting.term_colors {
				line = term.bright_green(line)
			}
			cprintln('# ${lnum:3d}: $line')
			lnum++
		}
		cprintln('# ----------------------------------------------------------------------')
	}
}

fn modfn(mname string, fname string) string {
	return '${mname}.$fname'
}

pub fn chdir(path string) {
	verbose_trace_strong(modfn(@MOD, @FN), 'cd $path')
	os.chdir(path)
}

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
	}
}

pub fn rmrf(path string) {
	verbose_trace_strong(modfn(@MOD, @FN), 'rm -rf $path')
	if os.exists(path) {
		if os.is_dir(path) {
			os.rmdir_all(path) or { panic(err) }
		} else {
			os.rm(path) or { panic(err) }
		}
	}
}

// execute a command, and return a result, or an error, if it failed in any way.
pub fn exec(cmd string) ?os.Result {
	verbose_trace_strong(modfn(@MOD, @FN), cmd)
	x := os.execute(cmd)
	if x.exit_code != 0 {
		verbose_trace(modfn(@MOD, @FN), '## failed.')
		return error(x.output)
	}
	verbose_trace_exec_result(x)
	return x
}

// run a command, tracing its results, and returning ONLY its output
pub fn run(cmd string) string {
	verbose_trace_strong(modfn(@MOD, @FN), cmd)
	x := os.execute(cmd)
	if x.exit_code < 0 {
		verbose_trace(modfn(@MOD, @FN), '## failed.')
		return ''
	}
	verbose_trace_exec_result(x)
	if x.exit_code == 0 {
		return x.output.trim_right('\r\n')
	}
	return ''
}

pub fn exit_0_status(cmd string) bool {
	verbose_trace_strong(modfn(@MOD, @FN), cmd)
	x := os.execute(cmd)
	if x.exit_code < 0 {
		verbose_trace(modfn(@MOD, @FN), '## failed.')
		return false
	}
	verbose_trace_exec_result(x)
	if x.exit_code == 0 {
		return true
	}
	return false
}

pub fn tool_must_exist(toolcmd string) {
	verbose_trace(modfn(@MOD, @FN), toolcmd)
	if exit_0_status('type $toolcmd') {
		return
	}
	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)
	}
}

pub fn show_sizes_of_files(files []string) {
	for f in files {
		size := os.file_size(f)
		println('$size $f') // println('${size:10d} $f')
	}
}