tools: support `v fmt -w -backup file.v`

pull/12102/head
Delyan Angelov 2021-10-07 13:59:13 +03:00
parent 09cc0c7247
commit fbe54e49f5
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 9 additions and 0 deletions

View File

@ -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 {

View File

@ -18,6 +18,9 @@ Options:
-l List files whose formatting differs from vfmt.
-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.