-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmodgit
More file actions
executable file
·425 lines (366 loc) · 10.2 KB
/
modgit
File metadata and controls
executable file
·425 lines (366 loc) · 10.2 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/bin/bash
version="0.1.0"
script=${0##*/}
usage="\
Module Git (v$version)
Global Commands:
$script [options] <command>
-------------------------------
init initialize .modgit folder
list list all modules that are currently installed
update-all update all modules that are currently installed
-h this help
-v display the modgit script's version
Module Commands:
$script [options] <command> <module> [<src>] [<git options>]
----------------------------------------------------------------
clone <module> <src> clone a git repository
update <module> update module
remove <module> remove module
[<git options>] specify additional parameters to git clone
-i include filter: -i app/ -i README [...]
-e exclude filter: -e file.txt -e lib/tests [...]
"
dry_run=0
includes=''
excludes=''
add_include()
{
if [ -z $includes ]; then
includes="$1"
else
includes="$includes\n$1"
fi
}
add_exclude()
{
if [ -z $excludes ]; then
excludes="$1"
else
excludes="$excludes\n$1"
fi
}
# Handle options
while getopts ":nvhi:e:" opt; do
case $opt in
h)
echo -e "$usage"
exit 0
;;
v)
echo "Module Git version: $version"
exit 0
;;
n)
dry_run=1
shift $((OPTIND-1)); OPTIND=1
;;
i)
add_include "$OPTARG"
shift $((OPTIND-1)); OPTIND=1
;;
e)
add_exclude "$OPTARG"
shift $((OPTIND-1)); OPTIND=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Echo in bold
echo_b()
{
if [ "$1" = "-e" ]; then
echo -e "$(tput bold)$2$(tput sgr0)"
else
echo "$(tput bold)$1$(tput sgr0)"
fi
}
# Initializes .modgit folder in root dir then quit
if [ "$1" = "init" ]; then
mkdir .modgit || { echo_b "Could not create .modgit directory" && exit 1; }
echo_b "Initialized Module Git at $(pwd)"
exit 0
fi
# Checks if module dir exists or quit
require_module_dir()
{
if ! [ -d "$modgit_dir/$1" ]; then
echo_b "ERROR: $1 does not exist.";
exit 1
fi
return 0
}
delete_files()
{
local module=$1
local module_dir="$modgit_dir/$module"
local deployed_file="$module_dir/deployed.modgit"
while read line; do
if ! [ -z $line ] && [ -f "$root/$line" ]; then
rm "$root/$line" 2>/dev/null
prev_dir=''
dir=$(dirname $line)
while [ "$dir" != "$prev_dir" ]; do
prev_dir=$dir
test_dir="$root/$dir"
if ! [ "$(ls -A $test_dir)" ]; then
rmdir $test_dir
fi
dir=${dir%/*}
done
fi
done < <(cat "$deployed_file" 2>/dev/null)
return 0
}
# Removes a module
delete_module()
{
local module=$1
local module_dir="$modgit_dir/$module"
if delete_files "$module" &&
rm -rf "$module_dir" 2>/dev/null;
then
return 0
else
return 1
fi
}
deploy_file()
{
mkdir -p $(dirname $2) && cp "$1" "$2"
}
# Synchronizes files from module dir to root dir, then store deployed files for easy remove
move_files()
{
local module=$1
local module_dir="$modgit_dir/$module"
local source_dir="$module_dir/source"
local includes_file="$module_dir/includes.modgit"
local excludes_file="$module_dir/excludes.modgit"
local modman_file="$source_dir/modman"
local deployed_file="$module_dir/deployed.modgit"
cd "$source_dir" || return 1
while read file; do
# Copy file by default
copy=1
target="$file"
# Include filters
if [ -s "$includes_file" ]; then
copy=0
while read filter; do
src=$(echo $filter | cut -d: -f1)
if [ -z $src ]; then
continue
fi
if [[ "$file" =~ ^$src ]]; then
copy=1
# Handle optional different target
real=$(echo $filter | cut -d: -f2)
if [ "$src" != "$real" ]; then
tmp_src=$(echo $src | sed 's/^[\/]*//;s/[\/]*$//')
tmp_src="$tmp_src/"
tmp_src="${tmp_src//\//\\/}"
tmp_real=$(echo $real | sed 's/^[\/]*//;s/[\/]*$//')
tmp_real="$tmp_real/"
tmp_real="${tmp_real//\//\\/}"
target=$(echo $file | sed "s/$tmp_src/$tmp_real/g")
fi
break
fi
done < <(cat "$includes_file")
fi
# Exclude filters
if [ -s "$excludes_file" ] && [ $copy -eq 1 ]; then
while read filter; do
if [[ "$file" =~ ^$filter ]]; then
copy=0
break
fi
done < <(cat "$excludes_file")
fi
if [ $copy -eq 1 ]; then
# Handle modman file
if [ -s "$modman_file" ]; then
while read line; do
if [ -z "$line" ] || [[ $line =~ ^# ]] || [[ $line =~ ^@ ]]; then
continue
fi
set -- $line
if [[ "$file" =~ ^$1 ]]; then
# Remove trailing slashes and escape paths for sed
src=$(echo $1 | sed 's/^[\/]*//;s/[\/]*$//')
dest=$(echo $2 | sed 's/^[\/]*//;s/[\/]*$//')
src="${src//\//\\/}"
dest="${dest//\//\\/}"
target=$(echo $file | sed "s/$src/$dest/g")
deploy_file "$file" "$root/$target"
echo "$target" >> "$deployed_file"
fi
done < <(cat "$modman_file")
else
deploy_file "$file" "$root/$target"
echo "$file" >> "$deployed_file"
fi
fi
done < <(find . -type f | grep -v '^\.\/.git.*' | sed 's/^\.\///')
return 0
}
# Creates module dir, clones git repo and optionally stores include/exclude filters
create_module()
{
local repo=$1;
local module=$2
local module_dir="$modgit_dir/$module"; shift 2
local source_dir="$module_dir/source"
local git_options=$@
cd "$modgit_dir"
if ! git clone --recursive $git_options -- $repo "$source_dir"; then
echo_b "Error cloning $repo to $source_dir."
rm -rf "$module_dir" 2>/dev/null
exit 1
fi
if ! [ -z $includes ]; then
echo -e $includes > "$module_dir/includes.modgit"
fi
if ! [ -z $excludes ]; then
echo -e $excludes > "$module_dir/excludes.modgit"
fi
return 0
}
# Updates a module
update_module()
{
local module=$1
local module_dir="$modgit_dir/$module"
local source_dir="$module_dir/source"
local success=0
require_module_dir "$module"
cd "$source_dir"
git pull && git submodule update --init --recursive && success=1
if [ $success -eq 1 ] &&
delete_files "$module" &&
move_files "$module";
then
return 0
else
return 1
fi
}
# Try to find .modgit dir
_pwd=$(pwd -P)
root=$_pwd
if ! [ -d $root/.modgit ]; then
echo_b "-e" "Module Git directory not found.\nRun \"$script init\" in the root of your project."
exit 1
fi
modgit_dir=$root/.modgit # path to .modgit
# list command
if [ "$1" = "list" ]; then
if [ -n "$2" ]; then echo "Too many arguments for list command."; exit 1; fi
while read -d $'\n' module; do
test -d "$modgit_dir/$module" || continue;
require_module_dir "$module" && echo "$module"
done < <(ls -1 "$modgit_dir")
exit 0
# update-all command
elif [ "$1" = "update-all" ]; then
if [ -n "$2" ]; then echo "Too many arguments for update-all command."; exit 1; fi
update_errors=0
updated=''
while read -d $'\n' module; do
test -d "$modgit_dir/$module" || continue;
echo "Updating $module..."
if ! update_module "$module"; then
echo_b "-e" "Error occurred while updating '$module'.\n"
update_errors=$((update_errors+1))
fi
done < <(ls -1 "$modgit_dir")
echo_b "Updated all modules with $update_errors update errors."
exit 0
# remove-all command
elif [ "$1" = "remove-all" ]; then
if [ -n "$2" ]; then echo "Too many arguments for remove-all command."; exit 1; fi
removeall_errors=0
removeall=''
while read -d $'\n' module; do
test -d "$modgit_dir/$module" || continue;
echo "Remove $module..."
if ! delete_module "$module"; then
echo_b "-e" "Error occurred while masive remove '$module'.\n"
removeall_errors=$((removeall_errors+1))
fi
done < <(ls -1 "$modgit_dir")
echo_b "Remove all modules with $removeall_errors remove errors."
exit 0
fi
#############################################
# Handle all other module-specific commands #
#############################################
REGEX_ACTION='(clone|update|remove|tmp)'
# Action is first argument
action=''
if [[ "$1" =~ $REGEX_ACTION ]]; then
action=$1; shift
fi
# If no module discovered or valid module is specified on command line
if [ -z "$module" ] || [ -n "$1" -a -d "$modgit_dir/$1" ]; then
module=$1; shift # module name is first argument
fi
[ -z "$module" ] && { echo_b "Not enough arguments (no module specified)"; exit 1; }
[ -z "$action" ] && { echo_b "Not enough arguments (no action specified)"; exit 1; }
cd "$_pwd" # restore old root
module_dir="$modgit_dir/$module" # working copy directory for module
# Handle module action
case "$action" in
clone)
if [[ "$module" =~ $REGEX_ACTION ]]; then
echo_b "-e" "You cannot $action a module with a name matching $REGEX_ACTION.\nModule specified: $module" && exit 1
fi
if [[ "$module" =~ [^a-z0-9_-]+ ]]; then
echo_b "-e" "You cannot $action a module with a name not matching [^a-z0-9_-]+ pattern.\nModule specified: $module" && exit 1
fi
if [ -d "$module_dir" ]; then
echo_b "A module with this name has already been cloned." && exit 1
fi
repo=$1; shift
create_module $repo $module $@ && success=1
if [ $success -eq 1 ]; then
if require_module_dir "$module" &&
move_files "$module";
then
echo_b "Clone of $module complete."
fi
else
if [ -d "$module_dir" ]; then rm -rf "$module_dir"; fi
echo_b "Error cloning $module, operation cancelled."
fi
;;
update)
if update_module "$module"
then
echo_b "Update of $module complete."
else
echo_b "Error updating $module, operation cancelled."
fi
;;
remove)
if require_module_dir "$module" &&
delete_module "$module";
then
echo_b "Removal of $module complete."
else
echo_b "Error removing $module, operation cancelled."
fi
;;
*)
echo -e "$usage"
echo_b "Invalid action: $action";
exit;
esac