-
Notifications
You must be signed in to change notification settings - Fork 0
/
up
196 lines (158 loc) · 5.45 KB
/
up
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
#!/usr/bin/env bash
# ↑はshebangのこと。
set -eu
# -eは、return codeが0以外のコマンド実行時に処理を終了する。エラーがあったら止める
# -uは、未定義変数を参照したときに処理を終了する。
set -o pipefail
# -oはオプションを有効にする
# 途中でエラーなら途中で終了
: ${DOTFILES_PATH:="$HOME/dotfiles"}
: ${DOTFILES_BRANCH:=master}
ARGS=('')
for a in "$@"; do
ARGS=(${ARGS[@]} "$a")
done
# $@は全引数が設定される変数
# ARGS=(${ARGS[@]} "$a")は配列へのappend!
#=== Utils
#==============================================================================================
compare_versions() {
declare -a v1=(${1//./ }) #declare -a は連想配列を作成?
declare -a v2=(${2//./ })
local i=""
for (( i=${#v1[@]}; i<${#v2[@]}; i++ )); do
v1[i]=0
done
for (( i=0; i<${#v1[@]}; i++ )); do
if [[ -z ${v2[i]} ]]; then #[[]]は0なら真
v2[i]=0
fi
if (( 10#${v1[i]} > 10#${v2[i]} )); then
printf ">"
return 0
fi
if (( 10#${v1[i]} < 10#${v2[i]} )); then
printf "<"
return 0
fi
done
printf "="
}
#=== Steps
#==============================================================================================
print_header() {
printf "\e[34m"
echo '--------------------------------------------------------------------------------'
echo ' '
echo ' 888 888 .d888 d8b 888 '
echo ' 888 888 d88P" Y8P 888 '
echo ' 888 888 888 888 '
echo ' .d88888 .d88b. 888888 888888 888 888 .d88b. .d8888b '
echo ' d88" 888 d88""88b 888 888 888 888 d8P Y8b 88K '
echo ' 888 888 888 888 888 888 888 888 88888888 "Y8888b. '
echo ' Y88b 888 Y88..88P Y88b. 888 888 888 Y8b. X88 '
echo ' "Y88888 "Y88P" "Y888 888 888 888 "Y8888 88888P" '
echo ' '
echo ' Harder, Better, Faster, Stronger '
echo ' '
echo ' github.com/creasty/dotfiles '
echo ' '
echo '--------------------------------------------------------------------------------'
printf "\e[0m\n"
}
check_os() {
if [ "$(uname -s)" == "Darwin" ]; then
# unameはOS名. -sもOS名.
if [ $(compare_versions "$(sw_vers -productVersion)" "10.9") == '<' ]; then
# sw_versはMacのバージョン確認
echo "Sorry, this script is intended only for OS X 10.9.0+"
exit 1
fi
else
echo "Sorry, this script is intended only for OS X"
exit 1
fi
}
clone_or_update_repo() {
local git_dir="$DOTFILES_PATH/.git" #ローカル変数
if [ -d "$git_dir" ]; then # -d はディレクトリが存在するか(?)
echo 'Updating repo...'
if [ "$(git --git-dir="$git_dir" symbolic-ref --short HEAD 2> /dev/null)" != "master" ]; then
# 2> はリダイレクト
echo 'skip (working on a non-master branch)'
return
fi
if ! git --git-dir="$git_dir" diff --no-ext-diff --quiet --exit-code > /dev/null 2>&1; then
echo 'skip (unstaged changes present)'
return
fi
if ! git --git-dir="$git_dir" diff-index --cached --quiet HEAD -- > /dev/null 2>&1; then
echo 'skip (uncommitted changes present)'
return
fi
git --git-dir="$git_dir" pull origin master
git --git-dir="$git_dir" submodule sync
echo 'done'
elif ! [ -d "$DOTFILES_PATH" ]; then
echo 'Cloning repo...'
git clone --recursive git://github.com/creasty/dotfiles.git --branch $DOTFILES_BRANCH $DOTFILES_PATH
echo 'done'
fi
}
check_xcode_license_approved() {
echo 'Agreeing to Xcode license...'
local has_error=0
if ! [[ "$(/usr/bin/xcrun clang 2>&1 || true)" =~ 'license' ]]; then
echo 'skip (already approved)'
return
fi
sudo expect -c '
set timeout 3
spawn
expect {
timeout { exit 2 }
-exact "for more" {
send "G"
exp_continue
}
-exact "agree, print, cancel" {
send "agree\n"
exp_continue
}
-exact "You can view the license agreements" {
exit 0
}
}
' > /dev/null || has_error=1
[ $has_error -eq 1 ] && sudo xcodebuild -license
echo 'done'
}
install_homebrew() {
command -v 'brew' > /dev/null 2>&1 && return
echo 'Installing homebrew...'
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null
echo 'done'
}
install_ansible() {
command -v 'ansible' > /dev/null 2>&1 && return
echo 'Installing ansible...'
brew install ansible
echo 'done'
}
run_provisioning() {
echo 'Provisioning...'
sh $DOTFILES_PATH/provisioning/run.sh ${ARGS[@]}
echo 'done'
}
#=== Entrypoint
#==============================================================================================
main() {
print_header
check_os
# clone_or_update_repo
check_xcode_license_approved
install_homebrew
install_ansible
run_provisioning
}
main