-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
executable file
·136 lines (114 loc) · 4.58 KB
/
update.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
#!/bin/bash
dp0="$(realpath "$(dirname "$0")")"
dp0_tools="$dp0/.tools" && source "$dp0_tools/env_tools.sh"
set -e
cd "$dp0"
self_name=AutoinstallCreator
relative_version_filepath="_$self_name/version.txt"
package_ext=".sh" && $is_windows_os && package_ext=".sh.bat"
package_grep_ext="\.sh" && $is_windows_os && package_grep_ext="\.sh\.bat"
disable_sleep="$MOCK_AUTOINSTALLCREATOR_DISABLE_SLEEP"
# Finishing update process. Stage 2
target_path=$1
if [[ ! -z "$target_path" ]]; then
target_path=$(realpath "$target_path")
echo "Finishing update from: $dp0"
echo "Target path: $target_path"
if [[ $target_path == $dp0 ]]; then
echo "Update failed. Cannot update to current directory"
exit 1
fi
if [[ ! -f "$target_path/$relative_version_filepath" ]]; then
echo "Update failed. Version not found: '$target_path/$relative_version_filepath'"
exit 1
fi
echo "Copying new files"
find "$dp0" -mindepth 1 -maxdepth 1 ! -name "tmp_*" \
-exec cp -rf "{}" "$target_path/" \;
echo "Removing orphaned files"
orphaned_files=$(cat $dp0/_$self_name/orphaned_files.txt)
for orphaned_file in $orphaned_files; do
rm -f "$target_path/$orphaned_file"
done
echo "Removing empty directories"
find "$target_path" -type d -mindepth 1 -maxdepth 1 -exec rmdir -p --ignore-fail-on-non-empty "{}" \;
echo "Update complete: $(cat $target_path/_$self_name/version.txt)"
exit 0
fi
# Starting update process. Stage 1
self_version_body=$(head -n 1 "$dp0/_$self_name/version.txt")
echo "self_version_body: $self_version_body"
if [[ -z "$self_version_body" ]]; then
echo "Cannot get 'self_version_body'"
exit 1
fi
self_version_count=$(echo "$self_version_body" | "$grep" --only-matching "(?<=$self_name\.)[^\s]+.(?=\.)" | head -n 1)
if [[ -z "$self_version_count" ]]; then
echo "Cannot get 'self_version_count' from $self_version_body"
exit 1
fi
self_version_hash=$(echo "$self_version_body" | "$grep" --only-matching "(?<=$self_name\.$self_version_count\.)[^\s]+." | head -n 1)
if [[ -z "$self_version_hash" ]]; then
echo "Cannot get 'self_version_hash' from $self_version_body"
exit 1
fi
latest_version="https://api.github.com/repos/hemnstill/$self_name/releases/tags/latest-master"
echo "Get latest version: '$latest_version' ..."
version_body="$MOCK_AUTOINSTALLCREATOR_VERSION_BODY"
if [[ -z "$version_body" ]]; then
version_body=$($curl --silent --location "$latest_version" | "$grep" --only-matching '(?<="body":\s")[^,]+.' | cut -d '\r\n' -f 1 | head -n 1)
fi
echo "Version_body: $version_body"
if [[ -z "$version_body" ]]; then
echo "Cannot get 'version_body'"
exit 1
fi
version_count=$(echo "$version_body" | "$grep" --only-matching "(?<=$self_name\.)[^\s]+.(?=\.)" | head -n 1)
if [[ -z "$version_count" ]]; then
echo "Cannot get 'version_count' from $version_body"
exit 1
fi
version_hash=$(echo "$version_body" | "$grep" --only-matching "(?<=$self_name\.$version_count\.)[^\s]+." | head -n 1)
if [[ -z "$version_hash" ]]; then
echo "Cannot get 'version_hash' from $version_body"
exit 1
fi
if [[ $self_version_count -eq $version_count ]] && [[ $self_version_hash == $version_hash ]]; then
echo "Version is up to date: $version_body" >$dp0/_update.log 2>&1
echo "Version is up to date: $version_body"
if [[ -z "$disable_sleep" ]]; then
sleep 10
fi
exit 0
fi
found_msg_prefix="Found new version"
if [[ $self_version_count -gt $version_count ]]; then
found_msg_prefix="Downgrade version"
echo "Warning. Downgrade to old version!"
if [[ -z "$disable_sleep" ]]; then
sleep 10
fi
fi
echo "$found_msg_prefix: $self_version_body -> $version_body"
grep_pattern="(?<=\"browser_download_url\":\s\")[^,]+.$package_grep_ext(?=\")"
download_url=$($curl --silent --location "$latest_version" | "$grep" --only-matching $grep_pattern | head -n 1)
if [[ -z "$download_url" ]]; then
echo "Cannot get release version"
exit 1
fi
echo "Removing old '$dp0/_$self_name/tmp_*' versions"
find "$dp0/_$self_name" -maxdepth 1 -type d -name "tmp_*" -exec rm -rf "{}" \;
echo "Downloading: $download_url ..."
package_filepath="$MOCK_AUTOINSTALLCREATOR_PACKAGE_FILEPATH"
if [[ -z "$package_filepath" ]]; then
package_filepath="$dp0/_$self_name/$self_name$package_ext"
$curl --location "$download_url" --output "$package_filepath"
fi
echo "Extracting to: $dp0/_$self_name/$version_body"
$MOCK_AUTOINSTALLCREATOR_XTERM "$package_filepath" --target "$dp0/_$self_name/tmp_$version_body" >$dp0/_update.log 2>&1
echo "Running extracted 'update.sh'"
"$dp0/_$self_name/tmp_$version_body/update.sh" "$dp0" >>$dp0/_update.log 2>&1
echo "Update complete: '$dp0/_update.log'"
if [[ -z "$disable_sleep" ]]; then
sleep 10
fi