forked from developer-portal/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
executable file
·237 lines (171 loc) · 4.35 KB
/
common.sh
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
#!/bin/bash
#
# . common.sh
# Intended for sourcing. Contains common presets and functions.
# Specific options order is required!
#
# Common options:
# -h this help
# -d debug mode
# -i interactive run
#
#
bash -n "$0" || exit 7
set -e
ME="$(readlink -e "$0")"
bash -n "$ME"
F="Failed to"
P="`pwd`"
LOGF="$(readlink -m "$P/log/`date -I'seconds'`_`basename -s '.sh' "$0"`.log")"
RSYN='rsync --del -a'
DST='[email protected]:'
LOGGPR="--> "
SITE='developer.fedoraproject.org'
getd () {
readlink -f "$P/$1"
}
usage () {
awk '{if(NR>1)print;if(NF==0)exit(0)}' < "$0" | sed '
s|^#[ ]||
s|^#$||
' | ${PAGER-more}
exit 0
}
upgit () {
local D="`getd "$1"`"
[[ -d "$D" ]] || {
scd "`dirname "$D"`"
vrun "git clone https://github.com/developer-portal/`basename "$1"`.git"
}
scd "$1"
vrun 'git stash'
vrun 'git checkout master'
[[ "`git branch | grep '^*' | cut -d' ' -f2`" == 'master' ]] || die "Invalid branch"
vrun 'git pull'
}
scd () {
local D="`getd "$1"`"
local S=
[[ "$2" == "-s" ]] && S=y
[[ "$D" ]] || {
[[ "$S" ]] && return 1
die "Dir nonexistent '$P/$1'"
}
cd "$D" || die "$F cd '$D'"
[[ "`pwd`" == "$D" ]] || die "Invalid cd '`pwd`' != '$D'"
[[ "$S" ]] || logg "cd '$D'"
return 0
}
die () {
logg "Error: ${1}!" >&2
exit 1
}
logg () {
local M="$@"
echo -e "$LOGGPR$M" | tr "\n" "\t"
echo
return 0
}
vrun () {
[[ "$1" == '-a' ]] && {
shift
[[ "$INT" ]] && ask "$1"
}
[[ "$1" == '-s' ]] && {
shift
logg "[NOT RUN] $1"
return 0
}
logg "$1"
[[ "$DEB" ]] && set -x
bash -c "$1 2>&1" || die "while running '$1'"
[[ "$DEB" ]] && { { set +x ; } &>/dev/null ; }
return 0
}
gitnchange () {
local X="`git remote -v update 2>&1 | tr -s '\t' ' ' | grep ' master -> origin/master$' | grep -v ' = \[up to date\]'`"
[[ "$X" ]] && {
logg "Git Updated: `pwd`"
logg "`date -I'seconds'`"
logg "$X"
return 1
}
return 0
}
buildsite () {
local Y="website/_site"
local X="`getd "$Y"`"
scd 'website'
[[ -d "$X" || -r "$X" ]] && {
logg "Removing '$X'"
rm -rf "$X"
}
logg 'Building site ...'
local f=
local O
O="$(vrun 'jekyll build')" || f=y
grep -v "Build Warning: Layout 'content' requested in" <<< "$O"
[[ -n "$f" ]] && die "Jekyll build failed!"
cleanup
}
cleanup () {
scd "website/_site"
logg 'Whitespace cleanup ...'
[[ "$DEB" ]] && set -x
find . -type f -iname '*.html' | while read z; do
TMP="`ruby -ne 'BEGIN{ \$pre = false } ; x = \$_ ; \$pre = ( (x =~ /<pre/i) || (\$pre && !(x =~ /<\/pre/i) ) ) ; print unless (!\$pre && x =~ /^\s*$/)' < "$z"`"
echo "$TMP" > "$z"
TMP=
done
logg 'Fixup urls ...'
grep -r 'http://0\.0\.0\.0:4000/' -l | xargs -n1 sed -i 's|http://0\.0\.0\.0\:4000|https://developer.fedoraproject.org|g'
[[ "$DEB" ]] && set +x
}
addmsg () {
MSG="`echo -e "$MSG\n$1"`"
}
ask () {
local R=
for u in {1..3}; do
R=
read -p "${LOGGPR}Run '$1'? (y/N) " R
[[ "${R^^}" == "Y" ]] && break
[[ "${R^^}" == "N" || -z "${R^^}" ]] && break
done
[[ "${R^^}" == "Y" ]] || die "User Quit"
}
prepgit () {
[[ "$1" ]] || die "Invalid prepgit"
scd "website"
addmsg "website commit: `git rev-parse --verify HEAD`"
scd "website/content"
addmsg "content commit: `git rev-parse --verify HEAD`"
upgit "$SITE"
scd "$SITE"
vrun "git checkout $1"
[[ "`git branch | grep '^*' | cut -d' ' -f2`" == "$1" ]] || die "Invalid branch"
vrun -a "rm -fr *"
vrun "cp -r `getd "website/_site"`/* ."
vrun "cp -r `getd "website"`/rss.py ."
vrun "gita -A"
local C="git commit -am"
echo "${LOGGPR}$C $MSG"
$C "$MSG" || die "Failed to create commit"
local L="`git show | cat -n | grep 'diff --git a/js/index.json b/js/index.json' | tr -s '\t' ' ' | cut -d' ' -f2`"
grep -qE '^[0-9]+$' <<< "$L" && {
let 'L += 5' 'K = L + 2'
logg "git show"
gith | sed -e "$L,${K}d" | colordiff
}
vrun 'git status'
}
[[ "$1" == "-h" || "$1" == "--help" ]] && usage
[[ "$1" == "-d" ]] && { DEB="$1" ; shift ; } || DEB=
[[ "$1" == "-i" ]] && { INT="$1" ; shift ; } || INT=
[[ "$LOGF" ]] || die "No LOGF"
mkdir -p "`dirname "$LOGF"`" || die "$F mkdir -p '`dirname "$LOGF"`'"
touch "$LOGF"
[[ -w "$LOGF" ]] || die "$F write '$LOGF'"
exec &> >(tee -a "$LOGF")
. $(dirname "`readlink -e "$0"`")/enable_scl.sh
set +e