tools: support `v fmt -w -backup file.v`
parent
09cc0c7247
commit
fbe54e49f5
|
@ -26,6 +26,7 @@ struct FormatOptions {
|
|||
is_noerror bool
|
||||
is_verify bool // exit(1) if the file is not vfmt'ed
|
||||
is_worker bool // true *only* in the worker processes. NB: workers can crash.
|
||||
is_backup bool // make a `file.v.bak` copy *before* overwriting a `file.v` in place with `-w`
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -53,6 +54,7 @@ fn main() {
|
|||
is_debug: '-debug' in args
|
||||
is_noerror: '-noerror' in args
|
||||
is_verify: '-verify' in args
|
||||
is_backup: '-backup' in args
|
||||
}
|
||||
if term_colors {
|
||||
os.setenv('VCOLORS', 'always', true)
|
||||
|
@ -249,6 +251,10 @@ fn (foptions &FormatOptions) post_process_file(file string, formatted_file_path
|
|||
}
|
||||
if foptions.is_w {
|
||||
if is_formatted_different {
|
||||
if foptions.is_backup {
|
||||
file_bak := '${file}.bak'
|
||||
os.cp(file, file_bak) or {}
|
||||
}
|
||||
os.mv_by_cp(formatted_file_path, file) or { panic(err) }
|
||||
eprintln('Reformatted file: $file')
|
||||
} else {
|
||||
|
|
|
@ -19,6 +19,9 @@ Options:
|
|||
|
||||
-w Write result to (source) file(s) instead of to stdout.
|
||||
|
||||
-backup In combination with `-w`, copy the original `file.v` to a `file.v.bak` backup,
|
||||
before overwriting the original source file.
|
||||
|
||||
-debug Print the kinds of encountered AST statements/expressions on stderr.
|
||||
|
||||
-verify Make sure the provided file is already formatted. Useful for checking code contributions
|
||||
|
|
Loading…
Reference in New Issue