Skip to content

Commit

Permalink
[setup-shell] Move some common functions to lib/setup.bashrc
Browse files Browse the repository at this point in the history
  • Loading branch information
progrhyme committed May 16, 2020
1 parent 6092642 commit dac7a06
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 78 deletions.
30 changes: 3 additions & 27 deletions envs/darwin/script/setup-shell.shrc
Expand Up @@ -11,43 +11,19 @@ UD_LINK_TARGETS=(.Brewfile)

common_setup() {
setup_symlinks

# Defined in $BASE_DIR/lib/setup.bashrc
setup_dotfiles
setup_clenv
}

chsh_zsh() {
if [[ ! "$SHELL" =~ /zsh$ ]]; then
# Assume zsh installed by Homebrew
if [[ -x /usr/local/bin/zsh ]]; then
sudo chsh -s /usr/local/bin/zsh $USER
else
echo "[error] /usr/local/bin/zsh not executable!"
return 1
fi
fi
}

setup_symlinks() {
local _lt
for _lt in ${UD_LINK_TARGETS[@]}; do
symlink $_lt
done
}

setup_dotfiles() {
local df_shell=
[[ $SETUP_SHELL = "zsh" ]] && df_shell=/bin/zsh
DF_SHELL=${df_shell} $BASE_DIR/script/setup-dotfiles.sh
}

setup_clenv() {
if [[ ! -d $HOME/.clenv ]]; then
git clone git@github.com:progrhyme/clenv.git $HOME/.clenv
. $BASE_DIR/common/shrc.d/load_clenv.shrc
fi
clam -r $BASE_DIR/common/Clamfile
}

#-------------
# Entry Point

Expand All @@ -58,7 +34,7 @@ case "$SETUP_SHELL" in
;;
"zsh" )
UD_LINK_TARGETS+=(.zshenv .zshrc)
chsh_zsh
change_shell_to /usr/local/bin/zsh
common_setup
;;
* )
Expand Down
54 changes: 3 additions & 51 deletions envs/ubuntu-desktop/script/setup-shell.shrc
Expand Up @@ -14,68 +14,20 @@ UD_LINK_TARGETS=(.Brewfile)

common_setup() {
setup_symlinks

# Defined in $BASE_DIR/lib/setup.bashrc
setup_dotfiles
setup_clenv
setup_ohmyzsh
}

chsh_zsh() {
if [[ $SHELL != "/bin/zsh" ]]; then
if [[ -x /bin/zsh ]]; then
sudo chsh -s /bin/zsh $USER
else
echo "[error] /bin/zsh not executable!"
return 1
fi
fi
}

setup_symlinks() {
local _lt
for _lt in ${UD_LINK_TARGETS[@]}; do
symlink $_lt
done
}

setup_dotfiles() {
local df_shell=
[[ $SETUP_SHELL = "zsh" ]] && df_shell=/bin/zsh
DF_SHELL=${df_shell} $BASE_DIR/script/setup-dotfiles.sh
}

setup_clenv() {
if [[ ! -d $HOME/.clenv ]]; then
git clone git@github.com:progrhyme/clenv.git $HOME/.clenv
. $BASE_DIR/common/shrc.d/load_clenv.shrc
fi
clam -r $BASE_DIR/common/Clamfile
}

setup_ohmyzsh() {
local ohmyzsh=$HOME/.oh-my-zsh
local ohmyzsh_custom_plugins=(
https://github.com/zsh-users/zsh-autosuggestions
)
local git_uri _basename _plugin_dir

if [[ ! -d $ohmyzsh ]]; then
# Install oh-my-zsh
curl -Lo /tmp/install.sh https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
RUNZSH=no sh /tmp/install.sh

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
fi

for git_uri in "${ohmyzsh_custom_plugins[@]}"; do
_basename=${git_uri##*/}
_basename=${_basename%.git}
_plugin_dir="${ohmyzsh}/custom/plugins/${_basename}"
if [[ ! -d $_plugin_dir ]]; then
git clone $git_uri $_plugin_dir
fi
done
}

#-------------
# Entry Point

Expand All @@ -86,7 +38,7 @@ case "$SETUP_SHELL" in
;;
"zsh" )
UD_LINK_TARGETS+=(.zshenv .zshrc)
chsh_zsh
change_shell_to /bin/zsh
common_setup
;;
* )
Expand Down
50 changes: 50 additions & 0 deletions lib/setup.bashrc
Expand Up @@ -75,3 +75,53 @@ pre_exec_script() {
[[ ! -x $pre_script ]] && return
$pre_script
}

change_shell_to() {
local target=$1
if [[ ! -x $target ]]; then
echo "[error] $target not executable!"
return 1
fi
if [[ "$(basename $target)" = "$(basename $SHELL)" ]]; then
echo "[notice] Already using $(basename $target). Skip."
return
fi
sudo chsh -s $target
}

setup_dotfiles() {
local df_shell=
[[ $SETUP_SHELL = "zsh" ]] && df_shell=/bin/zsh
DF_SHELL=${df_shell} $BASE_DIR/script/setup-dotfiles.sh
}

setup_clenv() {
if [[ ! -d $HOME/.clenv ]]; then
git clone git@github.com:progrhyme/clenv.git $HOME/.clenv
. $BASE_DIR/common/shrc.d/load_clenv.shrc
fi
clam -r $BASE_DIR/common/Clamfile
}

setup_ohmyzsh() {
local ohmyzsh=$HOME/.oh-my-zsh
local ohmyzsh_custom_plugins=(
https://github.com/zsh-users/zsh-autosuggestions
)
local git_uri _basename _plugin_dir

if [[ ! -d $ohmyzsh ]]; then
# Install oh-my-zsh
curl -Lo /tmp/install.sh https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
RUNZSH=no sh /tmp/install.sh
fi

for git_uri in "${ohmyzsh_custom_plugins[@]}"; do
_basename=${git_uri##*/}
_basename=${_basename%.git}
_plugin_dir="${ohmyzsh}/custom/plugins/${_basename}"
if [[ ! -d $_plugin_dir ]]; then
git clone $git_uri $_plugin_dir
fi
done
}

0 comments on commit dac7a06

Please sign in to comment.