This repository has been archived by the owner on Jun 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommonScript.sh
185 lines (176 loc) · 7.11 KB
/
commonScript.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
#!/usr/bin/env bash
if [ -z "$BOT_CDNJS_CHECK_DIR" ]; then
echo "BOT_CDNJS_CHECK_DIR missing"
exit 1
fi
if [ -z "$BOT_BASE_PATH" ]; then
echo "BOT_BASE_PATH missing"
exit 1
fi
function print-log() {
echo "$@"
}
function run() {
print-log "$(date), pwd: $(pwd), $*"
nice -n 20 "$@"
}
function reset() {
if git diff --quiet; then
print-log "no diff found, so do not reset!"
else
print-log "diff found, so reset!"
run git reset --hard
fi
}
function fix() {
find "${1}" -type d -exec chmod 755 {} \; || sudo chown "${USER}:${USER}" -R "${1}"
find "${1}" -type f -exec chmod 644 {} \;
}
function check() {
oldVer="$(git diff --color=never "${1}/package.json" | command grep '^\- "version"' | awk -F'"' '{print $4}')"
newVer="$(git diff --color=never "${1}/package.json" | command grep '^\+ "version"' | awk -F'"' '{print $4}')"
diffCount="$(git diff --color=never "${1}/package.json" | command grep -v '^\-\-\-' | command grep -c '^\- ')"
if [ "${diffCount}" -gt 1 ]; then
echo "Diff count > 1, should manually check it."
git diff "${1}/package.json"
elif [ -z "${oldVer}" ] || [ -z "${newVer}" ]; then
echo "Version diff not found"
else
rm -rf "$BOT_CDNJS_CHECK_DIR*"
tempD="$BOT_CDNJS_CHECK_DIR$(date --iso-8601)"
mkdir -p "${tempD}"
(
cd "${1}/${oldVer}"
find . | sort | command grep -v '\.map$\|\.ts$\|\.md$' > "${tempD}/${1}_${oldVer}_fileList"
) &
(
cd "${1}/${newVer}"
find . | sort | command grep -v '\.map$\|\.ts$\|\.md$' > "${tempD}/${1}_${newVer}_fileList"
) &
wait
diff "${tempD}/${1}_${oldVer}_fileList" "${tempD}/${1}_${newVer}_fileList"
ls "${1}/"
echo -n "Wanna add ${1}? (old: ${oldVer}, new: ${newVer}) "
read -r add
if [ "${add}" = "y" ] || [ "${add}" = "Y" ]; then
fix "${1}/${newVer}"
cdnjs-add "${1}" "${newVer}"
elif [ "${add}" = "b" ] || [ "${add}" = "beta" ]; then
git checkout "${1}/package.json"
fix "${1}/${newVer}"
cdnjs-add "${1}" "${newVer}"
elif [ "${add}" = "m" ] || [ "${add}" = "minify" ]; then
fix "${1}/${newVer}"
minify "${1}/${newVer}"
cdnjs-add "${1}" "${newVer}"
fi
fi
}
function autoadd() {
print-log "start to auto add process"
cd "$BOT_BASE_PATH/cdnjs/ajax/libs/"
for lib in $(git status ./ -uno | command grep -v '\.\.' | command grep package.json | awk '{print $2}' | awk -F'/' '{print $1}'); do
print-log "Found ${lib}"
oldVer="$(git diff "${lib}/package.json" | command grep '^\- "version"' | awk -F'"' '{print $4}')"
newVer="$(git diff "${lib}/package.json" | command grep '^\+ "version"' | awk -F'"' '{print $4}')"
if ! git status "${lib}/${newVer}" | grep -q "${lib}/${newVer}/"; then
print-log "${lib} ${newVer} invalid!"
continue
fi
diffCount="$(git diff "${lib}/package.json" | command grep -v '^\-\-\-' | command grep -c '^\- ')"
if [ "${diffCount}" -eq 1 ] && [ ! -z "${oldVer}" ] && [ ! -z "${newVer}" ]; then
print-log "found ver ${oldVer} -> ${newVer}"
tempD="$BOT_CDNJS_CHECK_DIR$(date --iso-8601)"
mkdir -p "${tempD}"
(
cd "$BOT_BASE_PATH/cdnjs/ajax/libs/${lib}/${oldVer}"
find . | sort | command grep -v '\.map$\|\.ts$\|\.md$' > "${tempD}/${lib}_${oldVer}_fileList"
) &
(
cd "$BOT_BASE_PATH/cdnjs/ajax/libs/${lib}/${newVer}"
find . -type f -exec chmod -x {} \;
find . | sort | command grep -v '\.map$\|\.ts$\|\.md$' > "${tempD}/${lib}_${newVer}_fileList"
) &
wait
if [ ! -e "${lib}/.donotoptimizepng" ] && test "$(find "${lib}/${newVer}" -name "*.png")"; then
find "${lib}/${newVer}" -name "*.png" | xargs -n 1 -P $(($(nproc) - 1)) zopflipng-f
fi
if test "$(find "${lib}/${newVer}" -name "*.jpeg" -o -name "*.jpg")"; then
find "${lib}/${newVer}" -name "*.jpeg" -o -name "*.jpg" | xargs -n 1 -P $(($(nproc) - 1)) jpegoptim
fi
oldmd5="$(md5sum "${tempD}/${lib}_${oldVer}_fileList" | cut -d ' ' -f 1)"
newmd5="$(md5sum "${tempD}/${lib}_${newVer}_fileList" | cut -d ' ' -f 1)"
if [ "${oldmd5}" = "${newmd5}" ]; then
cdnjs-add "${lib}" "${newVer}"
else
rsync -a --delete "${lib}/${newVer}/" "${tempD}/${lib}_${newVer}_x/"
(
cd "${tempD}/${lib}_${newVer}_x/"
$BOT_BASE_PATH/web-minify-helper/minify.sh
find . | sort | command grep -v '\.map$\|\.ts$\|\.md$' > "${tempD}/${lib}_${newVer}_x_fileList"
)
rsync -av "${tempD}/${lib}_${newVer}_x/" "${lib}/${newVer}/"
cdnjs-add "${lib}" "${newVer}"
fi
fi
done
reset
for lib in $(git status ./ | command grep '\/$' | awk -F '/' '{ print $1 }' | uniq | command grep -v 'aframe'); do
print-log "Found ${lib}"
test "$(grep '"version": ' "${lib}/package.json")" || return 1
oldVer="$(grep '"version": ' "${lib}/package.json" | awk -F'"' '{print $4}')"
for newVer in $(git status "${lib}" | command grep '\/$' | awk -F '/' '{ print $2 }' | tac); do
print-log "found ver ${newVer} (origin ${oldVer})"
tempD="$BOT_CDNJS_CHECK_DIR$(date --iso-8601)"
mkdir -p "${tempD}"
(
cd "$BOT_BASE_PATH/cdnjs/ajax/libs/${lib}/${oldVer}"
find . | sort | command grep -v '\.map$\|\.ts$\|\.md$' > "${tempD}/${lib}_${oldVer}_fileList"
) &
(
cd "$BOT_BASE_PATH/cdnjs/ajax/libs/${lib}/${newVer}"
find . -type f -exec chmod -x {} \;
find . | sort | command grep -v '\.map$\|\.ts$\|\.md$' > "${tempD}/${lib}_${newVer}_fileList"
) &
wait
if [ ! -e "${lib}/.donotoptimizepng" ] && test "$(find "${lib}/${newVer}" -name "*.png")"; then
find "${lib}/${newVer}" -name "*.png" | xargs -n 1 -P $(($(nproc) - 1)) zopflipng-f
fi
if test "$(find "${lib}/${newVer}" -name "*.jpeg" -o -name "*.jpg")"; then
find "${lib}/${newVer}" -name "*.jpeg" -o -name "*.jpg" | xargs -n 1 -P $(($(nproc) - 1)) jpegoptim
fi
oldmd5="$(md5sum "${tempD}/${lib}_${oldVer}_fileList" | cut -d ' ' -f 1)"
newmd5="$(md5sum "${tempD}/${lib}_${newVer}_fileList" | cut -d ' ' -f 1)"
if [ "${oldmd5}" = "${newmd5}" ]; then
cdnjs-add-no-package-json "${lib}" "${newVer}"
else
rsync -a --delete "${lib}/${newVer}/" "${tempD}/${lib}_${newVer}_x/"
(
cd "${tempD}/${lib}_${newVer}_x/"
$BOT_BASE_PATH/web-minify-helper/minify.sh
find . | sort | command grep -v '\.map$\|\.ts$\|\.md$' > "${tempD}/${lib}_${newVer}_x_fileList"
)
rsync -av "${tempD}/${lib}_${newVer}_x/" "${lib}/${newVer}/"
cdnjs-add-no-package-json "${lib}" "${newVer}"
fi
done
done
}
function cdnjs-add() {
if [ "${2}" = "old" ]; then
run git add -- "${1}"
run git reset -- "${1}/package.json"
run git commit -m "Add ${1} old versions"
else
local currentVer
currentVer="$(awk '{ if ("\"version\":" == $1) print $2}' "${1}/package.json" | awk -F '"' '{print $2}')"
if [ "${currentVer}" = "${2}" ]; then
run git add -- "${1}/package.json"
fi
cdnjs-add-no-package-json "${1}" "${2}"
fi
}
function cdnjs-add-no-package-json() {
run git add -- "${1}/${2}"
run git commit -m "Add ${1} v${2}"
}