-
Notifications
You must be signed in to change notification settings - Fork 0
/
the-dot.sh
executable file
·188 lines (149 loc) · 4.54 KB
/
the-dot.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
#!/bin/zsh
set -euo pipefail -o nullglob -o globdots -o re_match_pcre
function main {
declare self=${self:A}
declare repo=${self:h}
declare DRY_RUN=${DRY_RUN:-}
declare action=link-dotfiles
for arg in $@; do
case $arg in
--dry-run) DRY_RUN=1 ;;
--link-dotfiles|--link) action=link-dotfiles ;;
--print-config|--print|--test) action=print-config ;;
*)
echo "Invalid argument $arg" >&2
return 1
;;
esac
done
declare dotfiles=()
declare -A links=()
parse-config
$action
}
function parse-config {
cd $repo
trap 'cd -' EXIT
if [[ ! -f the-dot.conf ]]; then
echo "Config file ${repo/$HOME/~}/the-dot.conf does not exist" >&2
return 1
fi
declare tags=()
case $(uname) in
Darwin) tags+=( macos ) ;;
Linux) tags+=( linux ) ;;
esac
declare line=0
for directive in "${(@f)$(< the-dot.conf)}"
do
(( line += 1 ))
if [[ $directive =~ ^\s*$ ]]; then
continue
fi
if [[ ! $directive =~ '^\s*(?:\[(.+?)\]\s*)?(!)?\s*(.+?)(?:\s*=>\s*(.+))?\s*$' ]]; then
echo "Invalid config directive on line $line" >&2
echo $directive >&2
return 2
fi
declare tag=${match[1]:-}
declare exclude=${match[2]:-}
declare pattern=${match[3]:-}
declare link=${match[4]:-}
case ${tag:l} in
''|macos|linux) : ;;
*)
echo "Invalid config directive on line $line: invalid tag $tag" >&2
return 3
;;
esac
if [[ $exclude && $link ]]; then
echo "Invalid config directive on line $line: exclude pattern can't specify link-path" >&2
echo $directive >&2
return 4
fi
if [[ $pattern == /* ]]; then
echo "Invalid config directive on line $line: pattern can't be absolute" >&2
echo $directive >&2
return 5
fi
if [[ ${pattern:A} != $repo/* ]]; then
echo "Invalid config directive on line $line: pattern does not point within repo" >&2
echo $directive >&2
return 6
fi
if [[ $tag && ! ${tags[(r)${tag:l}]:-} ]]; then
continue
fi
declare matched=( ${~pattern} )
if (( ${#matched} == 0 )); then
echo "Invalid config directive on line $line: pattern did not match any files" >&2
echo $directive >&2
return 7
fi
for dotfile in $matched; do
if [[ $exclude ]]; then
dotfiles=( ${dotfiles:#$dotfile} )
unset links\[$dotfile\]
continue
fi
case ${dotfile:t} in
.DS_Store|.git|.gitmodules) continue ;;
esac
if [[ ${dotfile:t} == .gitignore && ${dotfile:h} != '' ]]; then
continue
fi
if [[ ! ${dotfiles[(r)$dotfile]:-} ]]; then
dotfiles+=( $dotfile )
fi
if [[ $link ]]; then
if [[ $pattern == *'*'* || $link == */ ]]; then
links[$dotfile]=${link%%/}/${dotfile:t}
else
links[$dotfile]=$link
fi
else
unset links\[$dotfile\]
fi
done
for dotfile in $dotfiles; do
if [[ ${dotfiles[(r)$dotfile/*]:-} ]]; then
dotfiles=( ${dotfiles:#$dotfile} )
unset links\[$dotfile\]
fi
done
done
}
function link-dotfiles {
for dotfile in $dotfiles; do
declare link=${links[$dotfile]:-$dotfile}
if [[ $link != /* ]]; then
link=~/$link
fi
if [[ -L $link && ${link:A} == $repo/$dotfile ]]; then
continue
fi
if [[ ! -e ${link:h} ]]; then
- mkdir -p ${link:h}
elif [[ ! -d ${link:h} ]]; then
echo "${link:h} already exists and is not a directory!" >&2
return 8
fi
if [[ -L $link ]]; then
- rm $link
elif [[ -e $link ]]; then
echo "$link already exists and is not a symlink!" >&2
return 9
fi
- ln -s $repo/$dotfile $link
done
}
function print-config {
for dotfile in $dotfiles
do
echo $dotfile ${links[$dotfile]:+'=>'} ${links[$dotfile]:-}
done
}
function - {
${DRY_RUN:+echo} $@
}
self=$0 main $@