2019-12-23 11:22:06 +01:00
|
|
|
module main
|
|
|
|
|
2020-04-26 08:32:05 +02:00
|
|
|
import os
|
|
|
|
import v.pref
|
2021-07-27 11:35:54 +02:00
|
|
|
import v.util.version
|
2021-01-18 08:33:33 +01:00
|
|
|
import v.util.recompilation
|
2019-11-01 11:14:59 +01:00
|
|
|
|
2020-06-08 13:45:51 +02:00
|
|
|
struct App {
|
|
|
|
is_verbose bool
|
2021-02-23 09:05:59 +01:00
|
|
|
is_prod bool
|
2020-06-08 13:45:51 +02:00
|
|
|
vexe string
|
|
|
|
vroot string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_app() App {
|
2020-08-25 18:17:41 +02:00
|
|
|
vexe := os.real_path(pref.vexe_path())
|
2021-01-18 08:33:33 +01:00
|
|
|
vroot := os.dir(vexe)
|
2020-06-08 13:45:51 +02:00
|
|
|
return App{
|
|
|
|
is_verbose: '-v' in os.args
|
2021-02-23 09:05:59 +01:00
|
|
|
is_prod: '-prod' in os.args
|
2020-06-08 13:45:51 +02:00
|
|
|
vexe: vexe
|
2021-01-18 08:33:33 +01:00
|
|
|
vroot: vroot
|
2020-06-08 13:45:51 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 16:56:33 +02:00
|
|
|
|
2020-06-08 13:45:51 +02:00
|
|
|
fn main() {
|
|
|
|
app := new_app()
|
2021-01-18 08:33:33 +01:00
|
|
|
recompilation.must_be_enabled(app.vroot, 'Please install V from source, to use `v up` .')
|
2021-08-28 11:34:10 +02:00
|
|
|
os.chdir(app.vroot) ?
|
2020-04-07 16:56:33 +02:00
|
|
|
println('Updating V...')
|
2020-09-17 01:00:56 +02:00
|
|
|
app.update_from_master()
|
2021-07-27 11:35:54 +02:00
|
|
|
v_hash := version.githash(false)
|
|
|
|
current_hash := version.githash(true)
|
2020-08-25 18:17:41 +02:00
|
|
|
// println(v_hash)
|
|
|
|
// println(current_hash)
|
|
|
|
if v_hash == current_hash {
|
2022-04-30 09:08:31 +02:00
|
|
|
println('V is already updated.')
|
2020-08-25 18:17:41 +02:00
|
|
|
app.show_current_v_version()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
app.backup('cmd/tools/vup.exe')
|
|
|
|
}
|
2022-04-30 09:08:31 +02:00
|
|
|
if !app.recompile_v() {
|
|
|
|
app.show_current_v_version()
|
|
|
|
eprintln('Recompiling V *failed*.')
|
|
|
|
eprintln('Try running `$get_make_cmd_name()` .')
|
|
|
|
exit(1)
|
|
|
|
}
|
2021-03-08 22:51:46 +01:00
|
|
|
app.recompile_vup()
|
2020-08-25 18:17:41 +02:00
|
|
|
app.show_current_v_version()
|
|
|
|
}
|
|
|
|
|
2021-01-27 22:58:13 +01:00
|
|
|
fn (app App) vprintln(s string) {
|
2020-08-25 18:54:27 +02:00
|
|
|
if app.is_verbose {
|
2021-01-27 22:58:13 +01:00
|
|
|
println(s)
|
2020-09-17 01:00:56 +02:00
|
|
|
}
|
2021-01-27 22:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (app App) update_from_master() {
|
|
|
|
app.vprintln('> updating from master ...')
|
2020-06-07 22:59:15 +02:00
|
|
|
if !os.exists('.git') {
|
|
|
|
// initialize as if it had been cloned
|
2020-06-08 13:45:51 +02:00
|
|
|
app.git_command('init')
|
|
|
|
app.git_command('remote add origin https://github.com/vlang/v')
|
|
|
|
app.git_command('fetch')
|
|
|
|
app.git_command('reset --hard origin/master')
|
|
|
|
app.git_command('clean --quiet -xdf --exclude v.exe --exclude cmd/tools/vup.exe')
|
2020-06-07 22:59:15 +02:00
|
|
|
} else {
|
|
|
|
// pull latest
|
2020-08-28 20:49:12 +02:00
|
|
|
app.git_command('pull https://github.com/vlang/v master')
|
2020-02-20 17:41:55 +01:00
|
|
|
}
|
2020-08-25 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
2022-04-30 09:08:31 +02:00
|
|
|
fn (app App) recompile_v() bool {
|
2022-03-06 18:01:22 +01:00
|
|
|
// Note: app.vexe is more reliable than just v (which may be a symlink)
|
2021-02-23 09:05:59 +01:00
|
|
|
opts := if app.is_prod { '-prod' } else { '' }
|
2022-01-22 20:13:16 +01:00
|
|
|
vself := '${os.quoted_path(app.vexe)} $opts self'
|
2021-01-27 22:58:13 +01:00
|
|
|
app.vprintln('> recompiling v itself with `$vself` ...')
|
2021-03-08 19:52:13 +01:00
|
|
|
self_result := os.execute(vself)
|
|
|
|
if self_result.exit_code == 0 {
|
|
|
|
println(self_result.output.trim_space())
|
2022-04-30 09:08:31 +02:00
|
|
|
return true
|
2021-03-08 19:52:13 +01:00
|
|
|
} else {
|
|
|
|
app.vprintln('`$vself` failed, running `make`...')
|
|
|
|
app.vprintln(self_result.output.trim_space())
|
2020-04-14 03:32:32 +02:00
|
|
|
}
|
2022-04-30 09:08:31 +02:00
|
|
|
return app.make(vself)
|
2020-08-25 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
2022-04-30 09:08:31 +02:00
|
|
|
fn (app App) recompile_vup() bool {
|
2022-01-22 20:13:16 +01:00
|
|
|
vup_result := os.execute('${os.quoted_path(app.vexe)} -g cmd/tools/vup.v')
|
2021-03-08 22:51:46 +01:00
|
|
|
if vup_result.exit_code != 0 {
|
|
|
|
eprintln('recompiling vup.v failed:')
|
|
|
|
eprintln(vup_result.output)
|
2022-04-30 09:08:31 +02:00
|
|
|
return false
|
2021-03-08 22:51:46 +01:00
|
|
|
}
|
2022-04-30 09:08:31 +02:00
|
|
|
return true
|
2021-03-08 22:51:46 +01:00
|
|
|
}
|
|
|
|
|
2022-04-30 09:08:31 +02:00
|
|
|
fn (app App) make(vself string) bool {
|
|
|
|
make := get_make_cmd_name()
|
2021-03-08 22:51:46 +01:00
|
|
|
make_result := os.execute(make)
|
|
|
|
if make_result.exit_code != 0 {
|
|
|
|
eprintln('> $make failed:')
|
|
|
|
eprintln('> make output:')
|
|
|
|
eprintln(make_result.output)
|
2022-04-30 09:08:31 +02:00
|
|
|
return false
|
2021-03-08 22:51:46 +01:00
|
|
|
}
|
2021-01-27 22:58:13 +01:00
|
|
|
app.vprintln(make_result.output)
|
2022-04-30 09:08:31 +02:00
|
|
|
return true
|
2020-05-16 21:16:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 13:45:51 +02:00
|
|
|
fn (app App) show_current_v_version() {
|
2022-01-22 20:13:16 +01:00
|
|
|
vout := os.execute('${os.quoted_path(app.vexe)} version')
|
2021-03-08 19:52:13 +01:00
|
|
|
if vout.exit_code >= 0 {
|
2020-10-30 14:52:40 +01:00
|
|
|
mut vversion := vout.output.trim_space()
|
|
|
|
if vout.exit_code == 0 {
|
|
|
|
latest_v_commit := vversion.split(' ').last().all_after('.')
|
2021-03-08 19:52:13 +01:00
|
|
|
latest_v_commit_time := os.execute('git show -s --format=%ci $latest_v_commit')
|
|
|
|
if latest_v_commit_time.exit_code == 0 {
|
|
|
|
vversion += ', timestamp: ' + latest_v_commit_time.output.trim_space()
|
2020-10-30 14:52:40 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-30 09:08:31 +02:00
|
|
|
println('Current V version: $vversion')
|
2020-10-30 14:52:40 +01:00
|
|
|
}
|
2020-04-22 19:54:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 13:45:51 +02:00
|
|
|
fn (app App) backup(file string) {
|
2020-04-22 19:54:39 +02:00
|
|
|
backup_file := '${file}_old.exe'
|
|
|
|
if os.exists(backup_file) {
|
2022-04-12 12:38:40 +02:00
|
|
|
os.rm(backup_file) or { eprintln('failed removing $backup_file: $err.msg()') }
|
2020-04-22 19:54:39 +02:00
|
|
|
}
|
2022-04-12 12:38:40 +02:00
|
|
|
os.mv(file, backup_file) or { eprintln('failed moving $file: $err.msg()') }
|
2019-11-01 11:14:59 +01:00
|
|
|
}
|
2020-06-07 22:59:15 +02:00
|
|
|
|
2020-06-08 13:45:51 +02:00
|
|
|
fn (app App) git_command(command string) {
|
2021-01-27 22:58:13 +01:00
|
|
|
app.vprintln('git_command: git $command')
|
2021-03-08 19:52:13 +01:00
|
|
|
git_result := os.execute('git $command')
|
|
|
|
if git_result.exit_code < 0 {
|
2021-01-30 11:53:57 +01:00
|
|
|
app.get_git()
|
|
|
|
// Try it again with (maybe) git installed
|
2021-07-20 13:04:35 +02:00
|
|
|
os.execute_or_exit('git $command')
|
2021-01-30 11:53:57 +01:00
|
|
|
}
|
2020-06-07 22:59:15 +02:00
|
|
|
if git_result.exit_code != 0 {
|
2021-01-27 22:58:13 +01:00
|
|
|
eprintln(git_result.output)
|
2020-06-07 22:59:15 +02:00
|
|
|
exit(1)
|
|
|
|
}
|
2021-01-27 22:58:13 +01:00
|
|
|
app.vprintln(git_result.output)
|
2020-06-07 22:59:15 +02:00
|
|
|
}
|
2021-01-30 11:53:57 +01:00
|
|
|
|
|
|
|
fn (app App) get_git() {
|
|
|
|
$if windows {
|
|
|
|
println('Downloading git 32 bit for Windows, please wait.')
|
|
|
|
// We'll use 32 bit because maybe someone out there is using 32-bit windows
|
2021-03-08 19:52:13 +01:00
|
|
|
res_download := os.execute('bitsadmin.exe /transfer "vgit" https://github.com/git-for-windows/git/releases/download/v2.30.0.windows.2/Git-2.30.0.2-32-bit.exe "$os.getwd()/git32.exe"')
|
|
|
|
if res_download.exit_code != 0 {
|
2021-01-30 11:53:57 +01:00
|
|
|
eprintln('Unable to install git automatically: please install git manually')
|
2021-03-08 19:52:13 +01:00
|
|
|
panic(res_download.output)
|
2021-01-30 11:53:57 +01:00
|
|
|
}
|
2022-01-22 20:13:16 +01:00
|
|
|
res_git32 := os.execute(os.quoted_path(os.join_path_single(os.getwd(), 'git32.exe')))
|
2021-03-08 19:52:13 +01:00
|
|
|
if res_git32.exit_code != 0 {
|
2021-01-30 11:53:57 +01:00
|
|
|
eprintln('Unable to install git automatically: please install git manually')
|
2021-03-08 19:52:13 +01:00
|
|
|
panic(res_git32.output)
|
2021-01-30 11:53:57 +01:00
|
|
|
}
|
|
|
|
} $else { // Probably some kind of *nix, usually need to get using a package manager.
|
|
|
|
eprintln("error: Install `git` using your system's package manager")
|
|
|
|
}
|
|
|
|
}
|
2022-04-30 09:08:31 +02:00
|
|
|
|
|
|
|
fn get_make_cmd_name() string {
|
|
|
|
$if windows {
|
|
|
|
return 'make.bat'
|
|
|
|
} $else {
|
|
|
|
return 'make'
|
|
|
|
}
|
|
|
|
}
|