2020-05-26 08:30:20 +02:00
|
|
|
import os
|
|
|
|
import v.pref
|
2021-02-18 17:11:09 +01:00
|
|
|
import v.util
|
2020-05-26 08:30:20 +02:00
|
|
|
|
2020-06-19 12:54:56 +02:00
|
|
|
$if windows {
|
|
|
|
$if tinyc {
|
2021-06-14 14:42:56 +02:00
|
|
|
#flag -ladvapi32
|
|
|
|
#flag -luser32
|
2020-06-19 12:54:56 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-08 12:23:10 +01:00
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
fn main() {
|
2021-02-18 17:11:09 +01:00
|
|
|
C.atexit(cleanup_vtmp_folder)
|
2022-02-08 12:23:10 +01:00
|
|
|
|
|
|
|
if os.args.len > 3 {
|
|
|
|
print('usage: v symlink [OPTIONS]')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
ci_mode := '-githubci' in os.args
|
|
|
|
|
2020-10-22 16:28:58 +02:00
|
|
|
vexe := os.real_path(pref.vexe_path())
|
2022-02-08 12:23:10 +01:00
|
|
|
if ci_mode {
|
|
|
|
setup_symlink_github()
|
|
|
|
} else {
|
|
|
|
$if windows {
|
|
|
|
setup_symlink_windows(vexe)
|
|
|
|
} $else {
|
|
|
|
setup_symlink_unix(vexe)
|
|
|
|
}
|
2020-05-26 08:30:20 +02:00
|
|
|
}
|
2020-05-26 12:34:01 +02:00
|
|
|
}
|
|
|
|
|
2021-02-18 17:11:09 +01:00
|
|
|
fn cleanup_vtmp_folder() {
|
2021-03-09 15:16:42 +01:00
|
|
|
os.rmdir_all(util.get_vtmp_folder()) or {}
|
2021-02-18 17:11:09 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 12:23:10 +01:00
|
|
|
fn setup_symlink_github() {
|
|
|
|
// We append V's install location (which should
|
|
|
|
// be the current directory) to the PATH environment variable.
|
|
|
|
|
|
|
|
// Resources:
|
|
|
|
// 1. https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files
|
|
|
|
// 2. https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable
|
|
|
|
mut content := os.read_file(os.getenv('GITHUB_PATH')) or {
|
|
|
|
panic('Failed to read GITHUB_PATH.')
|
|
|
|
}
|
|
|
|
content += '\n$os.getwd()\n'
|
|
|
|
os.write_file(os.getenv('GITHUB_PATH'), content) or { panic('Failed to write to GITHUB_PATH.') }
|
|
|
|
}
|
|
|
|
|
2021-02-18 17:11:09 +01:00
|
|
|
fn setup_symlink_unix(vexe string) {
|
2021-02-22 12:08:41 +01:00
|
|
|
mut link_path := '/data/data/com.termux/files/usr/bin/v'
|
2021-06-03 00:22:14 +02:00
|
|
|
if !os.is_dir('/data/data/com.termux/files') {
|
2021-02-22 12:08:41 +01:00
|
|
|
link_dir := '/usr/local/bin'
|
|
|
|
if !os.exists(link_dir) {
|
2021-03-01 00:18:14 +01:00
|
|
|
os.mkdir_all(link_dir) or { panic(err) }
|
2021-02-22 12:08:41 +01:00
|
|
|
}
|
|
|
|
link_path = link_dir + '/v'
|
2020-10-16 09:55:55 +02:00
|
|
|
}
|
2021-07-12 13:23:25 +02:00
|
|
|
os.rm(link_path) or {}
|
|
|
|
os.symlink(vexe, link_path) or {
|
2020-10-22 16:28:58 +02:00
|
|
|
eprintln('Failed to create symlink "$link_path". Try again with sudo.')
|
2021-07-12 13:23:25 +02:00
|
|
|
exit(1)
|
2020-05-26 08:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-26 12:34:01 +02:00
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
fn setup_symlink_windows(vexe string) {
|
2020-06-08 09:19:31 +02:00
|
|
|
$if windows {
|
2020-07-16 18:33:26 +02:00
|
|
|
// Create a symlink in a new local folder (.\.bin\.v.exe)
|
|
|
|
// Puts `v` in %PATH% without polluting it with anything else (like make.bat).
|
|
|
|
// This will make `v` available on cmd.exe, PowerShell, and MinGW(MSYS)/WSL/Cygwin
|
2020-10-15 15:17:52 +02:00
|
|
|
vdir := os.real_path(os.dir(vexe))
|
|
|
|
vsymlinkdir := os.join_path(vdir, '.bin')
|
2020-07-16 18:33:26 +02:00
|
|
|
mut vsymlink := os.join_path(vsymlinkdir, 'v.exe')
|
2021-02-27 08:39:36 +01:00
|
|
|
// Remove old symlink first (v could have been moved, symlink rerun)
|
2020-07-16 18:33:26 +02:00
|
|
|
if !os.exists(vsymlinkdir) {
|
2021-03-01 00:18:14 +01:00
|
|
|
os.mkdir(vsymlinkdir) or { panic(err) }
|
2021-02-27 08:39:36 +01:00
|
|
|
} else {
|
|
|
|
if os.exists(vsymlink) {
|
2021-03-01 00:18:14 +01:00
|
|
|
os.rm(vsymlink) or { panic(err) }
|
2021-02-27 08:39:36 +01:00
|
|
|
} else {
|
|
|
|
vsymlink = os.join_path(vsymlinkdir, 'v.bat')
|
|
|
|
if os.exists(vsymlink) {
|
2021-03-01 00:18:14 +01:00
|
|
|
os.rm(vsymlink) or { panic(err) }
|
2021-02-27 08:39:36 +01:00
|
|
|
}
|
|
|
|
vsymlink = os.join_path(vsymlinkdir, 'v.exe')
|
|
|
|
}
|
2020-06-08 09:19:31 +02:00
|
|
|
}
|
2020-10-22 16:28:58 +02:00
|
|
|
// First, try to create a native symlink at .\.bin\v.exe
|
2020-07-16 18:33:26 +02:00
|
|
|
os.symlink(vsymlink, vexe) or {
|
|
|
|
// typically only fails if you're on a network drive (VirtualBox)
|
|
|
|
// do batch file creation instead
|
2020-10-22 16:28:58 +02:00
|
|
|
eprintln('Could not create a native symlink: $err')
|
2020-07-16 18:33:26 +02:00
|
|
|
eprintln('Creating a batch file instead...')
|
|
|
|
vsymlink = os.join_path(vsymlinkdir, 'v.bat')
|
|
|
|
if os.exists(vsymlink) {
|
2021-03-01 00:18:14 +01:00
|
|
|
os.rm(vsymlink) or { panic(err) }
|
2020-06-08 09:19:31 +02:00
|
|
|
}
|
2021-11-12 09:29:57 +01:00
|
|
|
os.write_file(vsymlink, '@echo off\n"$vexe" %*') or { panic(err) }
|
2020-10-22 16:28:58 +02:00
|
|
|
eprintln('$vsymlink file written.')
|
2020-07-16 18:33:26 +02:00
|
|
|
}
|
|
|
|
if !os.exists(vsymlink) {
|
|
|
|
warn_and_exit('Could not create $vsymlink')
|
2020-06-08 09:19:31 +02:00
|
|
|
}
|
2020-10-22 16:28:58 +02:00
|
|
|
println('Symlink $vsymlink to $vexe created.')
|
|
|
|
println('Checking system %PATH%...')
|
2020-10-15 15:17:52 +02:00
|
|
|
reg_sys_env_handle := get_reg_sys_env_handle() or {
|
2021-02-28 21:20:21 +01:00
|
|
|
warn_and_exit(err.msg)
|
2020-06-08 09:19:31 +02:00
|
|
|
return
|
|
|
|
}
|
2020-09-19 22:26:32 +02:00
|
|
|
// TODO: Fix defers inside ifs
|
|
|
|
// defer {
|
2020-10-15 15:17:52 +02:00
|
|
|
// C.RegCloseKey(reg_sys_env_handle)
|
2020-09-19 22:26:32 +02:00
|
|
|
// }
|
2020-07-04 19:52:25 +02:00
|
|
|
// if the above succeeded, and we cannot get the value, it may simply be empty
|
2021-01-26 15:43:10 +01:00
|
|
|
sys_env_path := get_reg_value(reg_sys_env_handle, 'Path') or { '' }
|
2020-06-08 09:19:31 +02:00
|
|
|
current_sys_paths := sys_env_path.split(os.path_delimiter).map(it.trim('/$os.path_separator'))
|
2020-10-15 15:17:52 +02:00
|
|
|
mut new_paths := [vsymlinkdir]
|
2020-06-08 09:19:31 +02:00
|
|
|
for p in current_sys_paths {
|
2020-10-22 16:28:58 +02:00
|
|
|
if p == '' {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-08 09:19:31 +02:00
|
|
|
if p !in new_paths {
|
|
|
|
new_paths << p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_sys_env_path := new_paths.join(';')
|
|
|
|
if new_sys_env_path == sys_env_path {
|
2020-10-22 16:28:58 +02:00
|
|
|
println('System %PATH% was already configured.')
|
2020-10-15 15:17:52 +02:00
|
|
|
} else {
|
2020-10-22 16:28:58 +02:00
|
|
|
println('System %PATH% was not configured.')
|
|
|
|
println('Adding symlink directory to system %PATH%...')
|
2020-06-08 09:19:31 +02:00
|
|
|
set_reg_value(reg_sys_env_handle, 'Path', new_sys_env_path) or {
|
2020-09-19 22:26:32 +02:00
|
|
|
C.RegCloseKey(reg_sys_env_handle)
|
2021-02-28 21:20:21 +01:00
|
|
|
warn_and_exit(err.msg)
|
2020-06-08 09:19:31 +02:00
|
|
|
}
|
2021-02-27 08:39:36 +01:00
|
|
|
println('Done.')
|
2020-06-08 09:19:31 +02:00
|
|
|
}
|
2020-10-22 16:28:58 +02:00
|
|
|
println('Notifying running processes to update their Environment...')
|
2020-06-08 09:19:31 +02:00
|
|
|
send_setting_change_msg('Environment') or {
|
2020-10-22 16:28:58 +02:00
|
|
|
eprintln(err)
|
2020-09-19 22:26:32 +02:00
|
|
|
C.RegCloseKey(reg_sys_env_handle)
|
2021-02-27 08:39:36 +01:00
|
|
|
warn_and_exit('You might need to run this again to have the `v` command in your %PATH%')
|
2020-06-08 09:19:31 +02:00
|
|
|
}
|
2020-09-19 22:26:32 +02:00
|
|
|
C.RegCloseKey(reg_sys_env_handle)
|
2021-02-27 08:39:36 +01:00
|
|
|
println('Done.')
|
|
|
|
println('Note: Restart your shell/IDE to load the new %PATH%.')
|
|
|
|
println('After restarting your shell/IDE, give `v version` a try in another directory!')
|
2020-05-26 12:34:01 +02:00
|
|
|
}
|
2020-06-08 09:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn warn_and_exit(err string) {
|
|
|
|
eprintln(err)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the system environment registry handle
|
|
|
|
fn get_reg_sys_env_handle() ?voidptr {
|
2020-07-16 18:33:26 +02:00
|
|
|
$if windows { // wrap for cross-compile compat
|
2020-06-08 09:19:31 +02:00
|
|
|
// open the registry key
|
2020-10-15 15:17:52 +02:00
|
|
|
reg_key_path := 'Environment'
|
|
|
|
reg_env_key := voidptr(0) // or HKEY (HANDLE)
|
2020-07-16 18:33:26 +02:00
|
|
|
if C.RegOpenKeyEx(os.hkey_current_user, reg_key_path.to_wide(), 0, 1 | 2, ®_env_key) != 0 {
|
2020-06-08 09:19:31 +02:00
|
|
|
return error('Could not open "$reg_key_path" in the registry')
|
|
|
|
}
|
|
|
|
return reg_env_key
|
2020-05-26 12:34:01 +02:00
|
|
|
}
|
2020-06-08 09:19:31 +02:00
|
|
|
return error('not on windows')
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a value from a given $key
|
|
|
|
fn get_reg_value(reg_env_key voidptr, key string) ?string {
|
|
|
|
$if windows {
|
|
|
|
// query the value (shortcut the sizing step)
|
2021-04-05 15:06:03 +02:00
|
|
|
reg_value_size := u32(4095) // this is the max length (not for the registry, but for the system %PATH%)
|
|
|
|
mut reg_value := unsafe { &u16(malloc(int(reg_value_size))) }
|
2021-03-09 15:16:42 +01:00
|
|
|
if C.RegQueryValueExW(reg_env_key, key.to_wide(), 0, 0, reg_value, ®_value_size) != 0 {
|
2021-02-27 08:39:36 +01:00
|
|
|
return error('Unable to get registry value for "$key".')
|
2020-05-26 12:34:01 +02:00
|
|
|
}
|
2021-02-15 16:15:52 +01:00
|
|
|
return unsafe { string_from_wide(reg_value) }
|
2020-05-26 12:34:01 +02:00
|
|
|
}
|
2020-06-08 09:19:31 +02:00
|
|
|
return error('not on windows')
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets the value for the given $key to the given $value
|
|
|
|
fn set_reg_value(reg_key voidptr, key string, value string) ?bool {
|
|
|
|
$if windows {
|
2021-03-09 15:16:42 +01:00
|
|
|
if C.RegSetValueExW(reg_key, key.to_wide(), 0, C.REG_EXPAND_SZ, value.to_wide(),
|
2021-02-27 08:39:36 +01:00
|
|
|
value.len * 2) != 0 {
|
|
|
|
return error('Unable to set registry value for "$key". %PATH% may be too long.')
|
2020-06-08 09:19:31 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return error('not on windows')
|
|
|
|
}
|
|
|
|
|
2020-07-16 18:33:26 +02:00
|
|
|
// Broadcasts a message to all listening windows (explorer.exe in particular)
|
2020-06-08 09:19:31 +02:00
|
|
|
// letting them know that the system environment has changed and should be reloaded
|
|
|
|
fn send_setting_change_msg(message_data string) ?bool {
|
|
|
|
$if windows {
|
2021-04-05 15:06:03 +02:00
|
|
|
if C.SendMessageTimeoutW(os.hwnd_broadcast, os.wm_settingchange, 0, unsafe { &u32(message_data.to_wide()) },
|
2021-02-18 17:11:09 +01:00
|
|
|
os.smto_abortifhung, 5000, 0) == 0 {
|
2020-06-08 09:19:31 +02:00
|
|
|
return error('Could not broadcast WM_SETTINGCHANGE')
|
|
|
|
}
|
|
|
|
return true
|
2020-05-26 12:34:01 +02:00
|
|
|
}
|
2020-06-08 09:19:31 +02:00
|
|
|
return error('not on windows')
|
2020-05-26 12:34:01 +02:00
|
|
|
}
|