Skip to content

Commit

Permalink
tools: add profile file, and adapt bash/zsh/fish... (#69)
Browse files Browse the repository at this point in the history
- #66

1. independent xlings profile
2. auto config for local shell

Signed-off-by: sunrisepeak <[email protected]>
  • Loading branch information
Sunrisepeak authored Jan 10, 2025
1 parent 3c2b72e commit 6435075
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
46 changes: 37 additions & 9 deletions core/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tools/shell/xlings-profile.fish
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions tools/shell/xlings-profile.sh
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 6435075

Please sign in to comment.