-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·302 lines (275 loc) · 9.34 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·302 lines (275 loc) · 9.34 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/bin/bash
set -eu
declare UNAME="$(uname)"
declare DOTFILES_BACKUP
# DOTFILES_BACKUP が環境変数として定義されていなければ、false にする
: ${DOTFILES_BACKUP:=false}
declare DRY_RUN
# DRY_RUN が環境変数として定義されていなければ、false にする
: ${DRY_RUN:=false}
declare -a INSTALL_DOTFILES_IN_CODESPACES=(
bash_aliases
bash_profile
common_env
gitconfig
gitignore
inputrc
path_functions
tigrc
vimrc
zprofile
)
# 実験でいくつかだけ自動インストール
declare -a INSTALL_DOTFILES_IN_MACOS=(
bash_aliases
common_env
gitconfig
gitignore
nanorc
path_functions
tigrc
vimrc
)
declare -a INSTALL_DEB_PACKAGES_IN_CODESPACES=(
tig
bat
rcm
)
function main {
if is-codespaces ; then
install-codespaces-fundamental-commands-by-apt
setup-bat || true
if ! is-rcm-exist ; then
echo "rcm is not installed" >&2
# 取りあえず Ubuntu/Debian 決め打ちで入れてしまう
sudo apt-get update && sudo apt-get install -y rcm
fi
if [ ! -f "./rcrc" ] ; then
echo "./rcrc is not found" >&2
return 1
fi
backup-home-real-dotfiles
#create-home-dotfiles-dir-symlink
pwd # for debug
# Codespaces の場合、シンボリックリンクではなく、-C でコピーを作成する
# あと、-f で対話が発生しないようにする
# Codespaces の場合、~/.dotfiles 自体が揮発的になってしまうことと、
# これ自体をシンボリックリンクにしてしまうと、シンボリックリンクが多重となってしまい混乱を生む可能性があるため
# -C でコピーを作ることでもろもろ回避しようとしている
# ドットファイルに変更があれば、再度 rcup を実行したりすればよい
# Codespaces では .bashrc .zshrc は温存して、あとで追加する
# XXX: RCRC 環境変数が意味をなしていない?
env RCRC=./rcrc rcup -v -f -C -d "$PWD" \
"${INSTALL_DOTFILES_IN_CODESPACES[@]}"
# MEMO: bashrc と zshrc は Codespaces のものを採用して、あとで追記する
# MEMO: bash_completion を読み込むと、ディレクトリ補完で警告が発生するのと
# 特に現状 Codespaces 上でこれを読まないことで困ることがないので、読み込まないようにする
append-codespaces-shellrc
elif is-macos ; then
dispatch-macos
else
echo "currently, this script is only for codespaces or macOS" >&2
return 1
fi
}
function is-pwd-dotfiles-root {
test -f "./rcrc"
}
function is-rcm-exist {
type "rcup" > /dev/null 2>&1
}
function is-codespaces {
test -n "${CODESPACES:-}"
}
function is-macos {
test "$UNAME" = "Darwin"
}
function dispatch-macos {
backup-home-real-dotfiles
pwd # for debug
# macOS の場合、シンボリックリンクで実装する
if [ "$DRY_RUN" = "true" ]; then
echo "[DRY-RUN] Would execute: env RCRC=./rcrc rcup -v -d '$PWD' ${INSTALL_DOTFILES_IN_MACOS[*]}"
echo "[DRY-RUN] Files that would be linked:"
for file in "${INSTALL_DOTFILES_IN_MACOS[@]}"; do
if [ -f "./$file" ]; then
echo " $PWD/$file -> $HOME/.$file"
else
echo " [WARNING] Source file not found: ./$file"
fi
done
else
env RCRC=./rcrc rcup -v -d "$PWD" \
"${INSTALL_DOTFILES_IN_MACOS[@]}"
fi
}
# 今のディレクトリのシンボリックリンクとして ~/.dotfiles を作成する
# function create-home-dotfiles-dir-symlink {
# cloned_dotfiles_dir=$PWD
# ln -v -s $cloned_dotfiles_dir "$HOME/.dotfiles"
# }
# 既存のホームディレクトリにドットファイルがある場合、シンボリックでなければバックアップする
function backup-home-real-dotfiles {
# DOTFILES_BACKUP 環境変数が true で無ければバックアップしない
if [ "$DOTFILES_BACKUP" != "true" ] ; then
echo "backup-home-real-dotfiles: skip backup because \$DOTFILES_BACKUP is not true"
return 0
fi
local loopguard=0
find "$HOME" \
-maxdepth 1 \
-name '.??*' \
-not -name '.*.bak' \
-not -name .dotfiles \
-not -name .local \
-not -type l \
| while read dotfile ; do
backup_file="$dotfile.bak"
# すでにリアルの ~/.foo がある場合、コピーするものと差分がなければバックアップしない
if [ -f "$backup_file" ] && cmp -s "$dotfile" "$backup_file" ; then
echo "skip backup: $dotfile (because \"$backup_file\" is same as \"$dotfile\")"
continue
fi
# カレントディレクトリ(この install.sh があるディレクトリ)に ./foo がない場合、~/.foo はバックアップしない(rcup で書き込みがないので)
local base_dotfile=$(basename "$dotfile")
if [ ! -f "./${base_dotfile#.}" ] ; then
echo "skip backup: $dotfile (because \"./${base_dotfile#.}\" does not exists))"
continue
fi
echo "backup: $dotfile -> $backup_file"
cp -rp "$dotfile" "$backup_file"
if (( loopguard++ > 100 )); then
echo "loopguard" >&2
break
fi
done
}
# Codespaces 追記用に、自分のアカウント名を取得する
function get-my-account {
if [ -n "${GITHUB_USER:-}" ] ; then
# シェルインジェクション対策のためチェックする
if [[ "$GITHUB_USER" =~ ^[a-zA-Z0-9_-]+$ ]] ; then
echo "@${GITHUB_USER}"
else
echo "__myself__"
fi
else
echo "__myself__"
fi
}
# RCFILE チェック
function check-rcfile {
local RCFILE="$1"
local ME="$2"
if [ ! -f "$RCFILE" ] ; then
echo "RCFILE not found: $RCFILE" >&2
return 1
fi
if grep -q "$ME" "$RCFILE" ; then
echo "RCFILE already has '$ME': $RCFILE" >&2
return 1
fi
}
# Codespaces の .bashrc に自分のコンパクトな定義を追記する
function append-codespaces-bashrc {
local RCFILE="$HOME/.bashrc"
local ME=$(get-my-account)
if ! check-rcfile "$RCFILE" "$ME" ; then
return 0
fi
cat <<'EOF' | sed -e "s/__ME__/$ME/g" >> "$RCFILE"
###
### for Codespaces by __ME__
###
source ~/.path_functions
source ~/.common_env
source ~/.bash_aliases
source_if_readable ~/.bash_secret
if is_interactive_shell ; then
# source_if_readable ~/.bash_completion # これも念のため重複読み込み対策したい
# if [ -z "${BASH_COMPLETION_VERSINFO:-}" ] ; then
# source_if_readable /etc/bash_completion
# fi
function share_history {
history -a
history -c
history -r
}
shopt -u histappend
shopt -s checkwinsize
PROMPT_COMMAND=share_history
bind '"\C-g": "git "'
fi
# For Cursor + SSH Codespaces connection setting. This emulates the behavior of VSCode's initial connection
# by automatically changing to the repository directory when connected.
if [ "$CODESPACES" = "true" ] ; then
if [ "$PWD" = "$HOME" ] && [ -d "/workspaces/$RepositoryName" ] ; then
cd "/workspaces/$RepositoryName"
fi
fi
EOF
}
function append-codespaces-zshrc {
local RCFILE="$HOME/.zshrc"
local ME=$(get-my-account)
if ! check-rcfile "$RCFILE" "$ME" ; then
return 0
fi
cat <<'EOF' | sed -e "s/__ME__/$ME/g" >> "$RCFILE"
###
### for Codespaces by __ME__
###
source ~/.path_functions
source ~/.common_env
source ~/.bash_aliases
source_if_readable ~/.bash_secret
if is_interactive_shell ; then
export HISTSIZE=2000
export SAVEHIST=100000
setopt share_history
setopt hist_ignore_space
setopt hist_ignore_dups
setopt hist_no_store
setopt EXTENDED_HISTORY
bindkey -e # emacs like keybind
bindkey -s '^g' 'git '
fi
# For Cursor + SSH Codespaces connection setting. This emulates the behavior of VSCode's initial connection
# by automatically changing to the repository directory when connected.
if [ "$CODESPACES" = "true" ] ; then
if [ "$PWD" = "$HOME" ] && [ -d "/workspaces/$RepositoryName" ] ; then
cd "/workspaces/$RepositoryName"
fi
fi
EOF
}
function append-codespaces-shellrc {
local login_shell_name=$(basename "$SHELL")
case "$login_shell_name" in
bash)
append-codespaces-bashrc
;;
zsh)
append-codespaces-zshrc
;;
*)
echo "unsupported login shell: $login_shell_name" >&2
;;
esac
}
# apt-get が確認できる場合、Codespaces 用の基本的なコマンドをインストールする
function install-codespaces-fundamental-commands-by-apt {
# apt-get がないなら何もしない
type apt-get >/dev/null 2>&1 || return 0
sudo apt-get update && sudo apt-get install -y "${INSTALL_DEB_PACKAGES_IN_CODESPACES[@]}"
}
# bat パッケージを入れた後、batcat コマンドを bat コマンドとして実行できるようにする
function setup-bat {
if ! type batcat > /dev/null 2>&1 ; then
echo "batcat is not installed" >&2
return 1
fi
mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat
}
main "$@"