Skip to main content

Tidy up my script for installing Rust binaries

ID
ff64d78
date
2022-05-14 20:05:30+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
ccc0eae
message
Tidy up my script for installing Rust binaries

*   Add more comments and documentation
*   Make it generic so it can download multiple projects (but with aliases
    for command-line autocomplete)
*   Use the GitHub Release Assets API
changed files
5 files, 140 additions, 26 deletions

Changed files

_install-rust-bin (0) → _install-rust-bin (3408)

diff --git a/_install-rust-bin b/_install-rust-bin
new file mode 100755
index 0000000..a645581
--- /dev/null
+++ b/_install-rust-bin
@@ -0,0 +1,110 @@
+#!/usr/bin/env bash
+# Install one of my Rust binaries from GitHub.
+#
+# For my CLI tools written in Rust, I use a GitHub Action [1]
+# to compile binaries and add them to a release on GitHub.
+#
+# These are the "canonical" versions, rather than recompiling on
+# each machine and having slightly different versions because I
+# was using different checkouts of the code.
+#
+# This script gets the latest version of a binary from GitHub and
+# adds it to my $PATH.
+#
+# TODO: I publish macOS/Windows/Linux binaries, but this script only
+# fetches the macOS binary.  In theory, it could detect which OS it's
+# running on and pick the right binary.
+#
+# [1]: https://github.com/taiki-e/upload-rust-binary-action
+
+set -o errexit
+set -o nounset
+
+if (( "$#" != 1 ))
+then
+  echo "Usage: _install-rust-bin <REPO_NAME>"
+fi
+
+REPO_NAME="$1"
+
+RELEASES_API_URL="https://api.github.com/repos/alexwlchan/$REPO_NAME/releases/latest"
+
+# Fetch the asset URL using the GitHub Releases API [2].
+#
+# For the purposes of this script, these are the interesting bits of the
+# API response that we want to pay attention to:
+#
+#     {
+#       "assets": [
+#         {
+#           "name": "vfd-x86_64-apple-darwin.tar.gz",
+#           "url": "https://api.github.com/repos/alexwlchan/books.alexwlchan.net/releases/assets/64229966",
+#           ...
+#         },
+#         {
+#           "name": "vfd-x86_64-pc-windows-msvc.zip",
+#           "url": "https://api.github.com/repos/alexwlchan/books.alexwlchan.net/releases/assets/64229889",
+#           ...
+#         },
+#         {
+#           "name": "vfd-x86_64-unknown-linux-gnu.tar.gz",
+#           "url": "https://api.github.com/repos/alexwlchan/books.alexwlchan.net/releases/assets/64229611",
+#           ...
+#         }
+#       ],
+#     }
+#
+# [2]: https://docs.github.com/en/rest/releases/releases#get-the-latest-release
+#
+ASSET_URL=$(curl --silent "$RELEASES_API_URL" \
+  | jq -r '.assets | .[] | select(.name | contains("darwin")) | .url'
+)
+
+if [[ "$ASSET_URL" == "" ]]
+then
+  echo "No macOS download available for the latest version! Is it still building?" >&2
+  exit 1
+fi
+
+# Download and unpack the asset using the GitHub Release Assets API [3].
+#
+# We supply the headers required by the GitHub API, and the `--location`
+# flag caused curl to follow redirects.
+#
+# Note: this assumes the binary is packaged as a tar.gz.  The Windows
+# binaries are zipped instead of tar.gz-ed, so if you want to support
+# Windows, inspect the "content_type" field in the Releases API response.
+#
+# [3]: https://docs.github.com/en/rest/releases/assets#get-a-release-asset
+
+cd $(mktemp -d)
+
+curl \
+  --header "Accept: application/octet-stream" \
+  --location \
+  --silent \
+  "$ASSET_URL" > "asset.tar.gz"
+
+# Identify the name of the binary, which may be different from the repo name.
+#
+# We list all the files in the asset package, which should contain a single
+# file, and assume that's the name of the binary.
+ASSET_FILES=$(tar --list --file "asset.tar.gz")
+
+if (( $(echo "$ASSET_FILES" | wc -l) != 1 ))
+then
+  echo "Release asset doesn't contain exactly 1 file; not sure what to do:" >&2
+  echo "$ASSET_FILES" >&2
+  exit 1
+fi
+
+BINARY_NAME="$(echo "$ASSET_FILES" | head -n 1)"
+
+# Now actually extract the binary, make it executable, and add it to the PATH.
+tar --extract --gunzip --file "asset.tar.gz"
+
+chmod +x "$BINARY_NAME"
+mv "$BINARY_NAME" /usr/local/bin
+
+which "$BINARY_NAME"
+"$BINARY_NAME" --version

install-dominant_colours (0) → install-dominant_colours (252)

diff --git a/install-dominant_colours b/install-dominant_colours
new file mode 100755
index 0000000..bef52b9
--- /dev/null
+++ b/install-dominant_colours
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+# Gets the latest version of dominant_colours, my tool for fetching the
+# dominant colour palette of an image.
+#
+# See https://github.com/alexwlchan/dominant_colours
+
+set -o errexit
+set -o nounset
+
+_install-rust-bin dominant_colours
\ No newline at end of file

install-safari.rs (0) → install-safari.rs (238)

diff --git a/install-safari.rs b/install-safari.rs
new file mode 100755
index 0000000..f929742
--- /dev/null
+++ b/install-safari.rs
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+# Gets the latest version of safari.rs, the command-line tool I use
+# to get URLs from my running web browser.
+#
+# See https://github.com/alexwlchan/safari.rs
+
+set -o errexit
+set -o nounset
+
+_install-rust-bin safari.rs
\ No newline at end of file

install-vfd (0) → install-vfd (244)

diff --git a/install-vfd b/install-vfd
new file mode 100755
index 0000000..0d6513b
--- /dev/null
+++ b/install-vfd
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+# Gets the latest version of vfd, the command-line tool I use to build
+# my static books site.
+#
+# See https://github.com/alexwlchan/books.alexwlchan.net
+
+set -o errexit
+set -o nounset
+
+_install-rust-bin books.alexwlchan.net
\ No newline at end of file

update-vfd (750) → update-vfd (0)

diff --git a/update-vfd b/update-vfd
deleted file mode 100755
index f40bf71..0000000
--- a/update-vfd
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/usr/bin/env bash
-# Gets the latest version of vfd, the command-line tool I use to build
-# my static books site.
-#
-# See https://github.com/alexwlchan/books.alexwlchan.net
-
-set -o errexit
-set -o nounset
-
-DOWNLOAD_URL=$(curl --silent 'https://api.github.com/repos/alexwlchan/books.alexwlchan.net/releases/latest' \
-  | jq -r ' .assets | map(.browser_download_url) | map(select(test(".*darwin.*")))[0]'
-)
-
-if [[ "$DOWNLOAD_URL" == "null" ]]
-then
-  echo "No macOS download available for the latest version! Is it still building?" >&2
-  exit 1
-fi
-
-# The --location flag means we follow redirects
-curl --location "$DOWNLOAD_URL" > ~/.cargo/bin/vfd.tar.gz
-tar -xzf ~/.cargo/bin/vfd.tar.gz
-
-mv vfd ~/.cargo/bin/vfd
-chmod +x ~/.cargo/bin/vfd
-vfd --version