From 869b6d90921f89672a9c9779186daed1d4f357fb Mon Sep 17 00:00:00 2001 From: Szymon Osiecki Date: Wed, 4 Oct 2023 22:48:41 +0200 Subject: [PATCH] feat: zsh shell --- .assets/provision/install_zsh.sh | 47 +++++++++++ .assets/provision/setup_profile_user_zsh.sh | 91 +++++++++++++++++++++ wsl/wsl_setup.ps1 | 15 +++- 3 files changed, 151 insertions(+), 2 deletions(-) create mode 100755 .assets/provision/install_zsh.sh create mode 100755 .assets/provision/setup_profile_user_zsh.sh diff --git a/.assets/provision/install_zsh.sh b/.assets/provision/install_zsh.sh new file mode 100755 index 00000000..671c81f6 --- /dev/null +++ b/.assets/provision/install_zsh.sh @@ -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 diff --git a/.assets/provision/setup_profile_user_zsh.sh b/.assets/provision/setup_profile_user_zsh.sh new file mode 100755 index 00000000..01026768 --- /dev/null +++ b/.assets/provision/setup_profile_user_zsh.sh @@ -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 <>$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 <>$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 <>$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 <>~/.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 diff --git a/wsl/wsl_setup.ps1 b/wsl/wsl_setup.ps1 index 8bc5c5e9..fc4c17c8 100644 --- a/wsl/wsl_setup.ps1 +++ b/wsl/wsl_setup.ps1 @@ -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, @@ -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, @@ -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";', @@ -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 @@ -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) { @@ -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) {