tools: support --exclude/-e and --relative-paths/-r options in missdoc.v

pull/13405/head
Delyan Angelov 2022-02-08 11:10:19 +02:00
parent f0806822dd
commit ff02f94fd6
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 51 additions and 32 deletions

View File

@ -8,6 +8,7 @@ const (
tool_name = os.file_name(os.executable()) tool_name = os.file_name(os.executable())
tool_version = '0.0.3' tool_version = '0.0.3'
tool_description = 'Prints all V functions in .v files under PATH/, that do not yet have documentation comments.' tool_description = 'Prints all V functions in .v files under PATH/, that do not yet have documentation comments.'
work_dir_prefix = normalise_path(os.real_path(os.wd_at_startup) + '/')
) )
struct UndocumentedFN { struct UndocumentedFN {
@ -23,44 +24,34 @@ struct Options {
private bool private bool
js bool js bool
no_line_numbers bool no_line_numbers bool
exclude []string
relative_paths bool
} }
fn collect(path string, mut l []string, f fn (string, mut []string)) { fn (opt Options) report_undocumented_functions_in_path(path string) {
if !os.is_dir(path) {
return
}
mut files := os.ls(path) or { return }
for file in files {
p := path + os.path_separator + file
if os.is_dir(p) && !os.is_link(p) {
collect(p, mut l, f)
} else if os.exists(p) {
f(p, mut l)
}
}
return
}
fn report_undocumented_functions_in_path(opt Options, path string) {
mut files := []string{} mut files := []string{}
collect_fn := fn (path string, mut l []string) { collect(path, mut files, fn (npath string, mut accumulated_paths []string) {
if os.file_ext(path) == '.v' { if !npath.ends_with('.v') {
l << os.real_path(path) return
} }
if npath.ends_with('_test.v') {
return
} }
collect(path, mut files, collect_fn) accumulated_paths << npath
})
for file in files { for file in files {
if file.ends_with('_test.v') {
continue
}
if !opt.js && file.ends_with('.js.v') { if !opt.js && file.ends_with('.js.v') {
continue continue
} }
report_undocumented_functions_in_file(opt, file) if opt.exclude.len > 0 && opt.exclude.any(file.contains(it)) {
continue
}
opt.report_undocumented_functions_in_file(file)
} }
} }
fn report_undocumented_functions_in_file(opt Options, file string) { fn (opt &Options) report_undocumented_functions_in_file(nfile string) {
file := os.real_path(nfile)
contents := os.read_file(file) or { panic(err) } contents := os.read_file(file) or { panic(err) }
lines := contents.split('\n') lines := contents.split('\n')
mut info := []UndocumentedFN{} mut info := []UndocumentedFN{}
@ -104,17 +95,42 @@ fn report_undocumented_functions_in_file(opt Options, file string) {
} else { } else {
'' ''
} }
ofile := if opt.relative_paths {
nfile.replace(work_dir_prefix, '')
} else {
os.real_path(nfile)
}
if opt.deprecated { if opt.deprecated {
println('$file:$line_numbers$undocumented_fn.signature $tags_str') println('$ofile:$line_numbers$undocumented_fn.signature $tags_str')
} else { } else {
if 'deprecated' !in undocumented_fn.tags { if 'deprecated' !in undocumented_fn.tags {
println('$file:$line_numbers$undocumented_fn.signature $tags_str') println('$ofile:$line_numbers$undocumented_fn.signature $tags_str')
} }
} }
} }
} }
} }
fn normalise_path(path string) string {
return path.replace('\\', '/')
}
fn collect(path string, mut l []string, f fn (string, mut []string)) {
if !os.is_dir(path) {
return
}
mut files := os.ls(path) or { return }
for file in files {
p := normalise_path(os.join_path_single(path, file))
if os.is_dir(p) && !os.is_link(p) {
collect(p, mut l, f)
} else if os.exists(p) {
f(p, mut l)
}
}
return
}
fn collect_tags(line string) []string { fn collect_tags(line string) []string {
mut cleaned := line.all_before('/') mut cleaned := line.all_before('/')
cleaned = cleaned.replace_each(['[', '', ']', '', ' ', '']) cleaned = cleaned.replace_each(['[', '', ']', '', ' ', ''])
@ -137,18 +153,21 @@ fn main() {
deprecated: fp.bool('deprecated', `d`, false, 'Include deprecated functions in output.') deprecated: fp.bool('deprecated', `d`, false, 'Include deprecated functions in output.')
private: fp.bool('private', `p`, false, 'Include private functions in output.') private: fp.bool('private', `p`, false, 'Include private functions in output.')
js: fp.bool('js', 0, false, 'Include JavaScript functions in output.') js: fp.bool('js', 0, false, 'Include JavaScript functions in output.')
no_line_numbers: fp.bool('no-line-numbers', 0, false, 'Exclude line numbers in output.') no_line_numbers: fp.bool('no-line-numbers', `n`, false, 'Exclude line numbers in output.')
collect_tags: fp.bool('tags', `t`, false, 'Also print function tags if any is found.') collect_tags: fp.bool('tags', `t`, false, 'Also print function tags if any is found.')
exclude: fp.string_multi('exclude', `e`, '')
relative_paths: fp.bool('relative-paths', `r`, false, 'Use relative paths in output.')
} }
dump(opt)
if opt.show_help { if opt.show_help {
println(fp.usage()) println(fp.usage())
exit(0) exit(0)
} }
for path in os.args[1..] { for path in os.args[1..] {
if os.is_file(path) { if os.is_file(path) {
report_undocumented_functions_in_file(opt, path) opt.report_undocumented_functions_in_file(path)
} else { } else {
report_undocumented_functions_in_path(opt, path) opt.report_undocumented_functions_in_path(path)
} }
} }
} }