Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·166 lines (136 loc) · 4.01 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·166 lines (136 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env zsh
set -euo pipefail
dotfiles_dir="$(cd "$(dirname "$0")" && pwd -P)"
backup_dir="${HOME}/.dotfiles-backup/$(date +%Y%m%d-%H%M%S)"
dry_run=false
skip_brew=false
skip_gitconfig=false
usage() {
cat <<EOF
Usage: ./install.sh [--dry-run] [--skip-brew] [--skip-gitconfig]
安裝 zsh-first dotfiles,將設定檔 symlink 到家目錄。
預設會先執行 brew bundle 安裝 Brewfile 裡的 CLI 工具。
Git 設定會互動詢問 name/email 後產生。
既有檔案或不同來源的 symlink 會移到 ${backup_dir}。
EOF
}
link_file() {
local source="$1"
local target="$2"
local existing_source
if [ "$dry_run" = true ]; then
printf '[dry-run] link %s -> %s\n' "$target" "$source"
return
fi
mkdir -p "$(dirname "$target")"
if [ -L "$target" ]; then
existing_source="$(readlink "$target")"
if [ "$existing_source" = "$source" ]; then
return
fi
fi
if [ -L "$target" ] || [ -e "$target" ]; then
mkdir -p "$backup_dir"
mv "$target" "$backup_dir/"
fi
ln -s "$source" "$target"
}
backup_file() {
local target="$1"
if [ -L "$target" ] || [ -e "$target" ]; then
mkdir -p "$backup_dir"
mv "$target" "$backup_dir/"
fi
}
escape_sed_replacement() {
printf '%s' "$1" | sed 's/[\/&]/\\&/g'
}
install_brew_bundle() {
if [ "$skip_brew" = true ]; then
printf 'Skipping brew bundle\n'
return
fi
if [ "$dry_run" = true ]; then
printf '[dry-run] brew bundle --file %s\n' "$dotfiles_dir/Brewfile"
return
fi
if ! command -v brew >/dev/null 2>&1; then
printf 'Homebrew is not installed; skipping brew bundle\n' >&2
return
fi
brew bundle --file "$dotfiles_dir/Brewfile"
}
render_gitconfig() {
if [ "$skip_gitconfig" = true ]; then
printf 'Skipping gitconfig render\n'
return
fi
local target="${HOME}/.gitconfig"
local default_name
local default_email
local git_name
local git_email
local escaped_git_name
local escaped_git_email
default_name="$(git config --global --get user.name 2>/dev/null || true)"
default_email="$(git config --global --get user.email 2>/dev/null || true)"
if [ "$dry_run" = true ]; then
printf '[dry-run] render %s from %s\n' "$target" "$dotfiles_dir/.gitconfig.template"
return
fi
printf 'Git user.name [%s]: ' "$default_name"
read -r git_name
git_name="${git_name:-$default_name}"
while [ -z "$git_name" ]; do
printf 'Git user.name is required: '
read -r git_name
done
printf 'Git user.email [%s]: ' "$default_email"
read -r git_email
git_email="${git_email:-$default_email}"
while [ -z "$git_email" ]; do
printf 'Git user.email is required: '
read -r git_email
done
backup_file "$target"
escaped_git_name="$(escape_sed_replacement "$git_name")"
escaped_git_email="$(escape_sed_replacement "$git_email")"
sed \
-e "s/{{GIT_NAME}}/$escaped_git_name/g" \
-e "s/{{GIT_EMAIL}}/$escaped_git_email/g" \
"$dotfiles_dir/.gitconfig.template" > "$target"
}
while [ $# -gt 0 ]; do
case "$1" in
--dry-run)
dry_run=true
;;
--skip-brew)
skip_brew=true
;;
--skip-gitconfig)
skip_gitconfig=true
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage
exit 1
;;
esac
shift
done
echo "Installing zsh dotfiles"
install_brew_bundle
link_file "$dotfiles_dir/.zshrc" "$HOME/.zshrc"
link_file "$dotfiles_dir/.aliases" "$HOME/.aliases"
link_file "$dotfiles_dir/.exports" "$HOME/.exports"
link_file "$dotfiles_dir/.functions" "$HOME/.functions"
render_gitconfig
link_file "$dotfiles_dir/.osx" "$HOME/.osx"
link_file "$dotfiles_dir/.config/starship.toml" "$HOME/.config/starship.toml"
echo "Install done"
echo "Open a new shell or run: exec zsh"