diff options
| author | ruki <[email protected]> | 2022-02-15 17:11:47 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-02-15 17:11:47 +0800 |
| commit | 5433ec8160a84427214458bc11153f9a7fe9cba7 (patch) | |
| tree | 1a209c950866f11d5d51daf6d8491d52d1cf0641 | |
| parent | ba13df1da1e2001bfa026b85a464c0b82fc0e0bc (diff) | |
| parent | 3e6b8e42ccbf7eab03f57d98025139423d477e66 (diff) | |
Merge pull request #2059 from xq114/dev
add shell utility registration
| -rw-r--r-- | xmake/actions/update/main.lua | 92 | ||||
| -rw-r--r-- | xmake/actions/update/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_powershell.lua | 54 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_pwsh.lua | 54 | ||||
| -rw-r--r-- | xmake/modules/private/action/update/fetch_version.lua | 1 | ||||
| -rw-r--r-- | xmake/scripts/profile-unix.sh | 87 | ||||
| -rw-r--r-- | xmake/scripts/profile-win.ps1 | 14 | ||||
| -rw-r--r-- | xmake/toolchains/msvc/check.lua | 3 |
8 files changed, 307 insertions, 1 deletions
diff --git a/xmake/actions/update/main.lua b/xmake/actions/update/main.lua index 7db7eda58..5ff75f69e 100644 --- a/xmake/actions/update/main.lua +++ b/xmake/actions/update/main.lua @@ -34,6 +34,7 @@ import("private.action.require.impl.environment") import("private.action.update.fetch_version") import("utils.archive") import("lib.detect.find_file") +import("lib.detect.find_tool") -- the installer filename for windows local win_installer_name = "xmake-installer.exe" @@ -106,6 +107,33 @@ end -- do uninstall function _uninstall() + + -- remove shell profile + local profiles = {} + if is_host("windows") then + for _, shell in ipairs({"pwsh", "powershell"}) do + for _, type in ipairs({"AllUsersAllHosts", "CurrentUserAllHosts", "CurrentUserCurrentHost"}) do + local psshell = find_tool(shell) + if psshell then + local outdata, errdata = try {function () return os.iorunv(psshell.program, {"-c", "Write-Output $PROFILE." .. type}) end} + if outdata then + table.insert(profiles, outdata:trim()) + end + end + end + end + else + for _, filename in ipairs({".zshrc", ".bashrc", ".kshrc", ".bash_profile", ".profile"}) do + table.insert(profiles, path.join(os.getenv("HOME"), filename)) + end + end + for _, profile in ipairs(profiles) do + if os.isfile(profile) then + io.gsub(profile, "# >>> xmake >>>.-# <<< xmake <<<", "") + end + end + + -- remove program if is_host("windows") then local uninstaller = path.join(os.programdir(), "uninstall.exe") if os.isfile(uninstaller) then @@ -257,6 +285,64 @@ function _install_script(sourcedir) end end +-- initialize shells +function _initialize_shell() + + local target, command, profile + if is_host("windows") then + local psshell = find_tool("pwsh") or find_tool("powershell") + local outdata, errdata = try {function () return os.iorunv(psshell.program, {"-c", "Write-Output $PROFILE.CurrentUserAllHosts"}) end} + if outdata then + target = outdata:trim() + command = "if (Test-Path -Path \"%s\" -PathType Leaf) {\n . \"%s\"\n}" + profile = path.join(os.programdir(), "scripts", "profile-win.ps1") + else + raise("failed to get profile location from powershell!") + end + else + target = "~/.profile" + local shell = os.shell() + if shell:endswith("bash") then target = (is_host("macosx") and "~/.bash_profile" or "~/.bashrc") + elseif shell:endswith("zsh") then target = "~/.zshrc" + elseif shell:endswith("ksh") then target = "~/.kshrc" + end + command = "[[ -s \"%s\" ]] && source \"%s\"" + profile = path.join(os.programdir(), "scripts", "profile-unix.sh") + end + + -- trace + cprintf("\r${yellow} => ${clear}installing shell integration to %s .. ", target) + + local ok = try + { + function () + local file = "" + if os.isfile(target) then + file = io.readfile(target) + file = file:gsub("# >>> xmake >>>.-# <<< xmake <<<", "") + if file ~= "" and not file:endswith("\n") then + file = file .. "\n" + end + end + file = file .. "# >>> xmake >>>\n" .. format(command, profile, profile) .. "\n# <<< xmake <<<" + io.writefile(target, file) + return true + end, + catch + { + function (errors) + vprint(errors) + end + } + } + -- trace + if ok then + cprint("${color.success}${text.success}") + else + cprint("${color.failure}${text.failure}") + end +end + function _check_repo(sourcedir) -- this file will exists for long time if not os.isfile(path.join(sourcedir, "xmake/core/_xmake_main.lua")) then @@ -298,6 +384,12 @@ function main() return end + -- initialize for shell interaction + if option.get("integrate") then + _initialize_shell() + return + end + -- enter environment environment.enter() diff --git a/xmake/actions/update/xmake.lua b/xmake/actions/update/xmake.lua index 2672a1c97..e7a074024 100644 --- a/xmake/actions/update/xmake.lua +++ b/xmake/actions/update/xmake.lua @@ -40,7 +40,8 @@ task("update") { {nil, "uninstall", "k", nil, "Uninstall the current xmake program." } , { } - , {'s', "scriptonly", "k", nil, "Update script only" } + , {'s', "scriptonly", "k", nil, "Update scripts only." } + , {nil, "integrate", "k", nil, "Integrate xmake with default shell." } , {'f', "force", "k", nil, "Force to update and reinstall the given version." } , {nil, "xmakever", "v", nil, "The given xmake version, or a git source (and branch). ", "e.g.", diff --git a/xmake/modules/detect/tools/find_powershell.lua b/xmake/modules/detect/tools/find_powershell.lua new file mode 100644 index 000000000..78cb35afd --- /dev/null +++ b/xmake/modules/detect/tools/find_powershell.lua @@ -0,0 +1,54 @@ +--!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) 2015-present, TBOOX Open Source Group. +-- +-- @author xq114 +-- @file find_powershell.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") + +-- find powershell +-- +-- @param opt the argument options, e.g. {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local powershell = find_powershell() +-- +-- @endcode +-- +function main(opt) + + -- init options + opt = opt or {} + opt.command = opt.command or "--version" + + -- find program + local program = find_program(opt.program or "powershell", opt) + + -- find program version + local version = nil + if program and opt and opt.version then + version = find_programver(program, opt) + end + + -- ok? + return program, version +end diff --git a/xmake/modules/detect/tools/find_pwsh.lua b/xmake/modules/detect/tools/find_pwsh.lua new file mode 100644 index 000000000..4b42ad8bf --- /dev/null +++ b/xmake/modules/detect/tools/find_pwsh.lua @@ -0,0 +1,54 @@ +--!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) 2015-present, TBOOX Open Source Group. +-- +-- @author xq114 +-- @file find_pwsh.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") + +-- find pwsh +-- +-- @param opt the argument options, e.g. {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local pwsh = find_pwsh() +-- +-- @endcode +-- +function main(opt) + + -- init options + opt = opt or {} + opt.command = opt.command or "--version" + + -- find program + local program = find_program(opt.program or "pwsh", opt) + + -- find program version + local version = nil + if program and opt and opt.version then + version = find_programver(program, opt) + end + + -- ok? + return program, version +end diff --git a/xmake/modules/private/action/update/fetch_version.lua b/xmake/modules/private/action/update/fetch_version.lua index af43400ca..18ea762ef 100644 --- a/xmake/modules/private/action/update/fetch_version.lua +++ b/xmake/modules/private/action/update/fetch_version.lua @@ -30,6 +30,7 @@ import("net.fasturl") local official_sources = { "https://github.com/xmake-io/xmake.git", + "[email protected]:xmake-io/xmake.git", "https://gitlab.com/tboox/xmake.git", "https://gitee.com/tboox/xmake.git" } diff --git a/xmake/scripts/profile-unix.sh b/xmake/scripts/profile-unix.sh new file mode 100644 index 000000000..26d9d6a66 --- /dev/null +++ b/xmake/scripts/profile-unix.sh @@ -0,0 +1,87 @@ +# 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 +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 +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 diff --git a/xmake/scripts/profile-win.ps1 b/xmake/scripts/profile-win.ps1 new file mode 100644 index 000000000..493543040 --- /dev/null +++ b/xmake/scripts/profile-win.ps1 @@ -0,0 +1,14 @@ +# PowerShell parameter completion for xmake +Register-ArgumentCompleter -Native -CommandName xmake -ScriptBlock { + param($commandName, $wordToComplete, $cursorPosition) + $complete = "$wordToComplete" + if (-not $commandName) { + $complete = $complete + " " + } + $oldenv = $env:XMAKE_SKIP_HISTORY + $env:XMAKE_SKIP_HISTORY = 1 + xmake lua --root private.utils.complete "0" "nospace" "$complete" | ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) + } + $env:XMAKE_SKIP_HISTORY = $oldenv +}
\ No newline at end of file diff --git a/xmake/toolchains/msvc/check.lua b/xmake/toolchains/msvc/check.lua index ce3fdd8d9..b7d81a0e9 100644 --- a/xmake/toolchains/msvc/check.lua +++ b/xmake/toolchains/msvc/check.lua @@ -29,6 +29,9 @@ function _check_vsenv(toolchain) -- have been checked? local vs = toolchain:config("vs") or config.get("vs") + if vs then + vs = tostring(vs) + end local vcvars = toolchain:config("vcvars") if vs and vcvars then return vs |
