Skip to content

Commit

Permalink
feat: ✨ add auto updater
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Villard <[email protected]>
  • Loading branch information
eviweb committed Sep 16, 2022
1 parent 1652dde commit 1936add
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
34 changes: 34 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#

VERSION="0.2.0"
REMOTE_INSTALLER="https://raw.githubusercontent.com/eviweb/config-installer/main/install.sh"

main_dir()
{
Expand Down Expand Up @@ -199,6 +200,37 @@ initialize()
mkdir -p "$(main_dir)"/config/{copy,link,run}
}

get_latest_version()
{
curl -Ls "${REMOTE_INSTALLER}" | grep -Poe '\d+\.\d+\.\d+'
}

should_update()
{
local compareversions=( "${VERSION}" $@ )

test "$(echo "${compareversions[@]}" | tr " " "\n" | sort -rV | head -n 1)" != "${VERSION}";
}

check_for_update()
{
local latestversion="$(get_latest_version)"
local answer=""

if should_update "${latestversion}"; then
while ! [[ "${answer}" =~ ^[YyNn] ]]; do
read -p "A new version is available, do you want to update? (y/n)" answer
done

[[ "${answer}" =~ ^[Nn] ]] || {
log "Start updating..."
curl -Ls "${REMOTE_INSTALLER}" -o "$(main_dir)"/install.sh
log "Done, please restart the installer."
exit 0
}
fi
}

if ! is_sourced; then
VERBOSITY=0
OVERWRITING="-i"
Expand All @@ -220,6 +252,8 @@ if ! is_sourced; then
esac
done

check_for_update

before_scripts=(
$(find_scripts "before")
)
Expand Down
63 changes: 63 additions & 0 deletions tests/update-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
. "$(main_dir)"/install.sh

tempdir="$(mktemp -d)"

[ -d "${tempdir}" ] || {
echo -e "Failed to create ${tempdir}"
exit 1
}

major="${VERSION%%.*}"
minor="$(echo "${VERSION}" | grep -Poe '(?<=\.)\d+(?=\.)')"
patch="${VERSION##*.}"

declare -A versions=(
[0.1.0]=1
["${VERSION}"]=1
["${major}.${minor}.$((patch+1))"]=0
["${major}.$((minor+1)).${patch}"]=0
["$((major+1)).${minor}.${patch}"]=0
)

curl()
{
cat "$(main_dir)"/install.sh
}

alter_version()
{
local file="$1"
local version="$2"

sed -r "s/VERSION=\"[^\"]+\"/VERSION=\"${version}\"/" -i "${file}"
}

## test get_latest_version
assert_equals "$(get_latest_version)" "$(cat "$(main_dir)"/install.sh | grep -Poe '\d+\.\d+\.\d+')"

## test should_update
for version in "${!versions[@]}"; do
message="Should update because ${version} is greater than ${VERSION}."
[ "${versions["${version}"]}" -eq 0 ] || message="Should not update because ${version} is lesser than ${VERSION}."

should_update "${version}"
result="$?"

assert_equals "${result}" "${versions["${version}"]}" "${message}"
done

## test update
OLD_VERSION="0.1.0"
cp "$(main_dir)"/install.sh "${tempdir}"
alter_version "${tempdir}"/install.sh "${OLD_VERSION}"

## no update
assert_equals "${OLD_VERSION}" "$(echo 'n' | "${tempdir}"/install.sh -V | grep -Poe '\d+\.\d+\.\d+')"

## do update
unset curl
echo 'y' | "${tempdir}"/install.sh

assert_equals "${VERSION}" "$("${tempdir}"/install.sh -V | grep -Poe '\d+\.\d+\.\d+')"

rm -rf "${tempdir}"

0 comments on commit 1936add

Please sign in to comment.