jsumners / alfred-emoji

Alfred workflow for searching and copying emoji
741 stars 32 forks source link

Tweaks to updater bash script. #97

Closed whomwah closed 1 year ago

whomwah commented 1 year ago

Hi there. whilst working on my own workflow https://github.com/whomwah/alfred-github-workflow and making some tweaks to my updater bash script (thanks for the inspiration), I saw there were a few tweaks that could be made to reduce the API calls from 2 to 1. I also take a slightly different approach but there may still be something in there you could find useful.

# Setting github variables
readonly gh_repo='whomwah/alfred-github-workflow'
readonly gh_url="https://api.github.com/repos/${gh_repo}/releases/latest"

# Fetch latest version
function fetch_remote_version {
  echo $1 | grep 'tag_name' | head -1 | sed -E 's/.*tag_name": "v?(.*)".*/\1/'
}

# Fetch download url
function fetch_download_url {
  echo $1 | grep 'browser_download_url.*\.alfredworkflow"' | head -1 | sed -E 's/.*browser_download_url": "(.*)".*/\1/'
}

# Download and install workflow
function download_and_install {
  readonly tmpfile="$(mktemp).alfredworkflow"
  echo "Downloading and installing version ${2}…"
  curl --silent --location --output "${tmpfile}" "${1}"
  open "${tmpfile}"
  exit 0;
}

# Setting version and download url for later use
readonly response=$(curl --silent "${gh_url}")
readonly version="$(fetch_remote_version $response)"
readonly download_url="$(fetch_download_url $response)"

# Compare current version to installed version and download if required
[ $(printf "%d%03d%03d%03d\n" $(echo ${alfred_workflow_version} | tr '.' ' ')) -lt $(printf "%d%03d%03d%03d\n" $(echo ${version} | tr '.' ' ')) ] && download_and_install ${download_url} ${version}

echo "You are running the latest version (${alfred_workflow_version})
jsumners commented 1 year ago

Please highlight the suggestions you are making with a diff or some prose explaining them.