Skip to content

Commit

Permalink
Merge pull request #129 from szymonos/feat/zsh-shell
Browse files Browse the repository at this point in the history
feat: zsh shell
  • Loading branch information
szymonos authored Oct 5, 2023
2 parents 9687cf5 + 869b6d9 commit f2f777c
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
47 changes: 47 additions & 0 deletions .assets/provision/install_zsh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
: '
sudo .assets/provision/install_zsh.sh >/dev/null
'
if [ $EUID -ne 0 ]; then
printf '\e[31;1mRun the script as root.\e[0m\n'
exit 1
fi

# determine system id
SYS_ID="$(sed -En '/^ID.*(alpine|arch|fedora|debian|ubuntu|opensuse).*/{s//\1/;p;q}' /etc/os-release)"
# check if package installed already using package manager
APP='zsh'
case $SYS_ID in
alpine)
apk -e info $APP &>/dev/null && exit 0 || true
;;
arch)
pacman -Qqe $APP &>/dev/null && exit 0 || true
;;
fedora | opensuse)
rpm -q $APP &>/dev/null && exit 0 || true
;;
debian | ubuntu)
dpkg -s $APP &>/dev/null && exit 0 || true
;;
esac

printf "\e[92minstalling \e[1m$APP\e[0m\n" >&2
case $SYS_ID in
alpine)
apk add --no-cache $APP >&2 2>/dev/null
;;
arch)
pacman -Sy --needed --noconfirm $APP >&2 2>/dev/null
;;
fedora)
dnf install -y $APP >&2 2>/dev/null
;;
debian | ubuntu)
export DEBIAN_FRONTEND=noninteractive
apt-get update >&2 && apt-get install -y $APP >&2 2>/dev/null
;;
opensuse)
zypper in -y $APP >&2 2>/dev/null
;;
esac
91 changes: 91 additions & 0 deletions .assets/provision/setup_profile_user_zsh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env zsh
: '
.assets/provision/setup_profile_user_zsh.sh
'
# path variables
PROFILE_PATH='/etc/profile.d'
OMP_PATH='/usr/local/share/oh-my-posh'

# *install plugins
# ~zsh-autocomplete
# https://github.com/marlonrichert/zsh-autocomplete
if [ -d $HOME/.zsh/zsh-autocomplete ]; then
git -C $HOME/.zsh/zsh-autocomplete pull --quiet
else
git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git $HOME/.zsh/zsh-autocomplete
fi
if ! grep -w 'zsh-autocomplete.plugin.zsh' ~/.zshrc 2>/dev/null; then
cat <<'EOF' >>$HOME/.zshrc
# *plugins
source $HOME/.zsh/zsh-autocomplete/zsh-autocomplete.plugin.zsh
EOF
fi
# ~zsh-autosuggestions
# https://github.com/zsh-users/zsh-autosuggestions
if [ -d $HOME/.zsh/zsh-autosuggestions ]; then
git -C $HOME/.zsh/zsh-autosuggestions pull --quiet
else
git clone https://github.com/zsh-users/zsh-autosuggestions $HOME/.zsh/zsh-autosuggestions
fi
if ! grep -w 'zsh-autosuggestions.zsh' ~/.zshrc 2>/dev/null; then
echo 'source $HOME/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh' >>$HOME/.zshrc
fi
# ~zsh-syntax-highlighting
# https://github.com/zsh-users/zsh-syntax-highlighting
if [ -d $HOME/.zsh/zsh-syntax-highlighting ]; then
git -C $HOME/.zsh/zsh-syntax-highlighting pull --quiet
else
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $HOME/.zsh/zsh-syntax-highlighting
fi
if ! grep -w 'zsh-syntax-highlighting.zsh' ~/.zshrc 2>/dev/null; then
echo 'source $HOME/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh' >>$HOME/.zshrc
fi
if ! grep -q '^bindkey .* autosuggest-accept' $HOME/.zshrc; then
echo "bindkey '^ ' autosuggest-accept\n" >>$HOME/.zshrc
fi

# *aliases
# add common zsh aliases
grep -qw 'd/aliases.sh' ~/.zshrc 2>/dev/null || cat <<EOF >>$HOME/.zshrc
# *aliases
if [ -f "$PROFILE_PATH/aliases.sh" ]; then
source "$PROFILE_PATH/aliases.sh"
fi
EOF

# add git aliases
if ! grep -qw 'd/aliases_git.sh' ~/.zshrc 2>/dev/null && type git &>/dev/null; then
cat <<EOF >>$HOME/.zshrc
# git aliases
if [ -f "$PROFILE_PATH/aliases_git.sh" ] && type git &>/dev/null; then
source "$PROFILE_PATH/aliases_git.sh"
fi
EOF
fi

# add kubectl autocompletion and aliases
if ! grep -qw 'kubectl' ~/.zshrc 2>/dev/null && type -f kubectl &>/dev/null; then
cat <<EOF >>$HOME/.zshrc
# kubectl autocompletion and aliases
if type -f kubectl &>/dev/null; then
if [ -f "$PROFILE_PATH/aliases_kubectl.sh" ]; then
source "$PROFILE_PATH/aliases_kubectl.sh"
fi
fi
EOF
fi

