mirror of
https://github.com/DictXiong/dotfiles.git
synced 2026-11-23 20:38:16 +08:00
[refactor] clean, readable (#19)
* rebase zshrc, introduce DFS_NO_WALL * improve ci * improve ci * improve ci * update antigen url for DFS_NO_WALL * OK * improve ci for macos * improve test.zsh * staged * fix ci * fix ci * use local vars * introduce DFS_QUIET * mass mod install.sh * minor change * set DFS_DEV=1 to prevent update * debug: done -> fi * fix DFS_DEV * ubuntu.sh: set-mirror (ref: tuna) * preinstall_check * install.sh: -q to be quiet * install.sh: -d to DFS_DEV Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
This commit is contained in:
234
install.sh
234
install.sh
@@ -3,87 +3,144 @@
|
||||
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
|
||||
source "$THIS_DIR/tools/common.sh"
|
||||
|
||||
home_slashes=${HOME//\//\\\/}
|
||||
if [[ ! $DOTFILES == ${home_slashes}* ]]; then
|
||||
|
||||
if [[ ! "$DOTFILES" == "${HOME}"* ]]; then
|
||||
fmt_fatal "\"$DOTFILES\" is not under \"$HOME\". aborting ..."
|
||||
fi
|
||||
dotfile_home_path=${DOTFILES/${home_slashes}/\~}
|
||||
dotfile_relative_path=${DOTFILES#${home_slashes}\/}
|
||||
crontab_job="0 * * * * ${DOTFILES}/update.sh"
|
||||
DOTFILE_TILDE=${DOTFILES/"$HOME"/\~}
|
||||
|
||||
insert_if_not_exist()
|
||||
{
|
||||
filename=$1
|
||||
line=$2
|
||||
fmt_note "installing \"$line\" into \"$filename\" ..."
|
||||
mkdir -p $(dirname "$filename")
|
||||
if [ ! -f "$filename" ]; then
|
||||
touch $filename
|
||||
fi
|
||||
grep -qxF -- "$line" "$filename" || echo "$line" >> "$filename"
|
||||
}
|
||||
CRON_JOB="0 * * * * ${DOTFILES}/update.sh"
|
||||
declare -a HOME_FILES_PATH
|
||||
declare -a HOME_FILES_CONTENT
|
||||
HOME_FILES_PATH[0]=".zshrc"
|
||||
HOME_FILES_CONTENT[0]="source ${DOTFILE_TILDE}/.zshrc2"
|
||||
HOME_FILES_PATH[1]=".tmux.conf"
|
||||
HOME_FILES_CONTENT[1]="source-file ${DOTFILE_TILDE}/.tmux.conf2"
|
||||
HOME_FILES_PATH[2]=".vimrc"
|
||||
HOME_FILES_CONTENT[2]="source ${DOTFILE_TILDE}/.vimrc2"
|
||||
HOME_FILES_PATH[3]=".gitconfig"
|
||||
HOME_FILES_CONTENT[3]="[include] path = \"${DOTFILE_TILDE}/.gitconfig2\""
|
||||
|
||||
delete_if_exist()
|
||||
{
|
||||
filename=$1
|
||||
line=$2
|
||||
fmt_note "removing \"$line\" from \"$filename\" ..."
|
||||
if [ -f "$filename" ]; then
|
||||
grep -vxF -- "$line" "$filename" | tee "$filename"
|
||||
fi
|
||||
}
|
||||
declare -a HOME_SYMLINKS_SRC
|
||||
declare -a HOME_SYMLINKS_DST
|
||||
HOME_SYMLINKS_SRC[0]=".ssh/authorized_keys2"
|
||||
HOME_SYMLINKS_DST[0]=".ssh/authorized_keys2"
|
||||
|
||||
create_symlink()
|
||||
|
||||
preinstall_check()
|
||||
{
|
||||
src=$1
|
||||
dest=$2
|
||||
fmt_note "creating symlink \"$dest\" --> \"$src\" ..."
|
||||
if [ ! -f "$src" ]; then
|
||||
fmt_error "\"$src\" does not exist! aborting this job ..."
|
||||
return 1
|
||||
fi
|
||||
mkdir -p $(dirname "$dest")
|
||||
if [ -f "$dest" ]; then
|
||||
if [ "$(readlink $dest)" -ef "$src" ]; then
|
||||
return 0
|
||||
mandatory_commands=( "git" "zsh" "curl" "ping" )
|
||||
optional_commands=( "python3" "vim" "tmux" )
|
||||
for i in "${mandatory_commands[@]}"; do
|
||||
if [[ ! -x "$(command -v $i)" ]]; then
|
||||
fmt_info "all this utils are required: ${mandatory_commands[@]}"
|
||||
fmt_info "install them manually or check scripts in tools/"
|
||||
fmt_fatal "\"$i\" not found. aborting ..."
|
||||
fi
|
||||
fmt_warning "\"$dest\" already exists! stat output:"
|
||||
echo ----------
|
||||
stat $dest
|
||||
echo ----------
|
||||
ask_for_yN "would you like to replace ${dest}?"
|
||||
if [ $? -eq 1 ]; then
|
||||
rm $dest
|
||||
else
|
||||
fmt_error "\"$dest\" already exists! aborting this job ..."
|
||||
return 1
|
||||
done
|
||||
for i in "${optional_commands[@]}"; do
|
||||
if [[ ! -x "$(command -v $i)" ]]; then
|
||||
fmt_warning "\"$i\" not found"
|
||||
ask_for_Yn "continue anyway?"
|
||||
if [[ "$?" == "0" ]]; then
|
||||
fmt_info "all this utils are suggested: ${optional_commands[@]}"
|
||||
fmt_info "install them manually or check scripts in tools/"
|
||||
fmt_fatal "aborting ..."
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
ln -s $src $dest
|
||||
return $?
|
||||
done
|
||||
}
|
||||
|
||||
delete_link_if_match()
|
||||
install_file_content()
|
||||
{
|
||||
src=$1
|
||||
dest=$2
|
||||
if [ "$(readlink $dest)" -ef "$src" ]; then
|
||||
fmt_note "removing symlink \"$dest\" ..."
|
||||
echo ----------
|
||||
stat $dest
|
||||
echo ----------
|
||||
rm $dest
|
||||
fi
|
||||
for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do
|
||||
local filename="$HOME/${HOME_FILES_PATH[$i]}"
|
||||
local content=${HOME_FILES_CONTENT[$i]}
|
||||
fmt_note "installing \"$content\" into \"$filename\" ..."
|
||||
mkdir -p $(dirname "$filename")
|
||||
if [ ! -f "$filename" ]; then
|
||||
touch $filename
|
||||
fi
|
||||
grep -qxF -- "$content" "$filename" || echo "$content" >> "$filename"
|
||||
done
|
||||
}
|
||||
|
||||
uninstall_file_content()
|
||||
{
|
||||
for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do
|
||||
local filename="$HOME/${HOME_FILES_PATH[$i]}"
|
||||
local content=${HOME_FILES_CONTENT[$i]}
|
||||
fmt_note "removing \"$content\" from \"$filename\" ..."
|
||||
if [ -f "$filename" ]; then
|
||||
grep -vxF -- "$content" "$filename" | tee "$filename"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
install_symlink()
|
||||
{
|
||||
for ((i=0; i<${#HOME_SYMLINKS_SRC[@]}; i++)); do
|
||||
local src="$DOTFILES/${HOME_SYMLINKS_SRC[$i]}"
|
||||
local dst="$HOME/${HOME_SYMLINKS_DST[$i]}"
|
||||
fmt_note "creating symlink \"$dst\" --> \"$src\" ..."
|
||||
if [ ! -f "$src" ]; then
|
||||
fmt_error "\"$src\" does not exist! aborting this job ..."
|
||||
continue
|
||||
fi
|
||||
mkdir -p $(dirname "$dst")
|
||||
if [ -f "$dst" ]; then
|
||||
if [ "$(readlink $dst)" -ef "$src" ]; then
|
||||
continue
|
||||
fi
|
||||
fmt_warning "\"$dst\" already exists! stat output:"
|
||||
echo ----------
|
||||
stat $dst
|
||||
echo ----------
|
||||
ask_for_yN "would you like to replace ${dst}?"
|
||||
if [ $? -eq 1 ]; then
|
||||
rm $dst
|
||||
else
|
||||
fmt_error "aborting this job ..."
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
ln -s $src $dst
|
||||
done
|
||||
}
|
||||
|
||||
uninstall_symlink()
|
||||
{
|
||||
local src
|
||||
for src in "${!HOME_SYMLINKS[@]}"; do
|
||||
local dst=${HOME_SYMLINKS[$src]}
|
||||
src="$DOTFILES/$src"
|
||||
dst="$HOME/$dst"
|
||||
if [ "$(readlink $dst)" -ef "$src" ]; then
|
||||
fmt_note "removing symlink \"$dst\" ..."
|
||||
echo ----------
|
||||
stat $dst
|
||||
echo ----------
|
||||
rm $dst
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
install_crontab(){
|
||||
fmt_note "installing \"$crontab_job\" to crontab ..."
|
||||
( crontab -l | grep -vxF "${crontab_job}" | grep -v "no crontab for"; echo "$crontab_job" ) | crontab -
|
||||
if [[ -x $(command -v crontab) ]]; then
|
||||
fmt_note "installing \"$CRON_JOB\" to crontab ..."
|
||||
( crontab -l | grep -vxF "${CRON_JOB}" | grep -v "no crontab for"; echo "$CRON_JOB" ) | crontab -
|
||||
else
|
||||
fmt_warning "crontab does not exist. skipping ..."
|
||||
fi
|
||||
}
|
||||
|
||||
uninstall_crontab(){
|
||||
fmt_note "removing \"$crontab_job\" from crontab ..."
|
||||
( crontab -l | grep -vxF "$crontab_job" ) | crontab -
|
||||
if [[ -x $(command -v crontab) ]]; then
|
||||
fmt_note "removing \"$CRON_JOB\" from crontab ..."
|
||||
( crontab -l | grep -vxF "$CRON_JOB" ) | crontab -
|
||||
else
|
||||
fmt_note "crontab does not exist. skipping ..."
|
||||
fi
|
||||
}
|
||||
|
||||
install_tmux_tpm(){
|
||||
@@ -118,10 +175,8 @@ install_update(){
|
||||
fmt_note "installing update.sh ..."
|
||||
cp "${DOTFILES}/.update.sh" "${DOTFILES}/update.sh"
|
||||
chmod +x "${DOTFILES}/update.sh"
|
||||
if [[ -z "$DFS_NO_UPDATE" ]]; then
|
||||
fmt_note "running update.sh ..."
|
||||
${DOTFILES}/update.sh
|
||||
fi
|
||||
fmt_note "running update.sh ..."
|
||||
${DOTFILES}/update.sh
|
||||
}
|
||||
|
||||
uninstall_update(){
|
||||
@@ -130,13 +185,11 @@ uninstall_update(){
|
||||
}
|
||||
|
||||
install(){
|
||||
preinstall_check
|
||||
install_update
|
||||
install_crontab
|
||||
insert_if_not_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2"
|
||||
insert_if_not_exist "${HOME}/.tmux.conf" "source-file ${dotfile_home_path}/.tmux.conf2"
|
||||
insert_if_not_exist "${HOME}/.vimrc" "source ${dotfile_home_path}/.vimrc2"
|
||||
insert_if_not_exist "${HOME}/.gitconfig" "[include] path = \"${dotfile_home_path}/.gitconfig2\""
|
||||
create_symlink "${DOTFILES}/.ssh/authorized_keys2" "${HOME}/.ssh/authorized_keys2"
|
||||
install_file_content
|
||||
install_symlink
|
||||
# those that won't be uninstalled in the future
|
||||
install_tmux_tpm
|
||||
install_vim_vundle
|
||||
@@ -145,21 +198,26 @@ install(){
|
||||
|
||||
uninstall(){
|
||||
ask_for_yN "do you really want to uninstall?"
|
||||
if [[ $? == 1 ]]; then
|
||||
uninstall_update
|
||||
uninstall_crontab
|
||||
delete_if_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2"
|
||||
delete_if_exist "${HOME}/.tmux.conf" "source-file ${dotfile_home_path}/.tmux.conf2"
|
||||
delete_if_exist "${HOME}/.vimrc" "source ${dotfile_home_path}/.vimrc2"
|
||||
delete_if_exist "${HOME}/.gitconfig" "[include] path = \"${dotfile_home_path}/.gitconfig2\""
|
||||
delete_link_if_match "${DOTFILES}/.ssh/authorized_keys2" "${HOME}/.ssh/authorized_keys2"
|
||||
fmt_note "done uninstalling!"
|
||||
if [[ $? != 1 ]]; then
|
||||
fmt_error "aborting this job ..."
|
||||
return
|
||||
fi
|
||||
uninstall_update
|
||||
uninstall_crontab
|
||||
uninstall_file_content
|
||||
uninstall_symlink
|
||||
fmt_note "done uninstalling!"
|
||||
}
|
||||
|
||||
|
||||
case $1 in
|
||||
""|-i ) install ;;
|
||||
-r ) uninstall ;;
|
||||
* ) fmt_warning "unknown command \"$1\". available: -i, -r" ;;
|
||||
esac
|
||||
BIN=install
|
||||
while [[ $# > 0 ]]; do
|
||||
case $1 in
|
||||
-i ) BIN=install ;;
|
||||
-r ) BIN=uninstall ;;
|
||||
-q ) export DFS_QUIET=1 ;;
|
||||
-d ) export DFS_DEV=1 ;;
|
||||
* ) fmt_warning "unknown command \"$1\". available: -i, -r, -q, -d"; exit 1 ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
$BIN
|
||||
Reference in New Issue
Block a user