-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·286 lines (244 loc) · 7.86 KB
/
install.sh
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
#!/bin/bash -euC
#
# .vimrcファイルをインストールする
# 既存の.vimrcファイルが存在する場合、 ファイル名_backup_日時 というファイルで同一ディレクトリ下にバックアップする
# また、既存の.vimrcファイルが本スクリプトで生成したファイルであった場合でも、同様にバックアップする。
#
# Arguments:
# オプション
# -h, --help: 本スクリプトの説明を標準エラー出力する
# -r, --uninstall:
# 本スクリプトで生成したファイルを削除し、本スクリプトで変更したバックアップファイルから.vimrcファイルに戻す
# なお、このコマンドだけでは本リポジトリそのものの削除はしない
#
# Examples:
# ./install.sh [options]
#
HELP_MESSAGE="
This is installer for .vimrc.
If a .vimrc file exists, backup it under the same directory to rename the file name: \${file_name}_backup_\${date}.
Even if the existing .vimrc file is a file generated by this script, it will be backup as well.
Arguments:
Option
-h, --help: Show help message.
-r, --uninstall:
Remove a file generated by this script, and restore backup file to .vimrc.
But this script is not removed this repository.
Examples:
./install.sh [option]
"
VIMRC_DESCRIPTION="Morichan's vimrc file"
##
# ログとして標準エラー出力に表示する
#
# Arguments:
# ログとして出力したい文字列
#
function echo_log() {
echo "$1" 1>&2
}
##
# ホームディレクトリ絶対パスを標準出力する
#
function echo_vimrc_file_path() {
echo "$HOME/.vimrc"
}
##
# 本スクリプトを格納しているディレクトリ絶対パスを標準出力する
#
function echo_repository_path() {
echo "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
}
##
# 本スクリプト実行前に存在する.vimrcファイルをバックアップする
#
# Arguments:
# バックアップ対象の.vimrcファイル絶対パス
#
function backup_vimrc_file() {
local file_path=$1
if [[ "$file_path" = "" ]]; then
echo_log "Error: file path not set."
exit 1
elif [[ ! -f "$file_path" ]]; then
echo_log "File is not found, so don't backup: $file_path"
return 0
fi
local changed_file_path="${file_path}_backup_$(date +%Y%m%d%H%M%S)"
echo_log "+ mv \"$file_path\" \"$changed_file_path\""
mv "$file_path" "$changed_file_path"
}
##
# .vimrcファイルを生成する
# 動的生成するための変数が多いため、sampleファイルからのコピーはせずにcatコマンドで生成する
#
# Arguments:
# - 出力先の.vimrcファイル絶対パス
# - 出力先の.vimrcファイルの先頭に記載する説明文、これを元に is_generated_by_this_script の実行を判定する
#
function generate_vimrc_file() {
local output_file_path=$1
local description=$2
local repository_path=$(echo_repository_path)
local readme_path="$repository_path/README.md"
local rc_path="$repository_path/rc/"
if [[ $# -ne 2 ]]; then
echo_log "Error: property is insufficient."
exit 1
elif [[ "$output_file_path" = "" ]]; then
echo_log "Error: file path not set."
exit 1
elif [[ -f "$output_file_path" ]]; then
echo_log "File is already exists, so don't generate: $output_file_path"
exit 1
fi
echo_log "+ cat << EOF > $output_file_path ... EOF"
cat << EOF > $output_file_path
" $description
" This is custom settins, for more details: $readme_path
set runtimepath+=$rc_path
let g:VIM_DOTFILES_ROOT_DIR='$repository_path'
runtime! init.vim
EOF
}
##
# 対象ファイルが本スクリプトで生成したファイルの場合は正常終了する
# 2番目の引数の文字列が1行目に記載されている場合に、本スクリプトで生成したファイルとして判断する
#
# Arguments:
# - 対象ファイルの絶対パス
# - 本スクリプトで生成したファイルであると判断するための検索文字列
#
# Returns:
# 0: 対象ファイルは本スクリプトで生成したファイルである
# 1: 対象ファイルは本スクリプトで生成したファイルでない
#
function is_generated_by_this_script() {
local file_path=$1
local description=$2
if [[ $description = "" ]]; then
echo_log "Error: description is empty."
exit 1
fi
if [[ -f $file_path && "$(cat $file_path | head -1)" =~ "$description" ]]; then
return 0
else
return 1
fi
}
##
# バックアップファイルの絶対パスを標準出力する
# 複数存在する場合、lsによる出力結果の1番目のファイルを表示する
# 存在しない場合、空文字を表示する
#
function echo_vimrc_backup_file_path() {
local vimrc_file_path=$(echo_vimrc_file_path)
echo $(ls ${vimrc_file_path}_backup_* 2> /dev/null | head -1)
}
##
# 本スクリプトで生成した.vimrcファイルを削除する
# 本スクリプトで生成したか否かの判定は is_generated_by_this_script 関数に依存する
#
function remove_vimrc_file() {
local file_path=$(echo_vimrc_file_path)
local vimrc_description=$VIMRC_DESCRIPTION
if [[ ! -f "$file_path" ]]; then
echo_log "File is not found, so don't remove: $file_path"
return 0
elif ! is_generated_by_this_script $file_path $vimrc_description; then
echo_log "File is not generated by this script, so don't remove: $file_path"
return 0
fi
echo_log "+ rm $file_path"
rm $file_path
}
##
# バックアップファイルを.vimrcファイルにリストアする
# リストア対象のバックアップファイルの選定は echo_vimrc_backup_file_path 関数に依存する
#
function restore_vimrc_file() {
local file_path=$(echo_vimrc_file_path)
local backup_file_path=$(echo_vimrc_backup_file_path)
if [[ ! -f "$backup_file_path" ]]; then
echo_log "File is not found, so don't restore: ${file_path}_backup_*"
return 0
fi
echo_log "+ mv $backup_file_path $file_path"
mv "$backup_file_path" "$file_path"
}
##
# 本リポジトリの .vim ディレクトリ配下にdeinをインストールする
# インストール済みの場合、deinをアップデートする
#
function install_dein() {
local repository_path=$(echo_repository_path)
local path="$repository_path/.vim/dein/repos/github.com/Shougo"
local dein_path="$path/dein.vim"
if [[ -d "$dein_path" ]]; then
echo_log "+ git pull $dein_path"
cd $dein_path
git pull
cd - > /dev/null
return 0
fi
echo_log "+ mkdir -p $path"
mkdir -p $path
echo_log "+ git clone https://github.com/Shougo/dein.vim.git $dein_path"
git clone https://github.com/Shougo/dein.vim.git $dein_path
}
##
# 本リポジトリの .vim ディレクトリ配下にundoディレクトリを作成する
# undoディレクトリはvimのundo機能利用時に呼出す
#
function mkdir_undo() {
local repository_path=$(echo_repository_path)
local path="$repository_path/.vim/undo"
echo_log "+ mkdir -p $path"
mkdir -p $path
}
##
# 引数を確認する
#
# Arguments:
# スクリプト実行時の引数リスト
#
# Examples:
# check_properties $@
#
function check_properties() {
if [[ $# -eq 0 ]]; then
return 0
fi
if [[ $1 = "-h" || $1 = "--help" ]]; then
echo_log "$HELP_MESSAGE"
exit 1
elif [[ $1 = "-r" || $1 = "--uninstall" ]]; then
remove_vimrc_file
restore_vimrc_file
exit 1
fi
}
##
# メイン関数
#
# Arguments:
# スクリプト実行時の引数リスト
#
# Examples:
# main $@
#
function main() {
check_properties $@
local vimrc_description=$VIMRC_DESCRIPTION
local vimrc_file_path=$(echo_vimrc_file_path)
if is_generated_by_this_script $vimrc_file_path $vimrc_description; then
echo_log "Info: File is generated by this script: $vimrc_file_path"
fi
backup_vimrc_file "$vimrc_file_path"
generate_vimrc_file "$vimrc_file_path" "$vimrc_description"
install_dein
mkdir_undo
echo_log "Succeeded!"
exit 0
}
main $@