2020-12-05 11:10:54 +01:00
|
|
|
// Copyright (c) 2020 Lars Pontoppidan. All rights reserved.
|
2020-12-11 17:35:25 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-12-05 11:10:54 +01:00
|
|
|
import os
|
2020-12-11 17:35:25 +01:00
|
|
|
import flag
|
|
|
|
|
|
|
|
const (
|
|
|
|
tool_name = os.file_name(os.executable())
|
2022-02-06 14:44:26 +01:00
|
|
|
tool_version = '0.0.3'
|
2020-12-11 17:35:25 +01:00
|
|
|
tool_description = 'Prints all V functions in .v files under PATH/, that do not yet have documentation comments.'
|
2022-02-08 10:10:19 +01:00
|
|
|
work_dir_prefix = normalise_path(os.real_path(os.wd_at_startup) + '/')
|
2020-12-11 17:35:25 +01:00
|
|
|
)
|
2020-12-05 11:10:54 +01:00
|
|
|
|
|
|
|
struct UndocumentedFN {
|
|
|
|
line int
|
|
|
|
signature string
|
2020-12-11 17:35:25 +01:00
|
|
|
tags []string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Options {
|
2022-02-07 12:18:10 +01:00
|
|
|
show_help bool
|
|
|
|
collect_tags bool
|
|
|
|
deprecated bool
|
|
|
|
private bool
|
|
|
|
js bool
|
|
|
|
no_line_numbers bool
|
2022-02-08 10:10:19 +01:00
|
|
|
exclude []string
|
|
|
|
relative_paths bool
|
2020-12-05 11:10:54 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 10:10:19 +01:00
|
|
|
fn (opt Options) report_undocumented_functions_in_path(path string) {
|
2020-12-05 11:10:54 +01:00
|
|
|
mut files := []string{}
|
2022-02-08 10:10:19 +01:00
|
|
|
collect(path, mut files, fn (npath string, mut accumulated_paths []string) {
|
|
|
|
if !npath.ends_with('.v') {
|
|
|
|
return
|
2020-12-05 11:10:54 +01:00
|
|
|
}
|
2022-02-08 10:10:19 +01:00
|
|
|
if npath.ends_with('_test.v') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
accumulated_paths << npath
|
|
|
|
})
|
2021-01-05 15:13:01 +01:00
|
|
|
for file in files {
|
2022-02-08 10:10:19 +01:00
|
|
|
if !opt.js && file.ends_with('.js.v') {
|
2020-12-05 11:10:54 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-02-08 10:10:19 +01:00
|
|
|
if opt.exclude.len > 0 && opt.exclude.any(file.contains(it)) {
|
2022-02-06 14:44:26 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-02-08 10:10:19 +01:00
|
|
|
opt.report_undocumented_functions_in_file(file)
|
2021-01-05 15:13:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 10:10:19 +01:00
|
|
|
fn (opt &Options) report_undocumented_functions_in_file(nfile string) {
|
|
|
|
file := os.real_path(nfile)
|
2021-03-01 00:18:14 +01:00
|
|
|
contents := os.read_file(file) or { panic(err) }
|
2021-01-05 15:13:01 +01:00
|
|
|
lines := contents.split('\n')
|
|
|
|
mut info := []UndocumentedFN{}
|
|
|
|
for i, line in lines {
|
2022-02-06 14:44:26 +01:00
|
|
|
if line.starts_with('pub fn') || (opt.private && (line.starts_with('fn ')
|
|
|
|
&& !(line.starts_with('fn C.') || line.starts_with('fn main')))) {
|
2021-01-05 15:13:01 +01:00
|
|
|
// println('Match: $line')
|
|
|
|
if i > 0 && lines.len > 0 {
|
|
|
|
mut line_above := lines[i - 1]
|
|
|
|
if !line_above.starts_with('//') {
|
|
|
|
mut tags := []string{}
|
|
|
|
mut grab := true
|
|
|
|
for j := i - 1; j >= 0; j-- {
|
|
|
|
prev_line := lines[j]
|
|
|
|
if prev_line.contains('}') { // We've looked back to the above scope, stop here
|
|
|
|
break
|
|
|
|
} else if prev_line.starts_with('[') {
|
|
|
|
tags << collect_tags(prev_line)
|
|
|
|
continue
|
|
|
|
} else if prev_line.starts_with('//') { // Single-line comment
|
|
|
|
grab = false
|
|
|
|
break
|
2020-12-05 11:10:54 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-05 15:13:01 +01:00
|
|
|
if grab {
|
|
|
|
clean_line := line.all_before_last(' {')
|
|
|
|
info << UndocumentedFN{i + 1, clean_line, tags}
|
|
|
|
}
|
2020-12-05 11:10:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 15:13:01 +01:00
|
|
|
}
|
|
|
|
if info.len > 0 {
|
|
|
|
for undocumented_fn in info {
|
2022-02-07 12:18:10 +01:00
|
|
|
mut line_numbers := '$undocumented_fn.line:0:'
|
|
|
|
if opt.no_line_numbers {
|
|
|
|
line_numbers = ''
|
|
|
|
}
|
2021-03-24 22:37:10 +01:00
|
|
|
tags_str := if opt.collect_tags && undocumented_fn.tags.len > 0 {
|
|
|
|
'$undocumented_fn.tags'
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
}
|
2022-02-08 10:10:19 +01:00
|
|
|
ofile := if opt.relative_paths {
|
|
|
|
nfile.replace(work_dir_prefix, '')
|
|
|
|
} else {
|
|
|
|
os.real_path(nfile)
|
|
|
|
}
|
2021-01-05 15:13:01 +01:00
|
|
|
if opt.deprecated {
|
2022-02-08 10:10:19 +01:00
|
|
|
println('$ofile:$line_numbers$undocumented_fn.signature $tags_str')
|
2021-01-05 15:13:01 +01:00
|
|
|
} else {
|
|
|
|
if 'deprecated' !in undocumented_fn.tags {
|
2022-02-08 10:10:19 +01:00
|
|
|
println('$ofile:$line_numbers$undocumented_fn.signature $tags_str')
|
2020-12-11 17:35:25 +01:00
|
|
|
}
|
2020-12-05 11:10:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 10:10:19 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-11 17:35:25 +01:00
|
|
|
fn collect_tags(line string) []string {
|
|
|
|
mut cleaned := line.all_before('/')
|
|
|
|
cleaned = cleaned.replace_each(['[', '', ']', '', ' ', ''])
|
|
|
|
return cleaned.split(',')
|
|
|
|
}
|
|
|
|
|
2020-12-05 11:10:54 +01:00
|
|
|
fn main() {
|
|
|
|
if os.args.len == 1 {
|
2020-12-11 17:35:25 +01:00
|
|
|
println('Usage: $tool_name PATH \n$tool_description\n$tool_name -h for more help...')
|
2020-12-05 11:10:54 +01:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-12-11 17:35:25 +01:00
|
|
|
mut fp := flag.new_flag_parser(os.args[1..])
|
|
|
|
fp.application(tool_name)
|
|
|
|
fp.version(tool_version)
|
|
|
|
fp.description(tool_description)
|
|
|
|
fp.arguments_description('PATH [PATH]...')
|
|
|
|
// Collect tool options
|
|
|
|
opt := Options{
|
|
|
|
show_help: fp.bool('help', `h`, false, 'Show this help text.')
|
|
|
|
deprecated: fp.bool('deprecated', `d`, false, 'Include deprecated functions in output.')
|
2022-02-06 14:44:26 +01:00
|
|
|
private: fp.bool('private', `p`, false, 'Include private functions in output.')
|
|
|
|
js: fp.bool('js', 0, false, 'Include JavaScript functions in output.')
|
2022-02-08 10:10:19 +01:00
|
|
|
no_line_numbers: fp.bool('no-line-numbers', `n`, false, 'Exclude line numbers in output.')
|
2020-12-11 17:35:25 +01:00
|
|
|
collect_tags: fp.bool('tags', `t`, false, 'Also print function tags if any is found.')
|
2022-02-08 10:10:19 +01:00
|
|
|
exclude: fp.string_multi('exclude', `e`, '')
|
|
|
|
relative_paths: fp.bool('relative-paths', `r`, false, 'Use relative paths in output.')
|
2020-12-11 17:35:25 +01:00
|
|
|
}
|
|
|
|
if opt.show_help {
|
|
|
|
println(fp.usage())
|
|
|
|
exit(0)
|
|
|
|
}
|
2020-12-05 11:10:54 +01:00
|
|
|
for path in os.args[1..] {
|
2021-01-05 15:13:01 +01:00
|
|
|
if os.is_file(path) {
|
2022-02-08 10:10:19 +01:00
|
|
|
opt.report_undocumented_functions_in_file(path)
|
2021-01-05 15:13:01 +01:00
|
|
|
} else {
|
2022-02-08 10:10:19 +01:00
|
|
|
opt.report_undocumented_functions_in_path(path)
|
2021-01-05 15:13:01 +01:00
|
|
|
}
|
2020-12-05 11:10:54 +01:00
|
|
|
}
|
|
|
|
}
|