-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathupgrade-install-plugin.sh
executable file
·144 lines (120 loc) · 3.88 KB
/
upgrade-install-plugin.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
#!/bin/bash
# set -x
TARGET_PLUGIN="$1"
TARGET_VERSION="$2"
TARGET_PATH="$3"
TARGET_PLUGIN_BUNDLE_NAME="${TARGET_PLUGIN/\//@}-${TARGET_VERSION}"
REPO_BASE_URL="https://github.com/homebridge/plugins/releases/download/v1.0.0"
echo "Target Version: $TARGET_VERSION"
echo "Target Path: $TARGET_PATH"
echo ""
SHASUM_COMMAND=""
if command -v shasum > /dev/null; then
SHASUM_COMMAND="shasum -a 256"
elif command -v sha256sum > /dev/null; then
SHASUM_COMMAND="sha256sum"
else
echo "Failed to find required shasum or sha256sum command."
exit 1
fi
if ! command -v curl > /dev/null; then
echo "Failed to find required curl command."
fi
if ! command -v jq > /dev/null; then
echo "Failed to find required jq command."
fi
tmp_dir=$(mktemp -d -t "$TARGET_PLUGIN_BUNDLE_NAME".XXXXXXX)
if ! [ "$tmp_dir" ]; then
echo "Failed to create temporary directory."
exit 1
fi
echo "Downloading ${TARGET_PLUGIN_BUNDLE_NAME}.sha256..."
if ! curl -fsSL# -o "$tmp_dir/SHASUMS256.txt" "$REPO_BASE_URL/${TARGET_PLUGIN_BUNDLE_NAME}.sha256"; then
echo "Failed to download SHASUMS256.txt"
exit 1
fi
echo "Downloading ${TARGET_PLUGIN_BUNDLE_NAME}.tar.gz..."
if ! curl -fL# -o "$tmp_dir/${TARGET_PLUGIN_BUNDLE_NAME}.tar.gz" "$REPO_BASE_URL/${TARGET_PLUGIN_BUNDLE_NAME}.tar.gz"; then
echo "Failed to download homebridge-config-ui-x-${TARGET_VERSION}.tar.gz"
exit 1
fi
echo ""
echo "Verifying download..."
if ! cd "$tmp_dir"; then
echo "Failed to change directory to $tmp_dir"
exit 1
fi
$SHASUM_COMMAND -c SHASUMS256.txt
if ! $SHASUM_COMMAND -c SHASUMS256.txt; then
echo "Download failed integrity check."
rm -rf "$tmp_dir"
exit 1
fi
echo ""
if [ ! -d "$TARGET_PATH" ]; then
mkdir -p "$TARGET_PATH"
fi
echo "Creating backup..."
if [ -d "$TARGET_PATH/$TARGET_PLUGIN" ]; then
mv "$TARGET_PATH/$TARGET_PLUGIN" "$TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bak"
echo "Backup path: $TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bak"
if [ -d "$TARGET_PATH/.bin/" ]; then
for FILE in "$TARGET_PATH/.bin/"*; do
if [[ "$(readlink "$FILE")" == "../$TARGET_PLUGIN/"* ]]; then
mkdir -p "$TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bin.bak"
mv "$FILE" "$TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bin.bak/"
fi
done
fi
fi
echo ""
function postInstallCleanup() {
rm -rf "$TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bak"
rm -rf "$TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bin.bak/"
rm -rf "$tmp_dir"
}
function revertToBackup() {
rm -rf "${TARGET_PATH:?}/${TARGET_PLUGIN:?}"
if [ -d "$TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bak" ]; then
echo "Restoring previous version..."
mv "$TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bak" "$TARGET_PATH/$TARGET_PLUGIN"
[ -d "$TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bin.bak" ] && mv "$TARGET_PATH/.$TARGET_PLUGIN_BUNDLE_NAME.bin.bak/"* "$TARGET_PATH/.bin/"
echo "Restore Complete. Installation failed."
fi
postInstallCleanup
exit 1
}
echo "Extracting..."
if ! tar --no-same-owner -xmvf "$tmp_dir/${TARGET_PLUGIN_BUNDLE_NAME}.tar.gz" -C "$TARGET_PATH"; then
echo "Failed to extract."
revertToBackup
else
echo "Extracted to: $TARGET_PATH"
fi
echo ""
echo "Running post-install scripts..."
if ! cd "$TARGET_PATH/$TARGET_PLUGIN"; then
echo "Failed to change directory to $TARGET_PATH/$TARGET_PLUGIN"
exit 1
fi
if ! npm rebuild --foreground-scripts; then
echo "Failed to rebuild."
revertToBackup
fi
echo ""
PLUGIN_PACKAGE_JSON_PATH="$(dirname "$TARGET_PATH")/package.json"
if [ ! -f "$PLUGIN_PACKAGE_JSON_PATH" ]; then
echo "{}" > "$PLUGIN_PACKAGE_JSON_PATH"
fi
echo "Updating $PLUGIN_PACKAGE_JSON_PATH..."
if PACKAGE_JSON=$(jq -rM ".dependencies += { \"$TARGET_PLUGIN\": \"$TARGET_VERSION\"}" < "$PLUGIN_PACKAGE_JSON_PATH"); then
printf "%s" "$PACKAGE_JSON" > "$PLUGIN_PACKAGE_JSON_PATH"
else
echo "Failed to update $PLUGIN_PACKAGE_JSON_PATH"
exit 1
fi
echo ""
echo "Cleaning up..."
postInstallCleanup
echo ""
echo "Installed ${TARGET_PLUGIN} v${TARGET_VERSION}"