From 7585483c692b22a8510d81b69d8e83490fa1e1c7 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 26 May 2020 09:30:20 +0300 Subject: [PATCH] v symlink: extract to cmd/tools/vsymlink.v , use setx on windows --- cmd/tools/vsymlink.v | 36 +++++++++++++++++++++++++++ cmd/v/v.v | 58 ++++++++++---------------------------------- 2 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 cmd/tools/vsymlink.v diff --git a/cmd/tools/vsymlink.v b/cmd/tools/vsymlink.v new file mode 100644 index 0000000000..2350e53c91 --- /dev/null +++ b/cmd/tools/vsymlink.v @@ -0,0 +1,36 @@ +import os +import v.pref + +fn main(){ + vexe := pref.vexe_path() + $if windows { + vdir := os.real_path(os.dir(vexe)) + res := os.system('setx /M PATH "$vdir;%PATH%"') + if res == 0 { + println('v has been prepended to the path') + exit(0) + } + exit(1) + } + // + mut link_path := '/usr/local/bin/v' + mut ret := os.exec('ln -sf $vexe $link_path') or { + panic(err) + } + if ret.exit_code == 0 { + println('Symlink "$link_path" has been created') + } else if os.system("uname -o | grep -q \'[A/a]ndroid\'") == 0 { + println('Failed to create symlink "$link_path". Trying again with Termux path for Android.') + link_path = '/data/data/com.termux/files/usr/bin/v' + ret = os.exec('ln -sf $vexe $link_path') or { + panic(err) + } + if ret.exit_code == 0 { + println('Symlink "$link_path" has been created') + } else { + println('Failed to create symlink "$link_path". Try again with sudo.') + } + } else { + println('Failed to create symlink "$link_path". Try again with sudo.') + } +} diff --git a/cmd/v/v.v b/cmd/v/v.v index 81940103ad..2d80a84448 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -12,12 +12,14 @@ import v.util import v.builder const ( - simple_cmd = ['fmt', 'up' - 'self', 'test' - 'test-fmt', 'test-compiler', 'test-fixed', 'bin2v' - 'repl' - 'build-tools' - 'build-examples', 'build-vbinaries', 'setup-freetype' + simple_cmd = [ + 'fmt', 'up', + 'self', 'symlink', 'bin2v', + 'test', 'test-fmt', 'test-compiler', 'test-fixed', + 'repl', + 'build-tools', 'build-examples', + 'build-vbinaries', + 'setup-freetype' ] list_of_flags_that_allow_duplicates = ['cc', 'd', 'define', 'cf', 'cflags'] ) @@ -39,10 +41,10 @@ fn main_v() { } args_and_flags := util.join_env_vflags_and_os_args()[1..] prefs, command := pref.parse_args(args_and_flags) - //if prefs.is_verbose { - //println('command = "$command"') - //println(util.full_v_version(prefs.is_verbose)) - //} + // if prefs.is_verbose { + // println('command = "$command"') + // println(util.full_v_version(prefs.is_verbose)) + // } if args.len > 0 && (args[0] in ['version', '-V', '-version', '--version'] || (args[0] == '-v' && args.len == 1)) { // `-v` flag is for setting verbosity, but without any args it prints the version, like Clang @@ -86,16 +88,11 @@ fn main_v() { println(util.full_v_version(prefs.is_verbose)) return } - 'symlink' { - create_symlink() - return - } 'doc' { mut mod := '' if args.len == 1 { println('v doc [module]') - } - else if args.len > 1 { + } else if args.len > 1 { mod = args[1] } table := table.new_table() @@ -124,32 +121,3 @@ fn invoke_help_and_exit(remaining []string) { println('For usage information, use `v help`.') exit(1) } - -fn create_symlink() { - $if windows { - return - } - vexe := pref.vexe_path() - mut link_path := '/usr/local/bin/v' - mut ret := os.exec('ln -sf $vexe $link_path') or { - panic(err) - } - if ret.exit_code == 0 { - println('Symlink "$link_path" has been created') - } else if os.system("uname -o | grep -q \'[A/a]ndroid\'") == 0 { - println('Failed to create symlink "$link_path". Trying again with Termux path for Android.') - link_path = '/data/data/com.termux/files/usr/bin/v' - ret = os.exec('ln -sf $vexe $link_path') or { - panic(err) - } - if ret.exit_code == 0 { - println('Symlink "$link_path" has been created') - } else { - println('Failed to create symlink "$link_path". Try again with sudo.') - } - } else { - println('Failed to create symlink "$link_path". Try again with sudo.') - } -} - -