├── .npmignore ├── .gitignore ├── swivm-exec ├── package.json ├── actions ├── install │ └── action.yml └── load │ └── action.yml ├── .github └── workflows │ └── test.yml ├── LICENSE ├── bash_completion ├── Makefile ├── README.md ├── install.sh └── swivm.sh /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | v* 3 | alias 4 | 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HEAD 2 | src 3 | v* 4 | alias 5 | 6 | # For testing 7 | test/bak 8 | .urchin.log 9 | .urchin_stdout 10 | 11 | node_modules/ 12 | npm-debug.log 13 | 14 | .DS_Store 15 | current 16 | -------------------------------------------------------------------------------- /swivm-exec: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | DIR="$(command cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 | 5 | # shellcheck disable=SC1090 6 | \. "$DIR/swivm.sh" --no-use 7 | 8 | if [ -n "$SWI_VERSION" ]; then 9 | swivm use "$SWI_VERSION" > /dev/null || exit 127 10 | elif ! swivm use >/dev/null 2>&1; then 11 | echo "No SWI_VERSION provided; no .swivmrc file found" >&2 12 | exit 127 13 | fi 14 | 15 | exec "$@" 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "swivm", 3 | "version": "1.3.2", 4 | "description": "SWI-Prolog Version Manager - Bash script to manage multiple active SWI-Prolog versions", 5 | "scripts": {}, 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/fnogatz/swivm.git" 9 | }, 10 | "keywords": [ 11 | "swivm", 12 | "swi", 13 | "prolog", 14 | "version", 15 | "manager" 16 | ], 17 | "author": "Falco Nogatz ", 18 | "license": "MIT", 19 | "homepage": "https://github.com/fnogatz/swivm", 20 | "devDependencies": { 21 | "replace": "^1.1.1", 22 | "semver": "^5.0.1" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /actions/install/action.yml: -------------------------------------------------------------------------------- 1 | name: Setup swivm 2 | description: Installs the SWI-Prolog version manager (swivm) 3 | author: fnogatz 4 | 5 | runs: 6 | using: "composite" 7 | 8 | steps: 9 | - name: Install prerequisites 10 | shell: bash 11 | run: | 12 | sudo apt-get -qq update 13 | sudo apt-get install -y build-essential autoconf curl chrpath pkg-config ncurses-dev libreadline-dev libedit-dev libgmp-dev libssl-dev unixodbc-dev zlib1g-dev libarchive-dev libossp-uuid-dev libxext-dev libice-dev libjpeg-dev libxinerama-dev libxft-dev libxpm-dev libxt-dev libdb-dev libpcre3-dev libyaml-dev make ninja-build cmake junit 14 | 15 | - name: Install Java 16 | uses: actions/setup-java@v3 17 | with: 18 | distribution: 'zulu' 19 | java-version: '8' 20 | 21 | - name: Install swivm 22 | shell: bash 23 | run: | 24 | rm -rf ~/.swivm 25 | git clone https://github.com/fnogatz/swivm.git ~/.swivm 26 | (cd ~/.swivm && git checkout `git describe --abbrev=0 --tags`) 27 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Automatic tests 2 | 3 | on: [push, pull_request, workflow_dispatch] 4 | 5 | jobs: 6 | test_install_swivm: 7 | name: Install swivm 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v3 13 | 14 | - name: Install swivm 15 | uses: ./actions/install 16 | 17 | - name: Print swivm version 18 | run: | 19 | source ~/.swivm/swivm.sh 20 | swivm --version 21 | 22 | test_load_swipl: 23 | name: Install swivm and load SWI-Prolog version 24 | runs-on: ubuntu-latest 25 | 26 | strategy: 27 | matrix: 28 | swipl: [devel, stable, 6, 7, 8, 9] 29 | 30 | steps: 31 | - name: Checkout 32 | uses: actions/checkout@v3 33 | 34 | - name: Install swivm and load SWI-Prolog 35 | uses: ./actions/load 36 | with: 37 | swi-prolog-version: ${{ matrix.swipl }} 38 | 39 | - name: Print SWI-Prolog version 40 | run: | 41 | source ~/.swivm/swivm.sh 42 | swipl --version 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2019 Falco Nogatz 4 | Copyright (c) 2010-2014 Tim Caswell 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | this software and associated documentation files (the "Software"), to deal in 8 | the Software without restriction, including without limitation the rights to 9 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | the Software, and to permit persons to whom the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /actions/load/action.yml: -------------------------------------------------------------------------------- 1 | name: Setup swivm and SWI-Prolog 2 | description: Installs the SWI-Prolog version manager (swivm) and a SWI-Prolog instance 3 | author: fnogatz 4 | 5 | inputs: 6 | swi-prolog-version: 7 | description: SWI-Prolog version to install 8 | required: false 9 | default: devel 10 | 11 | runs: 12 | using: "composite" 13 | 14 | steps: 15 | - name: Install prerequisites 16 | shell: bash 17 | run: | 18 | sudo apt-get -qq update 19 | sudo apt-get install -y build-essential autoconf curl chrpath pkg-config ncurses-dev libreadline-dev libedit-dev libgmp-dev libssl-dev unixodbc-dev zlib1g-dev libarchive-dev libossp-uuid-dev libxext-dev libice-dev libjpeg-dev libxinerama-dev libxft-dev libxpm-dev libxt-dev libdb-dev libpcre3-dev libyaml-dev make ninja-build cmake junit 20 | 21 | - name: Install Java 22 | uses: actions/setup-java@v3 23 | with: 24 | distribution: 'zulu' 25 | java-version: '8' 26 | 27 | - name: Install swivm 28 | shell: bash 29 | run: | 30 | rm -rf ~/.swivm 31 | git clone https://github.com/fnogatz/swivm.git ~/.swivm 32 | (cd ~/.swivm && git checkout `git describe --abbrev=0 --tags`) 33 | 34 | - name: Install SWI-Prolog 35 | shell: bash 36 | run: | 37 | source ~/.swivm/swivm.sh 38 | swivm install ${{ inputs.swi-prolog-version }} 39 | swivm use ${{ inputs.swi-prolog-version }} 40 | swipl --version 41 | -------------------------------------------------------------------------------- /bash_completion: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # bash completion for SWI-Prolog Version Manager (SWIVM) 4 | 5 | __swivm_generate_completion() 6 | { 7 | declare current_word 8 | current_word="${COMP_WORDS[COMP_CWORD]}" 9 | COMPREPLY=($(compgen -W "$1" -- "$current_word")) 10 | return 0 11 | } 12 | 13 | __swivm_commands () 14 | { 15 | declare current_word 16 | declare command 17 | 18 | current_word="${COMP_WORDS[COMP_CWORD]}" 19 | 20 | COMMANDS='\ 21 | help install uninstall use run exec \ 22 | alias unalias reinstall-packages \ 23 | current list ls list-remote ls-remote \ 24 | clear-cache deactivate unload \ 25 | version which' 26 | 27 | if [ ${#COMP_WORDS[@]} == 4 ]; then 28 | 29 | command="${COMP_WORDS[COMP_CWORD-2]}" 30 | case "${command}" in 31 | alias) __swivm_installed_nodes ;; 32 | esac 33 | 34 | else 35 | 36 | case "${current_word}" in 37 | -*) __swivm_options ;; 38 | *) __swivm_generate_completion "$COMMANDS" ;; 39 | esac 40 | 41 | fi 42 | } 43 | 44 | __swivm_options () 45 | { 46 | OPTIONS='' 47 | __swivm_generate_completion "$OPTIONS" 48 | } 49 | 50 | __swivm_installed_nodes () 51 | { 52 | __swivm_generate_completion "$(swivm_ls) $(__swivm_aliases)" 53 | } 54 | 55 | __swivm_aliases () 56 | { 57 | declare aliases 58 | aliases="" 59 | if [ -d $SWIVM_DIR/alias ]; then 60 | aliases="`cd $SWIVM_DIR/alias && command ls`" 61 | fi 62 | echo "${aliases}" 63 | } 64 | 65 | __swivm_alias () 66 | { 67 | __swivm_generate_completion "$(__swivm_aliases)" 68 | } 69 | 70 | __swivm () 71 | { 72 | declare previous_word 73 | previous_word="${COMP_WORDS[COMP_CWORD-1]}" 74 | 75 | case "$previous_word" in 76 | use|run|exec|ls|list|uninstall) __swivm_installed_nodes ;; 77 | alias|unalias) __swivm_alias ;; 78 | *) __swivm_commands ;; 79 | esac 80 | 81 | return 0 82 | } 83 | 84 | # complete is a bash builtin, but recent versions of ZSH come with a function 85 | # called bashcompinit that will create a complete in ZSH. If the user is in 86 | # ZSH, load and run bashcompinit before calling the complete function. 87 | if [[ -n ${ZSH_VERSION-} ]]; then 88 | autoload -U +X bashcompinit && bashcompinit 89 | fi 90 | 91 | complete -o default -o nospace -F __swivm swivm 92 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Since we rely on paths relative to the makefile location, abort if make isn't being run from there. 2 | $(if $(findstring /,$(MAKEFILE_LIST)),$(error Please only invoke this makefile from the directory it resides in)) 3 | # Add the local npm packages' bin folder to the PATH, so that `make` can find them, when invoked directly. 4 | # Note that rather than using `$(npm bin)` the 'node_modules/.bin' path component is hard-coded, so that invocation works even from an environment 5 | # where npm is (temporarily) unavailable due to having deactivated an nvm instance loaded into the calling shell in order to avoid interference with tests. 6 | export PATH := $(shell printf '%s' "$$PWD/node_modules/.bin:$$PATH") 7 | # The list of all supporting utilities, installed with `npm install`. 8 | UTILS := replace semver 9 | # Make sure that all required utilities can be located. 10 | UTIL_CHECK := $(or $(shell PATH="$(PATH)" which $(UTILS) >/dev/null && echo 'ok'),$(error Did you forget to run `npm install` after cloning the repo? At least one of the required supporting utilities not found: $(UTILS))) 11 | # The files that need updating when incrementing the version number. 12 | VERSIONED_FILES := swivm.sh install.sh README.md package.json 13 | 14 | 15 | # Default target (by virtue of being the first non '.'-prefixed in the file). 16 | .PHONY: _no-target-specified 17 | _no-target-specified: 18 | $(error Please specify the target to make - `make list` shows targets. Alternatively, use `npm test` to run the default tests; `npm run` shows all tests) 19 | 20 | # Lists all targets defined in this makefile. 21 | .PHONY: list 22 | list: 23 | @$(MAKE) -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort 24 | 25 | .PHONY: _ensure-tag 26 | _ensure-tag: 27 | ifndef TAG 28 | $(error Please invoke with `make TAG= release`, where is either an increment specifier (patch, minor, major, prepatch, preminor, premajor, prerelease), or an explicit major.minor.patch version number) 29 | endif 30 | 31 | # Ensures that the git workspace is clean. 32 | .PHONY: _ensure-clean 33 | _ensure-clean: 34 | @[ -z "$$(git status --porcelain --untracked-files=no || echo err)" ] || { echo "Workspace is not clean; please commit changes first." >&2; exit 2; } 35 | 36 | # Makes a release; invoke with `make TAG= release`. 37 | .PHONY: release 38 | release: _ensure-tag _ensure-clean 39 | @old_ver=`git describe --abbrev=0 --tags --match 'v[0-9]*.[0-9]*.[0-9]*'` || { echo "Failed to determine current version." >&2; exit 1; }; old_ver=$${old_ver#v}; \ 40 | new_ver=`echo "$(TAG)" | sed 's/^v//'`; new_ver=$${new_ver:-patch}; \ 41 | if printf "$$new_ver" | grep -q '^[0-9]'; then \ 42 | semver "$$new_ver" >/dev/null || { echo 'Invalid version number specified: $(TAG) - must be major.minor.patch' >&2; exit 2; }; \ 43 | semver -r "> $$old_ver" "$$new_ver" >/dev/null || { echo 'Invalid version number specified: $(TAG) - must be HIGHER than current one.' >&2; exit 2; } \ 44 | else \ 45 | new_ver=`semver -i "$$new_ver" "$$old_ver"` || { echo 'Invalid version-increment specifier: $(TAG)' >&2; exit 2; } \ 46 | fi; \ 47 | printf "=== Bumping version **$$old_ver** to **$$new_ver** before committing and tagging:\n=== TYPE 'proceed' TO PROCEED, anything else to abort: " && read response && [ "$$response" = 'proceed' ] || { echo 'Aborted.' >&2; exit 2; }; \ 48 | replace "$$old_ver" "$$new_ver" -- $(VERSIONED_FILES) && \ 49 | git commit -m "v$$new_ver" $(VERSIONED_FILES) && \ 50 | git tag -a -m "v$$new_ver" "v$$new_ver" 51 | 52 | release-github: 53 | @old_ver=`git describe --abbrev=0 --tags --match 'v[0-9]*.[0-9]*.[0-9]*'` || { echo "Failed to determine current version." >&2; exit 1; }; old_ver=$${old_ver#v} && \ 54 | hub release create -m "v$$old_ver" "v$$old_ver" 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SWI-Prolog Version Manager 2 | 3 | swivm, the SWI-Prolog Version Manager, is a bash script to manage multiple active SWI-Prolog versions. It provides a uniform command line interface to install and execute SWI-Prolog since version 5. Being an adapted fork of the [Node Version Manager nvm](https://github.com/nvm-sh/nvm) it has a very similar interface. 4 | 5 | ## Installation 6 | 7 | swivm does not support Windows. Make sure your system has the requirements for a manual SWI-Prolog installation from source. 8 | 9 | Typically, the following libraries are required: 10 | 11 | ```sh 12 | sudo apt-get install \ 13 | build-essential autoconf curl chrpath pkg-config \ 14 | ncurses-dev libreadline-dev libedit-dev \ 15 | libunwind-dev \ 16 | libgmp-dev \ 17 | libssl-dev \ 18 | unixodbc-dev \ 19 | zlib1g-dev libarchive-dev \ 20 | libossp-uuid-dev \ 21 | libxext-dev libice-dev libjpeg-dev libxinerama-dev libxft-dev \ 22 | libxpm-dev libxt-dev \ 23 | libdb-dev \ 24 | libpcre3-dev \ 25 | libyaml-dev \ 26 | openjdk-8-jdk junit \ 27 | make ninja-build \ 28 | cmake 29 | ``` 30 | 31 | If you want to reduce resources, the following packages are optional: 32 | 33 | - `openjdk-8-jdk junit`: Without, you do not have Java connectivity (JPL). 34 | - `unixodbc-dev`: Without, you have no ODBC database connectivity (e.g., MySQL) 35 | - `libssl-dev`: Without, you have no SSL (and HTTPS) support. 36 | - `libgmp-dev`: Without, you lack unbounded integer support, rational numbers, good random number generators, etc. 37 | - `libarchive-dev`: Without, you can not unpack and install add-ons. 38 | - `libpcre3-dev`: Without, you have no regular expression support ([library(pcre)](http://www.swi-prolog.org/pldoc/doc/_SWI_/library/pcre.pl)). 39 | - `libyaml-dev`: Without, you have no YAML support ([library(yaml)](http://www.swi-prolog.org/pldoc/doc/_SWI_/library/yaml.pl)). 40 | 41 | Additionally a Java compiler is required, so make sure `javac -version` is possible. 42 | 43 | Building SWI-Prolog v7.7.20+ requires cmake version 3.5 or later. 44 | 45 | ### Install script 46 | 47 | To install or update swivm, you can use the [install script](https://github.com/fnogatz/swivm/blob/v1.3.2/install.sh) using cURL: 48 | 49 | ```sh 50 | curl -o- https://raw.githubusercontent.com/fnogatz/swivm/v1.3.2/install.sh | bash 51 | ``` 52 | 53 | or Wget: 54 | 55 | ```sh 56 | wget -qO- https://raw.githubusercontent.com/fnogatz/swivm/v1.3.2/install.sh | bash 57 | ``` 58 | 59 | The script clones the swivm repository to `~/.swivm/` and adds the source line to your profile (`~/.bash_profile`, `~/.zshrc` or `~/.profile`). 60 | 61 | You can customize the install source, directory and profile using the `SWIVM_SOURCE`, `SWIVM_DIR`, and `PROFILE` variables. 62 | Eg: `curl ... | SWIVM_DIR=/usr/local/swivm bash` for a global install. 63 | 64 | _NB. The installer can use `git`, `curl`, or `wget` to download swivm, whatever is available._ 65 | 66 | ### Manual install 67 | 68 | For manual install create a folder somewhere in your filesystem with the `swivm.sh` file inside it. I put mine in `~/.swivm/`. 69 | 70 | Or if you have `git` installed, then just clone it, and check out the latest version: 71 | 72 | ```sh 73 | git clone https://github.com/fnogatz/swivm.git ~/.swivm && cd ~/.swivm && git checkout `git describe --abbrev=0 --tags` 74 | ``` 75 | 76 | To activate swivm, you need to source it from your shell: 77 | 78 | ```sh 79 | . ~/.swivm/swivm.sh 80 | ``` 81 | 82 | Add these lines to your `~/.bashrc`, `~/.profile`, or `~/.zshrc` file to have it automatically sourced upon login: 83 | 84 | ```sh 85 | export SWIVM_DIR="$HOME/.swivm" 86 | [ -s "$SWIVM_DIR/swivm.sh" ] && . "$SWIVM_DIR/swivm.sh" # This loads swivm 87 | ``` 88 | 89 | ## Usage 90 | 91 | You can create an `.swivmrc` file containing version number in the project root directory (or any parent directory). 92 | `swivm use`, `swivm install`, `swivm exec`, `swivm run`, and `swivm which` will all respect an `.swivmrc` file when a version is not supplied. 93 | 94 | To download, compile, and install the latest v8.2.x release of SWI-Prolog, do this: 95 | 96 | ```sh 97 | swivm install 8.2 98 | ``` 99 | 100 | And then in any new shell just use the installed version: 101 | 102 | ```sh 103 | swivm use 8.2 104 | ``` 105 | 106 | Or you can just run it: 107 | 108 | ```sh 109 | swivm run 8.2 --version 110 | ``` 111 | 112 | Or, you can run any arbitrary command in a subshell with the desired version of SWI-Prolog: 113 | 114 | ```sh 115 | swivm exec 8.2 swipl --version 116 | ``` 117 | 118 | You can also get the path to the executable to where it was installed: 119 | 120 | ```sh 121 | swivm which 8.2 122 | ``` 123 | 124 | In place of a version pointer like "6.2" or "v7.3" or "6.6.8", you can use the following special aliases with `swivm install`, `swivm use`, `swivm run`, `swivm exec`, `swivm which`, etc.: 125 | 126 | - `stable`: this alias points to the most recent SWI-Prolog version with an even minor version number. 127 | - `devel`: this alias points to the most recent SWI-Prolog version with an odd minor version number. 128 | 129 | If you want to use the system-installed version of SWI-Prolog, you can use the special default alias "system". The system version is this one not installed by swivm. If you have installed SWI-Prolog by, e.g., `apt-get install swi-prolog` or system-wide self-compiled, this will be the system version. 130 | 131 | ```sh 132 | swivm use system 133 | swivm run system --version 134 | ``` 135 | 136 | If you want to see what versions are installed: 137 | 138 | ```sh 139 | swivm ls 140 | ``` 141 | 142 | If you want to see what versions are available to install: 143 | 144 | ```sh 145 | swivm ls-remote 146 | ``` 147 | 148 | To restore your PATH, you can deactivate swivm: 149 | 150 | ```sh 151 | swivm deactivate 152 | ``` 153 | 154 | To set a default SWI-Prolog version to be used in any new shell, use the alias 'default': 155 | 156 | ```sh 157 | swivm alias default 8.2 158 | ``` 159 | 160 | ## Usage with GitHub Actions 161 | 162 | swivm provides two workflows for usage with [GitHub Actions](https://docs.github.com/en/actions/learn-github-actions): 163 | 164 | - `fnogatz/swivm/actions/install@main` just installs the latest version of swivm into `~/.swivm/`, so it can be used after `. ~/.swivm/swivm.sh`. 165 | - `fnogatz/swivm/actions/load@main` installs swivm as well as SWI-Prolog. Its version can be specified by the `swi-prolog-version` input value (default: `devel`). swivm and SWI-Prolog are available after `. ~/.swivm/swivm.sh`. 166 | 167 | Here is a non-exhaustive list of projects that use the GitHub Actions provided by swivm: 168 | 169 | - [tap](https://github.com/fnogatz/tap) 170 | 171 | Please open an issue if you want to have your project listed here. 172 | 173 | ## Known Problems 174 | 175 | If you try to install a SWI-Prolog version and the installation fails, be sure to delete the SWI-Prolog downloads from src (`~/.swivm/src/`) and versions (`~/.swivm/versions/`) or you might get an error when trying to reinstall them again. 176 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | { # this ensures the entire script is downloaded # 4 | 5 | swivm_has() { 6 | type "$1" > /dev/null 2>&1 7 | } 8 | 9 | if [ -z "$SWIVM_DIR" ]; then 10 | SWIVM_DIR="$HOME/.swivm" 11 | fi 12 | 13 | swivm_latest_version() { 14 | echo "v1.3.2" 15 | } 16 | 17 | # 18 | # Outputs the location to SWIVM depending on: 19 | # * The availability of $SWIVM_SOURCE 20 | # * The method used ("script" or "git" in the script, defaults to "git") 21 | # SWIVM_SOURCE always takes precedence unless the method is "script-swivm-exec" 22 | # 23 | swivm_source() { 24 | local SWIVM_METHOD 25 | SWIVM_METHOD="$1" 26 | local SWIVM_SOURCE_URL 27 | SWIVM_SOURCE_URL="$SWIVM_SOURCE" 28 | if [ "_$SWIVM_METHOD" = "_script-swivm-exec" ]; then 29 | SWIVM_SOURCE_URL="https://raw.githubusercontent.com/fnogatz/swivm/$(swivm_latest_version)/swivm-exec" 30 | elif [ -z "$SWIVM_SOURCE_URL" ]; then 31 | if [ "_$SWIVM_METHOD" = "_script" ]; then 32 | SWIVM_SOURCE_URL="https://raw.githubusercontent.com/fnogatz/swivm/$(swivm_latest_version)/swivm.sh" 33 | elif [ "_$SWIVM_METHOD" = "_git" ] || [ -z "$SWIVM_METHOD" ]; then 34 | SWIVM_SOURCE_URL="https://github.com/fnogatz/swivm.git" 35 | else 36 | echo >&2 "Unexpected value \"$SWIVM_METHOD\" for \$SWIVM_METHOD" 37 | return 1 38 | fi 39 | fi 40 | echo "$SWIVM_SOURCE_URL" 41 | } 42 | 43 | swivm_download() { 44 | if swivm_has "curl"; then 45 | curl -q $* 46 | elif swivm_has "wget"; then 47 | # Emulate curl with wget 48 | ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \ 49 | -e 's/-L //' \ 50 | -e 's/-I /--server-response /' \ 51 | -e 's/-s /-q /' \ 52 | -e 's/-o /-O /' \ 53 | -e 's/-C - /-c /') 54 | wget $ARGS 55 | fi 56 | } 57 | 58 | install_swivm_from_git() { 59 | if [ -d "$SWIVM_DIR/.git" ]; then 60 | echo "=> swivm is already installed in $SWIVM_DIR, trying to update using git" 61 | printf "\r=> " 62 | cd "$SWIVM_DIR" && (command git fetch 2> /dev/null || { 63 | echo >&2 "Failed to update swivm, run 'git fetch' in $SWIVM_DIR yourself." && exit 1 64 | }) 65 | else 66 | # Cloning to $SWIVM_DIR 67 | echo "=> Downloading swivm from git to '$SWIVM_DIR'" 68 | printf "\r=> " 69 | mkdir -p "$SWIVM_DIR" 70 | command git clone "$(swivm_source git)" "$SWIVM_DIR" 71 | fi 72 | cd "$SWIVM_DIR" && command git checkout --quiet $(swivm_latest_version) 73 | if [ ! -z "$(cd "$SWIVM_DIR" && git show-ref refs/heads/master)" ]; then 74 | if git branch --quiet 2>/dev/null; then 75 | cd "$SWIVM_DIR" && command git branch --quiet -D master >/dev/null 2>&1 76 | else 77 | echo >&2 "Your version of git is out of date. Please update it!" 78 | cd "$SWIVM_DIR" && command git branch -D master >/dev/null 2>&1 79 | fi 80 | fi 81 | return 82 | } 83 | 84 | install_swivm_as_script() { 85 | local SWIVM_SOURCE_LOCAL 86 | SWIVM_SOURCE_LOCAL=$(swivm_source script) 87 | local SWIVM_EXEC_SOURCE 88 | SWIVM_EXEC_SOURCE=$(swivm_source script-swivm-exec) 89 | 90 | # Downloading to $SWIVM_DIR 91 | mkdir -p "$SWIVM_DIR" 92 | if [ -d "$SWIVM_DIR/swivm.sh" ]; then 93 | echo "=> swivm is already installed in $SWIVM_DIR, trying to update the script" 94 | else 95 | echo "=> Downloading swivm as script to '$SWIVM_DIR'" 96 | fi 97 | swivm_download -s "$SWIVM_SOURCE_LOCAL" -o "$SWIVM_DIR/swivm.sh" || { 98 | echo >&2 "Failed to download '$SWIVM_SOURCE_LOCAL'" 99 | return 1 100 | } 101 | swivm_download -s "$SWIVM_EXEC_SOURCE" -o "$SWIVM_DIR/swivm-exec" || { 102 | echo >&2 "Failed to download '$SWIVM_EXEC_SOURCE'" 103 | return 2 104 | } 105 | chmod a+x "$SWIVM_DIR/swivm-exec" || { 106 | echo >&2 "Failed to mark '$SWIVM_DIR/swivm-exec' as executable" 107 | return 3 108 | } 109 | } 110 | 111 | # 112 | # Detect profile file if not specified as environment variable 113 | # (eg: PROFILE=~/.myprofile) 114 | # The echo'ed path is guaranteed to be an existing file 115 | # Otherwise, an empty string is returned 116 | # 117 | swivm_detect_profile() { 118 | 119 | local DETECTED_PROFILE 120 | DETECTED_PROFILE='' 121 | local SHELLTYPE 122 | SHELLTYPE="$(basename /$SHELL)" 123 | 124 | if [ $SHELLTYPE = "bash" ]; then 125 | if [ -f "$HOME/.bashrc" ]; then 126 | DETECTED_PROFILE="$HOME/.bashrc" 127 | elif [ -f "$HOME/.bash_profile" ]; then 128 | DETECTED_PROFILE="$HOME/.bash_profile" 129 | fi 130 | elif [ $SHELLTYPE = "zsh" ]; then 131 | DETECTED_PROFILE="$HOME/.zshrc" 132 | fi 133 | 134 | if [ -z $DETECTED_PROFILE ]; then 135 | if [ -f "$PROFILE" ]; then 136 | DETECTED_PROFILE="$PROFILE" 137 | elif [ -f "$HOME/.profile" ]; then 138 | DETECTED_PROFILE="$HOME/.profile" 139 | elif [ -f "$HOME/.bashrc" ]; then 140 | DETECTED_PROFILE="$HOME/.bashrc" 141 | elif [ -f "$HOME/.bash_profile" ]; then 142 | DETECTED_PROFILE="$HOME/.bash_profile" 143 | elif [ -f "$HOME/.zshrc" ]; then 144 | DETECTED_PROFILE="$HOME/.zshrc" 145 | fi 146 | fi 147 | 148 | if [ ! -z $DETECTED_PROFILE ]; then 149 | echo "$DETECTED_PROFILE" 150 | fi 151 | } 152 | 153 | swivm_do_install() { 154 | if [ -z "$METHOD" ]; then 155 | # Autodetect install method 156 | if swivm_has "git"; then 157 | install_swivm_from_git 158 | elif swivm_has "swivm_download"; then 159 | install_swivm_as_script 160 | else 161 | echo >&2 "You need git, curl, or wget to install swivm" 162 | exit 1 163 | fi 164 | elif [ "~$METHOD" = "~git" ]; then 165 | if ! swivm_has "git"; then 166 | echo >&2 "You need git to install swivm" 167 | exit 1 168 | fi 169 | install_swivm_from_git 170 | elif [ "~$METHOD" = "~script" ]; then 171 | if ! swivm_has "swivm_download"; then 172 | echo >&2 "You need curl or wget to install swivm" 173 | exit 1 174 | fi 175 | install_swivm_as_script 176 | fi 177 | 178 | echo 179 | 180 | local SWIVM_PROFILE 181 | SWIVM_PROFILE=$(swivm_detect_profile) 182 | 183 | SOURCE_STR="\nexport SWIVM_DIR=\"$SWIVM_DIR\"\n[ -s \"\$SWIVM_DIR/swivm.sh\" ] && . \"\$SWIVM_DIR/swivm.sh\" # This loads swivm" 184 | 185 | if [ -z "$SWIVM_PROFILE" ] ; then 186 | echo "=> Profile not found. Tried $SWIVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile." 187 | echo "=> Create one of them and run this script again" 188 | echo "=> Create it (touch $SWIVM_PROFILE) and run this script again" 189 | echo " OR" 190 | echo "=> Append the following lines to the correct file yourself:" 191 | printf "$SOURCE_STR" 192 | echo 193 | else 194 | if ! command grep -qc '/swivm.sh' "$SWIVM_PROFILE"; then 195 | echo "=> Appending source string to $SWIVM_PROFILE" 196 | printf "$SOURCE_STR\n" >> "$SWIVM_PROFILE" 197 | else 198 | echo "=> Source string already in $SWIVM_PROFILE" 199 | fi 200 | fi 201 | 202 | echo "=> Close and reopen your terminal to start using swivm" 203 | swivm_reset 204 | } 205 | 206 | # 207 | # Unsets the various functions defined 208 | # during the execution of the install script 209 | # 210 | swivm_reset() { 211 | unset -f swivm_reset swivm_has swivm_latest_version \ 212 | swivm_source swivm_download install_swivm_as_script install_swivm_from_git \ 213 | swivm_detect_profile swivm_do_install 214 | } 215 | 216 | [ "_$SWIVM_ENV" = "_testing" ] || swivm_do_install 217 | 218 | } # this ensures the entire script is downloaded # 219 | -------------------------------------------------------------------------------- /swivm.sh: -------------------------------------------------------------------------------- 1 | # SWI-Prolog Version Manager 2 | # Implemented as a POSIX-compliant function 3 | # Should work on sh, dash, bash, ksh, zsh 4 | # To use source this file from your bash profile 5 | # 6 | # Implemented by Falco Nogatz . 7 | # Based on the Node Version Manager (nvm), by Tim 8 | # Caswell and Matthew Ranney 9 | 10 | { # this ensures the entire script is downloaded # 11 | 12 | SWIVM_SCRIPT_SOURCE="$_" 13 | 14 | swivm_is_zsh() { 15 | [ -n "${ZSH_VERSION-}" ] 16 | } 17 | 18 | swivm_echo() { 19 | command printf %s\\n "$*" 2>/dev/null 20 | } 21 | 22 | swivm_cd() { 23 | \cd "$@" 24 | } 25 | 26 | swivm_err() { 27 | >&2 swivm_echo "$@" 28 | } 29 | 30 | swivm_grep() { 31 | GREP_OPTIONS='' command grep "$@" 32 | } 33 | 34 | swivm_has() { 35 | type "${1-}" > /dev/null 2>&1 36 | } 37 | 38 | swivm_is_alias() { 39 | # this is intentionally not "command alias" so it works in zsh. 40 | \alias "$1" > /dev/null 2>&1 41 | } 42 | 43 | swivm_has_colors() { 44 | local SWIVM_COLORS 45 | if swivm_has tput; then 46 | SWIVM_COLORS="$(tput -T "${TERM:-vt100}" colors)" 47 | fi 48 | [ "${SWIVM_COLORS:--1}" -ge 8 ] 49 | } 50 | 51 | swivm_download() { 52 | if swivm_has "curl"; then 53 | curl -q $* 54 | elif swivm_has "wget"; then 55 | # Emulate curl with wget 56 | ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \ 57 | -e 's/-L //' \ 58 | -e 's/-I /--server-response /' \ 59 | -e 's/-s /-q /' \ 60 | -e 's/-o /-O /' \ 61 | -e 's/-C - /-c /') 62 | eval wget $ARGS 63 | fi 64 | } 65 | 66 | swivm_has_system() { 67 | [ "$(swivm deactivate >/dev/null 2>&1 && command -v swipl)" != '' ] 68 | } 69 | 70 | swivm_is_version_installed() { 71 | [ -n "${1-}" ] && [ -x "$(swivm_version_path "$1" 2>/dev/null)"/bin/swipl ] 72 | } 73 | 74 | # Make zsh glob matching behave same as bash 75 | # This fixes the "zsh: no matches found" errors 76 | if swivm_has "unsetopt"; then 77 | unsetopt nomatch 2>/dev/null 78 | SWIVM_CD_FLAGS="-q" 79 | fi 80 | 81 | # Auto detect the SWIVM_DIR when not set 82 | if [ -z "$SWIVM_DIR" ]; then 83 | if [ -n "$BASH_SOURCE" ]; then 84 | SWIVM_SCRIPT_SOURCE="${BASH_SOURCE[0]}" 85 | fi 86 | SWIVM_DIR="$(cd $SWIVM_CD_FLAGS "$(dirname "${SWIVM_SCRIPT_SOURCE:-$0}")" > /dev/null && \pwd)" 87 | export SWIVM_DIR 88 | fi 89 | unset SWIVM_SCRIPT_SOURCE 2> /dev/null 90 | 91 | 92 | # Setup mirror location if not already set 93 | if [ -z "$SWIVM_MIRROR" ]; then 94 | export SWIVM_MIRROR="https://www.swi-prolog.org/download/" 95 | fi 96 | if [ -z "$GITHUB_MIRROR" ]; then 97 | export GITHUB_MIRROR="https://github.com/SWI-Prolog" 98 | fi 99 | 100 | swivm_tree_contains_path() { 101 | local tree 102 | tree="$1" 103 | local swi_path 104 | swi_path="$2" 105 | 106 | if [ "@$tree@" = "@@" ] || [ "@$swi_path@" = "@@" ]; then 107 | >&2 echo "both the tree and the SWI-Prolog path are required" 108 | return 2 109 | fi 110 | 111 | local pathdir 112 | pathdir=$(dirname "$swi_path") 113 | while [ "$pathdir" != "" ] && [ "$pathdir" != "." ] && [ "$pathdir" != "/" ] && [ "$pathdir" != "$tree" ]; do 114 | pathdir=$(dirname "$pathdir") 115 | done 116 | [ "$pathdir" = "$tree" ] 117 | } 118 | 119 | # Traverse up in directory tree to find containing folder 120 | swivm_find_up() { 121 | local path 122 | path=$PWD 123 | while [ "$path" != "" ] && [ ! -f "$path/$1" ]; do 124 | path=${path%/*} 125 | done 126 | echo "$path" 127 | } 128 | 129 | 130 | swivm_find_swivmrc() { 131 | local dir 132 | dir="$(swivm_find_up '.swivmrc')" 133 | if [ -e "$dir/.swivmrc" ]; then 134 | echo "$dir/.swivmrc" 135 | fi 136 | } 137 | 138 | # Obtain swivm version from rc file 139 | swivm_rc_version() { 140 | export SWIVM_RC_VERSION='' 141 | local SWIVMRC_PATH 142 | SWIVMRC_PATH="$(swivm_find_swivmrc)" 143 | if [ ! -e "${SWIVMRC_PATH}" ]; then 144 | swivm_err "No .swivmrc file found" 145 | return 1 146 | fi 147 | SWIVM_RC_VERSION="$(command head -n 1 "${SWIVMRC_PATH}" | command tr -d '\r')" || command printf '' 148 | if [ -z "${SWIVM_RC_VERSION}" ]; then 149 | swivm_err "Warning: empty .swivmrc file found at \"${SWIVMRC_PATH}\"" 150 | return 2 151 | fi 152 | swivm_echo "Found '${SWIVMRC_PATH}' with version <${SWIVM_RC_VERSION}>" 153 | } 154 | 155 | swivm_version_greater() { 156 | local LHS 157 | LHS="$(swivm_normalize_version "$1")" 158 | local RHS 159 | RHS="$(swivm_normalize_version "$2")" 160 | [ "$LHS" -gt "$RHS" ]; 161 | } 162 | 163 | swivm_version_greater_than_or_equal_to() { 164 | local LHS 165 | LHS="$(swivm_normalize_version "$1")" 166 | local RHS 167 | RHS="$(swivm_normalize_version "$2")" 168 | [ "$LHS" -ge "$RHS" ]; 169 | } 170 | 171 | swivm_version_dir() { 172 | local SWIVM_WHICH_DIR 173 | SWIVM_WHICH_DIR="$1" 174 | echo "$SWIVM_DIR/versions" 175 | } 176 | 177 | swivm_alias_path() { 178 | echo "$SWIVM_DIR/alias" 179 | } 180 | 181 | swivm_version_path() { 182 | local VERSION 183 | VERSION="$1" 184 | if [ -z "$VERSION" ]; then 185 | swivm_echo "version is required" >&2 186 | return 3 187 | else 188 | swivm_echo "$(swivm_version_dir)/$VERSION" 189 | fi 190 | } 191 | 192 | swivm_lib_path() { 193 | local VERSION_PATH 194 | VERSION_PATH="$1" 195 | swivm_echo "${VERSION_PATH}/lib/swipl/lib/x86_64-linux" 196 | } 197 | 198 | swivm_ensure_version_installed() { 199 | local PROVIDED_VERSION 200 | PROVIDED_VERSION="$1" 201 | local LOCAL_VERSION 202 | local EXIT_CODE 203 | LOCAL_VERSION="$(swivm_version "$PROVIDED_VERSION")" 204 | EXIT_CODE="$?" 205 | local SWIVM_VERSION_DIR 206 | if [ "_$EXIT_CODE" = "_0" ]; then 207 | SWIVM_VERSION_DIR="$(swivm_version_path "$LOCAL_VERSION")" 208 | fi 209 | if [ "_$EXIT_CODE" != "_0" ] || [ ! -d "$SWIVM_VERSION_DIR" ]; then 210 | VERSION="$(swivm_resolve_alias "$PROVIDED_VERSION")" 211 | if [ $? -eq 0 ]; then 212 | echo "N/A: version \"$PROVIDED_VERSION -> $VERSION\" is not yet installed" >&2 213 | else 214 | echo "N/A: version \"$PROVIDED_VERSION\" is not yet installed" >&2 215 | fi 216 | return 1 217 | fi 218 | } 219 | 220 | # Expand a version using the version cache 221 | swivm_version() { 222 | local PATTERN 223 | PATTERN="${1-}" 224 | local VERSION 225 | # The default version is the current one 226 | if [ -z "${PATTERN}" ]; then 227 | PATTERN='current' 228 | fi 229 | 230 | if [ "${PATTERN}" = "current" ]; then 231 | swivm_ls_current 232 | return $? 233 | fi 234 | 235 | VERSION="$(swivm_ls "${PATTERN}" | command tail -1)" 236 | if [ -z "${VERSION}" ] || [ "_${VERSION}" = "_N/A" ]; then 237 | swivm_echo "N/A" 238 | return 3 239 | fi 240 | swivm_echo "${VERSION}" 241 | } 242 | 243 | swivm_remote_version() { 244 | local PATTERN 245 | PATTERN="${1-}" 246 | local VERSION 247 | if swivm_validate_implicit_alias "${PATTERN}" 2>/dev/null; then 248 | case "${PATTERN}" in 249 | *) 250 | VERSION="$(swivm_ls_remote "${PATTERN}")" &&: 251 | ;; 252 | esac 253 | else 254 | VERSION="$(swivm_remote_versions "${PATTERN}" | command tail -1)" 255 | fi 256 | if [ -n "${SWIVM_VERSION_ONLY-}" ]; then 257 | command awk 'BEGIN { 258 | n = split(ARGV[1], a); 259 | print a[1] 260 | }' "${VERSION}" 261 | else 262 | swivm_echo "${VERSION}" 263 | fi 264 | if [ "${VERSION}" = 'N/A' ]; then 265 | return 3 266 | fi 267 | } 268 | 269 | swivm_remote_versions() { 270 | local PATTERN 271 | PATTERN="$1" 272 | 273 | if swivm_validate_implicit_alias "$PATTERN" 2> /dev/null ; then 274 | echo >&2 "Implicit aliases are not supported in swivm_remote_versions." 275 | return 1 276 | fi 277 | VERSIONS="$(echo "$(swivm_ls_remote "$PATTERN")" | command grep -v "N/A" | command sed '/^$/d')" 278 | 279 | if [ -z "$VERSIONS" ]; then 280 | echo "N/A" 281 | return 3 282 | else 283 | echo "$VERSIONS" 284 | fi 285 | } 286 | 287 | swivm_is_valid_version() { 288 | if swivm_validate_implicit_alias "${1-}" 2>/dev/null; then 289 | return 0 290 | fi 291 | case "${1-}" in 292 | *) 293 | local VERSION 294 | VERSION="${1-}" 295 | swivm_version_greater_than_or_equal_to "${VERSION}" 0 296 | ;; 297 | esac 298 | } 299 | 300 | swivm_normalize_version() { 301 | echo "${1#v}" | command awk -F. '{ printf("%d%06d%06d\n", $1,$2,$3); }' 302 | } 303 | 304 | swivm_ensure_version_prefix() { 305 | local SWIVM_VERSION 306 | SWIVM_VERSION="$(echo "${1-}" | command sed -e 's/^\([0-9]\)/v\1/g')" 307 | swivm_echo "${SWIVM_VERSION}" 308 | } 309 | 310 | swivm_format_version() { 311 | local VERSION 312 | VERSION="$(swivm_ensure_version_prefix "${1-}")" 313 | local NUM_GROUPS 314 | NUM_GROUPS="$(swivm_num_version_groups "${VERSION}")" 315 | if [ "${NUM_GROUPS}" -lt 3 ]; then 316 | swivm_format_version "${VERSION%.}.0" 317 | else 318 | swivm_echo "${VERSION}" | command cut -f1-3 -d. 319 | fi 320 | } 321 | 322 | swivm_num_version_groups() { 323 | local VERSION 324 | VERSION="$1" 325 | VERSION="${VERSION#v}" 326 | VERSION="${VERSION%.}" 327 | if [ -z "$VERSION" ]; then 328 | echo "0" 329 | return 330 | fi 331 | local SWIVM_NUM_DOTS 332 | SWIVM_NUM_DOTS=$(echo "$VERSION" | command sed -e 's/[^\.]//g') 333 | local SWIVM_NUM_GROUPS 334 | SWIVM_NUM_GROUPS=".$SWIVM_NUM_DOTS" # add extra dot, since it's (n - 1) dots at this point 335 | echo "${#SWIVM_NUM_GROUPS}" 336 | } 337 | 338 | swivm_strip_path() { 339 | echo "$1" | command sed \ 340 | -e "s#$SWIVM_DIR/[^/]*$2[^:]*:##g" \ 341 | -e "s#:$SWIVM_DIR/[^/]*$2[^:]*##g" \ 342 | -e "s#$SWIVM_DIR/[^/]*$2[^:]*##g" \ 343 | -e "s#$SWIVM_DIR/versions/[^/]*$2[^:]*:##g" \ 344 | -e "s#:$SWIVM_DIR/versions/[^/]*$2[^:]*##g" \ 345 | -e "s#$SWIVM_DIR/versions/[^/]*$2[^:]*##g" 346 | } 347 | 348 | swivm_prepend_path() { 349 | if [ -z "$1" ]; then 350 | echo "$2" 351 | else 352 | echo "$2:$1" 353 | fi 354 | } 355 | 356 | swivm_change_path() { 357 | # if there’s no initial path, just return the supplementary path 358 | if [ -z "${1-}" ]; then 359 | swivm_echo "${3-}${2-}" 360 | # if the initial path doesn’t contain an swivm path, prepend the supplementary 361 | # path 362 | elif ! swivm_echo "${1-}" | swivm_grep -q "${SWIVM_DIR}/[^/]*${2-}" \ 363 | && ! swivm_echo "${1-}" | swivm_grep -q "${SWIVM_DIR}/versions/[^/]*/[^/]*${2-}"; then 364 | swivm_echo "${3-}${2-}:${1-}" 365 | # if the initial path contains BOTH an swivm path (checked for above) and 366 | # that swivm path is preceded by a system binary path, just prepend the 367 | # supplementary path instead of replacing it. 368 | # https://github.com/nvm-sh/nvm/issues/1652#issuecomment-342571223 369 | elif swivm_echo "${1-}" | swivm_grep -Eq "(^|:)(/usr(/local)?)?${2-}:.*${SWIVM_DIR}/[^/]*${2-}" \ 370 | || swivm_echo "${1-}" | swivm_grep -Eq "(^|:)(/usr(/local)?)?${2-}:.*${SWIVM_DIR}/versions/[^/]*/[^/]*${2-}"; then 371 | swivm_echo "${3-}${2-}:${1-}" 372 | # use sed to replace the existing swivm path with the supplementary path. This 373 | # preserves the order of the path. 374 | else 375 | swivm_echo "${1-}" | command sed \ 376 | -e "s#${SWIVM_DIR}/[^/]*${2-}[^:]*#${3-}${2-}#" \ 377 | -e "s#${SWIVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*#${3-}${2-}#" 378 | fi 379 | } 380 | 381 | swivm_print_formatted_alias() { 382 | local ALIAS 383 | ALIAS="${1-}" 384 | local DEST 385 | DEST="${2-}" 386 | local VERSION 387 | VERSION="${3-}" 388 | if [ -z "${VERSION}" ]; then 389 | VERSION="$(swivm_version "${DEST}")" ||: 390 | fi 391 | local VERSION_FORMAT 392 | local ALIAS_FORMAT 393 | local DEST_FORMAT 394 | ALIAS_FORMAT='%s' 395 | DEST_FORMAT='%s' 396 | VERSION_FORMAT='%s' 397 | local NEWLINE 398 | NEWLINE='\n' 399 | if [ "_${DEFAULT}" = '_true' ]; then 400 | NEWLINE=' (default)\n' 401 | fi 402 | local ARROW 403 | ARROW='->' 404 | if [ -z "${SWIVM_NO_COLORS}" ] && swivm_has_colors; then 405 | ARROW='\033[0;90m->\033[0m' 406 | if [ "_${DEFAULT}" = '_true' ]; then 407 | NEWLINE=' \033[0;37m(default)\033[0m\n' 408 | fi 409 | if [ "_${VERSION}" = "_${SWIVM_CURRENT-}" ]; then 410 | ALIAS_FORMAT='\033[0;32m%s\033[0m' 411 | DEST_FORMAT='\033[0;32m%s\033[0m' 412 | VERSION_FORMAT='\033[0;32m%s\033[0m' 413 | elif swivm_is_version_installed "${VERSION}"; then 414 | ALIAS_FORMAT='\033[0;34m%s\033[0m' 415 | DEST_FORMAT='\033[0;34m%s\033[0m' 416 | VERSION_FORMAT='\033[0;34m%s\033[0m' 417 | elif [ "${VERSION}" = '∞' ] || [ "${VERSION}" = 'N/A' ]; then 418 | ALIAS_FORMAT='\033[1;31m%s\033[0m' 419 | DEST_FORMAT='\033[1;31m%s\033[0m' 420 | VERSION_FORMAT='\033[1;31m%s\033[0m' 421 | fi 422 | elif [ "_${VERSION}" != '_∞' ] && [ "_${VERSION}" != '_N/A' ]; then 423 | VERSION_FORMAT='%s *' 424 | fi 425 | if [ "${DEST}" = "${VERSION}" ]; then 426 | command printf -- "${ALIAS_FORMAT} ${ARROW} ${VERSION_FORMAT}${NEWLINE}" "${ALIAS}" "${DEST}" 427 | else 428 | command printf -- "${ALIAS_FORMAT} ${ARROW} ${DEST_FORMAT} (${ARROW} ${VERSION_FORMAT})${NEWLINE}" "${ALIAS}" "${DEST}" "${VERSION}" 429 | fi 430 | } 431 | 432 | swivm_print_alias_path() { 433 | local SWIVM_ALIAS_DIR 434 | SWIVM_ALIAS_DIR="${1-}" 435 | if [ -z "${SWIVM_ALIAS_DIR}" ]; then 436 | swivm_err 'An alias dir is required.' 437 | return 1 438 | fi 439 | local ALIAS_PATH 440 | ALIAS_PATH="${2-}" 441 | if [ -z "${ALIAS_PATH}" ]; then 442 | swivm_err 'An alias path is required.' 443 | return 2 444 | fi 445 | local ALIAS 446 | ALIAS="${ALIAS_PATH##${SWIVM_ALIAS_DIR}\/}" 447 | local DEST 448 | DEST="$(swivm_alias "${ALIAS}" 2>/dev/null)" ||: 449 | if [ -n "${DEST}" ]; then 450 | SWIVM_NO_COLORS="${SWIVM_NO_COLORS-}" DEFAULT=false swivm_print_formatted_alias "${ALIAS}" "${DEST}" 451 | fi 452 | } 453 | 454 | swivm_print_default_alias() { 455 | local ALIAS 456 | ALIAS="${1-}" 457 | if [ -z "${ALIAS}" ]; then 458 | swivm_err 'A default alias is required.' 459 | return 1 460 | fi 461 | local DEST 462 | DEST="$(swivm_print_implicit_alias local "${ALIAS}")" 463 | if [ -n "${DEST}" ]; then 464 | SWIVM_NO_COLORS="${SWIVM_NO_COLORS-}" DEFAULT=true swivm_print_formatted_alias "${ALIAS}" "${DEST}" 465 | fi 466 | } 467 | 468 | swivm_make_alias() { 469 | local ALIAS 470 | ALIAS="${1-}" 471 | if [ -z "${ALIAS}" ]; then 472 | swivm_err "an alias name is required" 473 | return 1 474 | fi 475 | local VERSION 476 | VERSION="${2-}" 477 | if [ -z "${VERSION}" ]; then 478 | swivm_err "an alias target version is required" 479 | return 2 480 | fi 481 | swivm_echo "${VERSION}" | tee "$(swivm_alias_path)/${ALIAS}" >/dev/null 482 | } 483 | 484 | swivm_list_aliases() { 485 | local ALIAS 486 | ALIAS="${1-}" 487 | 488 | local SWIVM_CURRENT 489 | SWIVM_CURRENT="$(swivm_ls_current)" 490 | local SWIVM_ALIAS_DIR 491 | SWIVM_ALIAS_DIR="$(swivm_alias_path)" 492 | command mkdir -p "${SWIVM_ALIAS_DIR}" 493 | 494 | ( 495 | local ALIAS_PATH 496 | for ALIAS_PATH in "${SWIVM_ALIAS_DIR}/${ALIAS}"*; do 497 | SWIVM_NO_COLORS="${SWIVM_NO_COLORS-}" SWIVM_CURRENT="${SWIVM_CURRENT}" swivm_print_alias_path "${SWIVM_ALIAS_DIR}" "${ALIAS_PATH}" & 498 | done 499 | wait 500 | ) | sort 501 | 502 | ( 503 | local ALIAS_NAME 504 | for ALIAS_NAME in "stable" "devel"; do 505 | { 506 | if [ ! -f "${SWIVM_ALIAS_DIR}/${ALIAS_NAME}" ] && { [ -z "${ALIAS}" ] || [ "${ALIAS_NAME}" = "${ALIAS}" ]; }; then 507 | SWIVM_NO_COLORS="${SWIVM_NO_COLORS-}" SWIVM_CURRENT="${SWIVM_CURRENT}" swivm_print_default_alias "${ALIAS_NAME}" 508 | fi 509 | } & 510 | done 511 | wait 512 | ) | sort 513 | return 514 | } 515 | 516 | swivm_alias() { 517 | local ALIAS 518 | ALIAS="$1" 519 | if [ -z "$ALIAS" ]; then 520 | echo >&2 'An alias is required.' 521 | return 1 522 | fi 523 | 524 | local SWIVM_ALIAS_PATH 525 | SWIVM_ALIAS_PATH="$(swivm_alias_path)/$ALIAS" 526 | if [ ! -f "$SWIVM_ALIAS_PATH" ]; then 527 | echo >&2 'Alias does not exist.' 528 | return 2 529 | fi 530 | 531 | command cat "$SWIVM_ALIAS_PATH" 532 | } 533 | 534 | swivm_ls_current() { 535 | local SWIVM_LS_CURRENT_PATH 536 | if ! SWIVM_LS_CURRENT_PATH="$(command which swipl 2>/dev/null)"; then 537 | swivm_echo 'none' 538 | elif swivm_tree_contains_path "${SWIVM_DIR}" "${SWIVM_LS_CURRENT_PATH}"; then 539 | local VERSION 540 | VERSION="$(swipl --version | sed -E "s/^.* ([0-9]+(\.[0-9]+)*) .*$/\1/g" 2>/dev/null)" 541 | swivm_echo "v${VERSION}" 542 | else 543 | swivm_echo 'system' 544 | fi 545 | } 546 | 547 | swivm_resolve_alias() { 548 | if [ -z "${1-}" ]; then 549 | return 1 550 | fi 551 | 552 | local PATTERN 553 | PATTERN="${1-}" 554 | 555 | local ALIAS 556 | ALIAS="${PATTERN}" 557 | local ALIAS_TEMP 558 | 559 | local SEEN_ALIASES 560 | SEEN_ALIASES="${ALIAS}" 561 | while true; do 562 | ALIAS_TEMP="$(swivm_alias "${ALIAS}" 2>/dev/null || swivm_echo)" 563 | 564 | if [ -z "${ALIAS_TEMP}" ]; then 565 | break 566 | fi 567 | 568 | if command printf "${SEEN_ALIASES}" | swivm_grep -q -e "^${ALIAS_TEMP}$"; then 569 | ALIAS="∞" 570 | break 571 | fi 572 | 573 | SEEN_ALIASES="${SEEN_ALIASES}\\n${ALIAS_TEMP}" 574 | ALIAS="${ALIAS_TEMP}" 575 | done 576 | 577 | if [ -n "${ALIAS}" ] && [ "_${ALIAS}" != "_${PATTERN}" ]; then 578 | case "${ALIAS}" in 579 | '∞') 580 | swivm_echo "${ALIAS}" 581 | ;; 582 | *) 583 | swivm_ensure_version_prefix "${ALIAS}" 584 | ;; 585 | esac 586 | return 0 587 | fi 588 | 589 | if swivm_validate_implicit_alias "${PATTERN}" 2>/dev/null; then 590 | local IMPLICIT 591 | IMPLICIT="$(swivm_print_implicit_alias local "${PATTERN}" 2>/dev/null)" 592 | if [ -n "${IMPLICIT}" ]; then 593 | swivm_ensure_version_prefix "${IMPLICIT}" 594 | fi 595 | fi 596 | 597 | return 2 598 | } 599 | 600 | swivm_resolve_local_alias() { 601 | if [ -z "$1" ]; then 602 | return 1 603 | fi 604 | 605 | local VERSION 606 | local EXIT_CODE 607 | VERSION="$(swivm_resolve_alias "$1")" 608 | EXIT_CODE=$? 609 | if [ -z "$VERSION" ]; then 610 | return $EXIT_CODE 611 | fi 612 | if [ "_$VERSION" != "_∞" ]; then 613 | swivm_version "$VERSION" 614 | else 615 | echo "$VERSION" 616 | fi 617 | } 618 | 619 | swivm_version_mode() { 620 | local VERSION 621 | VERSION="$1" 622 | local NORMALIZED_VERSION 623 | NORMALIZED_VERSION="$(swivm_normalize_version "$VERSION")" 624 | local MOD 625 | MOD=$(expr "$NORMALIZED_VERSION" \/ 1000000 \% 2) 626 | local MODE 627 | MODE='stable' 628 | if [ "$MOD" -eq 1 ]; then 629 | MODE='devel' 630 | fi 631 | echo "$MODE" 632 | } 633 | 634 | swivm_is_stable_version() { 635 | local VERSION 636 | VERSION="$1" 637 | local MODE 638 | MODE="$(swivm_version_mode "$VERSION")" 639 | if [ "_$MODE" = "_stable" ]; then 640 | return 0 641 | fi 642 | return 1 643 | } 644 | 645 | swivm_ls() { 646 | local PATTERN 647 | PATTERN="${1-}" 648 | local VERSIONS 649 | VERSIONS='' 650 | if [ "${PATTERN}" = 'current' ]; then 651 | swivm_ls_current 652 | return 653 | fi 654 | 655 | case "${PATTERN}" in 656 | *) 657 | if swivm_resolve_local_alias "${PATTERN}"; then 658 | return 659 | fi 660 | PATTERN="$(swivm_ensure_version_prefix "${PATTERN}")" 661 | ;; 662 | esac 663 | if [ "${PATTERN}" = 'N/A' ]; then 664 | return 665 | fi 666 | # If it looks like an explicit version, don't do anything funny 667 | local SWIVM_PATTERN_STARTS_WITH_V 668 | case $PATTERN in 669 | v*) SWIVM_PATTERN_STARTS_WITH_V=true ;; 670 | *) SWIVM_PATTERN_STARTS_WITH_V=false ;; 671 | esac 672 | if [ $SWIVM_PATTERN_STARTS_WITH_V = true ] && [ "_$(swivm_num_version_groups "${PATTERN}")" = "_3" ]; then 673 | if swivm_is_version_installed "${PATTERN}"; then 674 | VERSIONS="${PATTERN}" 675 | fi 676 | else 677 | case "${PATTERN}" in 678 | "system") ;; 679 | *) 680 | local NUM_VERSION_GROUPS 681 | NUM_VERSION_GROUPS="$(swivm_num_version_groups "${PATTERN}")" 682 | if [ "${NUM_VERSION_GROUPS}" = "2" ] || [ "${NUM_VERSION_GROUPS}" = "1" ]; then 683 | PATTERN="${PATTERN%.}." 684 | fi 685 | ;; 686 | esac 687 | 688 | swivm_is_zsh && setopt local_options shwordsplit 689 | 690 | local SWIVM_DIRS_TO_SEARCH1 691 | SWIVM_DIRS_TO_SEARCH1='' 692 | SWIVM_ADD_SYSTEM=false 693 | 694 | SWIVM_DIRS_TO_SEARCH="$(swivm_version_dir)" 695 | if swivm_has_system ; then 696 | SWIVM_ADD_SYSTEM=true 697 | fi 698 | 699 | if ! [ -d "${SWIVM_DIRS_TO_SEARCH}" ] || ! (command ls -1qA "${SWIVM_DIRS_TO_SEARCH}" | swivm_grep -q .); then 700 | SWIVM_DIRS_TO_SEARCH='' 701 | fi 702 | 703 | local SEARCH_PATTERN 704 | if [ -z "${PATTERN}" ]; then 705 | PATTERN='v' 706 | SEARCH_PATTERN='.*' 707 | else 708 | SEARCH_PATTERN="$(swivm_echo "${PATTERN}" | command sed 's#\.#\\\.#g;')" 709 | fi 710 | 711 | if [ -n "${SWIVM_DIRS_TO_SEARCH}" ]; then 712 | VERSIONS="$(command find "${SWIVM_DIRS_TO_SEARCH}"/* -name . -o -type d -prune -o -path "${PATTERN}*" \ 713 | | command sed -e " 714 | s#^${SWIVM_DIR}/##; 715 | \\#^[^v]# d; 716 | \\#^versions\$# d; 717 | s#^versions/##; 718 | \\#${SEARCH_PATTERN}# !d; 719 | " \ 720 | -e 's#^\([^/]\{1,\}\)/\(.*\)$#\2.\1#;' \ 721 | | command sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n \ 722 | )" 723 | fi 724 | fi 725 | 726 | if [ "${SWIVM_ADD_SYSTEM-}" = true ]; then 727 | if [ -z "${PATTERN}" ] || [ "${PATTERN}" = 'v' ]; then 728 | VERSIONS="${VERSIONS}$(command printf '\n%s' 'system')" 729 | elif [ "${PATTERN}" = 'system' ]; then 730 | VERSIONS="$(command printf '%s' 'system')" 731 | fi 732 | fi 733 | 734 | if [ -z "${VERSIONS}" ]; then 735 | swivm_echo 'N/A' 736 | return 3 737 | fi 738 | 739 | swivm_echo "${VERSIONS}" 740 | } 741 | 742 | swivm_ls_remote() { 743 | local PATTERN 744 | PATTERN="${1-}" 745 | if swivm_validate_implicit_alias "${PATTERN}" 2>/dev/null ; then 746 | local IMPLICIT 747 | IMPLICIT="$(swivm_print_implicit_alias remote "${PATTERN}")" 748 | if [ -z "${IMPLICIT-}" ] || [ "${IMPLICIT}" = 'N/A' ]; then 749 | swivm_echo "N/A" 750 | return 3 751 | fi 752 | PATTERN="$(swivm_ls_remote "${IMPLICIT}" | command tail -1 | command awk '{ print $1 }')" 753 | elif [ -n "${PATTERN}" ]; then 754 | PATTERN="$(swivm_ensure_version_prefix "${PATTERN}")" 755 | else 756 | PATTERN=".*" 757 | fi 758 | 759 | swivm_ls_remote_index "$SWIVM_MIRROR" "${PATTERN}" 760 | } 761 | 762 | swivm_ls_remote_index() { 763 | if [ "$#" -lt 2 ]; then 764 | echo "not enough arguments" >&2 765 | return 5 766 | fi 767 | local PREFIX 768 | PREFIX='' 769 | local SORT_COMMAND 770 | SORT_COMMAND='sort -t. -u -k 1,1n -k 2,2n -k 3,3n' 771 | local MIRROR 772 | MIRROR="$1" 773 | local PATTERN 774 | PATTERN="$2" 775 | local VERSIONS 776 | if [ -z "$PATTERN" ]; then 777 | PATTERN=".*" 778 | fi 779 | ZHS_HAS_SHWORDSPLIT_UNSET=1 780 | if swivm_has "setopt"; then 781 | ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; echo $?) 782 | setopt shwordsplit 783 | fi 784 | VERSIONS="$(swivm_download -L -s "$MIRROR/devel/src/" "$MIRROR/stable/src/" -o - \ 785 | | command grep -E 'a href=\"(swi)?pl\-' \ 786 | | command sed -E 's/^.*a href="(swi)?pl\-(.*)\.tar\.gz".*$/\2/' \ 787 | | command sed -E 's/^/v/' \ 788 | | command grep -w "^$PATTERN" \ 789 | | command sed -E 's/^v//' \ 790 | | $SORT_COMMAND \ 791 | | command sed -E 's/^/v/')" 792 | if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && swivm_has "unsetopt"; then 793 | unsetopt shwordsplit 794 | fi 795 | if [ -z "$VERSIONS" ]; then 796 | echo "N/A" 797 | return 3 798 | fi 799 | echo "$VERSIONS" 800 | } 801 | 802 | swivm_print_versions() { 803 | local VERSION 804 | local FORMAT 805 | local SWIVM_CURRENT 806 | SWIVM_CURRENT=$(swivm_ls_current) 807 | local SWIVM_HAS_COLORS 808 | if [ -z "${SWIVM_NO_COLORS-}" ] && swivm_has_colors; then 809 | SWIVM_HAS_COLORS=1 810 | fi 811 | swivm_echo "${1-}" \ 812 | | command sed '1!G;h;$!d' \ 813 | | command sed '1!G;h;$!d' \ 814 | | while read -r VERSION_LINE; do 815 | VERSION="${VERSION_LINE%% *}" 816 | FORMAT='%15s' 817 | if [ "_${VERSION}" = "_${SWIVM_CURRENT}" ]; then 818 | if [ "${SWIVM_HAS_COLORS-}" = '1' ]; then 819 | FORMAT='\033[0;32m-> %12s\033[0m' 820 | else 821 | FORMAT='-> %12s *' 822 | fi 823 | elif [ "${VERSION}" = "system" ]; then 824 | if [ "${SWIVM_HAS_COLORS-}" = '1' ]; then 825 | FORMAT='\033[0;33m%15s\033[0m' 826 | else 827 | FORMAT='%15s *' 828 | fi 829 | elif swivm_is_version_installed "${VERSION}"; then 830 | if [ "${SWIVM_HAS_COLORS-}" = '1' ]; then 831 | FORMAT='\033[0;34m%15s\033[0m' 832 | else 833 | FORMAT='%15s *' 834 | fi 835 | fi 836 | command printf -- "${FORMAT}\\n" "${VERSION}" 837 | done 838 | } 839 | 840 | swivm_validate_implicit_alias() { 841 | case "$1" in 842 | "stable" | "devel" ) 843 | return 844 | ;; 845 | *) 846 | echo "Only implicit aliases 'stable' and 'devel' are supported." >&2 847 | return 1 848 | ;; 849 | esac 850 | } 851 | 852 | swivm_print_implicit_alias() { 853 | if [ "_$1" != "_local" ] && [ "_$1" != "_remote" ]; then 854 | echo "swivm_print_implicit_alias must be specified with local or remote as the first argument." >&2 855 | return 1 856 | fi 857 | 858 | local SWIVM_IMPLICIT 859 | SWIVM_IMPLICIT="$2" 860 | if ! swivm_validate_implicit_alias "$SWIVM_IMPLICIT"; then 861 | return 2 862 | fi 863 | 864 | local ZHS_HAS_SHWORDSPLIT_UNSET 865 | 866 | local SWIVM_COMMAND 867 | local SWIVM_ADD_PREFIX_COMMAND 868 | local LAST_TWO 869 | 870 | SWIVM_COMMAND="swivm_ls_remote" 871 | if [ "_$1" = "_local" ]; then 872 | SWIVM_COMMAND="swivm_ls" 873 | fi 874 | 875 | ZHS_HAS_SHWORDSPLIT_UNSET=1 876 | if swivm_has "setopt"; then 877 | ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; echo $?) 878 | setopt shwordsplit 879 | fi 880 | 881 | LAST_TWO=$($SWIVM_COMMAND | command cut -d . -f 1,2 | uniq) 882 | 883 | if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && swivm_has "unsetopt"; then 884 | unsetopt shwordsplit 885 | fi 886 | 887 | local MINOR 888 | local STABLE 889 | local UNSTABLE 890 | local MOD 891 | 892 | ZHS_HAS_SHWORDSPLIT_UNSET=1 893 | if swivm_has "setopt"; then 894 | ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; echo $?) 895 | setopt shwordsplit 896 | fi 897 | for MINOR in $LAST_TWO; do 898 | MOD="$(swivm_version_mode "$MINOR")" 899 | # TODO 900 | NORMALIZED_VERSION="$(swivm_normalize_version "$MINOR")" 901 | MOD=$(expr "$NORMALIZED_VERSION" \/ 1000000 \% 2) 902 | if [ "$MOD" -eq 0 ]; then 903 | STABLE="$MINOR" 904 | elif [ "$MOD" -eq 1 ]; then 905 | UNSTABLE="$MINOR" 906 | fi 907 | done 908 | if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && swivm_has "unsetopt"; then 909 | unsetopt shwordsplit 910 | fi 911 | 912 | if [ "_$2" = '_stable' ]; then 913 | echo "${STABLE}" 914 | elif [ "_$2" = '_devel' ]; then 915 | echo "${UNSTABLE}" 916 | fi 917 | } 918 | 919 | swivm_get_os() { 920 | local SWIVM_UNAME 921 | SWIVM_UNAME="$(uname -a)" 922 | local SWIVM_OS 923 | case "$SWIVM_UNAME" in 924 | Linux\ *) SWIVM_OS=linux ;; 925 | Darwin\ *) SWIVM_OS=darwin ;; 926 | SunOS\ *) SWIVM_OS=sunos ;; 927 | FreeBSD\ *) SWIVM_OS=freebsd ;; 928 | esac 929 | echo "$SWIVM_OS" 930 | } 931 | 932 | swivm_get_arch() { 933 | local HOST_ARCH 934 | local SWIVM_OS 935 | local EXIT_CODE 936 | 937 | SWIVM_OS="$(swivm_get_os)" 938 | # If the OS is SunOS, first try to use pkgsrc to guess 939 | # the most appropriate arch. If it's not available, use 940 | # isainfo to get the instruction set supported by the 941 | # kernel. 942 | if [ "_$SWIVM_OS" = "_sunos" ]; then 943 | HOST_ARCH=$(pkg_info -Q MACHINE_ARCH pkg_install) 944 | EXIT_CODE=$? 945 | if [ $EXIT_CODE -ne 0 ]; then 946 | HOST_ARCH=$(isainfo -n) 947 | fi 948 | else 949 | HOST_ARCH="$(uname -m)" 950 | fi 951 | 952 | local SWIVM_ARCH 953 | case "$HOST_ARCH" in 954 | x86_64 | amd64) SWIVM_ARCH="x64" ;; 955 | i*86) SWIVM_ARCH="x86" ;; 956 | *) SWIVM_ARCH="$HOST_ARCH" ;; 957 | esac 958 | echo "$SWIVM_ARCH" 959 | } 960 | 961 | swivm_ensure_default_set() { 962 | local VERSION 963 | VERSION="$1" 964 | if [ -z "$VERSION" ]; then 965 | echo 'swivm_ensure_default_set: a version is required' >&2 966 | return 1 967 | fi 968 | if swivm_alias default >/dev/null 2>&1; then 969 | # default already set 970 | return 0 971 | fi 972 | local OUTPUT 973 | OUTPUT="$(swivm alias default "$VERSION")" 974 | local EXIT_CODE 975 | EXIT_CODE="$?" 976 | echo "Creating default alias: $OUTPUT" 977 | return $EXIT_CODE 978 | } 979 | 980 | swivm_get_make_jobs() { 981 | if swivm_is_natural_num "${1-}"; then 982 | SWIVM_MAKE_JOBS="$1" 983 | swivm_echo "number of \`make\` jobs: ${SWIVM_MAKE_JOBS}" 984 | return 985 | elif [ -n "${1-}" ]; then 986 | unset SWIVM_MAKE_JOBS 987 | swivm_err "$1 is invalid for number of \`make\` jobs, must be a natural number" 988 | fi 989 | local SWIVM_OS 990 | SWIVM_OS="$(swivm_get_os)" 991 | local SWIVM_CPU_CORES 992 | case "_${SWIVM_OS}" in 993 | "_linux") 994 | SWIVM_CPU_CORES="$(swivm_grep -c -E '^processor.+: [0-9]+' /proc/cpuinfo)" 995 | ;; 996 | "_freebsd" | "_darwin") 997 | SWIVM_CPU_CORES="$(sysctl -n hw.ncpu)" 998 | ;; 999 | "_sunos") 1000 | SWIVM_CPU_CORES="$(psrinfo | wc -l)" 1001 | ;; 1002 | "_aix") 1003 | SWIVM_CPU_CORES="$(pmcycles -m | wc -l)" 1004 | ;; 1005 | esac 1006 | if ! swivm_is_natural_num "${SWIVM_CPU_CORES}"; then 1007 | swivm_err 'Can not determine how many core(s) are available, running in single-threaded mode.' 1008 | swivm_err 'Please report an issue on GitHub to help us make swivm run faster on your computer!' 1009 | SWIVM_MAKE_JOBS=1 1010 | else 1011 | swivm_echo "Detected that you have ${SWIVM_CPU_CORES} CPU core(s)" 1012 | if [ "${SWIVM_CPU_CORES}" -gt 2 ]; then 1013 | SWIVM_MAKE_JOBS=$((SWIVM_CPU_CORES - 1)) 1014 | swivm_echo "Running with ${SWIVM_MAKE_JOBS} threads to speed up the build" 1015 | else 1016 | SWIVM_MAKE_JOBS=1 1017 | swivm_echo 'Number of CPU core(s) less than or equal to 2, running in single-threaded mode' 1018 | fi 1019 | fi 1020 | } 1021 | 1022 | swivm_install() { 1023 | local VERSION 1024 | VERSION="$1" 1025 | 1026 | local SWIVM_MAKE_JOBS 1027 | SWIVM_MAKE_JOBS="${2-}" 1028 | 1029 | local ADDITIONAL_PARAMETERS 1030 | ADDITIONAL_PARAMETERS="$3" 1031 | 1032 | if [ -n "$ADDITIONAL_PARAMETERS" ]; then 1033 | echo "Additional options while compiling: $ADDITIONAL_PARAMETERS" 1034 | fi 1035 | 1036 | local VERSION_PATH 1037 | VERSION_PATH="$(swivm_version_path "$VERSION")" 1038 | local SWIVM_OS 1039 | SWIVM_OS="$(swivm_get_os)" 1040 | local MODE 1041 | MODE="$(swivm_version_mode "$VERSION")" 1042 | 1043 | local tarball 1044 | tarball='' 1045 | local make 1046 | make='make' 1047 | if [ "_$SWIVM_OS" = "_freebsd" ]; then 1048 | make='gmake' 1049 | MAKE_CXX="CXX=c++" 1050 | fi 1051 | local tmpdir 1052 | tmpdir="$SWIVM_DIR/src" 1053 | local tmptarball 1054 | tmptarball="$tmpdir/swipl-$VERSION.tar.gz" 1055 | 1056 | local VERSION_WITHOUT_V 1057 | VERSION_WITHOUT_V="${VERSION:1}" 1058 | if [ "$(swivm_download -L -s -I "$SWIVM_MIRROR/$MODE/src/swipl-$VERSION_WITHOUT_V.tar.gz" -o - | command grep '200 OK\|HTTP/2 200')" != '' ]; then 1059 | tarball="$SWIVM_MIRROR/$MODE/src/swipl-$VERSION_WITHOUT_V.tar.gz" 1060 | elif [ "$(swivm_download -L -s -I "$SWIVM_MIRROR/$MODE/src/pl-$VERSION_WITHOUT_V.tar.gz" -o - | command grep '200 OK\|HTTP/2 200')" != '' ]; then 1061 | tarball="$SWIVM_MIRROR/$MODE/src/pl-$VERSION_WITHOUT_V.tar.gz" 1062 | elif [ "$(swivm_download -L -s -I "$GITHUB_MIRROR/swipl-devel/archive/V$VERSION_WITHOUT_V.tar.gz" -o - 2>&1 | command grep '200 OK\|HTTP/2 200')" != '' ]; then 1063 | tarball="$GITHUB_MIRROR/swipl-devel/archive/V$VERSION_WITHOUT_V.tar.gz" 1064 | fi 1065 | 1066 | local SWIVM_ALIAS_DIR 1067 | SWIVM_ALIAS_DIR="$(swivm_alias_path)" 1068 | 1069 | local SRC_PATH 1070 | if ! ( 1071 | [ -n "$tarball" ] && \ 1072 | command mkdir -p "$tmpdir" && \ 1073 | echo "Downloading $tarball..." && \ 1074 | swivm_download -L --progress-bar "$tarball" -o "$tmptarball" && \ 1075 | command tar -xzf "$tmptarball" -C "$tmpdir" && \ 1076 | command mkdir -p "$SWIVM_DIR/versions" && \ 1077 | command mkdir -p "${SWIVM_ALIAS_DIR}" && \ 1078 | ( mv "$tmpdir/swipl-$VERSION_WITHOUT_V" "$VERSION_PATH" >/dev/null 2>&1 || \ 1079 | mv "$tmpdir/pl-$VERSION_WITHOUT_V" "$VERSION_PATH" >/dev/null 2>&1 || \ 1080 | mv "$tmpdir/swipl-devel-$VERSION_WITHOUT_V" "$VERSION_PATH" >/dev/null 2>&1 \ 1081 | ) && \ 1082 | cd "$VERSION_PATH" && \ 1083 | ( ([ "$tarball" = "$GITHUB_MIRROR/swipl-devel/archive/V$VERSION.tar.gz" ] && \ 1084 | # downloaded from GitHub \ 1085 | echo "Downloading packages..." && \ 1086 | swivm_download_git_submodules "$VERSION" \ 1087 | ) || true) && \ 1088 | cd "$VERSION_PATH" && \ 1089 | ( ([[ -f CMakeLists.txt ]] && \ 1090 | export SWIPL_INSTALL_PREFIX="$VERSION_PATH" && \ 1091 | ( ([[ -f packages/ssl/crypto4pl.c ]] && \ 1092 | command mv packages/ssl/crypto4pl.c packages/ssl/crypto4pl.c.original && \ 1093 | command sed "s@ else if ( a == ATOM_sslv23 \&\& mode == RSA_MODE ) \*padding = RSA_SSLV23_PADDING;@#ifdef RSA_SSLV23_PADDING\n else if ( a == ATOM_sslv23 \&\& mode == RSA_MODE ) \*padding = RSA_SSLV23_PADDING;\n#endif@g" packages/ssl/crypto4pl.c.original > packages/ssl/crypto4pl.c \ 1094 | ) || true) && \ 1095 | mkdir build && \ 1096 | cd build && \ 1097 | cmake .. && \ 1098 | make -j "${SWIVM_MAKE_JOBS}" && \ 1099 | make -j "${SWIVM_MAKE_JOBS}" install \ 1100 | ) || ( \ 1101 | echo "### [SWIVM] Prepare Installation Template ###" && \ 1102 | sed -e "s@PREFIX=\$HOME@PREFIX=$VERSION_PATH@g" build.templ > build.templ.2 && \ 1103 | sed -e "s@MAKE=make@MAKE=$make@g" build.templ.2 > build && \ 1104 | rm build.templ.2 && \ 1105 | chmod +x build && \ 1106 | echo "### [SWIVM] Prepare SWI-Prolog ###" && \ 1107 | ./prepare --yes --all && \ 1108 | echo "### [SWIVM] Build SWI-Prolog ###" && \ 1109 | ./build && \ 1110 | cd packages && \ 1111 | echo "### [SWIVM] Configure Packages ###" && \ 1112 | ./configure && \ 1113 | $MAKE && \ 1114 | echo "### [SWIVM] Install Packages ###" && \ 1115 | make install )) && \ 1116 | # fix server URL in prolog_pack.pl \ 1117 | cd "$VERSION_PATH" && \ 1118 | ( ([[ -f lib/swipl/library/prolog_pack.pl ]] && \ 1119 | command mv lib/swipl/library/prolog_pack.pl lib/swipl/library/prolog_pack.pl.original && \ 1120 | command sed "s@:- setting(server, atom, 'http://www.swi-prolog.org/pack/',@:- setting(server, atom, 'https://www.swi-prolog.org/pack/',@g" lib/swipl/library/prolog_pack.pl.original > lib/swipl/library/prolog_pack.pl \ 1121 | ) || true) && 1122 | ( ([[ -f library/prolog_pack.pl ]] && \ 1123 | command mv library/prolog_pack.pl library/prolog_pack.pl.original && \ 1124 | command sed "s@:- setting(server, atom, 'http://www.swi-prolog.org/pack/',@:- setting(server, atom, 'https://www.swi-prolog.org/pack/',@g" library/prolog_pack.pl.original > library/prolog_pack.pl \ 1125 | ) || true) \ 1126 | ) 1127 | then 1128 | echo "swivm: install $VERSION failed!" >&2 1129 | return 1 1130 | fi 1131 | 1132 | return $? 1133 | } 1134 | 1135 | swivm_download_git_submodules() { 1136 | local VERSION 1137 | VERSION="$1" 1138 | 1139 | local VERSION_PATH 1140 | VERSION_PATH="$(swivm_version_path "$VERSION")" 1141 | 1142 | local tmpdir 1143 | tmpdir="$VERSION_PATH/packages/src" 1144 | 1145 | local tmptarball 1146 | 1147 | command mkdir -p "$tmpdir" 1148 | 1149 | command sed -e '/^\[submodule .*\]$/ { 1150 | N; /\n.*path = .*$/ { 1151 | N; /\n.*url = .*$/ { 1152 | s/\[submodule "\(.*\)"\]\n.*path = \(.*\)\n.*url = \.\.\/\(.*\)\.git$/\1 \2 \3/ 1153 | } 1154 | } 1155 | }' "$VERSION_PATH/.gitmodules" | while read -r SUB_NAME SUB_PATH SUB_URL; do 1156 | if [[ "$SUB_NAME" != packages* ]]; then 1157 | # only packages 1158 | continue 1159 | fi 1160 | 1161 | # remove currently empty directory if exists 1162 | if [ -d "$VERSION_PATH/$SUB_NAME" ]; then command rmdir "$VERSION_PATH/$SUB_NAME"; fi 1163 | 1164 | tmptarball="$tmpdir/$SUB_URL.tar.gz" 1165 | 1166 | echo "Downloading package $SUB_NAME..." && \ 1167 | swivm_download -L --progress-bar "$GITHUB_MIRROR/$SUB_URL/archive/V$VERSION.tar.gz" -o "$tmptarball" 1168 | command tar -xzf "$tmptarball" -C "$VERSION_PATH/packages" 1169 | command mv "$VERSION_PATH/packages/$SUB_URL-$VERSION" "$VERSION_PATH/$SUB_NAME" 1170 | 1171 | done 1172 | 1173 | return $? 1174 | } 1175 | 1176 | swivm_lib() { 1177 | local VERSION 1178 | VERSION="$1" 1179 | local VERSION_PATH 1180 | VERSION_PATH="$(swivm_version_path "$VERSION")" 1181 | local LIB_PATH 1182 | LIB_PATH="$(swivm_lib_path "$VERSION_PATH")" 1183 | 1184 | local LIBSO 1185 | command find "${LIB_PATH}"/* -name libswipl.so* -type l -prune -print0 \ 1186 | | xargs -0 sudo cp -f -s --target-directory=/usr/lib 1187 | } 1188 | 1189 | swivm_match_version() { 1190 | local PROVIDED_VERSION 1191 | PROVIDED_VERSION="$1" 1192 | case "_${PROVIDED_VERSION}" in 1193 | '_system') 1194 | swivm_echo 'system' 1195 | ;; 1196 | *) 1197 | swivm_version "${PROVIDED_VERSION}" 1198 | ;; 1199 | esac 1200 | } 1201 | 1202 | swivm_die_on_prefix() { 1203 | local SWIVM_DELETE_PREFIX 1204 | SWIVM_DELETE_PREFIX="$1" 1205 | case "$SWIVM_DELETE_PREFIX" in 1206 | 0|1) ;; 1207 | *) 1208 | echo >&2 'First argument "delete the prefix" must be zero or one' 1209 | return 1 1210 | ;; 1211 | esac 1212 | local SWIVM_COMMAND 1213 | SWIVM_COMMAND="$2" 1214 | if [ -z "$SWIVM_COMMAND" ]; then 1215 | echo >&2 'Second argument "swivm command" must be nonempty' 1216 | return 2 1217 | fi 1218 | 1219 | if [ -n "$PREFIX" ] && ! (swivm_tree_contains_path "$SWIVM_DIR" "$PREFIX" >/dev/null 2>&1); then 1220 | swivm deactivate >/dev/null 2>&1 1221 | echo >&2 "swivm is not compatible with the \"PREFIX\" environment variable: currently set to \"$PREFIX\"" 1222 | echo >&2 "Run \`unset PREFIX\` to unset it." 1223 | return 3 1224 | fi 1225 | } 1226 | 1227 | swivm_is_natural_num() { 1228 | if [ -z "$1" ]; then 1229 | return 4 1230 | fi 1231 | case "$1" in 1232 | 0) return 1 ;; 1233 | -*) return 3 ;; # some BSDs return false positives for double-negated args 1234 | *) 1235 | [ "$1" -eq "$1" ] 2>/dev/null # returns 2 if it doesn't match 1236 | ;; 1237 | esac 1238 | } 1239 | 1240 | # Check version dir permissions 1241 | swivm_check_file_permissions() { 1242 | swivm_is_zsh && setopt local_options nonomatch 1243 | for FILE in "$1"/* "$1"/.[!.]* "$1"/..?* ; do 1244 | if [ -d "$FILE" ]; then 1245 | if ! swivm_check_file_permissions "$FILE"; then 1246 | return 2 1247 | fi 1248 | elif [ -e "$FILE" ] && [ ! -w "$FILE" ] && [ ! -O "$FILE" ]; then 1249 | swivm_err "file is not writable or self-owned: $(swivm_sanitize_path "$FILE")" 1250 | return 1 1251 | fi 1252 | done 1253 | return 0 1254 | } 1255 | 1256 | swivm_sanitize_path() { 1257 | local SANITIZED_PATH 1258 | SANITIZED_PATH="$1" 1259 | if [ "_$1" != "_$SWIVM_DIR" ]; then 1260 | SANITIZED_PATH="$(echo "$SANITIZED_PATH" | command sed "s#$SWIVM_DIR#\$SWIVM_DIR#g")" 1261 | fi 1262 | echo "$SANITIZED_PATH" | command sed "s#$HOME#\$HOME#g" 1263 | } 1264 | 1265 | swivm() { 1266 | if [ $# -lt 1 ]; then 1267 | swivm --help 1268 | return 1269 | fi 1270 | 1271 | local DEFAULT_IFS 1272 | DEFAULT_IFS=" $(swivm_echo t | command tr t \\t) 1273 | " 1274 | if [ "${-#*e}" != "$-" ]; then 1275 | set +e 1276 | local EXIT_CODE 1277 | IFS="${DEFAULT_IFS}" swivm "$@" 1278 | EXIT_CODE=$? 1279 | set -e 1280 | return $EXIT_CODE 1281 | elif [ "${IFS}" != "${DEFAULT_IFS}" ]; then 1282 | IFS="${DEFAULT_IFS}" swivm "$@" 1283 | return $? 1284 | fi 1285 | 1286 | local COMMAND 1287 | COMMAND="${1-}" 1288 | shift 1289 | 1290 | # initialize local variables 1291 | local VERSION 1292 | local ADDITIONAL_PARAMETERS 1293 | 1294 | case $COMMAND in 1295 | "help" ) 1296 | echo 1297 | echo "SWI-Prolog Version Manager" 1298 | echo 1299 | echo 'Note: refers to any version-like string swivm understands. This includes:' 1300 | echo ' - full or partial version numbers, starting with an optional "v" (6.6, v7.2.3, v5)' 1301 | echo " - default (built-in) aliases: stable, devel, system" 1302 | echo ' - custom aliases you define with `swivm alias foo`' 1303 | echo 1304 | echo 'Usage:' 1305 | echo ' swivm help Show this message' 1306 | echo ' swivm --version Print out the installed version of swivm' 1307 | echo ' swivm install Download and install a . Uses .swivmrc if available' 1308 | echo ' swivm uninstall Uninstall a version' 1309 | echo ' swivm use [--silent] Modify PATH to use . Uses .swivmrc if available' 1310 | echo ' swivm exec [--silent] [] Run on . Uses .swivmrc if available' 1311 | echo ' swivm run [--silent] [] Run `swipl` on with as arguments. Uses .swivmrc if available' 1312 | echo ' swivm current [--pack] Display currently activated version' 1313 | echo ' swivm ls List installed versions' 1314 | echo ' swivm ls List versions matching a given description' 1315 | echo ' swivm ls-remote List remote versions available for install' 1316 | echo ' swivm version Resolve the given description to a single local version' 1317 | echo ' swivm version-remote Resolve the given description to a single remote version' 1318 | echo ' swivm deactivate Undo effects of `swivm` on current shell' 1319 | echo ' swivm alias [] Show all aliases beginning with ' 1320 | echo ' swivm alias Set an alias named pointing to ' 1321 | echo ' swivm unalias Deletes the alias named ' 1322 | echo ' swivm unload Unload `swivm` from shell' 1323 | echo ' swivm which [] Display path to installed SWI-Prolog version. Uses .swivmrc if available' 1324 | echo ' swivm lib [] Set symbolic links to libswipl.so in /usr/lib for . Uses .swivmrc or current if available' 1325 | echo 1326 | echo 'Example:' 1327 | echo ' swivm install v6.6.2 Install a specific version number' 1328 | echo ' swivm use 7 Use the latest available 7.x.x release' 1329 | echo ' swivm run v8.0 example.pl Run example.pl using latest SWI-Prolog 8.0.x' 1330 | echo ' swivm exec 6.6.2 swipl example.pl Run `swipl example.pl` with the PATH pointing to SWI-Prolog v6.6.2' 1331 | echo ' swivm alias default 6.6.2 Set default SWI-Prolog version on a shell' 1332 | echo 1333 | echo 'Note:' 1334 | echo ' to remove, delete, or uninstall swivm - just remove the `$SWIVM_DIR` folder (usually `~/.swivm`)' 1335 | echo 1336 | ;; 1337 | 1338 | "debug" ) 1339 | local ZHS_HAS_SHWORDSPLIT_UNSET 1340 | if swivm_has "setopt"; then 1341 | ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; echo $?) 1342 | setopt shwordsplit 1343 | fi 1344 | echo >&2 "swivm --version: v$(swivm --version)" 1345 | echo >&2 "\$SHELL: $SHELL" 1346 | echo >&2 "\$HOME: $HOME" 1347 | echo >&2 "\$SWIVM_DIR: '$(swivm_sanitize_path "$SWIVM_DIR")'" 1348 | echo >&2 "\$PREFIX: '$(swivm_sanitize_path "$PREFIX")'" 1349 | local SWIVM_DEBUG_OUTPUT 1350 | for SWIVM_DEBUG_COMMAND in 'swivm current' 'which swipl' 1351 | do 1352 | SWIVM_DEBUG_OUTPUT="$($SWIVM_DEBUG_COMMAND 2>&1)" 1353 | echo >&2 "$SWIVM_DEBUG_COMMAND: $(swivm_sanitize_path "$SWIVM_DEBUG_OUTPUT")" 1354 | done 1355 | if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && swivm_has "unsetopt"; then 1356 | unsetopt shwordsplit 1357 | fi 1358 | return 42 1359 | ;; 1360 | 1361 | "install" | "i") 1362 | local version_not_provided 1363 | version_not_provided=0 1364 | local SWIVM_OS 1365 | SWIVM_OS="$(swivm_get_os)" 1366 | 1367 | if ! swivm_has "curl" && ! swivm_has "wget"; then 1368 | swivm_err 'swivm needs curl or wget to proceed.' 1369 | return 1 1370 | fi 1371 | 1372 | if [ $# -lt 1 ]; then 1373 | version_not_provided=1 1374 | fi 1375 | 1376 | while [ $# -ne 0 ]; do 1377 | case "$1" in 1378 | ---*) 1379 | swivm_err 'arguments with `---` are not supported - this is likely a typo' 1380 | return 55; 1381 | ;; 1382 | -j) 1383 | shift # consume "-j" 1384 | swivm_get_make_jobs "$1" 1385 | shift # consume job count 1386 | ;; 1387 | --no-progress) 1388 | noprogress=1 1389 | shift 1390 | ;; 1391 | *) 1392 | break # stop parsing args 1393 | ;; 1394 | esac 1395 | done 1396 | 1397 | local provided_version 1398 | provided_version="${1-}" 1399 | 1400 | if [ -z "${provided_version}" ]; then 1401 | swivm_rc_version 1402 | if [ $version_not_provided -eq 1 ] && [ -z "${SWIVM_RC_VERSION}" ]; then 1403 | unset SWIVM_RC_VERSION 1404 | >&2 swivm --help 1405 | return 127 1406 | fi 1407 | provided_version="${SWIVM_RC_VERSION}" 1408 | unset SWIVM_RC_VERSION 1409 | elif [ $# -gt 0 ]; then 1410 | shift 1411 | fi 1412 | 1413 | VERSION="$(SWIVM_VERSION_ONLY=true swivm_remote_version "${provided_version}")" 1414 | 1415 | if [ "${VERSION}" = 'N/A' ]; then 1416 | swivm_err "Version '${provided_version}' not found - try \`swivm ls-remote\` to browse available versions." 1417 | return 3 1418 | fi 1419 | 1420 | ADDITIONAL_PARAMETERS='' 1421 | 1422 | while [ $# -ne 0 ]; do 1423 | case "$1" in 1424 | *) 1425 | ADDITIONAL_PARAMETERS="${ADDITIONAL_PARAMETERS} $1" 1426 | ;; 1427 | esac 1428 | shift 1429 | done 1430 | 1431 | if swivm_is_version_installed "${VERSION}"; then 1432 | swivm_err "${VERSION} is already installed." 1433 | swivm_ensure_default_set "${provided_version}" 1434 | return $? 1435 | fi 1436 | 1437 | local EXIT_CODE 1438 | EXIT_CODE=-1 1439 | 1440 | if [ -z "${SWIVM_MAKE_JOBS-}" ]; then 1441 | swivm_get_make_jobs 1442 | fi 1443 | 1444 | SWIVM_NO_PROGRESS="${SWIVM_NO_PROGRESS:-${noprogress}}" swivm_install "$VERSION" "${SWIVM_MAKE_JOBS}" "$ADDITIONAL_PARAMETERS" 1445 | EXIT_CODE=$? 1446 | if [ "${EXIT_CODE}" != "0" ]; then 1447 | return $EXIT_CODE 1448 | fi 1449 | 1450 | swivm_ensure_default_set "${provided_version}" 1451 | return $? 1452 | ;; 1453 | "uninstall") 1454 | if [ $# -ne 1 ]; then 1455 | >&2 swivm --help 1456 | return 127 1457 | fi 1458 | 1459 | local PATTERN 1460 | PATTERN="${1-}" 1461 | case "${PATTERN-}" in 1462 | --) ;; 1463 | *) 1464 | VERSION="$(swivm_version "${PATTERN}")" 1465 | ;; 1466 | esac 1467 | 1468 | if [ "_${VERSION}" = "_$(swivm_ls_current)" ]; then 1469 | swivm_err "swivm: Cannot uninstall currently-active SWI-Prolog version, ${VERSION} (inferred from ${PATTERN})." 1470 | return 1 1471 | fi 1472 | 1473 | if ! swivm_is_version_installed "${VERSION}"; then 1474 | swivm_err "${VERSION} version is not installed..." 1475 | return 1476 | fi 1477 | 1478 | SWIVM_SUCCESS_MSG="Uninstalled SWI-Prolog ${VERSION}" 1479 | 1480 | local VERSION_PATH 1481 | VERSION_PATH="$(swivm_version_path "${VERSION}")" 1482 | if ! swivm_check_file_permissions "${VERSION_PATH}"; then 1483 | swivm_err 'Cannot uninstall, incorrect permissions on installation folder.' 1484 | swivm_err 'This is usually caused by running `swivm install -g` as root. Run the following commands as root to fix the permissions and then try again.' 1485 | swivm_err 1486 | swivm_err " chown -R $(whoami) \"$(swivm_sanitize_path "${VERSION_PATH}")\"" 1487 | swivm_err " chmod -R u+w \"$(swivm_sanitize_path "${VERSION_PATH}")\"" 1488 | return 1 1489 | fi 1490 | 1491 | # Delete all files related to target version. 1492 | command rm -rf \ 1493 | "${VERSION_PATH}" 2>/dev/null 1494 | swivm_echo "${SWIVM_SUCCESS_MSG}" 1495 | 1496 | # rm any aliases that point to uninstalled version. 1497 | for ALIAS in $(swivm_grep -l "${VERSION}" "$(swivm_alias_path)/*" 2>/dev/null); do 1498 | swivm unalias "$(command basename "${ALIAS}")" 1499 | done 1500 | ;; 1501 | "deactivate" ) 1502 | local NEWPATH 1503 | NEWPATH="$(swivm_strip_path "$PATH" "/bin")" 1504 | if [ "_$PATH" = "_$NEWPATH" ]; then 1505 | echo "Could not find $(swivm_version_dir)/*/bin in \$PATH" >&2 1506 | else 1507 | export PATH="$NEWPATH" 1508 | hash -r 1509 | echo "$(swivm_version_dir)/*/bin removed from \$PATH" 1510 | fi 1511 | 1512 | NEWPATH="$(swivm_strip_path "$MANPATH" "/share/man")" 1513 | if [ "_$MANPATH" = "_$NEWPATH" ]; then 1514 | echo "Could not find $SWIVM_DIR/*/share/man in \$MANPATH" >&2 1515 | else 1516 | export MANPATH="$NEWPATH" 1517 | echo "$SWIVM_DIR/*/share/man removed from \$MANPATH" 1518 | fi 1519 | ;; 1520 | "use" ) 1521 | local PROVIDED_VERSION 1522 | local SWIVM_USE_SILENT 1523 | SWIVM_USE_SILENT=0 1524 | local SWIVM_DELETE_PREFIX 1525 | SWIVM_DELETE_PREFIX=0 1526 | 1527 | while [ $# -ne 0 ]; do 1528 | case "$1" in 1529 | --silent) SWIVM_USE_SILENT=1 ;; 1530 | --delete-prefix) SWIVM_DELETE_PREFIX=1 ;; 1531 | --) ;; 1532 | --*) ;; 1533 | *) 1534 | if [ -n "${1-}" ]; then 1535 | PROVIDED_VERSION="$1" 1536 | fi 1537 | ;; 1538 | esac 1539 | shift 1540 | done 1541 | 1542 | if [ -z "${PROVIDED_VERSION-}" ]; then 1543 | swivm_rc_version 1544 | if [ -n "${SWIVM_RC_VERSION-}" ]; then 1545 | PROVIDED_VERSION="${SWIVM_RC_VERSION}" 1546 | VERSION="$(swivm_version "${PROVIDED_VERSION}")" 1547 | fi 1548 | unset SWIVM_RC_VERSION 1549 | if [ -z "${VERSION}" ]; then 1550 | swivm_err 'Please see `swivm --help` or https://github.com/fnogatz/swivm#usage for more information.' 1551 | return 127 1552 | fi 1553 | else 1554 | VERSION="$(swivm_match_version "${PROVIDED_VERSION}")" 1555 | fi 1556 | 1557 | if [ -z "${VERSION}" ]; then 1558 | >&2 swivm --help 1559 | return 127 1560 | fi 1561 | 1562 | if [ "_${VERSION}" = '_system' ]; then 1563 | if swivm_has_system && swivm deactivate >/dev/null 2>&1; then 1564 | if [ $SWIVM_USE_SILENT -ne 1 ]; then 1565 | swivm_echo "Now using system version of SWI-Prolog: $(swipl --version 2>/dev/null)" 1566 | fi 1567 | return 1568 | elif [ $SWIVM_USE_SILENT -ne 1 ]; then 1569 | swivm_err 'System version of SWI-Prolog not found.' 1570 | fi 1571 | return 127 1572 | elif [ "_${VERSION}" = "_∞" ]; then 1573 | if [ $SWIVM_USE_SILENT -ne 1 ]; then 1574 | swivm_err "The alias \"${PROVIDED_VERSION}\" leads to an infinite loop. Aborting." 1575 | fi 1576 | return 8 1577 | fi 1578 | if [ "${VERSION}" = 'N/A' ]; then 1579 | swivm_err "N/A: version \"${PROVIDED_VERSION} -> ${VERSION}\" is not yet installed." 1580 | swivm_err "" 1581 | swivm_err "You need to run \"swivm install ${PROVIDED_VERSION}\" to install it before using it." 1582 | return 3 1583 | # This swivm_ensure_version_installed call can be a performance bottleneck 1584 | # on shell startup. Perhaps we can optimize it away or make it faster. 1585 | elif ! swivm_ensure_version_installed "${VERSION}"; then 1586 | return $? 1587 | fi 1588 | 1589 | local SWIVM_VERSION_DIR 1590 | SWIVM_VERSION_DIR="$(swivm_version_path "${VERSION}")" 1591 | 1592 | # Change current version 1593 | PATH="$(swivm_change_path "${PATH}" "/bin" "${SWIVM_VERSION_DIR}")" 1594 | if swivm_has manpath; then 1595 | if [ -z "${MANPATH-}" ]; then 1596 | local MANPATH 1597 | MANPATH=$(manpath) 1598 | fi 1599 | # Change current version 1600 | MANPATH="$(swivm_change_path "${MANPATH}" "/share/man" "${SWIVM_VERSION_DIR}")" 1601 | export MANPATH 1602 | fi 1603 | export PATH 1604 | hash -r 1605 | export SWIVM_BIN="${SWIVM_VERSION_DIR}/bin" 1606 | if [ "${SWIVM_SYMLINK_CURRENT-}" = true ]; then 1607 | command rm -f "${SWIVM_DIR}/current" && ln -s "${SWIVM_VERSION_DIR}" "${SWIVM_DIR}/current" 1608 | fi 1609 | local SWIVM_USE_OUTPUT 1610 | SWIVM_USE_OUTPUT='' 1611 | if [ $SWIVM_USE_SILENT -ne 1 ]; then 1612 | SWIVM_USE_OUTPUT="Now using SWI-Prolog ${VERSION}" 1613 | fi 1614 | if [ "_${VERSION}" != "_system" ]; then 1615 | local SWIVM_USE_CMD 1616 | SWIVM_USE_CMD="swivm use --delete-prefix" 1617 | if [ -n "${PROVIDED_VERSION}" ]; then 1618 | SWIVM_USE_CMD="${SWIVM_USE_CMD} ${VERSION}" 1619 | fi 1620 | if [ $SWIVM_USE_SILENT -eq 1 ]; then 1621 | SWIVM_USE_CMD="${SWIVM_USE_CMD} --silent" 1622 | fi 1623 | if ! swivm_die_on_prefix "${SWIVM_DELETE_PREFIX}" "${SWIVM_USE_CMD}"; then 1624 | return 11 1625 | fi 1626 | fi 1627 | if [ -n "${SWIVM_USE_OUTPUT-}" ]; then 1628 | swivm_echo "${SWIVM_USE_OUTPUT}" 1629 | fi 1630 | ;; 1631 | "run" ) 1632 | local provided_version 1633 | local has_checked_swivmrc 1634 | has_checked_swivmrc=0 1635 | # run given version of SWI-Prolog 1636 | shift 1637 | 1638 | local SWIVM_SILENT 1639 | SWIVM_SILENT=0 1640 | if [ "_$1" = "_--silent" ]; then 1641 | SWIVM_SILENT=1 1642 | shift 1643 | fi 1644 | 1645 | if [ $# -lt 1 ]; then 1646 | if [ "$SWIVM_SILENT" -eq 1 ]; then 1647 | swivm_rc_version >/dev/null 2>&1 && has_checked_swivmrc=1 1648 | else 1649 | swivm_rc_version && has_checked_swivmrc=1 1650 | fi 1651 | if [ -n "$SWIVM_RC_VERSION" ]; then 1652 | VERSION="$(swivm_version "$SWIVM_RC_VERSION")" 1653 | else 1654 | VERSION='N/A' 1655 | fi 1656 | if [ $VERSION = "N/A" ]; then 1657 | >&2 swivm help 1658 | return 127 1659 | fi 1660 | fi 1661 | 1662 | provided_version="$1" 1663 | if [ -n "$provided_version" ]; then 1664 | VERSION="$(swivm_version "$provided_version")" 1665 | if [ "_$VERSION" = "_N/A" ] && ! swivm_is_valid_version "$provided_version"; then 1666 | provided_version='' 1667 | if [ $has_checked_swivmrc -ne 1 ]; then 1668 | if [ "$SWIVM_SILENT" -eq 1 ]; then 1669 | swivm_rc_version >/dev/null 2>&1 && has_checked_swivmrc=1 1670 | else 1671 | swivm_rc_version && has_checked_swivmrc=1 1672 | fi 1673 | fi 1674 | VERSION="$(swivm_version "$SWIVM_RC_VERSION")" 1675 | else 1676 | shift 1677 | fi 1678 | fi 1679 | 1680 | local ARGS 1681 | ARGS="$@" 1682 | local OUTPUT 1683 | local EXIT_CODE 1684 | 1685 | local ZHS_HAS_SHWORDSPLIT_UNSET 1686 | ZHS_HAS_SHWORDSPLIT_UNSET=1 1687 | if swivm_has "setopt"; then 1688 | ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; echo $?) 1689 | setopt shwordsplit 1690 | fi 1691 | if [ "_$VERSION" = "_N/A" ]; then 1692 | swivm_ensure_version_installed "$provided_version" 1693 | EXIT_CODE=$? 1694 | elif [ -z "$ARGS" ]; then 1695 | swivm exec "$VERSION" swipl 1696 | EXIT_CODE="$?" 1697 | else 1698 | [ $SWIVM_SILENT -eq 1 ] || echo "Running SWI-Prolog $VERSION$(swivm use --silent "$VERSION")" 1699 | OUTPUT="$(swivm use "$VERSION" >/dev/null && swipl $ARGS)" 1700 | EXIT_CODE="$?" 1701 | fi 1702 | if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && swivm_has "unsetopt"; then 1703 | unsetopt shwordsplit 1704 | fi 1705 | if [ -n "$OUTPUT" ]; then 1706 | echo "$OUTPUT" 1707 | fi 1708 | return $EXIT_CODE 1709 | ;; 1710 | "exec" ) 1711 | local SWIVM_SILENT 1712 | while [ $# -gt 0 ]; do 1713 | case "$1" in 1714 | --silent) SWIVM_SILENT='--silent' ; shift ;; 1715 | --) break ;; 1716 | --*) 1717 | swivm_err "Unsupported option \"$1\"." 1718 | return 55 1719 | ;; 1720 | *) 1721 | if [ -n "$1" ]; then 1722 | break 1723 | else 1724 | shift 1725 | fi 1726 | ;; # stop processing arguments 1727 | esac 1728 | done 1729 | 1730 | local provided_version 1731 | provided_version="$1" 1732 | if [ -n "${provided_version}" ]; then 1733 | VERSION="$(swivm_version "${provided_version}")" ||: 1734 | if [ "_${VERSION}" = '_N/A' ] && ! swivm_is_valid_version "${provided_version}"; then 1735 | if [ -n "${SWIVM_SILENT-}" ]; then 1736 | swivm_rc_version >/dev/null 2>&1 1737 | else 1738 | swivm_rc_version 1739 | fi 1740 | provided_version="${SWIVM_RC_VERSION}" 1741 | unset SWIVM_RC_VERSION 1742 | VERSION="$(swivm_version "${provided_version}")" ||: 1743 | else 1744 | shift 1745 | fi 1746 | fi 1747 | 1748 | swivm_ensure_version_installed "${provided_version}" 1749 | EXIT_CODE=$? 1750 | if [ "${EXIT_CODE}" != "0" ]; then 1751 | return $EXIT_CODE 1752 | fi 1753 | 1754 | if [ -z "${SWIVM_SILENT-}" ]; then 1755 | swivm_echo "Running SWI-Prolog ${VERSION}" 1756 | fi 1757 | SWI_VERSION="${VERSION}" "${SWIVM_DIR}/swivm-exec" "$@" 1758 | ;; 1759 | "ls" | "list" ) 1760 | local PATTERN 1761 | local SWIVM_NO_COLORS 1762 | local SWIVM_NO_ALIAS 1763 | while [ $# -gt 0 ]; do 1764 | case "${1}" in 1765 | --) ;; 1766 | --no-colors) SWIVM_NO_COLORS="${1}" ;; 1767 | --no-alias) SWIVM_NO_ALIAS="${1}" ;; 1768 | --*) 1769 | swivm_err "Unsupported option \"${1}\"." 1770 | return 55 1771 | ;; 1772 | *) 1773 | PATTERN="${PATTERN:-$1}" 1774 | ;; 1775 | esac 1776 | shift 1777 | done 1778 | if [ -n "${PATTERN-}" ] && [ -n "${SWIVM_NO_ALIAS-}" ]; then 1779 | swivm_err '`--no-alias` is not supported when a pattern is provided.' 1780 | return 55 1781 | fi 1782 | local SWIVM_LS_OUTPUT 1783 | local SWIVM_LS_EXIT_CODE 1784 | SWIVM_LS_OUTPUT=$(swivm_ls "${PATTERN-}") 1785 | SWIVM_LS_EXIT_CODE=$? 1786 | SWIVM_NO_COLORS="${SWIVM_NO_COLORS-}" swivm_print_versions "${SWIVM_LS_OUTPUT}" 1787 | if [ -z "${SWIVM_NO_ALIAS-}" ] && [ -z "${PATTERN-}" ]; then 1788 | if [ -n "${SWIVM_NO_COLORS-}" ]; then 1789 | swivm alias --no-colors 1790 | else 1791 | swivm alias 1792 | fi 1793 | fi 1794 | return $SWIVM_LS_EXIT_CODE 1795 | ;; 1796 | "ls-remote" | "list-remote") 1797 | local PATTERN 1798 | local SWIVM_NO_COLORS 1799 | while [ $# -gt 0 ]; do 1800 | case "${1-}" in 1801 | --) ;; 1802 | --no-colors) SWIVM_NO_COLORS="${1}" ;; 1803 | --*) 1804 | swivm_err "Unsupported option \"${1}\"." 1805 | return 55 1806 | ;; 1807 | *) 1808 | if [ -z "${PATTERN-}" ]; then 1809 | PATTERN="${1-}" 1810 | fi 1811 | ;; 1812 | esac 1813 | shift 1814 | done 1815 | 1816 | local SWIVM_OUTPUT 1817 | local EXIT_CODE 1818 | SWIVM_OUTPUT="$(swivm_remote_versions "${PATTERN}" &&:)" 1819 | EXIT_CODE=$? 1820 | if [ -n "${SWIVM_OUTPUT}" ]; then 1821 | SWIVM_NO_COLORS="${SWIVM_NO_COLORS-}" swivm_print_versions "${SWIVM_OUTPUT}" 1822 | return $EXIT_CODE 1823 | fi 1824 | SWIVM_NO_COLORS="${SWIVM_NO_COLORS-}" swivm_print_versions "N/A" 1825 | return 3 1826 | ;; 1827 | "current" ) 1828 | local SWIVM_PACK 1829 | while [ $# -gt 0 ]; do 1830 | case "${1-}" in 1831 | --) ;; 1832 | --pack) SWIVM_PACK="${1}" ;; 1833 | --*) 1834 | swivm_err "Unsupported option \"${1}\"." 1835 | return 55 1836 | ;; 1837 | esac 1838 | shift 1839 | done 1840 | 1841 | if [ -n "${SWIVM_PACK}" ]; then 1842 | OUTPUT="$(swipl -g "absolute_file_name(pack('.'),D),writeln(D)" -t 'halt(1)')" 1843 | EXIT_CODE="$?" 1844 | if [ -n "$OUTPUT" ]; then 1845 | echo "$OUTPUT" 1846 | fi 1847 | return $EXIT_CODE 1848 | fi 1849 | 1850 | swivm_version current 1851 | ;; 1852 | "which") 1853 | local provided_version 1854 | provided_version="${1-}" 1855 | if [ $# -eq 0 ]; then 1856 | swivm_rc_version 1857 | if [ -n "${SWIVM_RC_VERSION}" ]; then 1858 | provided_version="${SWIVM_RC_VERSION}" 1859 | VERSION=$(swivm_version "${SWIVM_RC_VERSION}") ||: 1860 | else 1861 | VERSION=$(swivm_ls_current) 1862 | fi 1863 | unset SWIVM_RC_VERSION 1864 | elif [ "_${1}" != '_system' ]; then 1865 | VERSION="$(swivm_version "${provided_version}")" ||: 1866 | else 1867 | VERSION="${1-}" 1868 | fi 1869 | if [ -z "${VERSION}" ]; then 1870 | >&2 swivm --help 1871 | return 127 1872 | fi 1873 | 1874 | if [ "_${VERSION}" = '_system' ]; then 1875 | if swivm_has_system >/dev/null 2>&1; then 1876 | local SWIVM_BIN 1877 | SWIVM_BIN="$(swivm use system >/dev/null 2>&1 && command which swipl)" 1878 | if [ -n "${SWIVM_BIN}" ]; then 1879 | swivm_echo "${SWIVM_BIN}" 1880 | return 1881 | fi 1882 | return 1 1883 | fi 1884 | swivm_err 'System version of SWI-Prolog not found.' 1885 | return 127 1886 | elif [ "_${VERSION}" = "_∞" ]; then 1887 | swivm_err "The alias \"$2\" leads to an infinite loop. Aborting." 1888 | return 8 1889 | fi 1890 | 1891 | swivm_ensure_version_installed "${provided_version}" 1892 | EXIT_CODE=$? 1893 | if [ "${EXIT_CODE}" != "0" ]; then 1894 | return $EXIT_CODE 1895 | fi 1896 | local SWIVM_VERSION_DIR 1897 | SWIVM_VERSION_DIR="$(swivm_version_path "${VERSION}")" 1898 | swivm_echo "${SWIVM_VERSION_DIR}/bin/swipl" 1899 | ;; 1900 | "alias" ) 1901 | local SWIVM_ALIAS_DIR 1902 | SWIVM_ALIAS_DIR="$(swivm_alias_path)" 1903 | local SWIVM_CURRENT 1904 | SWIVM_CURRENT="$(swivm_ls_current)" 1905 | 1906 | local ALIAS 1907 | local TARGET 1908 | local SWIVM_NO_COLORS 1909 | ALIAS='--' 1910 | TARGET='--' 1911 | while [ $# -gt 0 ]; do 1912 | case "${1-}" in 1913 | --) ;; 1914 | --no-colors) SWIVM_NO_COLORS="${1}" ;; 1915 | --*) 1916 | swivm_err "Unsupported option \"${1}\"." 1917 | return 55 1918 | ;; 1919 | *) 1920 | if [ "${ALIAS}" = '--' ]; then 1921 | ALIAS="${1-}" 1922 | elif [ "${TARGET}" = '--' ]; then 1923 | TARGET="${1-}" 1924 | fi 1925 | ;; 1926 | esac 1927 | shift 1928 | done 1929 | 1930 | if [ -z "${TARGET}" ]; then 1931 | # for some reason the empty string was explicitly passed as the target 1932 | # so, unalias it. 1933 | swivm unalias "${ALIAS}" 1934 | return $? 1935 | elif [ "${TARGET}" != '--' ]; then 1936 | # a target was passed: create an alias 1937 | if [ "${ALIAS#*\/}" != "${ALIAS}" ]; then 1938 | swivm_err 'Aliases in subdirectories are not supported.' 1939 | return 1 1940 | fi 1941 | VERSION="$(swivm_version "${TARGET}")" ||: 1942 | if [ "${VERSION}" = 'N/A' ]; then 1943 | swivm_err "! WARNING: Version '${TARGET}' does not exist." 1944 | fi 1945 | swivm_make_alias "${ALIAS}" "${TARGET}" 1946 | SWIVM_NO_COLORS="${SWIVM_NO_COLORS-}" SWIVM_CURRENT="${SWIVM_CURRENT-}" DEFAULT=false swivm_print_formatted_alias "${ALIAS}" "${TARGET}" "${VERSION}" 1947 | else 1948 | if [ "${ALIAS-}" = '--' ]; then 1949 | unset ALIAS 1950 | fi 1951 | 1952 | swivm_list_aliases "${ALIAS-}" 1953 | fi 1954 | ;; 1955 | "unalias" ) 1956 | local SWIVM_ALIAS_DIR 1957 | SWIVM_ALIAS_DIR="$(swivm_alias_path)" 1958 | command mkdir -p "${SWIVM_ALIAS_DIR}" 1959 | if [ $# -ne 1 ]; then 1960 | >&2 swivm --help 1961 | return 127 1962 | fi 1963 | if [ "${1#*\/}" != "${1-}" ]; then 1964 | swivm_err 'Aliases in subdirectories are not supported.' 1965 | return 1 1966 | fi 1967 | 1968 | local SWIVM_ALIAS_EXISTS 1969 | SWIVM_ALIAS_EXISTS=0 1970 | if [ -f "${SWIVM_ALIAS_DIR}/${1-}" ]; then 1971 | SWIVM_ALIAS_EXISTS=1 1972 | fi 1973 | 1974 | if [ $SWIVM_ALIAS_EXISTS -eq 0 ]; then 1975 | case "$1" in 1976 | "stable" | "devel" | "system") 1977 | swivm_err "${1-} is a default (built-in) alias and cannot be deleted." 1978 | return 1 1979 | ;; 1980 | esac 1981 | 1982 | swivm_err "Alias ${1-} doesn't exist!" 1983 | return 1984 | fi 1985 | 1986 | local SWIVM_ALIAS_ORIGINAL 1987 | SWIVM_ALIAS_ORIGINAL="$(swivm_alias "${1}")" 1988 | command rm -f "${SWIVM_ALIAS_DIR}/${1}" 1989 | swivm_echo "Deleted alias ${1} - restore it with \`swivm alias \"${1}\" \"${SWIVM_ALIAS_ORIGINAL}\"\`" 1990 | ;; 1991 | "lib" ) 1992 | local provided_version 1993 | provided_version="${1-}" 1994 | if [ $# -eq 0 ]; then 1995 | swivm_rc_version 1996 | if [ -n "${SWIVM_RC_VERSION}" ]; then 1997 | provided_version="${SWIVM_RC_VERSION}" 1998 | VERSION=$(swivm_version "${SWIVM_RC_VERSION}") ||: 1999 | else 2000 | VERSION=$(swivm_ls_current) 2001 | fi 2002 | unset SWIVM_RC_VERSION 2003 | elif [ "_${1}" != '_system' ]; then 2004 | VERSION="$(swivm_version "${provided_version}")" ||: 2005 | else 2006 | VERSION="${1-}" 2007 | fi 2008 | if [ -z "${VERSION}" ]; then 2009 | >&2 swivm --help 2010 | return 127 2011 | fi 2012 | 2013 | if [ "_${VERSION}" = '_system' ]; then 2014 | swivm_err 'Setting links for system version of SWI-Prolog not supported.' 2015 | return 127 2016 | elif [ "_${VERSION}" = "_∞" ]; then 2017 | swivm_err "The alias \"$2\" leads to an infinite loop. Aborting." 2018 | return 8 2019 | fi 2020 | 2021 | swivm_ensure_version_installed "${provided_version}" 2022 | EXIT_CODE=$? 2023 | if [ "${EXIT_CODE}" != "0" ]; then 2024 | return $EXIT_CODE 2025 | fi 2026 | 2027 | swivm_lib "${VERSION-}" 2028 | ;; 2029 | "clear-cache" ) 2030 | command rm -f "$SWIVM_DIR/v*" "$(swivm_version_dir)" 2>/dev/null 2031 | echo "Cache cleared." 2032 | ;; 2033 | "version" ) 2034 | swivm_version "${1}" 2035 | ;; 2036 | "version-remote" ) 2037 | swivm_remote_version "${1}" 2038 | ;; 2039 | "--version" ) 2040 | echo "1.3.2" 2041 | ;; 2042 | "unload") 2043 | swivm deactivate >/dev/null 2>&1 2044 | unset -f swivm \ 2045 | swivm_is_alias \ 2046 | swivm_ls_remote swivm_ls_remote_index \ 2047 | swivm_ls swivm_remote_version swivm_remote_versions \ 2048 | nvm_check_file_permissions \ 2049 | swivm_print_versions \ 2050 | swivm_version swivm_rc_version swivm_match_version \ 2051 | swivm_ensure_default_set swivm_get_arch swivm_get_os \ 2052 | swivm_print_implicit_alias swivm_validate_implicit_alias \ 2053 | swivm_resolve_alias swivm_ls_current swivm_alias \ 2054 | swivm_change_path swivm_strip_path \ 2055 | swivm_num_version_groups swivm_format_version swivm_ensure_version_prefix \ 2056 | swivm_normalize_version swivm_is_valid_version \ 2057 | swivm_ensure_version_installed \ 2058 | swivm_version_path swivm_alias_path swivm_version_dir \ 2059 | swivm_find_swivmrc swivm_find_up swivm_tree_contains_path \ 2060 | swivm_version_greater swivm_version_greater_than_or_equal_to \ 2061 | swivm_has_system \ 2062 | swivm_download swivm_has \ 2063 | swivm_supports_source_options swivm_auto \ 2064 | swivm_echo swivm_err swivm_grep swivm_cd \ 2065 | swivm_die_on_prefix swivm_get_make_jobs \ 2066 | swivm_is_natural_num swivm_is_version_installed \ 2067 | swivm_list_aliases swivm_make_alias swivm_print_alias_path \ 2068 | swivm_print_default_alias swivm_print_formatted_alias swivm_resolve_local_alias \ 2069 | swivm_sanitize_path swivm_has_colors swivm_process_parameters \ 2070 | swivm_lib swivm_lib_path \ 2071 | swivm_is_zsh \ 2072 | >/dev/null 2>&1 2073 | unset SWIVM_RC_VERSION SWIVM_MIRROR GITHUB_MIRROR SWIVM_DIR \ 2074 | SWIVM_CD_FLAGS SWIVM_BIN SWIVM_MAKE_JOBS \ 2075 | >/dev/null 2>&1 2076 | ;; 2077 | * ) 2078 | >&2 swivm help 2079 | return 127 2080 | ;; 2081 | esac 2082 | } 2083 | 2084 | swivm_supports_source_options() { 2085 | [ "_$(echo '[ $# -gt 0 ] && echo $1' | . /dev/stdin yes 2> /dev/null)" = "_yes" ] 2086 | } 2087 | 2088 | swivm_auto() { 2089 | local SWIVM_CURRENT 2090 | SWIVM_CURRENT="$(swivm_ls_current)" 2091 | local SWIVM_MODE 2092 | SWIVM_MODE="${1-}" 2093 | local VERSION 2094 | if [ "_${SWIVM_MODE}" = '_install' ]; then 2095 | VERSION="$(swivm_alias default 2>/dev/null || swivm_echo)" 2096 | if [ -n "${VERSION}" ]; then 2097 | swivm install "${VERSION}" >/dev/null 2098 | elif swivm_rc_version >/dev/null 2>&1; then 2099 | swivm install >/dev/null 2100 | fi 2101 | elif [ "_$SWIVM_MODE" = '_use' ]; then 2102 | if [ "_${SWIVM_CURRENT}" = '_none' ] || [ "_${SWIVM_CURRENT}" = '_system' ]; then 2103 | VERSION="$(swivm_resolve_local_alias default 2>/dev/null || swivm_echo)" 2104 | if [ -n "${VERSION}" ]; then 2105 | swivm use --silent "${VERSION}" >/dev/null 2106 | elif swivm_rc_version >/dev/null 2>&1; then 2107 | swivm use --silent >/dev/null 2108 | fi 2109 | else 2110 | swivm use --silent "${SWIVM_CURRENT}" >/dev/null 2111 | fi 2112 | elif [ "_${SWIVM_MODE}" != '_none' ]; then 2113 | swivm_err 'Invalid auto mode supplied.' 2114 | return 1 2115 | fi 2116 | } 2117 | 2118 | swivm_process_parameters() { 2119 | local SWIVM_AUTO_MODE 2120 | SWIVM_AUTO_MODE='use' 2121 | if swivm_supports_source_options; then 2122 | while [ $# -ne 0 ]; do 2123 | case "$1" in 2124 | --install) SWIVM_AUTO_MODE='install' ;; 2125 | --no-use) SWIVM_AUTO_MODE='none' ;; 2126 | esac 2127 | shift 2128 | done 2129 | fi 2130 | swivm_auto "${SWIVM_AUTO_MODE}" 2131 | } 2132 | 2133 | swivm_process_parameters "$@" 2134 | 2135 | } # this ensures the entire script is downloaded # 2136 | --------------------------------------------------------------------------------