2021-02-05 18:46:20 +03:00
|
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
|
// that can be found in the LICENSE file.
|
2020-02-20 15:41:03 +03:00
|
|
|
|
module main
|
|
|
|
|
|
|
|
|
|
import os
|
2021-01-10 16:58:45 +03:00
|
|
|
|
import os.cmdline
|
2020-11-03 03:04:14 +03:00
|
|
|
|
import rand
|
|
|
|
|
import term
|
2021-02-05 18:46:20 +03:00
|
|
|
|
import vhelp
|
2020-11-03 03:04:14 +03:00
|
|
|
|
import v.pref
|
2020-02-20 15:41:03 +03:00
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
too_long_line_length = 100
|
2020-11-03 03:04:14 +03:00
|
|
|
|
term_colors = term.can_show_color_on_stderr()
|
2021-01-10 16:58:45 +03:00
|
|
|
|
is_all = '-all' in os.args
|
|
|
|
|
hide_warnings = '-hide-warnings' in os.args
|
2021-02-05 18:46:20 +03:00
|
|
|
|
non_option_args = cmdline.only_non_options(os.args[2..])
|
2020-02-20 15:41:03 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2021-02-05 18:46:20 +03:00
|
|
|
|
if non_option_args.len == 0 || '-help' in os.args {
|
|
|
|
|
vhelp.show_topic('check-md')
|
2021-01-10 16:58:45 +03:00
|
|
|
|
exit(0)
|
|
|
|
|
}
|
2021-02-05 18:46:20 +03:00
|
|
|
|
if is_all {
|
|
|
|
|
println('´-all´ flag is deprecated. Please use ´v check-md .´ instead.')
|
|
|
|
|
exit(1)
|
|
|
|
|
}
|
|
|
|
|
mut files_paths := non_option_args.clone()
|
2020-10-20 21:14:56 +03:00
|
|
|
|
mut warnings := 0
|
2020-02-20 15:41:03 +03:00
|
|
|
|
mut errors := 0
|
2020-11-03 03:04:14 +03:00
|
|
|
|
mut oks := 0
|
|
|
|
|
mut all_md_files := []MDFile{}
|
2021-01-10 16:58:45 +03:00
|
|
|
|
if term_colors {
|
|
|
|
|
os.setenv('VCOLORS', 'always', true)
|
|
|
|
|
}
|
2021-02-05 18:46:20 +03:00
|
|
|
|
for i := 0; i < files_paths.len; i++ {
|
|
|
|
|
file_path := files_paths[i]
|
|
|
|
|
if os.is_dir(file_path) {
|
|
|
|
|
files_paths << md_file_paths(file_path)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-03-20 18:41:18 +03:00
|
|
|
|
real_path := os.real_path(file_path)
|
2020-02-20 15:41:03 +03:00
|
|
|
|
lines := os.read_lines(real_path) or {
|
2020-10-20 21:14:56 +03:00
|
|
|
|
println('"$file_path" does not exist')
|
|
|
|
|
warnings++
|
2020-02-20 15:41:03 +03:00
|
|
|
|
continue
|
|
|
|
|
}
|
2020-11-03 03:04:14 +03:00
|
|
|
|
mut mdfile := MDFile{
|
|
|
|
|
path: file_path
|
|
|
|
|
}
|
2021-02-05 18:46:20 +03:00
|
|
|
|
for j, line in lines {
|
2020-02-20 15:41:03 +03:00
|
|
|
|
if line.len > too_long_line_length {
|
2020-11-18 20:28:28 +03:00
|
|
|
|
if mdfile.state == .vexample {
|
2021-02-05 18:46:20 +03:00
|
|
|
|
wprintln(wline(file_path, j, line.len, 'long V example line'))
|
2021-01-24 01:13:21 +03:00
|
|
|
|
wprintln(line)
|
2020-11-18 20:28:28 +03:00
|
|
|
|
warnings++
|
|
|
|
|
} else if mdfile.state == .codeblock {
|
2021-02-05 18:46:20 +03:00
|
|
|
|
wprintln(wline(file_path, j, line.len, 'long code block line'))
|
2021-01-24 01:13:21 +03:00
|
|
|
|
wprintln(line)
|
2020-11-18 20:28:28 +03:00
|
|
|
|
warnings++
|
|
|
|
|
} else if line.starts_with('|') {
|
2021-02-05 18:46:20 +03:00
|
|
|
|
wprintln(wline(file_path, j, line.len, 'long table'))
|
2021-01-24 01:13:21 +03:00
|
|
|
|
wprintln(line)
|
2020-10-20 21:14:56 +03:00
|
|
|
|
warnings++
|
|
|
|
|
} else if line.contains('https') {
|
2021-02-05 18:46:20 +03:00
|
|
|
|
wprintln(wline(file_path, j, line.len, 'long link'))
|
2021-01-24 01:13:21 +03:00
|
|
|
|
wprintln(line)
|
2020-10-20 21:14:56 +03:00
|
|
|
|
warnings++
|
|
|
|
|
} else {
|
2021-02-05 18:46:20 +03:00
|
|
|
|
eprintln(eline(file_path, j, line.len, 'line too long'))
|
2021-01-24 01:13:21 +03:00
|
|
|
|
eprintln(line)
|
2020-10-20 21:14:56 +03:00
|
|
|
|
errors++
|
|
|
|
|
}
|
2020-02-20 15:41:03 +03:00
|
|
|
|
}
|
2021-02-05 18:46:20 +03:00
|
|
|
|
mdfile.parse_line(j, line)
|
2020-02-20 15:41:03 +03:00
|
|
|
|
}
|
2020-11-03 03:04:14 +03:00
|
|
|
|
all_md_files << mdfile
|
|
|
|
|
}
|
|
|
|
|
for mut mdfile in all_md_files {
|
|
|
|
|
new_errors, new_oks := mdfile.check_examples()
|
|
|
|
|
errors += new_errors
|
|
|
|
|
oks += new_oks
|
2020-02-20 15:41:03 +03:00
|
|
|
|
}
|
2020-11-03 03:04:14 +03:00
|
|
|
|
if warnings > 0 || errors > 0 || oks > 0 {
|
|
|
|
|
println('\nWarnings: $warnings | Errors: $errors | OKs: $oks')
|
2020-10-20 21:14:56 +03:00
|
|
|
|
}
|
2020-02-20 15:41:03 +03:00
|
|
|
|
if errors > 0 {
|
|
|
|
|
exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-03 03:04:14 +03:00
|
|
|
|
|
2021-02-05 18:46:20 +03:00
|
|
|
|
fn md_file_paths(dir string) []string {
|
2020-11-18 20:28:28 +03:00
|
|
|
|
mut files_to_check := []string{}
|
2021-02-05 18:46:20 +03:00
|
|
|
|
md_files := os.walk_ext(dir, '.md')
|
2020-11-18 20:28:28 +03:00
|
|
|
|
for file in md_files {
|
2021-02-05 18:46:20 +03:00
|
|
|
|
if file.contains_any_substr(['/thirdparty/', 'CHANGELOG']) {
|
2020-12-22 23:01:54 +03:00
|
|
|
|
continue
|
|
|
|
|
}
|
2020-11-18 20:28:28 +03:00
|
|
|
|
files_to_check << file
|
|
|
|
|
}
|
|
|
|
|
return files_to_check
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 18:46:20 +03:00
|
|
|
|
fn wprintln(s string) {
|
|
|
|
|
if !hide_warnings {
|
|
|
|
|
println(s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 03:04:14 +03:00
|
|
|
|
fn ftext(s string, cb fn (string) string) string {
|
|
|
|
|
if term_colors {
|
|
|
|
|
return cb(s)
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn btext(s string) string {
|
|
|
|
|
return ftext(s, term.bold)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn mtext(s string) string {
|
|
|
|
|
return ftext(s, term.magenta)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn rtext(s string) string {
|
|
|
|
|
return ftext(s, term.red)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn wline(file_path string, lnumber int, column int, message string) string {
|
2021-01-24 01:13:21 +03:00
|
|
|
|
return btext('$file_path:${lnumber + 1}:${column + 1}:') + btext(mtext(' warn:')) +
|
|
|
|
|
rtext(' $message')
|
2020-11-03 03:04:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn eline(file_path string, lnumber int, column int, message string) string {
|
|
|
|
|
return btext('$file_path:${lnumber + 1}:${column + 1}:') + btext(rtext(' error: $message'))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
default_command = 'compile'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
struct VCodeExample {
|
|
|
|
|
mut:
|
|
|
|
|
text []string
|
|
|
|
|
command string
|
|
|
|
|
sline int
|
|
|
|
|
eline int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum MDFileParserState {
|
|
|
|
|
markdown
|
|
|
|
|
vexample
|
2020-11-18 20:28:28 +03:00
|
|
|
|
codeblock
|
2020-11-03 03:04:14 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MDFile {
|
2021-01-24 01:13:21 +03:00
|
|
|
|
path string
|
2020-11-03 03:04:14 +03:00
|
|
|
|
mut:
|
|
|
|
|
examples []VCodeExample
|
|
|
|
|
current VCodeExample
|
|
|
|
|
state MDFileParserState = .markdown
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn (mut f MDFile) parse_line(lnumber int, line string) {
|
|
|
|
|
if line.starts_with('```v') {
|
|
|
|
|
if f.state == .markdown {
|
|
|
|
|
f.state = .vexample
|
|
|
|
|
mut command := line.replace('```v', '').trim_space()
|
|
|
|
|
if command == '' {
|
|
|
|
|
command = default_command
|
2021-02-05 21:25:27 +03:00
|
|
|
|
} else if command == 'nofmt' {
|
|
|
|
|
command += ' $default_command'
|
2020-11-03 03:04:14 +03:00
|
|
|
|
}
|
|
|
|
|
f.current = VCodeExample{
|
|
|
|
|
sline: lnumber
|
|
|
|
|
command: command
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-11-18 20:28:28 +03:00
|
|
|
|
if line.starts_with('```') {
|
|
|
|
|
match f.state {
|
|
|
|
|
.vexample {
|
|
|
|
|
f.state = .markdown
|
|
|
|
|
f.current.eline = lnumber
|
|
|
|
|
f.examples << f.current
|
|
|
|
|
f.current = VCodeExample{}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
.codeblock {
|
|
|
|
|
f.state = .markdown
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
.markdown {
|
|
|
|
|
f.state = .codeblock
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-03 03:04:14 +03:00
|
|
|
|
}
|
|
|
|
|
if f.state == .vexample {
|
|
|
|
|
f.current.text << line
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-06 20:09:28 +03:00
|
|
|
|
fn (mut f MDFile) debug() {
|
2020-11-03 03:04:14 +03:00
|
|
|
|
for e in f.examples {
|
|
|
|
|
eprintln('f.path: $f.path | example: $e')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-10 16:58:45 +03:00
|
|
|
|
fn cmdexecute(cmd string) int {
|
2021-03-08 21:52:13 +03:00
|
|
|
|
res := os.execute(cmd)
|
|
|
|
|
if res.exit_code < 0 {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
2021-01-10 16:58:45 +03:00
|
|
|
|
if res.exit_code != 0 {
|
|
|
|
|
eprint(res.output)
|
|
|
|
|
}
|
|
|
|
|
return res.exit_code
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn silent_cmdexecute(cmd string) int {
|
2021-03-08 21:52:13 +03:00
|
|
|
|
res := os.execute(cmd)
|
2021-01-07 22:13:07 +03:00
|
|
|
|
return res.exit_code
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-10 16:58:45 +03:00
|
|
|
|
fn get_fmt_exit_code(vfile string, vexe string) int {
|
|
|
|
|
return silent_cmdexecute('"$vexe" fmt -verify $vfile')
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 03:04:14 +03:00
|
|
|
|
fn (mut f MDFile) check_examples() (int, int) {
|
|
|
|
|
mut errors := 0
|
|
|
|
|
mut oks := 0
|
|
|
|
|
vexe := pref.vexe_path()
|
|
|
|
|
for e in f.examples {
|
|
|
|
|
if e.command == 'ignore' {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if e.command == 'wip' {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
fname := os.base(f.path).replace('.md', '_md')
|
|
|
|
|
uid := rand.ulid()
|
|
|
|
|
vfile := os.join_path(os.temp_dir(), 'check_${fname}_example_${e.sline}__${e.eline}__${uid}.v')
|
|
|
|
|
mut should_cleanup_vfile := true
|
|
|
|
|
// eprintln('>>> checking example $vfile ...')
|
2020-12-06 00:54:41 +03:00
|
|
|
|
vcontent := e.text.join('\n') + '\n'
|
2021-03-01 02:18:14 +03:00
|
|
|
|
os.write_file(vfile, vcontent) or { panic(err) }
|
2020-11-03 03:04:14 +03:00
|
|
|
|
mut acommands := e.command.split(' ')
|
2020-12-06 00:54:41 +03:00
|
|
|
|
nofmt := 'nofmt' in acommands
|
2020-11-03 03:04:14 +03:00
|
|
|
|
for command in acommands {
|
2021-01-07 22:13:07 +03:00
|
|
|
|
fmt_res := if nofmt { 0 } else { get_fmt_exit_code(vfile, vexe) }
|
2020-11-03 03:04:14 +03:00
|
|
|
|
match command {
|
|
|
|
|
'compile' {
|
2021-01-10 16:58:45 +03:00
|
|
|
|
res := cmdexecute('"$vexe" -w -Wfatal-errors -o x.c $vfile')
|
2020-11-03 03:04:14 +03:00
|
|
|
|
os.rm('x.c') or { }
|
2020-12-06 00:54:41 +03:00
|
|
|
|
if res != 0 || fmt_res != 0 {
|
|
|
|
|
if res != 0 {
|
|
|
|
|
eprintln(eline(f.path, e.sline, 0, 'example failed to compile'))
|
|
|
|
|
}
|
|
|
|
|
if fmt_res != 0 {
|
|
|
|
|
eprintln(eline(f.path, e.sline, 0, 'example is not formatted'))
|
|
|
|
|
}
|
2020-11-03 03:04:14 +03:00
|
|
|
|
eprintln(vcontent)
|
|
|
|
|
should_cleanup_vfile = false
|
|
|
|
|
errors++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
oks++
|
|
|
|
|
}
|
2020-11-14 21:02:10 +03:00
|
|
|
|
'live' {
|
2021-01-10 16:58:45 +03:00
|
|
|
|
res := cmdexecute('"$vexe" -w -Wfatal-errors -live -o x.c $vfile')
|
2020-12-06 00:54:41 +03:00
|
|
|
|
if res != 0 || fmt_res != 0 {
|
|
|
|
|
if res != 0 {
|
|
|
|
|
eprintln(eline(f.path, e.sline, 0, 'example failed to compile with -live'))
|
|
|
|
|
}
|
|
|
|
|
if fmt_res != 0 {
|
|
|
|
|
eprintln(eline(f.path, e.sline, 0, 'example is not formatted'))
|
|
|
|
|
}
|
2020-11-14 21:02:10 +03:00
|
|
|
|
eprintln(vcontent)
|
|
|
|
|
should_cleanup_vfile = false
|
|
|
|
|
errors++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
oks++
|
|
|
|
|
}
|
2020-11-03 03:04:14 +03:00
|
|
|
|
'failcompile' {
|
2021-01-10 16:58:45 +03:00
|
|
|
|
res := silent_cmdexecute('"$vexe" -w -Wfatal-errors -o x.c $vfile')
|
2020-11-03 03:04:14 +03:00
|
|
|
|
os.rm('x.c') or { }
|
|
|
|
|
if res == 0 {
|
|
|
|
|
eprintln(eline(f.path, e.sline, 0, '`failcompile` example compiled'))
|
|
|
|
|
eprintln(vcontent)
|
|
|
|
|
should_cleanup_vfile = false
|
|
|
|
|
errors++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
oks++
|
|
|
|
|
}
|
|
|
|
|
'oksyntax' {
|
2021-01-10 16:58:45 +03:00
|
|
|
|
res := cmdexecute('"$vexe" -w -Wfatal-errors -check-syntax $vfile')
|
2020-12-06 00:54:41 +03:00
|
|
|
|
if res != 0 || fmt_res != 0 {
|
|
|
|
|
if res != 0 {
|
|
|
|
|
eprintln(eline(f.path, e.sline, 0, '`oksyntax` example with invalid syntax'))
|
|
|
|
|
}
|
|
|
|
|
if fmt_res != 0 {
|
|
|
|
|
eprintln(eline(f.path, e.sline, 0, '`oksyntax` example is not formatted'))
|
|
|
|
|
}
|
2020-11-03 03:04:14 +03:00
|
|
|
|
eprintln(vcontent)
|
|
|
|
|
should_cleanup_vfile = false
|
|
|
|
|
errors++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
oks++
|
|
|
|
|
}
|
|
|
|
|
'badsyntax' {
|
2021-01-10 16:58:45 +03:00
|
|
|
|
res := silent_cmdexecute('"$vexe" -w -Wfatal-errors -check-syntax $vfile')
|
2020-11-03 03:04:14 +03:00
|
|
|
|
if res == 0 {
|
|
|
|
|
eprintln(eline(f.path, e.sline, 0, '`badsyntax` example can be parsed fine'))
|
|
|
|
|
eprintln(vcontent)
|
|
|
|
|
should_cleanup_vfile = false
|
|
|
|
|
errors++
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
oks++
|
|
|
|
|
}
|
2020-12-06 00:54:41 +03:00
|
|
|
|
'nofmt' {}
|
2020-11-03 03:04:14 +03:00
|
|
|
|
else {
|
|
|
|
|
eprintln(eline(f.path, e.sline, 0, 'unrecognized command: "$command", use one of: wip/ignore/compile/failcompile/oksyntax/badsyntax'))
|
|
|
|
|
should_cleanup_vfile = false
|
|
|
|
|
errors++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if should_cleanup_vfile {
|
2021-03-01 02:18:14 +03:00
|
|
|
|
os.rm(vfile) or { panic(err) }
|
2020-11-03 03:04:14 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return errors, oks
|
|
|
|
|
}
|