-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwfm
executable file
·568 lines (521 loc) · 10.6 KB
/
wfm
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#!/bin/bash
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
NC='\033[0m' # No Color
BASE_REPO="https://github.com/feedhenry-raincatcher"
WFM_CLI_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $WFM_CLI_DIR
WFM_DIR="$WFM_CLI_DIR/.."
echo $WFM_DIR
pwd
MONGO_DIR="$WFM_DIR/mongo"
MONGO_DATA="$MONGO_DIR/data"
MONGO_LOG="$MONGO_DIR/mongodb.log"
MONGO_PID="$MONGO_DIR/mongodb.pid"
APPS=`cat $WFM_CLI_DIR/apps`
MODULES=`cat $WFM_CLI_DIR/modules`
UI_MODULES=`cat $WFM_CLI_DIR/ui_modules`
#echo $MODULES
ORIG_DIR=$(pwd)
REPO_DIRS=$APPS
for D in $MODULES; do
REPO_DIRS=${REPO_DIRS}" modules/"${D}
done
echoHeader () {
echo
echo -e "${YELLOW}################################################################################${NC}"
echo -e "${GREEN}${@}${NC}"
echo -e "${YELLOW}################################################################################${NC}"
}
confirm () {
# call with a prompt string or use a default
read -r -p "${1:-Are you sure? [y/N]} " response
case $response in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
requoteArgs () {
local res=""
for x in "${@}" ; do
# try to figure out if quoting was required for the $x:
grep -q "[[:space:]]" <<< "$x" && res="${res} '${x}'" || res="${res} ${x}"
done
# remove first space and print:
sed -e 's/^ //' <<< "${res}"
}
chooseTargets () {
TARGETS=""
if [ -z "$1" ]; then
TARGETS=$REPO_DIRS;
elif [ "$1" = "." ]; then
TARGETS=$(realpath --relative-to="${WFM_DIR}" "${ORIG_DIR}")
elif [ "$1" = "apps" ]; then
TARGETS=$APPS;
elif [ "$1" = "modules" ]; then
for D in $MODULES; do
TARGETS=${TARGETS}" modules/"${D}
done
elif [ "$1" = "ui_modules" ]; then
for D in $UI_MODULES; do
TARGETS=${TARGETS}" modules/"${D}
done
elif [[ $MODULES =~ $1 ]]; then
TARGETS="modules/"${1}
elif [[ $APPS =~ $1 ]]; then
TARGETS=$1
fi
echo $TARGETS
}
pull () {
shift
local TARGETS=$(chooseTargets "$@")
for D in $TARGETS; do
cd "$WFM_DIR/$D"
echoHeader "Pulling: $D"
git pull
done
}
link () {
shift
local TARGETS=$(chooseTargets "$@")
linkFromModules
for D in $TARGETS; do
cd "$WFM_DIR/$D"
echoHeader "Linking: $D"
linkToModules
done
}
unlink () {
shift
local TARGETS=$(chooseTargets "$@")
for D in $TARGETS; do
cd "$WFM_DIR/$D"
echoHeader "Linking: $D"
unlinkModules
done
}
linkToModules () {
MODULES_TO_LINK=$(cat package.json | perl -ne 'print unless 1.../Dependencies/i' | grep fh-wfm | cut -d':' -f1 | tr -d ' "' )
for M in $MODULES_TO_LINK; do
npm link $M
done
}
unlinkModules () {
MODULES_TO_LINK=$(cat package.json | perl -ne 'print unless 1.../Dependencies/i' | grep fh-wfm | cut -d':' -f1 | tr -d ' "' )
for M in $MODULES_TO_LINK; do
npm unlink $M
done
}
linkFromModules () {
local TARGETS=$(chooseTargets modules)
for D in $TARGETS; do
cd "$WFM_DIR/$D"
npm link
done
}
clean () {
shift
local TARGETS=$(chooseTargets "$@")
for D in $TARGETS; do
cd "$WFM_DIR/$D"
echoHeader "Cleaning: $D"
rm -rf node_modules
done
}
buildTemplates () {
shift
local TARGETS=$(chooseTargets "ui_modules")
for D in $TARGETS; do
cd "$WFM_DIR/$D"
echoHeader "Building: $D"
npm run build
done
}
install () {
shift
local TARGETS=$(chooseTargets "$@")
for D in $TARGETS; do
cd "$WFM_DIR/$D"
echoHeader "Installing: $D"
npm install
done
buildTemplates
}
clone () {
cd "$WFM_DIR"
if [ ! -d "modules" ]; then
mkdir "modules"
fi
shift
local TARGETS=$(chooseTargets "$@")
for D in $TARGETS; do
cd "${WFM_DIR}"
if [ ! -d "$D" ]; then
echoHeader "Cloning: $D"
REPO=""
if [[ $D == "modules/"* ]]; then
cd "${WFM_DIR}/modules"
REPO=${D#modules/}
else
cd "${WFM_DIR}"
REPO=$D
fi
git clone "$BASE_REPO/${REPO}.git"
else
echo -e "${GREEN}${D} already exists${NC}"
fi
done
}
status() {
shift
local TARGETS=$(chooseTargets "$@")
for D in $TARGETS; do
cd "$WFM_DIR/$D"
STATUS="$(git status -s)"
AHEAD_BEHIND="$(git status -s -b | grep -E 'ahead|behind')"
if [ -n "$STATUS" ] || [ -n "$AHEAD_BEHIND" ]; then
STATUS="$(git -c color.status=always status -s -b)"
echoHeader "Status: $D"
echo "$STATUS"
fi
done
}
diff () {
shift
local TARGETS=$(chooseTargets "$@")
for D in $TARGETS; do
diffRepo $D
done
}
diffRepo() {
D=$1
shift
cd "$WFM_DIR/$D"
DIFF="$(git diff --color $@)"
if [ -n "$DIFF" ]; then
echoHeader "Diff: $D\n$ git diff --color $@"
echo "$DIFF"
fi
}
mongo () {
if [ ! -d $MONGO_DIR ]; then
mkdir $MONGO_DIR
fi
if [ ! -d "$MONGO_DATA" ]; then
mkdir $MONGO_DATA
fi
PID=`ps x | grep -v grep | grep "mongod" | grep $MONGO_DATA |awk '{ print $1 }'`
case $1 in
start)
if [ ! -z $PID ]; then
echo "Mongo is already running. pid: $PID"
else
mongod --dbpath $MONGO_DATA --logpath ${MONGO_LOG} --fork --logappend
fi
;;
stop)
if [ -z $PID ]; then
echo "Mongo is already stopped."
else
echo "Mongo stopping. pid: $PID"
kill $PID
fi
;;
status)
if [ ! -z $PID ]; then
echo "Mongo is running. pid: $PID"
else
echo "Mongo is not running."
fi
;;
*)
echo "Usage: wfm mongo {start|stop|status}"
esac
#
}
redis () {
PID=`ps x | grep -v grep | grep "redis-server" | awk '{ print $1 }'`
case $1 in
start)
if [ ! -z $PID ]; then
echo "Redis is already running. pid: $PID"
else
redis-server &
fi
;;
stop)
if [ -z $PID ]; then
echo "Redis is already stopped."
else
echo "Redis stopping. pid: $PID"
kill $PID
fi
;;
status)
if [ ! -z $PID ]; then
echo "Redis is running. pid: $PID"
else
echo "Redis is not running."
fi
;;
*)
echo "Usage: wfm redis {start|stop|status}"
esac
#
}
start () {
cd $WFM_CLI_DIR
WFM_PATH=$WFM_DIR tmuxinator wfm
}
ackWfm () {
cd $WFM_DIR
shift
ack-grep "$@" $REPO_DIRS
}
commit () {
ARGS=$(requoteArgs "${@}")
for D in $REPO_DIRS; do
commitRepo $D
done
}
commitRepo() {
D=$1
cd "$WFM_DIR/$D"
STATUS="$(git status -s)"
if [ -n "$STATUS" ]; then
echoHeader "Commit: $D\n$ git $ARGS"
git status
confirm && bash -c "git ${ARGS}"
fi
}
gitWfm () {
cd $WFM_DIR
shift
for D in $REPO_DIRS; do
echoHeader "Git: $D\n$ git $@"
cd "$WFM_DIR/$D"
git "$@"
done
}
execWfm () {
shift
ARGS=$(requoteArgs "${@}")
cd $WFM_DIR
for D in $REPO_DIRS; do
echoHeader "Exec: $D\n$ ${ARGS}"
cd "$WFM_DIR/$D"
eval "$ARGS"
done
}
atomWfm() {
cd $WFM_DIR
atom raincatcher-* modules/*
}
allHeadersWfm() {
for D in $REPO_DIRS; do
cd "$WFM_DIR/$D"
echoHeader "Headers: $D"
headerWfm
done
}
headerWfm() {
JS_FILES=$(ack . -l --js --ignore-file=is:feedhenry.js \
--ignore-file=is:bundle.js \
)
for FILE in $JS_FILES; do
if ! grep -q Copyright "${FILE}"; then
cat "${WFM_CLI_DIR}/headers/copyright.js" "${FILE}" >"${FILE}.new" && mv "${FILE}.new" "$FILE"
fi
done
CSS_FILES=$(ack . -l --sass )
for FILE in $CSS_FILES; do
if ! grep -q Copyright "${FILE}"; then
cat "${WFM_CLI_DIR}/headers/copyright.css" "${FILE}" >"${FILE}.new" && mv "${FILE}.new" "$FILE"
fi
done
HTML_FILES=$(ack . -l --html )
for FILE in $HTML_FILES; do
if ! grep -q Copyright "${FILE}"; then
cat "${WFM_CLI_DIR}/headers/copyright.html" "${FILE}" >"${FILE}.new" && mv "${FILE}.new" "$FILE"
fi
done
}
license() {
for D in $REPO_DIRS; do
cp "${WFM_CLI_DIR}/LICENSE.txt" "${D}"
done
}
release() {
GROUP_1="raincatcher-template-build raincatcher-mediator";
GROUP_2="raincatcher-signature raincatcher-sync"
GROUP_3="";
for D in $MODULES; do
if [[ ! ${GROUP_1} =~ $D && ! ${GROUP_2} =~ $D ]]; then
GROUP_3="${GROUP_3}$D "
fi
done
WFM_GROUPS="$GROUP_1 $GROUP_2 $GROUP_3"
for D in $WFM_GROUPS; do
updateModule "modules/${D}"
versionModule "modules/${D}"
done
}
version() {
shift
local TARGET=$1
if [ -z "$TARGET" ]; then
TARGET="modules"
fi;
local TARGETS=$(chooseTargets ${TARGET})
for D in $TARGETS; do
cd "$WFM_DIR/$D"
versionModule "${D}"
done
}
versionModule() {
local D=$1;
cd "$WFM_DIR/$D"
LATEST_VERSION=$(git describe --abbrev=0 --tags)
COMMIT_COUNT=$(git log --oneline ${LATEST_VERSION}..master -q | wc -l)
echoHeader "wfm version ${2}: ${D}\nCommit count: ${COMMIT_COUNT}"
if [ $COMMIT_COUNT -gt 0 ]; then
npm version patch
confirm "Publish ${D}? [y/N] " && publishModule "${D}"
else
echo "${LATEST_VERSION} release of ${D} is current"
fi
}
update() {
shift
local TARGET=$1
if [ -z "$TARGET" ]; then
TARGET="modules"
fi;
local TARGETS=$(chooseTargets ${TARGET})
for D in $TARGETS; do
cd "$WFM_DIR/$D"
updateModule "${D}"
done
}
updateModule() {
local D=$1;
cd "$WFM_DIR/$D"
TTY=$(/usr/bin/tty)
local RESULT=$(ncu --jsonUpgraded -f /^fh-wfm/ < $TTY)
if [ $RESULT != "{}" ]; then
echoHeader "wfm update: ${D}"
ncu -f /^fh-wfm/ < $TTY
confirm "Apply updates? [y/N] " && ncu -f /^fh-wfm/ -u < $TTY && \
diffRepo $D | more && \
cd "$WFM_DIR/$D" && \
confirm "Commit result? [y/N] " && git add package.json && git commit -m 'Updated fh-wfm* dependencies'
fi
}
publish() {
shift
local TARGET=$1
if [ -z "$TARGET" ]; then
TARGET="modules"
fi;
local TARGETS=$(chooseTargets ${TARGET})
for D in $TARGETS; do
publishModule $D
done
}
publishModule() {
local D=$1;
cd "$WFM_DIR/$D"
echoHeader "npm publish: ${D}"
npm publish
}
case $1 in
pull)
pull "$@"
;;
link)
link "$@"
;;
unlink)
unlink "$@"
;;
clean)
clean "$@"
;;
install)
install "$@"
;;
clone)
clone "$@"
;;
status)
status "$@" | less -r
;;
diff)
diff "$@" | less -r
;;
path)
echo "Script PATH is:"
echo $WFM_CLI_DIR
echo $REPO_DIRS
;;
mongo)
mongo $2
;;
redis)
redis $2
;;
start)
start
;;
grep)
ackWfm "$@"
;;
ack)
ackWfm "$@"
;;
commit)
commit "$@"
;;
git)
gitWfm "$@"
;;
atom)
atomWfm
;;
exec)
execWfm "$@"
;;
header)
allHeadersWfm "$@"
;;
license)
license
;;
version)
version "$@"
;;
update)
update "$@"
;;
publish)
publish "$@"
;;
release)
release "$@"
;;
pwd)
cd $WFM_DIR
pwd
;;
shortlist)
echo pull link clean install clone status diff path mongo redis start grep ack commit git atom exec header license pwd
;;
*)
echo "Usage: wfm {clone|status|diff|pull|clean|install|link|path|start|mongo|redis|ack}"
esac