2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-12-23 13:02:50 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module main
|
|
|
|
|
2020-04-16 16:54:17 +03:00
|
|
|
import os
|
|
|
|
import os.cmdline
|
2020-10-18 13:46:30 +03:00
|
|
|
import rand
|
2021-02-03 11:03:41 +03:00
|
|
|
import term
|
2020-04-16 16:54:17 +03:00
|
|
|
import v.ast
|
|
|
|
import v.pref
|
|
|
|
import v.fmt
|
|
|
|
import v.util
|
2021-06-22 18:52:34 +03:00
|
|
|
import v.util.diff
|
2020-04-16 16:54:17 +03:00
|
|
|
import v.parser
|
2023-02-01 12:18:23 +03:00
|
|
|
import v.help
|
2019-12-23 13:02:50 +03:00
|
|
|
|
|
|
|
struct FormatOptions {
|
2019-12-27 19:59:04 +03:00
|
|
|
is_l bool
|
2022-03-06 20:01:22 +03:00
|
|
|
is_c bool // Note: This refers to the '-c' fmt flag, NOT the C backend
|
2019-12-23 13:02:50 +03:00
|
|
|
is_w bool
|
|
|
|
is_diff bool
|
|
|
|
is_verbose bool
|
|
|
|
is_all bool
|
2019-12-27 19:59:04 +03:00
|
|
|
is_debug bool
|
2019-12-30 07:42:23 +03:00
|
|
|
is_noerror bool
|
2020-07-16 20:46:03 +03:00
|
|
|
is_verify bool // exit(1) if the file is not vfmt'ed
|
2022-03-06 20:01:22 +03:00
|
|
|
is_worker bool // true *only* in the worker processes. Note: workers can crash.
|
2021-10-07 13:59:13 +03:00
|
|
|
is_backup bool // make a `file.v.bak` copy *before* overwriting a `file.v` in place with `-w`
|
2022-03-18 13:15:55 +03:00
|
|
|
mut:
|
|
|
|
diff_cmd string // filled in when -diff or -verify is passed
|
2019-12-23 13:02:50 +03:00
|
|
|
}
|
|
|
|
|
2019-12-27 19:59:04 +03:00
|
|
|
const (
|
2021-01-30 13:38:54 +03:00
|
|
|
formatted_file_token = '\@\@\@' + 'FORMATTED_FILE: '
|
2022-11-03 10:24:52 +03:00
|
|
|
vtmp_folder = os.vtmp_dir()
|
2021-02-03 11:03:41 +03:00
|
|
|
term_colors = term.can_show_color_on_stderr()
|
2019-12-27 19:59:04 +03:00
|
|
|
)
|
|
|
|
|
2019-12-23 13:02:50 +03:00
|
|
|
fn main() {
|
2020-04-16 16:54:17 +03:00
|
|
|
// if os.getenv('VFMT_ENABLE') == '' {
|
|
|
|
// eprintln('v fmt is disabled for now')
|
|
|
|
// exit(1)
|
|
|
|
// }
|
2019-12-23 13:02:50 +03:00
|
|
|
toolexe := os.executable()
|
2020-04-03 18:38:41 +03:00
|
|
|
util.set_vroot_folder(os.dir(os.dir(os.dir(toolexe))))
|
2020-04-11 10:18:59 +03:00
|
|
|
args := util.join_env_vflags_and_os_args()
|
2020-12-27 16:36:59 +03:00
|
|
|
mut foptions := FormatOptions{
|
2019-12-27 19:59:04 +03:00
|
|
|
is_c: '-c' in args
|
|
|
|
is_l: '-l' in args
|
2020-06-26 04:22:27 +03:00
|
|
|
is_w: '-w' in args
|
2019-12-24 05:43:31 +03:00
|
|
|
is_diff: '-diff' in args
|
|
|
|
is_verbose: '-verbose' in args || '--verbose' in args
|
|
|
|
is_all: '-all' in args || '--all' in args
|
|
|
|
is_worker: '-worker' in args
|
2019-12-27 19:59:04 +03:00
|
|
|
is_debug: '-debug' in args
|
2019-12-30 07:42:23 +03:00
|
|
|
is_noerror: '-noerror' in args
|
2020-07-16 20:46:03 +03:00
|
|
|
is_verify: '-verify' in args
|
2021-10-07 13:59:13 +03:00
|
|
|
is_backup: '-backup' in args
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
2021-02-03 11:03:41 +03:00
|
|
|
if term_colors {
|
|
|
|
os.setenv('VCOLORS', 'always', true)
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
foptions.vlog('vfmt foptions: ${foptions}')
|
2019-12-24 05:43:31 +03:00
|
|
|
if foptions.is_worker {
|
|
|
|
// -worker should be added by a parent vfmt process.
|
|
|
|
// We launch a sub process for each file because
|
|
|
|
// the v compiler can do an early exit if it detects
|
|
|
|
// a syntax error, but we want to process ALL passed
|
|
|
|
// files if possible.
|
|
|
|
foptions.format_file(cmdline.option(args, '-worker', ''))
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
// we are NOT a worker at this stage, i.e. we are a parent vfmt process
|
2020-02-16 14:42:28 +03:00
|
|
|
possible_files := cmdline.only_non_options(cmdline.options_after(args, ['fmt']))
|
2019-12-23 13:02:50 +03:00
|
|
|
if foptions.is_verbose {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('vfmt toolexe: ${toolexe}')
|
2019-12-23 13:02:50 +03:00
|
|
|
eprintln('vfmt args: ' + os.args.str())
|
|
|
|
eprintln('vfmt env_vflags_and_os_args: ' + args.str())
|
2019-12-24 05:43:31 +03:00
|
|
|
eprintln('vfmt possible_files: ' + possible_files.str())
|
2019-12-23 13:02:50 +03:00
|
|
|
}
|
2021-03-24 13:39:09 +03:00
|
|
|
files := util.find_all_v_files(possible_files) or {
|
2022-04-12 13:38:40 +03:00
|
|
|
verror(err.msg())
|
2021-03-24 13:39:09 +03:00
|
|
|
return
|
2019-12-23 13:02:50 +03:00
|
|
|
}
|
2021-04-14 08:50:50 +03:00
|
|
|
if os.is_atty(0) == 0 && files.len == 0 {
|
2021-01-30 13:38:54 +03:00
|
|
|
foptions.format_pipe()
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
if files.len == 0 || '-help' in args || '--help' in args {
|
2023-02-01 12:18:23 +03:00
|
|
|
help.print_and_exit('fmt')
|
2019-12-23 13:02:50 +03:00
|
|
|
exit(0)
|
|
|
|
}
|
2020-04-26 14:49:31 +03:00
|
|
|
mut cli_args_no_files := []string{}
|
2022-01-22 22:13:16 +03:00
|
|
|
for idx, a in os.args {
|
|
|
|
if idx == 0 {
|
|
|
|
cli_args_no_files << os.quoted_path(a)
|
|
|
|
continue
|
|
|
|
}
|
2020-04-26 07:39:23 +03:00
|
|
|
if a !in files {
|
2019-12-24 05:43:31 +03:00
|
|
|
cli_args_no_files << a
|
|
|
|
}
|
2019-12-23 13:02:50 +03:00
|
|
|
}
|
2020-04-23 02:16:58 +03:00
|
|
|
mut errors := 0
|
2019-12-23 13:02:50 +03:00
|
|
|
for file in files {
|
2020-03-20 18:41:18 +03:00
|
|
|
fpath := os.real_path(file)
|
2020-04-23 02:16:58 +03:00
|
|
|
mut worker_command_array := cli_args_no_files.clone()
|
2020-11-16 20:26:44 +03:00
|
|
|
worker_command_array << ['-worker', util.quote_path(fpath)]
|
2019-12-24 05:43:31 +03:00
|
|
|
worker_cmd := worker_command_array.join(' ')
|
2022-11-15 16:53:13 +03:00
|
|
|
foptions.vlog('vfmt worker_cmd: ${worker_cmd}')
|
2021-03-08 21:52:13 +03:00
|
|
|
worker_result := os.execute(worker_cmd)
|
2020-12-27 17:26:51 +03:00
|
|
|
// Guard against a possibly crashing worker process.
|
2019-12-31 08:02:49 +03:00
|
|
|
if worker_result.exit_code != 0 {
|
|
|
|
eprintln(worker_result.output)
|
|
|
|
if worker_result.exit_code == 1 {
|
2021-03-10 21:21:39 +03:00
|
|
|
eprintln('Internal vfmt error while formatting file: ${file}.')
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
2019-12-24 05:43:31 +03:00
|
|
|
errors++
|
2019-12-31 08:02:49 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if worker_result.output.len > 0 {
|
2020-05-22 18:36:09 +03:00
|
|
|
if worker_result.output.contains(formatted_file_token) {
|
|
|
|
wresult := worker_result.output.split(formatted_file_token)
|
2019-12-31 08:02:49 +03:00
|
|
|
formatted_warn_errs := wresult[0]
|
2020-06-15 14:42:41 +03:00
|
|
|
formatted_file_path := wresult[1].trim_right('\n\r')
|
2020-12-27 18:15:26 +03:00
|
|
|
foptions.post_process_file(fpath, formatted_file_path) or { errors = errors + 1 }
|
2019-12-31 08:02:49 +03:00
|
|
|
if formatted_warn_errs.len > 0 {
|
|
|
|
eprintln(formatted_warn_errs)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-12-24 05:43:31 +03:00
|
|
|
}
|
2019-12-31 08:02:49 +03:00
|
|
|
errors++
|
2019-12-24 05:43:31 +03:00
|
|
|
}
|
|
|
|
if errors > 0 {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('Encountered a total of: ${errors} errors.')
|
2019-12-30 07:42:23 +03:00
|
|
|
if foptions.is_noerror {
|
|
|
|
exit(0)
|
|
|
|
}
|
2020-12-27 17:26:51 +03:00
|
|
|
if foptions.is_verify {
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
if foptions.is_c {
|
|
|
|
exit(2)
|
|
|
|
}
|
2021-02-04 17:00:08 +03:00
|
|
|
exit(1)
|
2019-12-23 13:02:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 11:10:12 +03:00
|
|
|
fn setup_preferences_and_table() (&pref.Preferences, &ast.Table) {
|
|
|
|
table := ast.new_table()
|
2020-04-28 16:04:37 +03:00
|
|
|
mut prefs := pref.new_preferences()
|
2020-06-18 19:48:23 +03:00
|
|
|
prefs.is_fmt = true
|
2022-04-23 11:10:12 +03:00
|
|
|
prefs.skip_warnings = true
|
|
|
|
return prefs, table
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (foptions &FormatOptions) vlog(msg string) {
|
2019-12-23 13:02:50 +03:00
|
|
|
if foptions.is_verbose {
|
2022-04-23 11:10:12 +03:00
|
|
|
eprintln(msg)
|
2020-04-03 18:38:41 +03:00
|
|
|
}
|
2022-04-23 11:10:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (foptions &FormatOptions) format_file(file string) {
|
2022-11-15 16:53:13 +03:00
|
|
|
foptions.vlog('vfmt2 running fmt.fmt over file: ${file}')
|
2022-04-23 11:10:12 +03:00
|
|
|
prefs, table := setup_preferences_and_table()
|
2021-07-18 15:29:34 +03:00
|
|
|
file_ast := parser.parse_file(file, table, .parse_comments, prefs)
|
2022-04-23 11:10:12 +03:00
|
|
|
// checker.new_checker(table, prefs).check(file_ast)
|
2021-02-02 11:14:34 +03:00
|
|
|
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug)
|
2020-04-03 18:38:41 +03:00
|
|
|
file_name := os.file_name(file)
|
2020-10-18 13:46:30 +03:00
|
|
|
ulid := rand.ulid()
|
2022-11-15 16:53:13 +03:00
|
|
|
vfmt_output_path := os.join_path(vtmp_folder, 'vfmt_${ulid}_${file_name}')
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(vfmt_output_path, formatted_content) or { panic(err) }
|
2022-11-15 16:53:13 +03:00
|
|
|
foptions.vlog('fmt.fmt worked and ${formatted_content.len} bytes were written to ${vfmt_output_path} .')
|
|
|
|
eprintln('${formatted_file_token}${vfmt_output_path}')
|
2019-12-31 08:02:49 +03:00
|
|
|
}
|
|
|
|
|
2021-01-30 13:38:54 +03:00
|
|
|
fn (foptions &FormatOptions) format_pipe() {
|
2022-04-23 11:10:12 +03:00
|
|
|
foptions.vlog('vfmt2 running fmt.fmt over stdin')
|
|
|
|
prefs, table := setup_preferences_and_table()
|
2021-01-30 13:38:54 +03:00
|
|
|
input_text := os.get_raw_lines_joined()
|
2021-07-18 15:29:34 +03:00
|
|
|
file_ast := parser.parse_text(input_text, '', table, .parse_comments, prefs)
|
2022-04-23 11:10:12 +03:00
|
|
|
// checker.new_checker(table, prefs).check(file_ast)
|
2022-11-05 09:08:01 +03:00
|
|
|
formatted_content := fmt.fmt(file_ast, table, prefs, foptions.is_debug, source_text: input_text)
|
2021-01-30 13:38:54 +03:00
|
|
|
print(formatted_content)
|
2022-05-30 19:15:05 +03:00
|
|
|
flush_stdout()
|
2022-11-15 16:53:13 +03:00
|
|
|
foptions.vlog('fmt.fmt worked and ${formatted_content.len} bytes were written to stdout.')
|
2021-01-30 13:38:54 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 16:54:17 +03:00
|
|
|
fn print_compiler_options(compiler_params &pref.Preferences) {
|
|
|
|
eprintln(' os: ' + compiler_params.os.str())
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln(' ccompiler: ${compiler_params.ccompiler}')
|
|
|
|
eprintln(' path: ${compiler_params.path} ')
|
|
|
|
eprintln(' out_name: ${compiler_params.out_name} ')
|
|
|
|
eprintln(' vroot: ${compiler_params.vroot} ')
|
|
|
|
eprintln('lookup_path: ${compiler_params.lookup_path} ')
|
|
|
|
eprintln(' out_name: ${compiler_params.out_name} ')
|
|
|
|
eprintln(' cflags: ${compiler_params.cflags} ')
|
|
|
|
eprintln(' is_test: ${compiler_params.is_test} ')
|
|
|
|
eprintln(' is_script: ${compiler_params.is_script} ')
|
2020-02-09 12:08:04 +03:00
|
|
|
}
|
|
|
|
|
2022-03-18 13:15:55 +03:00
|
|
|
fn (mut foptions FormatOptions) find_diff_cmd() string {
|
|
|
|
if foptions.diff_cmd != '' {
|
|
|
|
return foptions.diff_cmd
|
|
|
|
}
|
|
|
|
if foptions.is_verify || foptions.is_diff {
|
|
|
|
foptions.diff_cmd = diff.find_working_diff_command() or {
|
|
|
|
eprintln(err)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return foptions.diff_cmd
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:49:50 +03:00
|
|
|
fn (mut foptions FormatOptions) post_process_file(file string, formatted_file_path string) ! {
|
2019-12-24 05:43:31 +03:00
|
|
|
if formatted_file_path.len == 0 {
|
|
|
|
return
|
2019-12-23 13:02:50 +03:00
|
|
|
}
|
2022-03-18 13:15:55 +03:00
|
|
|
fc := os.read_file(file) or {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('File ${file} could not be read')
|
2022-03-18 13:15:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
formatted_fc := os.read_file(formatted_file_path) or {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('File ${formatted_file_path} could not be read')
|
2022-03-18 13:15:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
is_formatted_different := fc != formatted_fc
|
2019-12-23 13:02:50 +03:00
|
|
|
if foptions.is_diff {
|
2022-03-18 13:15:55 +03:00
|
|
|
if !is_formatted_different {
|
2019-12-23 13:02:50 +03:00
|
|
|
return
|
|
|
|
}
|
2022-03-18 13:15:55 +03:00
|
|
|
diff_cmd := foptions.find_diff_cmd()
|
2022-11-15 16:53:13 +03:00
|
|
|
foptions.vlog('Using diff command: ${diff_cmd}')
|
2023-02-08 21:37:04 +03:00
|
|
|
diff_ := diff.color_compare_files(diff_cmd, file, formatted_file_path)
|
|
|
|
if diff_.len > 0 {
|
|
|
|
println(diff_)
|
2021-04-19 18:55:55 +03:00
|
|
|
}
|
2019-12-23 13:02:50 +03:00
|
|
|
return
|
|
|
|
}
|
2020-07-16 20:46:03 +03:00
|
|
|
if foptions.is_verify {
|
2022-03-18 13:15:55 +03:00
|
|
|
if !is_formatted_different {
|
2020-07-16 20:46:03 +03:00
|
|
|
return
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
println("${file} is not vfmt'ed")
|
2022-04-09 13:03:52 +03:00
|
|
|
return error('')
|
2020-07-16 20:46:03 +03:00
|
|
|
}
|
2020-05-19 18:12:47 +03:00
|
|
|
if foptions.is_c {
|
2019-12-27 19:59:04 +03:00
|
|
|
if is_formatted_different {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('File is not formatted: ${file}')
|
2020-12-27 17:26:51 +03:00
|
|
|
return error('')
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if foptions.is_l {
|
|
|
|
if is_formatted_different {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('File needs formatting: ${file}')
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-12-23 13:02:50 +03:00
|
|
|
if foptions.is_w {
|
2019-12-27 19:59:04 +03:00
|
|
|
if is_formatted_different {
|
2021-10-07 13:59:13 +03:00
|
|
|
if foptions.is_backup {
|
|
|
|
file_bak := '${file}.bak'
|
|
|
|
os.cp(file, file_bak) or {}
|
|
|
|
}
|
2022-02-01 15:49:06 +03:00
|
|
|
mut perms_to_restore := u32(0)
|
|
|
|
$if !windows {
|
|
|
|
fm := os.inode(file)
|
|
|
|
perms_to_restore = fm.bitmask()
|
|
|
|
}
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mv_by_cp(formatted_file_path, file) or { panic(err) }
|
2022-02-01 15:49:06 +03:00
|
|
|
$if !windows {
|
|
|
|
os.chmod(file, int(perms_to_restore)) or { panic(err) }
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('Reformatted file: ${file}')
|
2020-04-16 16:54:17 +03:00
|
|
|
} else {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('Already formatted file: ${file}')
|
2019-12-23 13:02:50 +03:00
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
return
|
2019-12-23 13:02:50 +03:00
|
|
|
}
|
2019-12-31 08:02:49 +03:00
|
|
|
print(formatted_fc)
|
2022-05-30 19:15:05 +03:00
|
|
|
flush_stdout()
|
2019-12-23 13:02:50 +03:00
|
|
|
}
|
|
|
|
|
2020-04-19 01:07:57 +03:00
|
|
|
fn (f FormatOptions) str() string {
|
2021-04-03 19:30:25 +03:00
|
|
|
return
|
2022-11-15 16:53:13 +03:00
|
|
|
'FormatOptions{ is_l: ${f.is_l}, is_w: ${f.is_w}, is_diff: ${f.is_diff}, is_verbose: ${f.is_verbose},' +
|
|
|
|
' is_all: ${f.is_all}, is_worker: ${f.is_worker}, is_debug: ${f.is_debug}, is_noerror: ${f.is_noerror},' +
|
|
|
|
' is_verify: ${f.is_verify}" }'
|
2019-12-27 19:59:04 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 16:54:17 +03:00
|
|
|
fn file_to_mod_name_and_is_module_file(file string) (string, bool) {
|
2020-04-23 02:16:58 +03:00
|
|
|
mut mod_name := 'main'
|
|
|
|
mut is_module_file := false
|
2020-12-27 18:15:26 +03:00
|
|
|
flines := read_source_lines(file) or { return mod_name, is_module_file }
|
2019-12-27 19:59:04 +03:00
|
|
|
for fline in flines {
|
|
|
|
line := fline.trim_space()
|
|
|
|
if line.starts_with('module ') {
|
|
|
|
if !line.starts_with('module main') {
|
|
|
|
is_module_file = true
|
|
|
|
mod_name = line.replace('module ', ' ').trim_space()
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-04-16 16:54:17 +03:00
|
|
|
return mod_name, is_module_file
|
2019-12-24 05:43:31 +03:00
|
|
|
}
|
2020-01-11 01:33:35 +03:00
|
|
|
|
2023-03-02 16:49:50 +03:00
|
|
|
fn read_source_lines(file string) ![]string {
|
2022-11-15 16:53:13 +03:00
|
|
|
source_lines := os.read_lines(file) or { return error('can not read ${file}') }
|
2020-02-13 01:19:45 +03:00
|
|
|
return source_lines
|
2020-01-11 01:33:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_compile_name_of_potential_v_project(file string) string {
|
|
|
|
// This function get_compile_name_of_potential_v_project returns:
|
|
|
|
// a) the file's folder, if file is part of a v project
|
|
|
|
// b) the file itself, if the file is a standalone v program
|
2020-03-20 18:41:18 +03:00
|
|
|
pfolder := os.real_path(os.dir(file))
|
2020-01-11 01:33:35 +03:00
|
|
|
// a .v project has many 'module main' files in one folder
|
|
|
|
// if there is only one .v file, then it must be a standalone
|
2021-03-01 02:18:14 +03:00
|
|
|
all_files_in_pfolder := os.ls(pfolder) or { panic(err) }
|
2020-04-26 14:49:31 +03:00
|
|
|
mut vfiles := []string{}
|
2020-01-11 01:33:35 +03:00
|
|
|
for f in all_files_in_pfolder {
|
2020-03-09 04:23:34 +03:00
|
|
|
vf := os.join_path(pfolder, f)
|
2020-01-11 01:33:35 +03:00
|
|
|
if f.starts_with('.') || !f.ends_with('.v') || os.is_dir(vf) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vfiles << vf
|
|
|
|
}
|
|
|
|
if vfiles.len == 1 {
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
// /////////////////////////////////////////////////////////////
|
|
|
|
// At this point, we know there are many .v files in the folder
|
|
|
|
// We will have to read them all, and if there are more than one
|
|
|
|
// containing `fn main` then the folder contains multiple standalone
|
|
|
|
// v programs. If only one contains `fn main` then the folder is
|
|
|
|
// a project folder, that should be compiled with `v pfolder`.
|
2020-04-23 02:16:58 +03:00
|
|
|
mut main_fns := 0
|
2020-01-11 01:33:35 +03:00
|
|
|
for f in vfiles {
|
2021-03-01 02:18:14 +03:00
|
|
|
slines := read_source_lines(f) or { panic(err) }
|
2020-01-11 01:33:35 +03:00
|
|
|
for line in slines {
|
|
|
|
if line.contains('fn main()') {
|
|
|
|
main_fns++
|
|
|
|
if main_fns > 1 {
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pfolder
|
|
|
|
}
|
2020-02-09 12:08:04 +03:00
|
|
|
|
2021-07-05 05:05:37 +03:00
|
|
|
[noreturn]
|
2020-04-16 16:54:17 +03:00
|
|
|
fn verror(s string) {
|
2020-04-03 18:38:41 +03:00
|
|
|
util.verror('vfmt error', s)
|
|
|
|
}
|