diff options
| author | ruki <[email protected]> | 2023-03-03 14:20:59 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-03-03 14:20:59 +0800 |
| commit | 0973396b09535d70d6c4c5613ea72bf5ee832137 (patch) | |
| tree | 617d3c1055f0168a12f3f253c2ae7d68a89b2669 /xmake/scripts | |
| parent | d469035fc49b6565a2ba7366d148d7f239bc352e (diff) | |
| parent | 3d05713324f5192728cf4f3131fc7295b3ef72a5 (diff) | |
Merge pull request #3447 from xmake-io/profile
improve shell profile
Diffstat (limited to 'xmake/scripts')
| -rwxr-xr-x | xmake/scripts/completions/register-completions.bash | 45 | ||||
| -rwxr-xr-x | xmake/scripts/completions/register-completions.fish | 69 | ||||
| -rwxr-xr-x | xmake/scripts/completions/register-completions.zsh | 36 | ||||
| -rw-r--r-- | xmake/scripts/profile-unix.fish | 26 | ||||
| -rw-r--r-- | xmake/scripts/profile-unix.sh | 132 | ||||
| -rw-r--r-- | xmake/scripts/virtualenvs/register-virtualenvs.sh | 89 |
6 files changed, 293 insertions, 104 deletions
diff --git a/xmake/scripts/completions/register-completions.bash b/xmake/scripts/completions/register-completions.bash new file mode 100755 index 000000000..5569b4af8 --- /dev/null +++ b/xmake/scripts/completions/register-completions.bash @@ -0,0 +1,45 @@ +# A cross-platform build utility based on Lua +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:##www.apache.org#licenses#LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Copyright (C) 2022-present, TBOOX Open Source Group. +# +# @author ruki +# @homepage register-completions.bash +# + +# bash parameter completion for xmake +_xmake_bash_complete() +{ + local word=${COMP_WORDS[COMP_CWORD]} + local completions + completions="$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.utils.complete "${COMP_POINT}" "nospace-nokey" "${COMP_LINE}")" + if [ $? -ne 0 ]; then + completions="" + fi + COMPREPLY=( $(compgen -W "$completions") ) +} +complete -o default -o nospace -F _xmake_bash_complete xmake + +# bash parameter completion for xrepo +_xrepo_bash_complete() +{ + local word=${COMP_WORDS[COMP_CWORD]} + local completions + completions="$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.xrepo.complete "${COMP_POINT}" "nospace-nokey" "${COMP_LINE}")" + if [ $? -ne 0 ]; then + completions="" + fi + COMPREPLY=( $(compgen -W "$completions") ) +} +complete -o default -o nospace -F _xrepo_bash_complete xrepo diff --git a/xmake/scripts/completions/register-completions.fish b/xmake/scripts/completions/register-completions.fish new file mode 100755 index 000000000..5cb73492e --- /dev/null +++ b/xmake/scripts/completions/register-completions.fish @@ -0,0 +1,69 @@ +# A cross-platform build utility based on Lua +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:##www.apache.org#licenses#LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Copyright (C) 2022-present, TBOOX Open Source Group. +# +# @author ruki, Dragon1573 +# @homepage register-completions.fish +# + +# fish parameter completion for xmake +function _xmake_fish_complete + # Read the current command line + set -l raw_cmd (commandline) + set -l words (commandline -o) + # Get the current token + set -l token (commandline -ot) + + # Call XMake built-in completion to get available results + set -l result (MAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.utils.complete 0 nospace "$raw_cmd") + if test (count $result) -lt 1 + # When there are no available completions, the function ends immediately + # Otherwise, the subsequent `contains` command will trigger an error + return 1 + end + + # Are there multiple selectable results? + # When there are multiple options, the user's current token is definitely incomplete + test (count $result) -gt 1 + set -l is_multiple_choice $status + # Is the only selectable option identical to the current token under the cursor (which may be empty)? + # Identical means the user has already completed the process + # Repeated option outputs are not completions at the current cursor position + contains -- $token $result + set -l is_token_incomplete $status + # Is the only selectable option identical to the last visible token of the command line array? + # When completing without a prefix in every token position, the current token is an empty string + # The last item of the command line tokenized array is the token before the cursor + # This is mainly used to handle completions triggered after a space without a prefix + contains -- $words[-1] $result + set -l not_token_completed $status + + test \( $is_token_incomplete -eq 1 \) -a \( $not_token_completed -eq 1 \) + test \( $is_multiple_choice -eq 0 \) -o \( $status -eq 0 \) + if test $status -eq 0 + # When the completion list has more than one item, or the current token is different from the unique token in the completion list + # The user must not have completed the token and needs to be provided with completion options + for item in $result + # Each result takes up a separate line, and for the "-a" parameter, it needs to be echoed separately + # Although the result itself has a newline, "-a" still treats it as a whole, + # Without tokenizing it as multiple parameters + if not string match -- '*error*' "$item" > /dev/null + echo $item + end + end + end +end + +complete -c xmake -f -a "(_xmake_fish_complete)" diff --git a/xmake/scripts/completions/register-completions.zsh b/xmake/scripts/completions/register-completions.zsh new file mode 100755 index 000000000..eeaa42e7c --- /dev/null +++ b/xmake/scripts/completions/register-completions.zsh @@ -0,0 +1,36 @@ +# A cross-platform build utility based on Lua +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:##www.apache.org#licenses#LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Copyright (C) 2022-present, TBOOX Open Source Group. +# +# @author ruki +# @homepage register-completions.zsh +# + +# zsh parameter completion for xmake +_xmake_zsh_complete() +{ + local completions=("$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.utils.complete 0 nospace "$words")") + reply=( "${(ps:\n:)completions}" ) +} +compctl -f -S "" -K _xmake_zsh_complete xmake + +# zsh parameter completion for xrepo +_xrepo_zsh_complete() +{ + local completions=("$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.xrepo.complete 0 nospace "$words")") + reply=( "${(ps:\n:)completions}" ) +} +compctl -f -S "" -K _xrepo_zsh_complete xrepo + diff --git a/xmake/scripts/profile-unix.fish b/xmake/scripts/profile-unix.fish new file mode 100644 index 000000000..ea0f243de --- /dev/null +++ b/xmake/scripts/profile-unix.fish @@ -0,0 +1,26 @@ +# A cross-platform build utility based on Lua +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:##www.apache.org#licenses#LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Copyright (C) 2022-present, TBOOX Open Source Group. +# +# @author ruki +# @homepage profile-unix.fish +# + +# register environments +export XMAKE_SHELL=fish + +# register completions +. "$XMAKE_PROGRAM_DIR/scripts/completions/register-completions.fish" + diff --git a/xmake/scripts/profile-unix.sh b/xmake/scripts/profile-unix.sh index cd511b04f..c3e62a734 100644 --- a/xmake/scripts/profile-unix.sh +++ b/xmake/scripts/profile-unix.sh @@ -1,106 +1,30 @@ -# parameter completions for *nix shell -if [[ "$SHELL" = */zsh ]]; then - # zsh parameter completion for xmake - _xmake_zsh_complete() - { - local completions=("$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.utils.complete 0 nospace "$words")") - reply=( "${(ps:\n:)completions}" ) - } - compctl -f -S "" -K _xmake_zsh_complete xmake - # zsh parameter completion for xrepo - _xrepo_zsh_complete() - { - local completions=("$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.xrepo.complete 0 nospace "$words")") - reply=( "${(ps:\n:)completions}" ) - } - compctl -f -S "" -K _xrepo_zsh_complete xrepo +# A cross-platform build utility based on Lua +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:##www.apache.org#licenses#LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Copyright (C) 2022-present, TBOOX Open Source Group. +# +# @author ruki +# @homepage profile-unix.sh +# + +# register completions +if [[ "$SHELL" = */zsh ]]; then + . "$XMAKE_PROGRAM_DIR/scripts/completions/register-completions.zsh" elif [[ "$SHELL" = */bash ]]; then - # bash parameter completion for xmake - _xmake_bash_complete() - { - local word=${COMP_WORDS[COMP_CWORD]} - local completions - completions="$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.utils.complete "${COMP_POINT}" "nospace-nokey" "${COMP_LINE}")" - if [ $? -ne 0 ]; then - completions="" - fi - COMPREPLY=( $(compgen -W "$completions") ) - } - complete -o default -o nospace -F _xmake_bash_complete xmake - # bash parameter completion for xrepo - _xrepo_bash_complete() - { - local word=${COMP_WORDS[COMP_CWORD]} - local completions - completions="$(XMAKE_SKIP_HISTORY=1 XMAKE_ROOT=y xmake lua private.xrepo.complete "${COMP_POINT}" "nospace-nokey" "${COMP_LINE}")" - if [ $? -ne 0 ]; then - completions="" - fi - COMPREPLY=( $(compgen -W "$completions") ) - } - complete -o default -o nospace -F _xrepo_bash_complete xrepo + . "$XMAKE_PROGRAM_DIR/scripts/completions/register-completions.bash" fi -# virtual environments for *nix shell -function xrepo { - if [ $# -ge 2 ] && [ "$1" = "env" ]; then - local cmd="${2-x}" - case "$cmd" in - shell) - if test "${XMAKE_PROMPT_BACKUP}"; then - PS1="${XMAKE_PROMPT_BACKUP}" - source "${XMAKE_ENV_BACKUP}" || return 1 - unset XMAKE_PROMPT_BACKUP - unset XMAKE_ENV_BACKUP - fi - xmake lua private.xrepo.action.env.info config || return 1 - local prompt="$(xmake lua --quiet private.xrepo.action.env.info prompt)" || return 1 - if [ -z "${prompt+x}" ]; then - return 1 - fi - local activateCommand="$(xmake lua private.xrepo.action.env.info script.bash)" || return 1 - export XMAKE_ENV_BACKUP="$(xmake lua private.xrepo.action.env.info envfile)" - export XMAKE_PROMPT_BACKUP="${PS1}" - xmake lua private.xrepo.action.env.info backup.bash 1>"$XMAKE_ENV_BACKUP" - eval "$activateCommand" - PS1="${prompt} $PS1" - ;; - quit) - if test "${XMAKE_PROMPT_BACKUP}"; then - PS1="${XMAKE_PROMPT_BACKUP}" - source "${XMAKE_ENV_BACKUP}" || return 1 - unset XMAKE_PROMPT_BACKUP - unset XMAKE_ENV_BACKUP - fi - ;; - -b|--bind) - if [ "$4" = "shell" ]; then - local bnd="${3-x}" - if test "${XMAKE_PROMPT_BACKUP}"; then - PS1="${XMAKE_PROMPT_BACKUP}" - source "${XMAKE_ENV_BACKUP}" || return 1 - unset XMAKE_PROMPT_BACKUP - unset XMAKE_ENV_BACKUP - fi - xmake lua private.xrepo.action.env.info config $bnd || return 1 - local prompt="$(xmake lua --quiet private.xrepo.action.env.info prompt $bnd)" || return 1 - if [ -z "${prompt+x}" ]; then - return 1 - fi - local activateCommand="$(xmake lua --quiet private.xrepo.action.env.info script.bash $bnd)" || return 1 - export XMAKE_ENV_BACKUP="$(xmake lua private.xrepo.action.env.info envfile $bnd)" - export XMAKE_PROMPT_BACKUP="${PS1}" - xmake lua --quiet private.xrepo.action.env.info backup.bash $bnd 1>"$XMAKE_ENV_BACKUP" - eval "$activateCommand" - PS1="${prompt} $PS1" - else - xmake lua private.xrepo "$@" - fi - ;; - *) - xmake lua private.xrepo "$@" - ;; - esac - else - xmake lua private.xrepo "$@" - fi -}
\ No newline at end of file + +# register virtualenvs +. "$XMAKE_PROGRAM_DIR/scripts/virtualenvs/register-virtualenvs.sh" + diff --git a/xmake/scripts/virtualenvs/register-virtualenvs.sh b/xmake/scripts/virtualenvs/register-virtualenvs.sh new file mode 100644 index 000000000..e853a54ba --- /dev/null +++ b/xmake/scripts/virtualenvs/register-virtualenvs.sh @@ -0,0 +1,89 @@ +# A cross-platform build utility based on Lua +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:##www.apache.org#licenses#LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Copyright (C) 2022-present, TBOOX Open Source Group. +# +# @author ruki, xq114 +# @homepage register-virtualenvs.sh +# + +if test "${XMAKE_ROOTDIR}"; then + export XMAKE_EXE=${XMAKE_ROOTDIR}/xmake +else + export XMAKE_EXE=xmake +fi + +function xrepo { + if [ $# -ge 2 ] && [ "$1" = "env" ]; then + local cmd="${2-x}" + case "$cmd" in + shell) + if test "${XMAKE_PROMPT_BACKUP}"; then + PS1="${XMAKE_PROMPT_BACKUP}" + source "${XMAKE_ENV_BACKUP}" || return 1 + unset XMAKE_PROMPT_BACKUP + unset XMAKE_ENV_BACKUP + fi + "$XMAKE_EXE" lua private.xrepo.action.env.info config || return 1 + local prompt="$("$XMAKE_EXE" lua --quiet private.xrepo.action.env.info prompt)" || return 1 + if [ -z "${prompt+x}" ]; then + return 1 + fi + local activateCommand="$("$XMAKE_EXE" lua private.xrepo.action.env.info script.bash)" || return 1 + export XMAKE_ENV_BACKUP="$("$XMAKE_EXE" lua private.xrepo.action.env.info envfile)" + export XMAKE_PROMPT_BACKUP="${PS1}" + "$XMAKE_EXE" lua private.xrepo.action.env.info backup.bash 1>"$XMAKE_ENV_BACKUP" + eval "$activateCommand" + PS1="${prompt} $PS1" + ;; + quit) + if test "${XMAKE_PROMPT_BACKUP}"; then + PS1="${XMAKE_PROMPT_BACKUP}" + source "${XMAKE_ENV_BACKUP}" || return 1 + unset XMAKE_PROMPT_BACKUP + unset XMAKE_ENV_BACKUP + fi + ;; + -b|--bind) + if [ "$4" = "shell" ]; then + local bnd="${3-x}" + if test "${XMAKE_PROMPT_BACKUP}"; then + PS1="${XMAKE_PROMPT_BACKUP}" + source "${XMAKE_ENV_BACKUP}" || return 1 + unset XMAKE_PROMPT_BACKUP + unset XMAKE_ENV_BACKUP + fi + "$XMAKE_EXE" lua private.xrepo.action.env.info config $bnd || return 1 + local prompt="$("$XMAKE_EXE" lua --quiet private.xrepo.action.env.info prompt $bnd)" || return 1 + if [ -z "${prompt+x}" ]; then + return 1 + fi + local activateCommand="$("$XMAKE_EXE" lua --quiet private.xrepo.action.env.info script.bash $bnd)" || return 1 + export XMAKE_ENV_BACKUP="$("$XMAKE_EXE" lua private.xrepo.action.env.info envfile $bnd)" + export XMAKE_PROMPT_BACKUP="${PS1}" + "$XMAKE_EXE" lua --quiet private.xrepo.action.env.info backup.bash $bnd 1>"$XMAKE_ENV_BACKUP" + eval "$activateCommand" + PS1="${prompt} $PS1" + else + "$XMAKE_EXE" lua private.xrepo "$@" + fi + ;; + *) + "$XMAKE_EXE" lua private.xrepo "$@" + ;; + esac + else + "$XMAKE_EXE" lua private.xrepo "$@" + fi +} |
