Repository: FuelLabs/fuelup
Pull request #195 (2022-09-20 09:20:57)

support forc-wallet
Allows fuelup to download and use `forc-wallet`.


Patch:
diff --git a/.github/workflows/scripts/append_package_to_channel.sh b/.github/workflows/scripts/append_package_to_channel.sh
new file mode 100755
index 00000000..a89306af
--- /dev/null
+++ b/.github/workflows/scripts/append_package_to_channel.sh
@@ -0,0 +1,98 @@
+#!/bin/bash
+set -e
+
+add_url_and_hash() {
+    _url="https://github.com/FuelLabs/$1/releases/download/$2/$3"
+    _err=$(curl -sSf "${_url}" -L -o "${3}" 2>&1)
+    if echo "$_err" | grep -q 404; then
+        printf "Could not download from %s - the release binary might not be ready yet. You can check if a binary is available here: https://github.com/FuelLabs/%s/releases/%s\n" "${_url}" "${1}" "${2}"
+        exit 1
+    fi
+    # shasum generates extra output so we take the first 64 bytes.
+    _hash=$(shasum -a 256 "$3" | head -c 64)
+    RETVAL="url = \"${_url}\"\nhash = \"${_hash}\"\n\n"
+}
+
+create_new_pkg() {
+    _header=$(printf "[pkg.%s]\nversion = \"%s\"\n" "${1}" "${2}")
+    RETVAL="$_header"
+}
+
+create_pkg_in_channel() {
+    CHANNEL_TOML_NAME=$3
+    version=$2
+    date="$(date +'%Y%m%d')"
+    tag="v${2}"
+    case "${1}" in
+        "forc")
+            _targets=("darwin_amd64" "darwin_arm64" "linux_amd64" "linux_arm64")
+            _repo="sway"
+            _tarball_prefix="forc-binaries"
+            ;;
+        "forc-client")
+            _targets=("aarch64-apple-darwin" "aarch64-unknown-linux-gnu" "x86_64-apple-darwin" "x86_64-unknown-linux-gnu")
+            _repo="forc-client"
+            _tarball_prefix="forc-client"
+
+            if [ "${2}" != "nightly" ]; then
+                _tarball_prefix+="-${version}"
+            fi
+            ;;
+        "forc-wallet")
+            _targets=("aarch64-apple-darwin" "aarch64-unknown-linux-gnu" "x86_64-apple-darwin" "x86_64-unknown-linux-gnu")
+            _repo="forc-wallet"
+            _tarball_prefix="forc-wallet"
+
+            if [ "${2}" != "nightly" ]; then
+                _tarball_prefix+="-${version}"
+            fi
+            ;;
+        "fuel-core")
+            _targets=("aarch64-apple-darwin" "aarch64-unknown-linux-gnu" "x86_64-apple-darwin" "x86_64-unknown-linux-gnu")
+            _repo="fuel-core"
+            _tarball_prefix="fuel-core"
+
+            if [ "${2}" != "nightly" ]; then
+                _tarball_prefix+="-${version}"
+            fi
+
+            ;;
+    esac
+
+    if [ "${2}" = "nightly" ]; then
+        _repo="sway-nightly-binaries"
+        version="$(curl -s https://api.github.com/repos/FuelLabs/${_repo}/releases | grep "tag_name" | grep "nightly.${date}" | grep "${_tarball_prefix}" | head -n 1 | cut -d "-" -f3- | cut -d "\"" -f1)"
+        _tarball_prefix+="-${version}"
+        # Replace '+' within string with '%2B' to be URL friendly
+        tag=$(echo "${_tarball_prefix}" | sed -r "s/\+/\%2B/g")
+    fi
+
+    # We need to recreate channel-fuel-latest.toml, generating new URLs and sha256 hashes for the download links.
+    printf "%s: Generating new package\n" "${1}"
+    create_new_pkg "$1" "${version}"
+    _header="$RETVAL"
+    _content=""
+    for target in "${_targets[@]}"; do
+        _content+="[pkg.${1}.target.${target}]\n"
+        # TAG is either: v0.22.1 or forc-binaries-nightly-2022-08-25
+        add_url_and_hash $_repo "${tag}" "${_tarball_prefix}-${target}.tar.gz"
+        _content+="$RETVAL"
+    done
+
+    # Only write to file if there's no problem downloading and hashing all the above releases.
+    _package=$(printf "%s\n%s" "${_header}" "${_content}")
+    echo -ne "$_package" >>"$CHANNEL_TOML_NAME"
+}
+
+main() {
+    COMPONENT=$1
+    VERSION=$2
+    CHANNEL_TOML_NAME=$3
+    trap 'rm *.tar.gz' ERR EXIT
+
+    create_pkg_in_channel "${COMPONENT}" "${VERSION}" "${CHANNEL_TOML_NAME}"
+
+    exit 0
+}
+
+main "$@" || exit 1
diff --git a/components.toml b/components.toml
index 7c9fd464..49069cb8 100644
--- a/components.toml
+++ b/components.toml
@@ -46,3 +46,12 @@ executables = ["fuel-core"]
 repository_name = "fuel-core"
 targets = [ "aarch64-unknown-linux-gnu", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "x86_64-apple-darwin" ]
 publish = true
+
+[component.forc-wallet]
+name = "forc-wallet"
+tarball_prefix = "forc-wallet"
+is_plugin = true
+executables = ["forc-wallet"]
+repository_name = "forc-wallet"
+targets = [ "aarch64-unknown-linux-gnu", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "x86_64-apple-darwin" ]
+publish = true


Test patch:
diff --git a/tests/commands.rs b/tests/commands.rs
index 9208b2c7..87179a6b 100644
--- a/tests/commands.rs
+++ b/tests/commands.rs
@@ -117,6 +117,7 @@ my_toolchain (default)
     - forc-explore - not found
     - forc-fmt - not found
     - forc-lsp - not found
+    - forc-wallet - not found
   fuel-core - not found
 "#;
         assert!(stdout.contains(expected_stdout));