mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
move vfmt frontend program to tools/vfmt.v
This commit is contained in:
committed by
Alexander Medvednikov
parent
28594a65a8
commit
42b1660c7e
@@ -1132,32 +1132,6 @@ pub fn env_vflags_and_os_args() []string {
|
||||
return non_empty(args)
|
||||
}
|
||||
|
||||
pub fn vfmt(args []string) {
|
||||
file := args.last()
|
||||
if !os.exists(file) {
|
||||
println('"$file" does not exist')
|
||||
exit(1)
|
||||
}
|
||||
if !file.ends_with('.v') {
|
||||
println('v fmt can only be used on .v files')
|
||||
exit(1)
|
||||
}
|
||||
vexe := vexe_path()
|
||||
// launch_tool('vfmt', '-d vfmt')
|
||||
vroot := os.dir(vexe)
|
||||
os.chdir(vroot)
|
||||
println('building vfmt... (it will be cached soon)')
|
||||
ret := os.system('$vexe -o $vroot/tools/vfmt -d vfmt v.v')
|
||||
if ret != 0 {
|
||||
println('err')
|
||||
return
|
||||
}
|
||||
println('running vfmt...')
|
||||
os.exec('$vroot/tools/vfmt $file')or{
|
||||
panic(err)
|
||||
}
|
||||
// if !os.exists('
|
||||
}
|
||||
|
||||
pub fn create_symlink() {
|
||||
$if windows {
|
||||
|
||||
@@ -240,7 +240,6 @@ fn (p mut Parser) fremove_last() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
[if vfmt]
|
||||
fn (p &Parser) gen_fmt() {
|
||||
if p.pass != .main {
|
||||
@@ -250,6 +249,11 @@ fn (p &Parser) gen_fmt() {
|
||||
if p.file_name == '' {
|
||||
return
|
||||
}
|
||||
is_all := os.getenv('VFMT_OPTION_ALL') == 'yes'
|
||||
if p.file_path != p.v.dir && !is_all {
|
||||
// skip everything except the last file (given by the CLI argument)
|
||||
return
|
||||
}
|
||||
//s := p.scanner.fmt_out.str().replace('\n\n\n', '\n').trim_space()
|
||||
//s := p.scanner.fmt_out.str().trim_space()
|
||||
//p.scanner.fgenln('// nice')
|
||||
@@ -260,29 +264,40 @@ fn (p &Parser) gen_fmt() {
|
||||
') or{', ') or {',
|
||||
])
|
||||
*/
|
||||
//.replace('\n\n\n\n', '\n\n')
|
||||
.replace_each([
|
||||
' \n', '\n',
|
||||
') or{', ') or {',
|
||||
')or{', ') or {',
|
||||
] )
|
||||
|
||||
//.replace('\n\n\n\n', '\n\n')
|
||||
.replace_each([
|
||||
' \n', '\n',
|
||||
') or{', ') or {',
|
||||
')or{', ') or {',
|
||||
])
|
||||
|
||||
if s == '' {
|
||||
return
|
||||
}
|
||||
//files := ['get_type.v']
|
||||
if p.file_path.contains('vfmt') {return}
|
||||
if p.file_path.contains('compiler/vfmt.v') {return}
|
||||
//if !(p.file_name in files) { return }
|
||||
path := os.tmpdir() + '/' + p.file_name
|
||||
println('generating ${path}')
|
||||
mut out := os.create(path) or {
|
||||
verror('failed to create os_nix.v')
|
||||
return
|
||||
if is_all {
|
||||
if p.file_path.len > 0 {
|
||||
path := write_formatted_source( p.file_name, s )
|
||||
os.cp( path, p.file_path ) or { panic(err) }
|
||||
eprintln('Written fmt file to: $p.file_path')
|
||||
}
|
||||
}
|
||||
if p.file_path == p.v.dir {
|
||||
res_path := write_formatted_source( p.file_name, s )
|
||||
os.setenv('VFMT_FILE_RESULT', res_path, true )
|
||||
}
|
||||
println('replacing ${p.file_path}...\n')
|
||||
out.writeln(s.trim_space())//p.scanner.fmt_out.str().trim_space())
|
||||
out.writeln('')
|
||||
out.close()
|
||||
os.mv(path, p.file_path)
|
||||
}
|
||||
|
||||
fn write_formatted_source(file_name string, s string) string {
|
||||
path := os.tmpdir() + '/' + file_name
|
||||
mut out := os.create(path) or {
|
||||
verror('failed to create file $path')
|
||||
return ''
|
||||
}
|
||||
//eprintln('replacing ${p.file_path} ...\n')
|
||||
out.writeln(s.trim_space())//p.scanner.fmt_out.str().trim_space())
|
||||
out.close()
|
||||
return path
|
||||
}
|
||||
|
||||
@@ -3,22 +3,25 @@ module compiler
|
||||
import os
|
||||
|
||||
pub fn launch_tool(tname string) {
|
||||
is_verbose := '-verbose' in os.args || '--verbose' in os.args
|
||||
vexe := vexe_path()
|
||||
vroot := os.dir(vexe)
|
||||
mut oargs := os.args
|
||||
oargs[0] = '"$vexe"' // make it more explicit
|
||||
set_vroot_folder( vroot ) // needed by tools to find back v
|
||||
tool_args := os.args[1..].join(' ')
|
||||
tool_exe := os.realpath('$vroot/tools/$tname')
|
||||
tool_source := os.realpath('$vroot/tools/${tname}.v')
|
||||
// ////////////////////////////////////////////////////
|
||||
tool_args := oargs.join(' ')
|
||||
tool_command := '"$tool_exe" $tool_args'
|
||||
// println('Launching: "$tool_command" ...')
|
||||
if is_verbose {
|
||||
eprintln('launch_tool vexe : $vroot')
|
||||
eprintln('launch_tool vroot : $vroot')
|
||||
eprintln('launch_tool tool_args : $tool_args')
|
||||
eprintln('launch_tool tool_command: $tool_command')
|
||||
}
|
||||
mut tool_should_be_recompiled := false
|
||||
if !os.exists(tool_exe) {
|
||||
// fresh checkout
|
||||
tool_should_be_recompiled = true
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if os.file_last_mod_unix(tool_exe) <= os.file_last_mod_unix(vexe) {
|
||||
// v was recompiled, maybe after v up ...
|
||||
// rebuild the tool too just in case
|
||||
@@ -29,16 +32,26 @@ pub fn launch_tool(tname string) {
|
||||
tool_should_be_recompiled = true
|
||||
}
|
||||
}
|
||||
|
||||
if is_verbose {
|
||||
eprintln('launch_tool tool_should_be_recompiled: $tool_should_be_recompiled')
|
||||
}
|
||||
|
||||
if tool_should_be_recompiled {
|
||||
compilation_command := '"$vexe" "$tool_source"'
|
||||
// println('Compiling $tname with: "$compilation_command"')
|
||||
tool_compilation := os.exec(compilation_command)or{
|
||||
panic(err)
|
||||
mut compilation_options := ''
|
||||
if tname == 'vfmt' { compilation_options = '-d vfmt' }
|
||||
compilation_command := '"$vexe" $compilation_options "$tool_source"'
|
||||
if is_verbose {
|
||||
eprintln('Compiling $tname with: "$compilation_command"')
|
||||
}
|
||||
tool_compilation := os.exec(compilation_command) or { panic(err) }
|
||||
if tool_compilation.exit_code != 0 {
|
||||
panic('V tool "$tool_source" could not be compiled\n' + tool_compilation.output)
|
||||
}
|
||||
}
|
||||
exit(os.system(tool_command))
|
||||
if is_verbose {
|
||||
eprintln('launch_tool running tool command: $tool_command ...')
|
||||
}
|
||||
|
||||
exit( os.system(tool_command) )
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user