| 1 | import os |
| 2 | |
| 3 | const vexe = os.real_path(os.getenv_opt('VEXE') or { @VEXE }) |
| 4 | const symlink_usage = 'usage: v symlink [directory]' |
| 5 | |
| 6 | struct SymlinkOptions { |
| 7 | link_dir string |
| 8 | github_ci bool |
| 9 | } |
| 10 | |
| 11 | fn main() { |
| 12 | at_exit(|| os.rmdir_all(os.vtmp_dir()) or {}) or {} |
| 13 | options := parse_symlink_options(os.args[1..]) or { |
| 14 | println(symlink_usage) |
| 15 | exit(1) |
| 16 | } |
| 17 | if options.github_ci { |
| 18 | setup_symlink_github() |
| 19 | return |
| 20 | } |
| 21 | setup_symlink(options.link_dir) |
| 22 | } |
| 23 | |
| 24 | fn parse_symlink_options(raw_args []string) !SymlinkOptions { |
| 25 | args := trim_symlink_command(raw_args) |
| 26 | if args.len == 0 { |
| 27 | return SymlinkOptions{} |
| 28 | } |
| 29 | if args == ['-githubci'] { |
| 30 | // TODO: [AFTER 2024-09-31] remove `-githubci` flag and function and only print usage and exit(1) . |
| 31 | if os.getenv('GITHUB_JOB') != '' { |
| 32 | println('::warning::Use `v symlink` instead of `v symlink -githubci`') |
| 33 | } |
| 34 | return SymlinkOptions{ |
| 35 | github_ci: true |
| 36 | } |
| 37 | } |
| 38 | if args.len == 1 { |
| 39 | return SymlinkOptions{ |
| 40 | link_dir: args[0] |
| 41 | } |
| 42 | } |
| 43 | return error(symlink_usage) |
| 44 | } |
| 45 | |
| 46 | fn trim_symlink_command(raw_args []string) []string { |
| 47 | if raw_args.len > 0 && raw_args[0] == 'symlink' { |
| 48 | return raw_args[1..] |
| 49 | } |
| 50 | return raw_args |
| 51 | } |
| 52 | |
| 53 | fn normalized_link_dir(custom_link_dir string) string { |
| 54 | if custom_link_dir != '' { |
| 55 | return os.expand_tilde_to_home(custom_link_dir) |
| 56 | } |
| 57 | return default_link_dir() |
| 58 | } |
| 59 | |
| 60 | fn setup_symlink_github() { |
| 61 | // We append V's install location (which should |
| 62 | // be the current directory) to the PATH environment variable. |
| 63 | |
| 64 | // Resources: |
| 65 | // 1. https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files |
| 66 | // 2. https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable |
| 67 | mut content := os.read_file(os.getenv('GITHUB_PATH')) or { |
| 68 | eprintln('The `GITHUB_PATH` env variable is not defined.') |
| 69 | eprintln(' This command: `v symlink -githubci` is intended to be used within GithubActions .yml files.') |
| 70 | eprintln(' It also needs to be run *as is*, *** without `sudo` ***, otherwise it will not work.') |
| 71 | eprintln(' For local usage, outside CIs, on !windows, prefer `sudo ./v symlink` .') |
| 72 | eprintln(' On windows, use `.\\v.exe symlink` instead.') |
| 73 | exit(1) |
| 74 | } |
| 75 | content += '\n${os.getwd()}\n' |
| 76 | os.write_file(os.getenv('GITHUB_PATH'), content) or { panic('Failed to write to GITHUB_PATH.') } |
| 77 | } |
| 78 | |