2020-02-28 13:02:56 +01:00
|
|
|
module main
|
|
|
|
|
2020-04-26 08:32:05 +02:00
|
|
|
import os
|
2020-10-15 21:39:59 +02:00
|
|
|
import os.cmdline
|
2020-04-26 08:32:05 +02:00
|
|
|
import v.pref
|
2020-02-28 13:02:56 +01:00
|
|
|
|
|
|
|
fn main() {
|
2020-02-28 16:04:22 +01:00
|
|
|
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-05-08 18:04:24 +02:00
|
|
|
os.setenv('VCOLORS', 'always', true)
|
2020-04-24 12:36:27 +02:00
|
|
|
self_idx := os.args.index('self')
|
|
|
|
args := os.args[1..self_idx]
|
2020-10-15 21:39:59 +02:00
|
|
|
jargs := args.join(' ')
|
2020-10-16 14:05:57 +02:00
|
|
|
obinary := cmdline.option(args, '-o', '')
|
2020-10-15 21:39:59 +02:00
|
|
|
sargs := if obinary != '' { jargs } else { '$jargs -o v2 ' }
|
|
|
|
cmd := '$vexe $sargs cmd/v'
|
|
|
|
options := if args.len > 0 { '($sargs)' } else { '' }
|
2020-04-24 12:36:27 +02:00
|
|
|
println('V self compiling ${options}...')
|
2020-10-15 21:39:59 +02:00
|
|
|
compile(vroot, cmd)
|
|
|
|
if obinary != '' {
|
|
|
|
// When -o was given, there is no need to backup/rename the original.
|
|
|
|
// The user just wants an independent copy of v, and so we are done.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
backup_old_version_and_rename_newer()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile(vroot string, cmd string) {
|
2020-10-15 15:17:52 +02:00
|
|
|
result := os.exec(cmd) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-04-07 16:56:33 +02:00
|
|
|
if result.exit_code != 0 {
|
2020-10-15 21:39:59 +02:00
|
|
|
eprintln('cannot compile to `$vroot`: \n$result.output')
|
2020-02-28 16:04:22 +01:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-04-07 16:56:33 +02:00
|
|
|
if result.output.len > 0 {
|
2020-07-14 17:45:44 +02:00
|
|
|
println(result.output.trim_space())
|
2020-04-07 16:56:33 +02:00
|
|
|
}
|
2020-10-15 21:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn backup_old_version_and_rename_newer() {
|
2020-02-28 16:04:22 +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' }
|
|
|
|
if os.exists(bak_file) {
|
|
|
|
os.rm(bak_file)
|
2020-02-28 13:02:56 +01:00
|
|
|
}
|
2020-02-28 16:04:22 +01:00
|
|
|
os.mv(v_file, bak_file)
|
|
|
|
os.mv(v2_file, v_file)
|
2020-02-28 13:02:56 +01:00
|
|
|
}
|