Skip to content

fix: add a label of master #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fmt-check:
.PHONY: fmt-check

lint:
shellcheck $(SH_SRCFILES)
shellcheck $(SH_SRCFILES) -x
.PHONY: lint

test:
Expand Down
23 changes: 20 additions & 3 deletions bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ set -eo pipefail
# shellcheck disable=SC1091
source "$(dirname "$0")/../lib/json.bash"

# shellcheck disable=SC1091
source "$(dirname "$0")/../lib/check_and_update_master.bash"

trap cleanup SIGINT SIGTERM ERR

cleanup() {
Expand Down Expand Up @@ -51,7 +54,9 @@ install_zig() {
esac

local json
json=$(curl --fail --silent --location https://ziglang.org/download/index.json)
json=$(curl --fail --progress-bar --location https://ziglang.org/download/index.json)

check_master "$version" "$platform" "$architecture" "$json"

local download_url
download_url=$(
Expand All @@ -61,10 +66,22 @@ install_zig() {
tr -d '"'
)

local source_path="${install_path}/zig-${platform}-${architecture}-${version}.tar.xz"
local expected_shasum
local source_path
local computed_shasum

expected_shasum=$(json_parse "$json" | grep "\[\"$version\",\"${architecture}-${platform}\",\"shasum\"\]" | awk '{print $2}' | tr -d '"')
source_path="${install_path}/zig-${platform}-${architecture}-${version}.tar.xz"

echo "∗ Downloading and installing Zig..."
curl --fail --silent --location --create-dirs --output "$source_path" "$download_url"
curl --fail --progress-bar --location --create-dirs --output "$source_path" "$download_url"
computed_shasum="$(shasum -a 256 "$source_path" | awk '{print $1}')"

if [[ $computed_shasum != "$expected_shasum" ]]; then
version=$(json_parse "$json" | grep "\[\"master\",\"version\"\]" | awk '{print $2}' | tr -d '"')
fail "High Security Alert: The computed shasum does not match the expected shasum. Please verify the integrity of the downloaded file and try again."
fi

tar -xf "$source_path" -C "$install_path" --strip-components=1
mkdir -p "${install_path}/bin"
ln -s "${install_path}/zig" "${install_path}/bin/zig"
Expand Down
5 changes: 3 additions & 2 deletions bin/list-all
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -Eo pipefail
# shellcheck disable=SC1091
source "$(dirname "$0")/../lib/json.bash"

json=$(curl --fail --silent --location https://ziglang.org/download/index.json)
json=$(curl --fail --progress-bar --location https://ziglang.org/download/index.json)

sort_versions() {
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' |
Expand All @@ -15,7 +15,8 @@ sort_versions() {
list_all_versions() {
json_parse "$json" |
sed 's/^\[\(.*\)\].*$/\1/' |
grep -Eo '[0-9]+.[0-9]+.[0-9]+' |
grep -Eo '"master"|[0-9]+\.[0-9]+\.[0-9]+' |
tr -d '"' |
uniq
}

Expand Down
54 changes: 54 additions & 0 deletions lib/check_and_update_master.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

source "$(dirname "$0")/../lib/json.bash"

fail() {
echo -e "\e[31mFail:\e[m $*"
exit 1
}

check_master() {
local version="$1"
local platform="$2"
local architecture="$3"
local json="$4"

if [[ "$version" == "master" ]]; then
local tarball_path="$(find ~/.asdf/installs/zig/master -name "*.tar.xz")"

if [[ -z "$tarball_path" ]]; then
fail "Tarball not found in ~/.asdf/installs/zig/master"
fi

local computed_shasum="$(shasum -a 256 "$tarball_path" | awk '{print $1}')"
echo "Computed shasum: $computed_shasum"
local expected_shasum=$(json_parse "$json" | grep "\[\"$version\",\"${architecture}-${platform}\",\"shasum\"\]" | awk '{print $2}' | tr -d '"')
echo "Expected shasum: $expected_shasum"

if [[ "$computed_shasum" == "$expected_shasum" ]]; then
version=$(json_parse "$json" | grep "\[\"master\",\"version\"\]" | awk '{print $2}' | tr -d '"')
echo "Same match, new version: $version"

update_master "$version"
fi
fi
}

update_master() {
local version="$1"

mv ~/.asdf/installs/zig/master ~/.asdf/installs/zig/"$version"
sed -i '' "1a\\
# asdf-plugin: zig ${version}
" ~/.asdf/shims/zig
}

rollback_master() {
local version="$1"
local new_version="master"

mv ~/.asdf/installs/zig/"$version" ~/.asdf/installs/zig/master
sed -i '' "1a\\
# asdf-plugin: zig ${new_version}
" ~/.asdf/shims/zig
}
Loading