v/cmd/tools/vself.v

45 lines
936 B
V
Raw Normal View History

2020-02-28 13:02:56 +01:00
module main
import (
os
v.pref
)
fn main() {
vexe := pref.vexe_path()
2020-03-07 22:26:26 +01:00
vroot := os.dir(vexe)
2020-02-28 13:02:56 +01:00
os.chdir(vroot)
2020-03-16 15:46:38 +01:00
mut cmd := '$vexe -o v2 cmd/v'
if os.args.len >= 3 && os.args[2] == '-prod' {
cmd = '$vexe -o v2 -prod cmd/v'
2020-03-25 14:24:48 +01:00
println('V self compiling (-prod mode)...')
2020-03-16 15:46:38 +01:00
}
else {
2020-03-25 14:24:48 +01:00
println('V self compiling...')
2020-02-28 13:02:56 +01:00
}
2020-03-16 15:46:38 +01:00
result := os.exec(cmd) or { panic(err) }
if result.exit_code != 0 {
mut err := 'Permission denied'
if !result.output.contains('Permission denied') {
err = '\n$result.output'
}
eprintln('cannot compile to $vroot: $err')
exit(1)
}
if result.output.len > 0 {
println(result.output)
}
2020-03-16 15:46:38 +01:00
v_file := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
v2_file := if os.user_os() == 'windows' { 'v2.exe' } else { 'v2' }
bak_file := if os.user_os() == 'windows' { 'v_old.exe' } else { 'v_old' }
2020-03-16 15:46:38 +01:00
if os.exists(bak_file) {
os.rm(bak_file)
2020-02-28 13:02:56 +01:00
}
os.mv(v_file, bak_file)
os.mv(v2_file, v_file)
2020-02-28 13:02:56 +01:00
}