2020-02-28 15:02:56 +03:00
|
|
|
module main
|
|
|
|
|
2020-04-26 09:32:05 +03:00
|
|
|
import os
|
2020-10-15 22:39:59 +03:00
|
|
|
import os.cmdline
|
2020-04-26 09:32:05 +03:00
|
|
|
import v.pref
|
2021-01-18 10:33:33 +03:00
|
|
|
import v.util.recompilation
|
2020-02-28 15:02:56 +03:00
|
|
|
|
2021-01-28 07:26:28 +03:00
|
|
|
const is_debug = os.args.contains('-debug')
|
|
|
|
|
2020-02-28 15:02:56 +03:00
|
|
|
fn main() {
|
2020-02-28 18:04:22 +03:00
|
|
|
vexe := pref.vexe_path()
|
2020-03-08 00:26:26 +03:00
|
|
|
vroot := os.dir(vexe)
|
2021-01-18 10:33:33 +03:00
|
|
|
recompilation.must_be_enabled(vroot, 'Please install V from source, to use `v self` .')
|
2020-02-28 15:02:56 +03:00
|
|
|
os.chdir(vroot)
|
2020-05-08 19:04:24 +03:00
|
|
|
os.setenv('VCOLORS', 'always', true)
|
2020-04-24 13:36:27 +03:00
|
|
|
self_idx := os.args.index('self')
|
|
|
|
args := os.args[1..self_idx]
|
2020-10-15 22:39:59 +03:00
|
|
|
jargs := args.join(' ')
|
2020-10-16 15:05:57 +03:00
|
|
|
obinary := cmdline.option(args, '-o', '')
|
2020-12-10 19:17:25 +03:00
|
|
|
sargs := if obinary != '' { jargs } else { '$jargs -o v2' }
|
2020-10-15 22:39:59 +03:00
|
|
|
cmd := '$vexe $sargs cmd/v'
|
|
|
|
options := if args.len > 0 { '($sargs)' } else { '' }
|
2020-04-24 13:36:27 +03:00
|
|
|
println('V self compiling ${options}...')
|
2020-10-15 22:39:59 +03: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
|
|
|
|
}
|
2021-03-01 02:18:14 +03:00
|
|
|
backup_old_version_and_rename_newer() or { panic(err) }
|
2021-01-05 05:00:15 +03:00
|
|
|
println('V built successfully!')
|
2020-10-15 22:39:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn compile(vroot string, cmd string) {
|
2021-03-01 02:18:14 +03:00
|
|
|
result := os.exec(cmd) or { panic(err) }
|
2020-04-07 17:56:33 +03:00
|
|
|
if result.exit_code != 0 {
|
2020-10-15 22:39:59 +03:00
|
|
|
eprintln('cannot compile to `$vroot`: \n$result.output')
|
2020-02-28 18:04:22 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-04-07 17:56:33 +03:00
|
|
|
if result.output.len > 0 {
|
2020-07-14 18:45:44 +03:00
|
|
|
println(result.output.trim_space())
|
2020-04-07 17:56:33 +03:00
|
|
|
}
|
2020-10-15 22:39:59 +03:00
|
|
|
}
|
|
|
|
|
2021-01-28 07:26:28 +03:00
|
|
|
fn list_folder(bmessage string, message string) {
|
|
|
|
if !is_debug {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if bmessage != '' {
|
|
|
|
println(bmessage)
|
|
|
|
}
|
|
|
|
if os.user_os() == 'windows' {
|
|
|
|
os.system('dir v*.exe')
|
|
|
|
} else {
|
|
|
|
os.system('ls -lartd v*')
|
|
|
|
}
|
|
|
|
println(message)
|
|
|
|
}
|
|
|
|
|
2021-01-28 00:58:13 +03:00
|
|
|
fn backup_old_version_and_rename_newer() ?bool {
|
|
|
|
mut errors := []string{}
|
2021-01-28 01:45:38 +03:00
|
|
|
short_v_file := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
|
|
|
|
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 07:26:28 +03:00
|
|
|
|
|
|
|
list_folder('before:', 'removing $bak_file ...')
|
|
|
|
os.rm(bak_file) or { errors << 'failed removing $bak_file: $err' }
|
|
|
|
|
|
|
|
list_folder('', 'moving $v_file to $bak_file ...')
|
2021-01-28 00:58:13 +03:00
|
|
|
os.mv(v_file, bak_file) or { errors << err }
|
2021-01-28 07:26:28 +03:00
|
|
|
|
|
|
|
list_folder('', 'removing $v_file ...')
|
|
|
|
os.rm(v_file) or { }
|
|
|
|
|
|
|
|
list_folder('', 'moving $v2_file to $v_file ...')
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mv_by_cp(v2_file, v_file) or { panic(err) }
|
2021-01-28 07:26:28 +03:00
|
|
|
|
|
|
|
list_folder('after:', '')
|
|
|
|
|
2021-01-28 00:58:13 +03:00
|
|
|
if errors.len > 0 {
|
2021-01-28 01:45:38 +03:00
|
|
|
eprintln('backup errors:\n >> ' + errors.join('\n >> '))
|
2021-01-28 00:58:13 +03:00
|
|
|
}
|
|
|
|
return true
|
2020-02-28 15:02:56 +03:00
|
|
|
}
|