-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev_tool.sh
More file actions
executable file
·492 lines (443 loc) · 15 KB
/
dev_tool.sh
File metadata and controls
executable file
·492 lines (443 loc) · 15 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/bin/bash
#
#
# author: lorenzo chianura
# mail: lorenzo.chianura_at_gmail.com
# version: 0.2
#
#
## tools, helpers, data structures, colors
USER=$(whoami)
SDKDIR=""
DATAFILE="pkgs.data"
PKGS=
REPOS=
GITSTATUS="git status --porcelain"
GITREMOTEUPDATE="git remote update"
# associative arrays
declare -Ag libs_to_repos
declare -Ag libs_to_branch
# colors
RED_COLOR="\e[01;31m"
GREEN_COLOR="\e[01;32m"
YELLOW_COLOR="\e[01;33m"
BLUE_COLOR="\e[01;34m"
END_COLOR="\e[0m"
# useful fields for data mining
LIBNAME="1"
BRANCH="2"
GITURL="3"
## functions
# color helpers
red() { echo -e "$RED_COLOR $* $END_COLOR"; }
green() { echo -e "$GREEN_COLOR $* $END_COLOR"; }
blue() { echo -e "$BLUE_COLOR $* $END_COLOR"; }
yellow() { echo -e "$YELLOW_COLOR $* $END_COLOR"; }
# check if USER is not root, otherwise exit
check_uid() {
if [ "$EUID" -eq 0 ]; then
echo "[$(red WARNING)] do not run this script as root!"
exit 1
fi
}
# check if SDK is installed, if TRUE check for write permissions
check_sdk() {
if [ ! -d "$SDKDIR" ]; then
echo "[$(red WARNING)] SDK path does not exist!"
exit 1
else
if [ "$(stat -c %U "${SDKDIR}")" != "$USER" ]; then
echo -n "[$(yellow WARNING)]$(blue "${USER}")does not have write "
echo -n "permissions on $SDKDIR, fix this? [y,n]: "
read -r yn
case $yn in
[Yy]* ) sudo chown "$USER":"$USER" -R $SDKDIR;;
[Nn]* ) exit 0;;
* ) echo "Please answer yes or no."; exit 1;;
esac
fi
echo "[$(blue INFO)] SDK sysroot path: $SDKDIR"
fi
}
# check if DATAFILE is provided
check_data() {
if [ ! -f "$DATAFILE" ]; then
echo "[$(yellow WARNING)] cannot found $DATAFILE !"
echo -n "create an empty one? [y,n]: "
read -r yn
case $yn in
[Yy]* ) echo "# lines beginning with # will be ignored" > $DATAFILE;;
[Nn]* ) exit 1;;
* ) echo "Please answer yer or no."; exit 1;;
esac
else
PKGS=( `grep -v '^[[:space:]]*#' ${DATAFILE} | awk -v idx=$LIBNAME '{print $idx}'` )
REPOS=( `grep -v '^[[:space:]]*#' ${DATAFILE} | awk -v idx=$GITURL '{print $idx}'` )
BRANCHES=( `grep -v '^[[:space:]]*#' ${DATAFILE} | awk -v idx=$BRANCH '{print $idx}'` )
# check if arrays length is correct
if [ "${#PKGS[*]}" -ne "${#REPOS[*]}" -o "${#PKGS[*]}" -ne "${#BRANCHES[*]}" ]; then
echo "[$(red ERROR)] $DATAFILE is malformed!"
else
# build associative arrays
idx=0
while [ "$idx" -lt "${#PKGS[@]}" ]; do
tlib="${PKGS[$idx]}"
trepo="${REPOS[$idx]}"
tbranch="${BRANCHES[$idx]}"
# TODO: why I have to "duplicate" tlib and trepo vars in order
# to avoid error assignment in the associative array?
# I should be able to use:
# libs_to_repos[$tlib]=$trepo
# but it ends with "wrong index" error
sub_idx=$tlib
sub_arg_repo=$trepo
sub_arg_branch=$tbranch
libs_to_repos[$sub_idx]=$sub_arg_repo
libs_to_branch[$sub_idx]=$sub_arg_branch
let idx=idx+1
done
fi
fi
}
# list packages
list_pkgs() {
for pkg in "${PKGS[@]}"; do
if [ ! -d $pkg ]; then
# pkg listed in DATAFILE, but no dir exist
echo "$pkg $(yellow listed but not cloned)"
else
echo "$pkg"
fi
done
echo
}
# first time setup: fetch all packages and build them
setup_env() {
if [ "${#PKGS[*]}" -ne 0 ]; then
for pkg in "${PKGS[@]}"; do
echo -n "[$(yellow FETCHING)] ${pkg} ..."
if [ -d "${pkg}" ]; then
cls_row
echo -n "[$(red FETCHING)] ${pkg} ..."
echo "$(red a directory with the same name already exist)"
else
clone_pkg $pkg
fi
done
else
echo -n "[$(yellow WARNING)] $DATAFILE does not contain any package"
echo
exit 0
fi
}
# clone $1 pkg
clone_pkg() {
cls_row
echo -n "[$(yellow FETCHING)] $1 ..."
# check if the remote branch exist
git ls-remote ${libs_to_repos[$1]} | grep ${libs_to_branch[$1]} &> /dev/null
if [ "$?" -ne 0 ]; then
cls_row
echo -n "[$(red FETCHING)] $1 ..."
echo "$(red branch)$(blue ${libs_to_branch[$1]})$(red not found in upstream repo)"
else
git clone ${libs_to_repos[$1]} -b ${libs_to_branch[$1]} $1 &> /dev/null
if [ $? -eq 0 ]; then
cls_row
echo -n "[$(green FETCHING)] $1 ..."
echo "$(green done) [$(blue ${libs_to_branch[$1]})]"
else
cls_row
echo -n "[$(red FETCHING)] $1 ..."
echo "$(red failed)"
fi
fi
}
# ask for cloning
ask_for_reclone() {
echo -n "[$(blue INFO)] clone everything again? "
echo "(obviously, undeleted directories will not be overwritten) [y,n]: "
read -r yn
case $yn in
[Yy]* ) setup_env; exit 0;;
[Nn]* ) exit 0;;
* ) echo "Please answer yes[y] or no[n]."; exit 1;;
esac
}
# add packages to DATAFILE
add_pkg() {
if [[ -z ${1} || -z ${2} ]]; then
echo
echo "missing arguments, usage:"
echo " ./dev_tool.sh -a git@your.git.repo/project.git branch"
echo
else
idx=0
flag=0
while [ $idx -lt ${#REPOS[@]} ]; do
repo="${REPOS[$idx]}"
if [ "${repo}" == "${1}" ]; then
echo -n "[$(yellow WARNING)] ${1} already in ${DATAFILE}"
echo
flag=1
break
fi
let idx=idx+1
done
if [ "${flag}" -eq 0 ]; then
pkgname=$(echo "${1}" | awk -F "/" '{print $NF}' | cut -d. -f1)
echo ${pkgname} ${2} ${1} >> ${DATAFILE}
echo "[$(blue INFO)] $pkgname added"
check_data
echo "[$(blue INFO)] packages list updated"
echo -n "[$(blue INFO)] clone ${pkgname} now? [y,n]: "
read -r yn
case $yn in
[Yy]* ) clone_pkg ${pkgname}; exit 0;;
[Nn]* ) exit 0;;
* ) echo "Please answer yes[y] or no[n]."; exit 1;;
esac
fi
fi
}
# remove all cloned repos
remove_all() {
if [ -z "${1}" ]; then
echo "[$(blue INFO)] you're going to $(red permanently) delete all your cloned repositories"
echo " ($(yellow NOTE) that in case you decide to proceed, local repos that have unstaged work will remain untouched )"
echo "are you sure? [y,n]: "
read -r yn
case $yn in
[Yy]* ) ;;
[Nn]* ) exit 0;;
* ) echo "Please answer yes[y] or no[n]."; exit 1;;
esac
#delete all cloned dirs
for pkg in "${PKGS[@]}"; do
echo -n "[$(yellow DELETING)] ${pkg} ..."
if [ ! -d ${pkg} ]; then
cls_row
echo -n "[$(red DELETING)] ${pkg} ..."
echo "$(red not found)"
else
cd $pkg &>/dev/null
unstaged=$(${GITSTATUS} 2>&1 | awk '{print $1}' | wc -l)
if [ ${unstaged} -gt 0 ]; then
cls_row
echo -n "[$(red DELETING)] ${pkg} ..."
echo "$(red cannot proceed)"
echo
echo -e "\t cannot delete $(blue ${pkg}) because you have"
echo -e "\t unstaged work in your local repository. Please"
echo -e "\t commit and push your work or stash/delete it."
echo
cd - &>/dev/null
continue;
else
cd - &>/dev/null
fi
rm -rf ${pkg} &> /dev/null
if [ $? -eq 0 ]; then
echo "$(green done)"
else
echo "$(red failed)"
fi
fi
done
echo
ask_for_reclone
else
pkgname=${1%/}
echo -n "[$(yellow DELETING)] ${pkgname} ..."
if [ ! -d "${pkgname}" ]; then
cls_row
echo -n "[$(red DELETING)] ${pkgname} ..."
echo "$(red not found)"
else
cd $pkgname &>/dev/null
unstaged=$(${GITSTATUS} 2>&1 | awk '{print $1}' | wc -l)
if [ $unstaged -gt 0 ]; then
cls_row
echo -n "[$(red DELETING)] $pkgname ..."
echo "$(red cannot proceed)"
echo
echo -e "\t cannot delete $(blue $pkgname) because you have"
echo -e "\t unstaged work in your local repository. Please"
echo -e "\t commit and push your work or stash/delete it."
echo
cd - &>/dev/null
else
cd - &>/dev/null
fi
rm -rf ${pkgname} &> /dev/null
if [ $? -eq 0 ]; then
echo "$(green done)"
else
echo "$(red failed)"
fi
echo -n "[$(blue INFO)] delete $pkgname entry from local db? Future updates will avoid to update it [y,n]: "
read -r yn
case $yn in
[Yy]* ) sed -i "/"$pkgname"/d" $DATAFILE;;
[Nn]* ) exit 0;;
* ) echo "Please answer yes[y] or no[n]."; exit 1;;
esac
fi
fi
}
# remove all the stuff without check git status
remove_force() {
echo "[$(blue INFO)] you're going to $(red permanently) delete all your cloned repositories,"
echo "also, keep in mind that no check will be performed on the actual local status of every repository,"
echo "proceeding will rm -rf every cloned pkg."
echo "Are you sure? [y,n]: "
read -r yn
case $yn in
[Yy]* ) ;;
[Nn]* ) exit 0;;
* ) echo "Please answer yes[y] or no[n]."; exit 1;;
esac
echo "are you $(red REALLY sure?) [y,n]): "
read -r yn
case $yn in
[Yy]* ) ;;
[Nn]* ) exit 0;;
* ) echo "Please answer yes[y] or no[n]."; exit 1;;
esac
#delete all cloned dirs
for pkg in "${PKGS[@]}"; do
echo -n "[$(yellow DELETING)] ${pkg} ..."
if [ ! -d ${pkg} ]; then
echo "$(red not found)"
else
rm -rf ${pkg} &> /dev/null
if [ $? -eq 0 ]; then
echo "$(green done)"
else
echo "$(red failed)"
fi
fi
done
echo
ask_for_reclone
}
# print a "first time run" warning
setup_warn() {
echo
echo "[$(yellow README)] You're going to all the"
echo " packages listed in ${DATAFILE}: if a previous "
echo " run of this script has already fetched all or some "
echo " of the packages, the git fetch will fail."
echo
}
# clean stdout row
# TODO: a clever way should exist, find it
cls_row() {
echo -ne " \r"
}
# update packages
update_pkgs() {
if [ -z "${1}" ]; then
for pkg in "${PKGS[@]}"; do
update_pkg ${pkg}
done
else
update_pkg ${1}
fi
echo
}
# update single package
update_pkg() {
echo
cls_row
pkgname=${1%/}
echo -ne "[$(yellow UPDATING)] ${pkgname} ...\r"
if [ ! -d "${pkgname}" ]; then
cls_row
echo -ne "[$(red UPDATING)] ${pkgname} ... $(red not found)\r"
else
cd "$1" &>/dev/null
unstaged=$(${GITSTATUS} 2>&1 | awk '{print $1}' | wc -l)
if [ "${unstaged}" -gt 0 ]; then
cls_row
echo -ne "[$(red UNSTAGED)] ${pkgname} ... $(red cannot update)\r"
echo
echo -e "\t cannot update $(blue $pkgname) because you have"
echo -e "\t unstaged work in your local repository. Please"
echo -e "\t commit and push your work or stash/delete it."
echo
else
# TODO: check git remote update failures
# for stuff like timeouts and others related
cls_row
echo -ne "[$(yellow UPDATING)] ${pkgname} ... checking remote\r"
lines=$(${GITREMOTEUPDATE} 2>&1 | wc -l)
if [ "$lines" -gt 1 ]; then
cls_row
echo -ne "[$(yellow UPDATING)] ${pkgname} ... fetching\r"
git pull &>/dev/null
if [ $? -eq 0 ]; then
cls_row
echo -ne "[$(blue UPDATING)] ${pkgname} ... $(blue updated)\r"
else
cls_row
echo -ne "[$(yellow UPDATING)] ${pkgname} ... $(red failed)\r"
fi
else
cls_row
echo -ne "[$(green UPDATING)] ${pkgname} ... $(green already up to date)\r"
fi
fi
cd - &>/dev/null
fi
}
# print a useful help
print_help() {
echo "
Use: $0 [command] <options>
[command] could be:
-a, --add <repo> <branch> add <repo> to DATAFILE, <branch> will
be used when cloning
-s, --setup fetch all the repositories listed in DATAFILE.
Use this option only during the first setup of
your development environment
-i, --init <repo> clone the given <repo>, that should be already present in
${DATAFILE}
-l, --list list packages contained in ${DATAFILE}
-r, --remove <pkg> if <pkg> is provided, remove cloned <pkg> otherwise
delete all cloned repos.
If you have unstaged changes in one or more local repo/s
a warning message will inform you and the related
repository will not be deleted
-u, --update <pkg> for every package, check if remote (git) has changed,
if TRUE try to pull from the configured branch (see
the ${DATAFILE} for all the infos); if there are
unstaged changes a warning message will inform you
and the git pull will not be performed. If <pkg> is given
only <pkg> will be updated.
-rf, --remove-force delete all the existing cloned repositories,
WITHOUT checking the git status
-h, --help prints this help and exit
This tool use a file named ${DATAFILE} (if you want to use a different
file change DATAFILE variable) to retrieve informations about name, branch
and repository url.
A typical entry should be formatted as:
project_name branch repo_url
where project_name can be a name of your choice.
Lines which starts with # character will be ignored.
"
}
# main stuff
check_uid
case ${1} in
-a|--add ) check_data; add_pkg ${2} ${3}; exit 0;;
-i|--init ) check_data; clone_pkg ${2}; exit 0;;
-l|--list ) check_data; list_pkgs; exit 0;;
-r|--remove ) check_data; remove_all ${2}; exit 0;;
-rf|--remove-force ) check_data; remove_force; exit 0;;
-s|--setup ) check_data; setup_warn; setup_env; exit 0;;
-u|--update ) check_data; update_pkgs ${2}; exit 0;;
-h|--help ) print_help; exit 0;;
* ) echo; echo "you should add a command, try with: ${0} --help"; echo; exit 1;;
esac
exit 0