# *add conda initialization
if ! grep -qw '__conda_setup' ~/.zshrc 2>/dev/null && [ -f $HOME/miniconda3/bin/conda ]; then
$HOME/miniconda3/bin/conda init zsh >/dev/null
fi

# *add oh-my-posh invocation
if ! grep -qw 'oh-my-posh' ~/.zshrc 2>/dev/null && type oh-my-posh &>/dev/null; then
cat <<EOF >>~/.zshrc
# initialize oh-my-posh prompt
if [ -f "$OMP_PATH/theme.omp.json" ] && type oh-my-posh &>/dev/null; then
eval "\$(oh-my-posh --init --shell zsh --config "$OMP_PATH/theme.omp.json")"
fi
EOF
fi
15 changes: 13 additions & 2 deletions wsl/wsl_setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ List of installation scopes. Valid values:
- python: pip, venv, miniconda
- rice: btop, cmatrix, cowsay, fastfetch
- shell: bat, eza, oh-my-posh, ripgrep, yq
- zsh: zsh shell with plugins
.PARAMETER OmpTheme
Specify to install oh-my-posh prompt theme engine and name of the theme to be used.
You can specify one of the three included profiles: base, powerline, nerd,
Expand Down Expand Up @@ -74,7 +75,7 @@ param (

[Parameter(ParameterSetName = 'Setup')]
[Parameter(ParameterSetName = 'GitHub')]
[ValidateScript({ $_.ForEach({ $_ -in @('az', 'distrobox', 'docker', 'k8s_base', 'k8s_ext', 'oh_my_posh', 'pwsh', 'python', 'rice', 'shell') }) -notcontains $false },
[ValidateScript({ $_.ForEach({ $_ -in @('az', 'distrobox', 'docker', 'k8s_base', 'k8s_ext', 'oh_my_posh', 'pwsh', 'python', 'rice', 'shell', 'zsh') }) -notcontains $false },
ErrorMessage = 'Wrong scope provided. Valid values: az distrobox docker k8s_base k8s_ext python rice shell')]
[string[]]$Scope,

Expand Down Expand Up @@ -189,6 +190,7 @@ process {
$cmd = [string]::Join('',
'[ -f /usr/bin/rg ] && shell="true" || shell="false";',
'[ -f /usr/bin/pwsh ] && pwsh="true" || pwsh="false";',
'[ -f /usr/bin/zsh ] && zsh="true" || zsh="false";',
'[ -f /usr/bin/kubectl ] && k8s_base="true" || k8s_base="false";',
'[ -f /usr/local/bin/k3d ] && k8s_ext="true" || k8s_ext="false";',
'[ -f /usr/bin/oh-my-posh ] && omp="true" || omp="false";',
Expand All @@ -204,7 +206,7 @@ process {
'grep -Fqw "dark" /etc/profile.d/gtk_theme.sh 2>/dev/null && gtkd="true" || gtkd="false";',
'printf "{\"user\":\"$(id -un)\",\"shell\":$shell,\"k8s_base\":$k8s_base,\"k8s_ext\":$k8s_ext,',
'\"omp\":$omp,\"az\":$az,\"wslg\":$wslg,\"python\":$python,\"systemd\":$systemd,\"gtkd\":$gtkd,',
'\"pwsh\":$pwsh,\"git_user\":$git_user,\"git_email\":$git_email,\"ssh_key\":$ssh_key}"'
'\"pwsh\":$pwsh,\"zsh\":$zsh,\"git_user\":$git_user,\"git_email\":$git_email,\"ssh_key\":$ssh_key}"'
)
# check existing distro setup
$chk = wsl.exe -d $Distro --exec sh -c $cmd | ConvertFrom-Json -AsHashtable
Expand All @@ -225,6 +227,7 @@ process {
az { $scopes.Add('python') | Out-Null }
k8s_ext { @('docker', 'k8s_base').ForEach({ $scopes.Add($_) | Out-Null }) }
pwsh { $scopes.Add('shell') | Out-Null }
zsh { $scopes.Add('shell') | Out-Null }
}
# determine 'oh_my_posh' scope
if ($chk.omp -or $OmpTheme) {
Expand Down Expand Up @@ -376,6 +379,14 @@ process {
wsl.exe --distribution $Distro --exec .assets/provision/setup_profile_user.sh
continue
}
zsh {
Write-Host 'installing zsh...' -ForegroundColor Cyan
wsl.exe --distribution $Distro --user root --exec .assets/provision/install_zsh.sh
# *setup profiles
Write-Host 'setting up zsh profile for current user...' -ForegroundColor Cyan
wsl.exe --distribution $Distro --exec .assets/provision/setup_profile_user_zsh.sh
continue
}
}
# *set gtk theme for wslg
if ($lx.Version -eq 2 -and $chk.wslg) {
Expand Down

0 comments on commit f2f777c

Please sign in to comment.