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
|
2021-01-18 08:33:33 +01:00
|
|
|
import v.util.recompilation
|
2020-02-28 13:02:56 +01:00
|
|
|
|
2021-01-28 05:26:28 +01:00
|
|
|
const is_debug = os.args.contains('-debug')
|
|
|
|
|
2020-02-28 13:02:56 +01:00
|
|
|
fn main() {
|
2022-01-10 14:45:28 +01:00
|
|
|
// support a renamed `v` executable too:
|
2020-02-28 16:04:22 +01:00
|
|
|
vexe := pref.vexe_path()
|
2020-03-07 22:26:26 +01:00
|
|
|
vroot := os.dir(vexe)
|
2022-01-10 14:45:28 +01:00
|
|
|
vexe_name := os.file_name(vexe)
|
|
|
|
short_v_name := vexe_name.all_before('.')
|
|
|
|
//
|
|
|
|
recompilation.must_be_enabled(vroot, 'Please install V from source, to use `$vexe_name self` .')
|
2021-08-28 11:44:03 +02:00
|
|
|
os.chdir(vroot) ?
|
2020-05-08 18:04:24 +02:00
|
|
|
os.setenv('VCOLORS', 'always', true)
|
2021-08-06 05:21:00 +02:00
|
|
|
args := os.args[1..].filter(it != 'self')
|
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-12-10 17:17:25 +01:00
|
|
|
sargs := if obinary != '' { jargs } else { '$jargs -o v2' }
|
2022-01-22 20:13:16 +01:00
|
|
|
cmd := '${os.quoted_path(vexe)} $sargs ${os.quoted_path('cmd/v')}'
|
2020-10-15 21:39:59 +02:00
|
|
|
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
|
|
|
|
}
|
2022-01-10 14:45:28 +01:00
|
|
|
backup_old_version_and_rename_newer(short_v_name) or { panic(err.msg) }
|
|
|
|
println('V built successfully as executable "$vexe_name".')
|
2020-10-15 21:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn compile(vroot string, cmd string) {
|
2021-07-20 13:04:35 +02:00
|
|
|
result := os.execute_or_exit(cmd)
|
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
|
|
|
}
|
|
|
|
|
2022-01-10 14:45:28 +01:00
|
|
|
fn list_folder(short_v_name string, bmessage string, message string) {
|
2021-01-28 05:26:28 +01:00
|
|
|
if !is_debug {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if bmessage != '' {
|
|
|
|
println(bmessage)
|
|
|
|
}
|
|
|
|
if os.user_os() == 'windows' {
|
2022-01-10 14:45:28 +01:00
|
|
|
os.system('dir $short_v_name*.exe')
|
2021-01-28 05:26:28 +01:00
|
|
|
} else {
|
2022-01-10 14:45:28 +01:00
|
|
|
os.system('ls -lartd $short_v_name*')
|
2021-01-28 05:26:28 +01:00
|
|
|
}
|
|
|
|
println(message)
|
|
|
|
}
|
|
|
|
|
2022-01-10 14:45:28 +01:00
|
|
|
fn backup_old_version_and_rename_newer(short_v_name string) ?bool {
|
2021-01-27 22:58:13 +01:00
|
|
|
mut errors := []string{}
|
2022-01-10 14:45:28 +01:00
|
|
|
short_v_file := if os.user_os() == 'windows' { '${short_v_name}.exe' } else { '$short_v_name' }
|
2021-01-27 23:45:38 +01:00
|
|
|
short_v2_file := if os.user_os() == 'windows' { 'v2.exe' } else { 'v2' }
|
|
|
|
short_bak_file := if os.user_os() == 'windows' { 'v_old.exe' } else { 'v_old' }
|
|
|
|
v_file := os.real_path(short_v_file)
|
|
|
|
v2_file := os.real_path(short_v2_file)
|
|
|
|
bak_file := os.real_path(short_bak_file)
|
2021-01-28 05:26:28 +01:00
|
|
|
|
2022-01-10 14:45:28 +01:00
|
|
|
list_folder(short_v_name, 'before:', 'removing $bak_file ...')
|
2021-04-08 06:27:56 +02:00
|
|
|
if os.exists(bak_file) {
|
|
|
|
os.rm(bak_file) or { errors << 'failed removing $bak_file: $err.msg' }
|
|
|
|
}
|
2021-01-28 05:26:28 +01:00
|
|
|
|
2022-01-10 14:45:28 +01:00
|
|
|
list_folder(short_v_name, '', 'moving $v_file to $bak_file ...')
|
2021-03-16 20:02:52 +01:00
|
|
|
os.mv(v_file, bak_file) or { errors << err.msg }
|
2021-01-28 05:26:28 +01:00
|
|
|
|
2022-01-10 14:45:28 +01:00
|
|
|
list_folder(short_v_name, '', 'removing $v_file ...')
|
2021-03-16 20:02:52 +01:00
|
|
|
os.rm(v_file) or {}
|
2021-01-28 05:26:28 +01:00
|
|
|
|
2022-01-10 14:45:28 +01:00
|
|
|
list_folder(short_v_name, '', 'moving $v2_file to $v_file ...')
|
2021-03-16 20:02:52 +01:00
|
|
|
os.mv_by_cp(v2_file, v_file) or { panic(err.msg) }
|
2021-01-28 05:26:28 +01:00
|
|
|
|
2022-01-10 14:45:28 +01:00
|
|
|
list_folder(short_v_name, 'after:', '')
|
2021-01-28 05:26:28 +01:00
|
|
|
|
2021-01-27 22:58:13 +01:00
|
|
|
if errors.len > 0 {
|
2021-01-27 23:45:38 +01:00
|
|
|
eprintln('backup errors:\n >> ' + errors.join('\n >> '))
|
2021-01-27 22:58:13 +01:00
|
|
|
}
|
|
|
|
return true
|
2020-02-28 13:02:56 +01:00
|
|
|
}
|