Skip to content

Commit

Permalink
fix: fix auto update error
Browse files Browse the repository at this point in the history
  • Loading branch information
Nauxscript committed Dec 26, 2022
1 parent 1dda829 commit b05370f
Showing 1 changed file with 25 additions and 53 deletions.
78 changes: 25 additions & 53 deletions src/update.sh
Original file line number Diff line number Diff line change
@@ -1,77 +1,49 @@


# THESE VARIABLES MUST BE SET. SEE THE ONEUPDATER README FOR AN EXPLANATION OF EACH.
# Based On The OneUpdater workflow, but heavily simplified for github releases
# refer to https://github.com/vitorgalvao/alfred-workflows/tree/master/OneUpdater
readonly workflow_url='Nauxscript/alfred-open-port'
readonly download_type='github_release'
readonly github_repo='Nauxscript/alfred-open-port'
readonly frequency_check='1'

# FROM HERE ON, CODE SHOULD BE LEFT UNTOUCHED!
readonly info_plist='info.plist'

function abort {
echo "${1}" >&2
exit 1
}

function url_exists {
curl --silent --location --output /dev/null --fail --range 0-0 "${1}"
function notification {
osascript -e "display notification \"${1}\" with title \"${alfred_workflow_name}\" subtitle \"A new version is available\""
}

function notification {
local -r notificator="$(find . -type f -name 'notificator')"
function fetch_remote_version {
curl --silent "https://api.github.com/repos/${github_repo}/releases/latest" | grep 'tag_name' | head -1 | sed -E 's/.*tag_name": "v?(.*)".*/\1/'
}

if [[ -f "${notificator}" && "$(/usr/bin/file --brief --mime-type "${notificator}")" == 'text/x-shellscript' ]]; then
"${notificator}" --message "${1}" --title "${alfred_workflow_name}" --subtitle 'A new version is available'
return
fi
function fetch_download_url {
curl --silent "https://api.github.com/repos/${github_repo}/releases/latest" | grep 'browser_download_url.*\.alfredworkflow"' | head -1 | sed -E 's/.*browser_download_url": "(.*)".*/\1/'
}

osascript -e "display notification \"${1}\" with title \"${alfred_workflow_name}\" subtitle \"A new version is available\""
function download_and_install {
readonly tmpfile="$(mktemp).alfredworkflow"
notification 'Downloading and installing…'
curl --silent --location --output "${tmpfile}" "${1}"
open "${tmpfile}"
}

# Local sanity checks
readonly local_info_plist='info.plist'
readonly local_version="$(/usr/libexec/PlistBuddy -c 'print version' "${local_info_plist}")"

[[ -n "${local_version}" ]] || abort 'You need to set a workflow version in the configuration sheet.'
[[ "${download_type}" =~ ^(direct|page|github_release)$ ]] || abort "'download_type' (${download_type}) needs to be one of 'direct', 'page', or 'github_release'."
[[ "${frequency_check}" =~ ^[0-9]+$ ]] || abort "'frequency_check' (${frequency_check}) needs to be a number."
[[ -n "${alfred_workflow_version}" ]] || abort "'alfred_workflow_version' must be set."
[[ -n "${alfred_workflow_name}" ]] || abort "'alfred_workflow_name' must be set."
[[ "${github_repo}" =~ ^.+\/.+$ ]] || abort "'github_repo' (${github_repo}) must be in the format 'username/repo'."
[[ "${frequency_check}" =~ ^[0-9]+$ ]] || abort "'frequency_check' (${frequency_check}) must be a number."

# Check for updates
if [[ $(find "${local_info_plist}" -mtime +"${frequency_check}"d) ]]; then
# Remote sanity check
if ! url_exists "${remote_info_plist}"; then
abort "'remote_info_plist' (${remote_info_plist}) appears to not be reachable."
fi

readonly tmp_file="$(mktemp)"
curl --silent --location --output "${tmp_file}" "${remote_info_plist}"
readonly remote_version="$(/usr/libexec/PlistBuddy -c 'print version' "${tmp_file}")"
rm "${tmp_file}"

if [[ "${local_version}" == "${remote_version}" ]]; then
touch "${local_info_plist}" # Reset timer by touching local file
if [[ $(find "${info_plist}" -mtime +"${frequency_check}"d) ]]; then
if [[ "${alfred_workflow_version}" == "$(fetch_remote_version)" ]]; then
touch "${info_plist}" # Reset timer by touching local file
exit 0
fi

if [[ "${download_type}" == 'page' ]]; then
notification 'Opening download page…'
open "${workflow_url}"
exit 0
fi

readonly download_url="$(
if [[ "${download_type}" == 'github_release' ]]; then
osascript -l JavaScript -e 'function run(argv) { return JSON.parse(argv[0])["assets"].find(asset => asset["browser_download_url"].endsWith(".alfredworkflow"))["browser_download_url"] }' "$(curl --silent "https://api.github.com/repos/${workflow_url}/releases/latest")"
else
echo "${workflow_url}"
fi
)"

if url_exists "${download_url}"; then
notification 'Downloading and installing…'
readonly download_name="$(basename "${download_url}")"
curl --silent --location --output "${HOME}/Downloads/${download_name}" "${download_url}"
open "${HOME}/Downloads/${download_name}"
else
abort "'workflow_url' (${download_url}) appears to not be reachable."
fi
download_and_install "$(fetch_download_url)"
fi

0 comments on commit b05370f

Please sign in to comment.