This repository has been archived by the owner on May 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
package.sh
executable file
·263 lines (223 loc) · 8.11 KB
/
package.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
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
#!/usr/bin/env bash
# ----------------------------------------------------------------------------------------------------------------------
# https://github.com/eth-p/unraid | Copyright (C) 2018 Ethan P. (eth-p)
# A collection of eth-p's Unraid plugins.
#
# File: package.sh
# A script to build/package Unraid plugins.
#
# Usage:
# ./package.sh -- Builds all modified plugins.
# ./package.sh [plugin] -- Builds a specific plugin.
# ----------------------------------------------------------------------------------------------------------------------
# Polyfill: md5sum
[ -z "$(type -t md5sum)" ] && command -v md5 &>/dev/null && md5sum() {
local line
while read -r line; do
local file="$(sed -e 's/^MD5 (\([^)]*\).*$/\1/' <<< "$line")"
local hash="$(tr ' ' '\n' <<< "$line" | tail -n1)"
printf "%s %s" "$hash" "$file"
done < <(command md5 "$@")
}
# ----------------------------------------------------------------------------------------------------------------------
# Polyfill: realpath
[ -z "$(type -t realpath)" ] && realpath() {
python -c "import os,sys; print os.path.realpath(sys.argv[1])" "$1"
}
# ----------------------------------------------------------------------------------------------------------------------
# Function: log
# Logs a message.
# $1: ["inf"|"wrn"|"err"] The message level.
# $2: [String] The format string.
# ... [String] The format arguments.
if grep -e "color" -e "256" <<< "$TERM" &>/dev/null; then
log() {
local level="$1"
local format="${LOG_PREFIX}$2"
local args=("${@:3}")
local color=""
case "$level" in
"inf") color='';;
"wrn") color='\x1B[33m';;
"err") color='\x1B[31m';;
"ist") color='\x1B[35m';;
"bld") color='\x1B[36m';;
esac
printf "\x1B[2m%s\x1B[0m ${color}[%s]\x1B[0m ${format}\n" "$(date +"%H:%M:%S")" "$level" "${args[@]}"
}
else
log() {
local level="$1"
local format="${LOG_PREFIX}$2"
local args=("${@:2}")
printf "%s %s: ${format}\n" "$(date +"%H:%M:%S")" "${args[@]}"
}
fi
# ----------------------------------------------------------------------------------------------------------------------
# Function: run
# Runs a command.
# $1: [String] The command.
# ... [String] The command arguments.
if grep -e "color" -e "256" <<< "$TERM" &>/dev/null; then
run() {
local command="$1"
local args=("${@:2}")
if $VERBOSE; then
printf "\x1B[34m\$ %s\x1B[0m" "$command"
printf " %q" "${args[@]}"
printf "\n"
fi
"$command" "${args[@]}" 2> >(run_display_err) 1> >(run_display_out)
}
run_display_err() {
while read -r line; do
printf "\x1B[31m|\x1B[0m %s\n" "$line"
done
}
run_display_out() {
if $VERBOSE_EXTRA; then
while read -r line; do
printf "\x1B[33m|\x1B[0m %s\n" "$line"
done
else
cat - >/dev/null
fi
}
else
run() {
local command="$1"
local args=("${@:2}")
if $VERBOSE; then
printf "$ %s" "$command"
printf " %q" "${args[@]}"
printf "\n"
fi
"$command" "${args[@]}" 2> >(run_display_err) 1> >(run_display_out)
}
run_display_err() {
while read -r line; do
printf "! %s\n" "$line"
done
}
run_display_out() {
if $VERBOSE_EXTRA; then
while read -r line; do
$VERBOSE && printf "| %s\n" "$line"
done
fi
}
fi
# ----------------------------------------------------------------------------------------------------------------------
# Function: create_plugin_package
# Creates a plugin package.
# This uses the PLUGIN* and BUILD* variables for arguments.
create_plugin_package() {
({
log inf "Creating package..."
local md5=''
local out_package="${BUILD_ROOT}/out/${PLUGIN_NAME}.txz"
local out_plgfile="${BUILD_ROOT}/out/${PLUGIN_NAME}.plg"
# Create xz tarball.
(cd "${BUILD_ROOT}/in" && run tar --exclude='.*' -vcJf "$out_package" *) 2> >(run_display_out)
# Update plugin MD5.
md5="$(md5sum "$out_package" | cut -d' ' -f1)"
sed -e 's/\(MD5 \{1,\}\)"[^"]\{1,\}"/\1'"\"$md5\"/" < "$PLUGIN_FILE" > "$out_plgfile"
log inf "Created package with hash: %s" "$md5"
})
}
# ----------------------------------------------------------------------------------------------------------------------
# Function: release_plugin_package
# Releases a plugin package.
# This uses the PLUGIN* and BUILD* variables for arguments.
release_plugin_package() {
({
local out_package="${BUILD_ROOT}/out/${PLUGIN_NAME}.txz"
local out_plgfile="${BUILD_ROOT}/out/${PLUGIN_NAME}.plg"
run cp -v -- "$out_package" "$RELEASES/$(basename "$out_package")"
run cp -v -- "$out_plgfile" "$RELEASES/$(basename "$out_plgfile")"
log inf "Prepared %s (%s)" "${PLUGIN_NAME}" "${PLUGIN_VERSION}"
log ist "You need to do a git push to release the plugin."
})
}
# ----------------------------------------------------------------------------------------------------------------------
# Function: patch_list_files
# Lists modified files in one or more patches.
# $1: [String] The patch file.
# ... [String] ...
patch_list_files() {
cat "$@" | grep "+++\|---" | sed 's/[+\-]\{3\} [a|b]\/\(.*\)$/\1/' | uniq
}
# ----------------------------------------------------------------------------------------------------------------------
# Function: __plugin_build
# Runs a plugin build script.
# $1: [String] The path to the plugin file.
__plugin_build() {
GLOBIGNORE=".:..:.DS_Store:._*";
PLUGIN_FILE="$(realpath "$1")"
PLUGIN_HOME="$(dirname "$PLUGIN_FILE")"
PLUGIN_NAME="$(grep -e 'name \{1,\}"[0-9a-zA-Z\.\-]\{1,\}"' < "$PLUGIN_FILE" | grep -o '[^"]*">$' | sed -e 's/..$//')"
PLUGIN_VERSION="$(grep -e 'version \{1,\}"[0-9a-z\.]\{1,\}"' < "$PLUGIN_FILE" | grep -o '[^"]*">$' | sed -e 's/..$//')"
RELEASES="${SELF}/release"
BUILD_TARGET="release"
BUILD_ROOT="${SELF}/target/${PLUGIN_NAME}/${BUILD_TARGET}"
PACKAGE_ROOT="${BUILD_ROOT}/in"
PACKAGE_PLUGIN="${BUILD_ROOT}/in/usr/local/emhttp/plugins/${PLUGIN_NAME}"
# Safety checks.
if [ -z "$PLUGIN_NAME" ]; then log err '$PLUGIN_NAME is empty.'; return 1; fi
if [ -z "$PLUGIN_NAME" ]; then log err '$PLUGIN_VERSION is empty.'; return 1; fi
if [ -z "$BUILD_ROOT" ]; then log err '$BUILD_ROOT is empty.'; return 1; fi
if [ -z "$PACKAGE_ROOT" ]; then log err '$PACKAGE_ROOT is empty.'; return 1; fi
# Check for a build script.
if ! [ -f "${PLUGIN_HOME}/build.sh" ]; then
log err "%s: no build.sh script!" "${PLUGIN_NAME}"
return 255
fi
# Log the action.
printf -- '-%.0s' $(seq 1 $(tput cols))
printf "Building: %s.plg\n" "$PLUGIN_NAME"
printf "Version: %s\n\n" "$PLUGIN_VERSION"
# Create the build directories.
$CLEAN && rm -rf "$BUILD_ROOT"
[ -d "$BUILD_ROOT" ] || mkdir -p "$BUILD_ROOT"
[ -d "${BUILD_ROOT}/out" ] || mkdir -p "${BUILD_ROOT}/out"
[ -d "$PACKAGE_PLUGIN" ] || mkdir -p "$PACKAGE_PLUGIN"
[ -d "$RELEASES" ] || mkdir -p "$RELEASES"
# Run the build script.
LOG_PREFIX="$(rev <<< "$PLUGIN_NAME" | cut -d'.' -f1 | rev): "
(shopt -u dotglob; source "${PLUGIN_HOME}/build.sh")
LOG_PREFIX=""
}
# ----------------------------------------------------------------------------------------------------------------------
# Main: (Arguments)
SELF="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
VERBOSE=false
VERBOSE_EXTRA=false
CLEAN=false
while true; do
case "$1" in
"--") shift; break;;
"-v"|"--verbose") shift; VERBOSE=true;;
"-vv"|"--extra-verbose") shift; VERBOSE=true; VERBOSE_EXTRA=true;;
"-c"|"--clean") shift; CLEAN=true;;
"-"*) echo "Unknown argument: $1"; exit 255;;
""|*) break;;
esac
done
# ----------------------------------------------------------------------------------------------------------------------
# Main: (Changed Plugins)
if [ $# -eq 0 ]; then
for plugin in "$(dirname "${BASH_SOURCE[0]}")"/source/*/manifest.plg; do
if [ -n "$(git status --porcelain "$(dirname "$plugin")")" ]; then
__plugin_build "$plugin"
fi
done
fi
# ----------------------------------------------------------------------------------------------------------------------
# Main: (Manual)
for plugin in "$@"; do
if ! [ -e "$(dirname "${BASH_SOURCE[0]}")/source/${plugin}"/manifest.plg ]; then
log err "package.sh: unknown plugin '%s'" "$plugin"
continue
fi
__plugin_build "$(dirname "${BASH_SOURCE[0]}")/source/${plugin}/manifest.plg"
done