-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.sh
More file actions
209 lines (192 loc) · 5.5 KB
/
functions.sh
File metadata and controls
209 lines (192 loc) · 5.5 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
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
cleanup() {
# echo "Cleaning up... "
# cat $TMPD/*
rm -rf $TMPD
}
TMPD=""
mkTempDir() {
[[ -z "$TMPD" ]] && {
# echo " --- creating temp dir"
TMPD=$(mktemp -d /tmp/.tmp-HeliosLang.XXXXXX)
trap "cleanup" EXIT
}
}
IS_TTY=""
[[ -t 0 ]] && IS_TTY=1
optionalPager() {
[[ $IS_TTY ]] && {
LESS="${LESS:-} -F --raw-control-chars --no-init" ${PAGER:-less}
}
[[ $IS_TTY ]] || {
echo "stdout is not a terminal; not using pager" >&2
cat
}
}
labeledOutput() {
LABEL=$1
EXTRA=${2:-}
while read output ; do {
printf "%-18s |${EXTRA} %b\n" "$LABEL" "$output"
} done
}
labeledErrors() {
LABEL=$1
while read output ; do {
printf "${YELLOW}%-15sERR | %b ${NC}\n" "$LABEL" "$output"
} done
}
eachRepoUsage() {
{
err=$1
[[ -z "$err" ]] || {
echo "Error: eachRepo(): $err"
echo
}
echo " Usage: eachRepo [parallel [buffered]] [nocd] \"‹activity description›\" \"callbackFuncName\" [...repos]"
echo
echo " Your named callback function will be called for each repo"
echo " ... with \`pwd\` set to the repo dir"
echo " ... and shell variables \$REPO, \$DIR, \$LABEL available"
echo
echo " The function returns when all repos are done processing through your callback. Other notes:"
echo " - If 'parallel' is specified as the first arg, repo tasks are run in parallel. "
echo " - With 'parallel buffered', each result is collected, & emitted only as it finishes."
echo " - If 'nocd' is specified, the callback function is run without changing directories."
echo " - With a list of repos, only those repos are processed. Otherwise, all Helios repos are processed."
echo
echo aborted
} >&2
exit 42
}
REPOS=""
fetchRepoList() {
[[ -z "$REPOS" ]] && {
[[ -f ./.heliosRepos ]] || {
echo "created ./.heliosRepos"
cp essentialRepos .heliosRepos
}
[[ -f ./.heliosRepos ]] && {
REPOS=$(cat ./.heliosRepos)
return
}
echo -n " -- fetching Helios repo list ... "
OK=""
REPOS=$(
curl --silent https://github.com/orgs/HeliosLang/repositories.json |
jq -r '.payload.repositories[].name' |
# grep -ev "^cli$" |
sort
)
if [[ $? -ne 0 ]] ; then {
echo "failed!"
echo
echo "Error: failed fetching Helios repo list ... are you online?"
} else {
OK=1
echo "$REPOS" > ./.heliosRepos
echo "created ./.heliosRepos"
echo ok
} ; fi
[[ -z "$OK" ]] && exit 42
} >&2
}
eachRepo() {
parallel=""
buffered=""
nocd=""
[[ "$1" == "parallel" ]] && {
parallel="$1"
shift
[[ "$1" == "buffered" ]] && {
buffered="$1"
mkTempDir
shift
}
}
[[ "$1" == "nocd" ]] && {
nocd="$1"
shift
}
[[ -z "$1" ]] && eachRepoUsage "missing activity description ($*)"
[[ "$1" == "buffered" ]] && {
eachRepoUsage "'buffered' option invalid without 'parallel' specified first"
}
activity=$1
# [[ $# -gt 2 ]] && eachRepoUsage "extra args ($*)"
[[ -z "$2" ]] && eachRepoUsage "missing callbackFunctionName ($*)"
shift
func=$1
[[ -z $(type -t "$func") ]] && eachRepoUsage "callback function not found: '$func'"
shift
if [[ $# -gt 0 ]] ; then {
ITERATE_REPOS=$*
} else {
fetchRepoList
ITERATE_REPOS=$REPOS
} ; fi
echo " -- $activity ..." >&2
echo >&2
echo " -- repos: " $ITERATE_REPOS >&2
# set -x
for REPO in $ITERATE_REPOS ; do {
DIR=$REPO
LABEL=$REPO
if [[ "workspace" == "${REPO}" ]] ; then
DIR="."
LABEL="workspace:./"
fi
[[ ! -d $DIR && ! $nocd ]] && {
echo "skipping missing directory: $DIR" >&2
continue
}
{
TEMP=""
[[ -z "$buffered" ]] || {
TEMP=$(mktemp $TMPD/${REPO}.XXXXXX)
# echo mkTemp in $TMPD: $TEMP
}
[[ $parallel ]] && {
[[ $buffered ]] && {
# background, buffered
{
[[ -z $nocd ]] && pushd $DIR >/dev/null
$func > $TEMP 2> >(labeledErrors $LABEL)
cat $TEMP
[[ -z $nocd ]] && popd > /dev/null
} &
}
[[ $buffered ]] || {
# background, unbuffered
{
[[ -z $nocd ]] && pushd $DIR >/dev/null
$func 2> >(labeledErrors $LABEL)
[[ -z $nocd ]] && popd > /dev/null
} &
}
}
[[ -z $parallel ]] && {
# foreground; buffering is senseless
[[ -z $nocd ]] && pushd $DIR >/dev/null
[[ $buffered ]] && {
$func 2> >(labeledErrors $LABEL)
}
[[ $buffered ]] || {
$func
}
[[ -z $nocd ]] && popd > /dev/null
}
# [[ "compiler" == "${REPO}" ]] && {
# break
# }
}
} done
[[ -z parallel ]] || {
wait
}
echo >&2
}