v / cmd / tools
Raw file | 75 loc (71 sloc) | 1.39 KB | Latest commit hash 017ace6ea
1import os
2import log
3
4const should_clean = os.args.contains('-c')
5
6fn main() {
7 mut files := []string{}
8 args := os.args#[2..].filter(it != '-c')
9 for a in args {
10 if os.is_file(a) {
11 files << a
12 continue
13 }
14 if os.is_dir(a) {
15 files << os.walk_ext(a, '.v')
16 continue
17 }
18 }
19 files.sort()
20 if files.len == 0 {
21 println('0 .v files found.\n')
22 println('Usage:')
23 println(' v should-compile-all [-c] examples/ some/deep/file.v another/')
24 println('... will try to compile all .v files found in the given folders and files, one by one.')
25 println('If every single one of them compiles, the command will exit with an error code of 0.')
26 println('If *any* of them *fail* to compile, the command will exit with an error code of 1.')
27 println('')
28 println(' -c will remove all the compiled executables at the end.')
29 println('')
30 println('Note: this command is intended to be used in CI pipelines for v modules, like this:')
31 println(' cd module/ ; v should-compile-all examples/ \n')
32 exit(1)
33 }
34 mut executables := []string{}
35 mut failed_commands := []string{}
36 for idx, example in files {
37 folder_of_example := os.dir(example)
38 if os.is_file(os.join_path_single(folder_of_example, '.skip_should_compile_all')) {
39 log.info('>>> skipping file: ${example}, because a `.skip_should_compile_all` file is present next to it.')
40 continue
41 }
42 cmd := '${os.quoted_path(@VEXE)} ${os.quoted_path(example)}'
43 log.info('> compiling ${idx + 1:4}/${files.len:-4}: ${cmd}')
44 if 0 != os.system(cmd) {
45 failed_commands << cmd
46 } else {
47 executables << executable_name(example)
48 }
49 }
50 if should_clean {
51 log.info('Removing ${executables.len} successfully build executables...')
52 for f in executables {
53 os.rm(f) or { log.error('>> could not remove ${f}, err: ${err}') }
54 }
55 }
56 if failed_commands.len > 0 {
57 for idx, fcmd in failed_commands {
58 log.error('>>> FAILED command ${idx + 1:4}/${failed_commands.len:-4}: ${fcmd}')
59 }
60 log.info('Summary: ${failed_commands.len:4}/${files.len:-4} file(s) failed to compile.')
61 exit(1)
62 }
63 log.info('Summary: all ${files.len} file(s) compiled successfully.')
64}
65
66const exe_extension = if os.user_os() == 'windows' {
67 '.exe'
68} else {
69 ''
70}
71
72fn executable_name(source string) string {
73 basepath := source.replace(os.file_ext(source), '')
74 return basepath + exe_extension
75}