1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tools, examples: add --only-watch=*.v option to v watch (#18974)

This commit is contained in:
Delyan Angelov
2023-07-27 09:50:26 +03:00
committed by GitHub
parent 7d6fd9dade
commit d25e213aa8
5 changed files with 172 additions and 22 deletions

View File

@ -95,6 +95,7 @@ mut:
ignore_exts []string // extensions of files that will be ignored, even if they change (useful for sqlite.db files for example)
cmd_before_run string // a command to run before each re-run
cmd_after_run string // a command to run after each re-run
only_watch []string // If not empty, *all* files that trigger updates, should match *at least one* of these s.match_glob() patterns. This is also triggered for vweb apps, to monitor for just *.v,*.js,*.css,*.html in vweb projects.
}
[if debug_vwatch ?]
@ -106,6 +107,33 @@ fn (context &Context) str() string {
return 'Context{ pid: ${context.pid}, is_worker: ${context.is_worker}, check_period_ms: ${context.check_period_ms}, vexe: ${context.vexe}, opts: ${context.opts}, is_exiting: ${context.is_exiting}, vfiles: ${context.vfiles}'
}
fn (mut context Context) is_ext_ignored(pf string, pf_ext string) bool {
for ipattern in context.ignore_exts {
if pf_ext.match_glob(ipattern) {
return true
}
}
if pf_ext in ['', '.so', '.a'] {
// on unix, the executables saved by compilers, usually do not have extensions at all, and shared libs are .so
return true
}
if pf_ext in ['.exe', '.dll', '.def'] {
// on windows, files with these extensions will be generated by the compiler
return true
}
// ignore common backup files saved by editors like emacs/jed/vim:
if pf_ext == '.bak' {
return true
}
if pf.starts_with('.#') {
return true
}
if pf.ends_with('~') {
return true
}
return false
}
fn (mut context Context) get_stats_for_affected_vfiles() []VFileStat {
if context.affected_paths.len == 0 {
mut apaths := map[string]bool{}
@ -121,34 +149,63 @@ fn (mut context Context) get_stats_for_affected_vfiles() []VFileStat {
vfiles := os.execute(cmd)
if vfiles.exit_code == 0 {
paths_trimmed := vfiles.output.trim_space()
reported_used_files := paths_trimmed.split('\n')
reported_used_files := paths_trimmed.split_any('\n')
$if trace_reported_used_files ? {
context.elog('reported_used_files: ${reported_used_files}')
}
paths << reported_used_files
}
mut is_vweb_found := false
for vf in paths {
apaths[os.real_path(os.dir(vf))] = true
if vf.contains('vweb.v') {
is_vweb_found = true
}
}
if is_vweb_found {
if !os.args.any(it.starts_with('--only-watch')) {
// vweb is often used with SQLite .db or .sqlite3 files right next to the executable/source,
// that are updated by the vweb app, causing a restart of the app, which in turn causes the
// browser to reload the current page, that probably triggered the update in the first place.
// Note that the problem is not specific to SQLite, any database that stores its files in the
// current (project) folder, will also cause this.
println('`v watch` detected that you are compiling a vweb project.')
println(' Because of that, the `--only-watch=*.v,*.html,*.css,*.js` flag was also implied.')
println(' In result, `v watch` will ignore changes to other files.')
println(' Add your own --only-watch filter, if you wish to override that choice.')
println('')
context.only_watch = '*.v,*.html,*.css,*.js'.split_any(',')
}
}
context.affected_paths = apaths.keys()
// context.elog('vfiles paths to be scanned: $context.affected_paths')
}
// scan all files in the found folders
// scan all files in the found folders:
mut newstats := []VFileStat{}
for path in context.affected_paths {
mut files := os.ls(path) or { []string{} }
for pf in files {
next_file: for pf in files {
pf_path := os.join_path_single(path, pf)
if context.only_watch.len > 0 {
// in the whitelist mode, first only allow files, which match at least one of the patterns in context.only_watch:
mut matched_pattern_idx := -1
for ow_pattern_idx, ow_pattern in context.only_watch {
if pf_path.match_glob(ow_pattern) {
matched_pattern_idx = ow_pattern_idx
context.elog('> ${@METHOD} matched --only-watch pattern: ${ow_pattern}, for file: ${pf_path}')
break
}
}
if matched_pattern_idx == -1 {
context.elog('> ${@METHOD} --only-watch ignored file: ${pf_path}')
continue
}
}
// by default allow everything, except very specific extensions (backup files, executables etc):
pf_ext := os.file_ext(pf).to_lower()
if pf_ext in ['', '.bak', '.exe', '.dll', '.so', '.def'] {
continue
}
if pf_ext in context.ignore_exts {
continue
}
if pf.starts_with('.#') {
continue
}
if pf.ends_with('~') {
if context.is_ext_ignored(pf, pf_ext) {
context.elog('> ${@METHOD} ignored extension: ${pf_ext}, for file: ${pf_path}')
continue
}
f := os.join_path(path, pf)
@ -323,19 +380,21 @@ fn main() {
// Options after `run` should be ignored, since they are intended for the user program, not for the watcher.
// For example, `v watch run x.v -a -b -k', should pass all of -a -b -k to the compiled and run program.
only_watch_options, has_run := all_before('run', all_args_after_watch_cmd)
mut fp := flag.new_flag_parser(only_watch_options)
fp.application('v watch')
fp.version('0.0.2')
fp.description('Collect all .v files needed for a compilation, then re-run the compilation when any of the source changes.')
fp.arguments_description('[--silent] [--clear] [--ignore .db] [--add /path/to/a/file.v] [run] program.v')
fp.allow_unknown_args()
fp.limit_free_args_to_at_least(1)!
context.is_worker = fp.bool('vwatchworker', 0, false, 'Internal flag. Used to distinguish vwatch manager and worker processes.')
context.silent = fp.bool('silent', `s`, false, 'Be more silent; do not print the watch timestamp before each re-run.')
context.clear_terminal = fp.bool('clear', `c`, false, 'Clears the terminal before each re-run.')
context.keep_running = fp.bool('keep', `k`, false, 'Keep the program running. Restart it automatically, if it exits by itself. Useful for gg/ui apps.')
context.add_files = fp.string('add', `a`, '', 'Add more files to be watched. Useful with `v watch -add=/tmp/feature.v run cmd/v /tmp/feature.v`, if you change *both* the compiler, and the feature.v file.').split(',')
context.ignore_exts = fp.string('ignore', `i`, '', 'Ignore files having these extensions. Useful with `v watch -ignore=.db run server.v`, if your server writes to an sqlite.db file in the same folder.').split(',')
context.add_files = fp.string('add', `a`, '', 'Add more files to be watched. Useful with `v watch --add=/tmp/feature.v run cmd/v /tmp/feature.v`, if you change *both* the compiler, and the feature.v file.').split_any(',')
context.ignore_exts = fp.string('ignore', `i`, '', 'Ignore files having these extensions. Useful with `v watch --ignore=.db run server.v`, if your server writes to an sqlite.db file in the same folder.').split_any(',')
context.only_watch = fp.string('only-watch', `o`, '', 'Watch only files matching these globe patterns. Example for a markdown renderer project: `v watch --only-watch=*.v,*.md run .`').split_any(',')
show_help := fp.bool('help', `h`, false, 'Show this help screen.')
context.cmd_before_run = fp.string('before', 0, '', 'A command to execute *before* each re-run.')
context.cmd_after_run = fp.string('after', 0, '', 'A command to execute *after* each re-run.')
@ -351,6 +410,7 @@ fn main() {
context.opts << all_args_before_watch_cmd
context.opts << remaining_options
if has_run {
context.opts << 'run'
context.opts << all_after('run', all_args_after_watch_cmd)
}
context.elog('>>> context.pid: ${context.pid}')
@ -360,6 +420,7 @@ fn main() {
context.elog('>>> context.clear_terminal: ${context.clear_terminal}')
context.elog('>>> context.add_files: ${context.add_files}')
context.elog('>>> context.ignore_exts: ${context.ignore_exts}')
context.elog('>>> context.only_watch: ${context.only_watch}')
if context.is_worker {
context.worker_main()
} else {
@ -407,7 +468,7 @@ fn all_before(needle string, all []string) ([]string, bool) {
if needle_pos == -1 {
return all, false
}
return all#[..needle_pos + 1], true
return all#[..needle_pos], true
}
fn all_after(needle string, all []string) []string {