-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotdeploy
More file actions
executable file
·187 lines (163 loc) · 4.55 KB
/
dotdeploy
File metadata and controls
executable file
·187 lines (163 loc) · 4.55 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
#!/bin/sh
# dotdeploy - configurable dotfile and directory management
#
# Running this script looks for all *.deploy files in PWD,
# and sources them. Consequently, those need sh compliant
# syntax and definitions distinct from the script's.
#
# Example Usage:
# ./dotdeploy conffiles # where example.deploy is in .
#
# Example conf:
# # in file: example.deploy
# conffiles=('conf_home') # defines a compound target
# # of sequential subtargets
# conf_home=( # begin target definition
# . ~ # defines relative base source and base target for actions
# d bin # mkdir ~/bin
# f .bash_history # touch ~/.bash_history
# @ . .bashrc # ln -s ././.bashrc ~/.bashrc
# ) # end target definition
### setup output
if [ "$1" = "-v" ] ; then
alias vecho='echo '
shift
else
alias vecho='true '
fi
alias errcho='>&2 echo \! '
### load user conf
vecho "loading:"
for conf in $PWD/*.deploy; do
vecho " - $(basename $conf)"
source $conf
done
### DSL actions
# $1 target base dir
# $2 directory to create
action_mkdir () {
local target="$1/$2"
vecho " - mkdir: $target"
mkdir -p "$target"
}
# $1 target base dir
# $2 file to create
action_mkfile () {
local target="$1/$2"
vecho " - touch: $target"
touch "$target"
}
# $1 source base dir
# $2 target base dir
# $3 source directory for file, rel. to $1
# $4 target file, rel. to $2
action_mirror () {
local source="$1/$3/$(basename $4)"
local target="$2/$4"
vecho " - mirror: $target → $source"
# check if file already exists
if ! [ -e "$target" ]; then
cp "$source" "$target"
elif [ "$(sha1sum $target)" != "$(sha1sum $source)" ]; then
errcho "mirror: refusing to overwrite existing differing file ($target)"
fi
}
# $1 source base dir
# $2 target base dir
# $3 source directory for file, rel. to $1
# $4 target file, rel. to $2
action_hardlink () {
local source="$1/$3/$(basename $4)"
local target="$2/$4"
vecho " - hardlink: $target → $source"
# check if file already exists
if ! [ -e "$target" ]; then
cp -l "$source" "$target"
elif [ "$(sha1sum $target)" != "$(sha1sum $source)" ]; then
errcho "hardlink: refusing to replace existing differing file ($target)"
fi
}
# $1 source base dir
# $2 target base dir
# $3 source directory for file, rel. to $1
# $4 target file, rel. to $2
action_symlink () {
local source="$1/$3/$(basename $4)"
local target="$2/$4"
vecho " - ln: $target → $source"
# check if link already exists
if [ -L "$target" ]; then
if [ "$source" != "$(readlink $target)" ]; then
echo " - ln: updating existing differing symlink ($target)"
echo " was: $(readlink $target)"
fi
ln -sfT "$source" "$target"
# check if any other sort of file exists
elif [ -e "$target" ]; then
errcho "ln: refusing to replace existing non-link file ($target)"
# no file exists, symlinking is safe
else
ln -s "$source" "$target"
fi
}
### DSL parsing & dispatch
# $1 source base dir
# $2 target base dir
# $3 array of commands, arguments and $3
handle_actions () {
source_base_dir=$(cd "$1"; pwd)
[ -d "$2" ] || echo " mkdir $2" && mkdir -p "$2"
target_base_dir=$(cd "$2"; pwd)
shift 2
while [ ! $# -eq 0 ] ; do
case "$1" in
d)
action_mkdir "$target_base_dir" "$2"
shift
;;
f)
action_mkfile "$target_base_dir" "$2"
shift
;;
l)
action_hardlink "$source_base_dir" "$target_base_dir" "$2" "$3"
shift 2
;;
m)
action_mirror "$source_base_dir" "$target_base_dir" "$2" "$3"
shift 2
;;
@)
action_symlink "$source_base_dir" "$target_base_dir" "$2" "$3"
shift 2
;;
*)
errcho "illegal directive: $2"
;;
esac
shift
done
}
# $1 target name
handle_target () {
if [ "${!1}" = '@' ] ; then
vecho " # destructuring compound target $1"
target_array="\${${1}[@]:1}"
for target in $(eval echo "$target_array"); do
handle_target "$target"
done
else
vecho " - target $1:";
arg_array="${1}[@]"
handle_actions "${!arg_array}"
fi
}
# $@ targets to handle
handle_targets () {
vecho 'executing:'
for target in "$@"; do
handle_target "$target"
done
}
handle_targets "$@"
exit 0