From 9ecc47d88d2b0cd9587ad240824a63221c7f773e Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Fri, 16 Feb 2024 13:22:57 -0800 Subject: [PATCH] Create Cadence 1.0 pre-release installer (#1410) --- install.ps1 | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- install.sh | 34 ++++++++++++++++++++++++++++------ 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/install.ps1 b/install.ps1 index 0e13ed48a..9d8c67d88 100644 --- a/install.ps1 +++ b/install.ps1 @@ -46,9 +46,49 @@ $webRequestOptions = if ($githubToken) { @{} } -if (!$version) { - $q = (Invoke-WebRequest -Uri "$versionURL" -UseBasicParsing @webRequestOptions) | ConvertFrom-Json - $version = $q.tag_name +# Function to get the latest version +function Get-Version { + param ( + [string]$repo, + [string]$searchTerm, + [hashtable]$webRequestOptions + ) + + $page = 1 + $version = $null + + while (-not $version) { + $response = Invoke-WebRequest -Uri "https://api.github.com/repos/$repo/releases?per_page=100&page=$page" -UseBasicParsing @webRequestOptions -ErrorAction SilentlyContinue + $status = $response.StatusCode + + if ($status -eq 403 -and $githubTokenHeader) { + Write-Output "Failed to get latest version from Github API, is your GITHUB_TOKEN valid? Trying without authentication..." + $githubTokenHeader = "" + continue + } + + if ($status -ne 200) { + Write-Output "Failed to get latest version from Github API, please manually specify a version to install as an argument to this script." + return $null + } + + $jsonResponse = $response.Content | ConvertFrom-Json + + foreach ($release in $jsonResponse) { + if ($release.tag_name -like "*$searchTerm*") { + $version = $release.tag_name + break + } + } + + $page++ + } + + return $version +} + +if (-not $version) { + $version = Get-Version -repo $repo -searchTerm "cadence-v1.0.0" -webRequestOptions $webRequestOptions } Write-Output("Installing version {0} ..." -f $version) @@ -67,7 +107,7 @@ try { } catch {} -Move-Item -Path "$directory\flow-cli.exe" -Destination "$directory\flow.exe" -Force +Move-Item -Path "$directory\flow-cli.exe" -Destination "$directory\flow-c1.exe" -Force # Check if the directory is already in the PATH $existingPaths = [Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::User).Split(';') @@ -80,6 +120,8 @@ if ($addToPath -and $existingPaths -notcontains $directory) { [System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User) } -Write-Output "Done." +Write-Output "" +Write-Output "Successfully installed Flow CLI $version" +Write-Output "PRE-RELEASE: Use the 'flow-c1' command to interact with this Cadence 1.0 CLI pre-release." Start-Sleep -Seconds 1 \ No newline at end of file diff --git a/install.sh b/install.sh index a2da2bfe6..c639eb185 100755 --- a/install.sh +++ b/install.sh @@ -9,6 +9,8 @@ ASSETS_URL="https://github.com/$REPO/releases/download/" VERSION="$1" # The architecture string, set by get_architecture ARCH="" +# The tag search term to use if no version is specified (first match is used) +SEARCH_TERM="cadence-v1.0.0" # Optional environment variable for Github API token # If GITHUB_TOKEN is set, use it in the curl requests to avoid rate limiting @@ -60,23 +62,39 @@ get_architecture() { } # Get the latest version from remote if none specified in args. +page=1 get_version() { if [ -z "$VERSION" ] then VERSION="" if [ -n "$github_token_header" ] then - VERSION=$(curl -H "$github_token_header" -s "https://api.github.com/repos/$REPO/releases/latest" | grep -E 'tag_name' | cut -d '"' -f 4) + response=$(curl -H "$github_token_header" -s "https://api.github.com/repos/$REPO/releases?per_page=100&page=$page" -w "%{http_code}") else - VERSION=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep -E 'tag_name' | cut -d '"' -f 4) + response=$(curl -s "https://api.github.com/repos/$REPO/releases?per_page=100&page=$page" -w "%{http_code}") fi - if [ -z "$VERSION" ] && [ -n "$github_token_header" ] + status=$(echo "$response" | tail -n 1) + if [ "$status" -eq "403" ] && [ -n "$github_token_header" ] then echo "Failed to get latest version from Github API, is your GITHUB_TOKEN valid? Trying without authentication..." github_token_header="" get_version fi + + if [ "$status" -ne "200" ] + then + echo "Failed to get latest version from Github API, please manually specify a version to install as an argument to this script." + return 1 + fi + + VERSION=$(echo "$response" | grep -E 'tag_name' | grep -E "$SEARCH_TERM" | head -n 1 | cut -d '"' -f 4) + + if [ -z "$VERSION" ] + then + page=$((page+1)) + get_version + fi fi } @@ -109,11 +127,15 @@ main() { [ -d $TARGET_PATH ] || mkdir -p $TARGET_PATH tar -xf $tmpfile -C $TARGET_PATH - mv $TARGET_PATH/flow-cli $TARGET_PATH/flow - chmod +x $TARGET_PATH/flow + mv $TARGET_PATH/flow-cli $TARGET_PATH/flow-c1 + chmod +x $TARGET_PATH/flow-c1 - echo "Successfully installed the Flow CLI to $TARGET_PATH." + echo "" + echo "Successfully installed Flow CLI $VERSION to $TARGET_PATH." echo "Make sure $TARGET_PATH is in your \$PATH environment variable." + echo "" + echo "PRE-RELEASE: Use the 'flow-c1' command to interact with this Cadence 1.0 CLI pre-release." + echo "" } main