From 6435075a1687fe811620c668ec1970868638ba60 Mon Sep 17 00:00:00 2001 From: SPeak Date: Sat, 11 Jan 2025 01:33:40 +0800 Subject: [PATCH] tools: add profile file, and adapt bash/zsh/fish... (#69) - #66 1. independent xlings profile 2. auto config for local shell Signed-off-by: sunrisepeak --- core/common.lua | 46 ++++++++++++++++++++++++++------- tools/shell/xlings-profile.fish | 13 ++++++++++ tools/shell/xlings-profile.sh | 13 ++++++++++ 3 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 tools/shell/xlings-profile.fish create mode 100644 tools/shell/xlings-profile.sh diff --git a/core/common.lua b/core/common.lua index 0e9c5d7..a221b7b 100644 --- a/core/common.lua +++ b/core/common.lua @@ -248,21 +248,49 @@ function xlings_install() cprint("[xlings]: create bindir %s", bindir) os.cp(path.join(install_dir, "bin"), bindir, {force = true}) + -- copy profile to rcachedir + cprint("[xlings]: copy profile to rcachedir...") + os.cp(path.join(install_dir, "tools", "shell", "xlings-profile.*"), rcachedir, {force = true}) + -- add bin to linux bashrc and windows's path env cprint("[xlings]: add bin to linux's .bashrc or windows's path env") if is_host("linux") then - local bashrc = os.getenv("HOME") .. "/.bashrc" - local content = string.format("\nexport PATH=%s:$PATH", platform.get_config_info().bindir) - -- append to bashrc when not include xlings str in .bashrc - if not os.isfile(bashrc) then - xlings_create_file_and_write(bashrc, content) - else - local bashrc_content = io.readfile(bashrc) - if not string.find(bashrc_content, content, 1, true) then - xlings_file_append(bashrc, content) + local source_cmd_template = "\ntest -f %s && source %s" + local posix_profile = path.join(rcachedir, "xlings-profile.sh") + local fish_profile = path.join(rcachedir, "xlings-profile.fish") + local shells = { + bash = { + profile = ".bashrc", + command = format(source_cmd_template, posix_profile, posix_profile) + }, + zsh = { + profile = ".zshrc", + command = format(source_cmd_template, posix_profile, posix_profile) + }, + fish = { + profile = ".config/fish/config.fish", + command = format("\ntest -f %s; and source %s", fish_profile, fish_profile) + } + } + + function config_shell(shell) + local profile = path.join(os.getenv("HOME"), shells[shell].profile) + local command = shells[shell].command + if os.isfile(profile) then + cprint("[xlings]: config [%s] shell - %s", shell, profile) + local profile_content = io.readfile(profile) + if not string.find(profile_content, command, 1, true) then + xlings_file_append(profile, command) + else + cprint("[xlings]: [%s] already configed", shell) + end end end + + for shell, _ in pairs(shells) do + config_shell(shell) + end else local path_env = os.getenv("PATH") if not string.find(path_env, install_dir, 1, true) then diff --git a/tools/shell/xlings-profile.fish b/tools/shell/xlings-profile.fish new file mode 100644 index 0000000..439501e --- /dev/null +++ b/tools/shell/xlings-profile.fish @@ -0,0 +1,13 @@ +# Xlings +set -x XLINGS_HOME "$HOME/.xlings" +set -x XLINGS_DATA "$HOME/.xlings_data" +set -x XLINGS_BIN "$HOME/.xlings_data/bin" +set -x PATH "$XLINGS_BIN" $PATH + +# XVM +set -x XVM_WORKSPACE_NAME "global" + +# Function to update the workspace name +function xvm-workspace + set -x XVM_WORKSPACE_NAME $argv[1] +end \ No newline at end of file diff --git a/tools/shell/xlings-profile.sh b/tools/shell/xlings-profile.sh new file mode 100644 index 0000000..f07e74e --- /dev/null +++ b/tools/shell/xlings-profile.sh @@ -0,0 +1,13 @@ +# Xlings +export XLINGS_HOME="$HOME/.xlings" +export XLINGS_DATA="$HOME/.xlings_data" +export XLINGS_BIN="$HOME/.xlings_data/bin" +export PATH="$XLINGS_BIN:$PATH" + +# XVM +export XVM_WORKSPACE_NAME="global" + +# Function to update the workspace name +xvm-workspace() { + export XVM_WORKSPACE_NAME="$1" +} \ No newline at end of file