From 8c07c9b3a053e59d35c1fb49b0872a682c8cf1b9 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Tue, 13 Feb 2024 23:17:09 -0800 Subject: [PATCH 1/3] Create Cadence 1.0 pre-release installer --- install.ps1 | 59 ++++++++++++++++++++++++++++++++++++++++++++--------- install.sh | 34 ++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/install.ps1 b/install.ps1 index 0e13ed48a..23c9b223b 100644 --- a/install.ps1 +++ b/install.ps1 @@ -39,16 +39,54 @@ $repo = "onflow/flow-cli" $versionURL = "https://api.github.com/repos/$repo/releases/latest" $assetsURL = "https://github.com/$repo/releases/download" -# Add the GitHub token to the web request headers if it was provided -$webRequestOptions = if ($githubToken) { - @{ 'Headers' = @{ 'Authorization' = "Bearer $githubToken" } } -} else { - @{} +# Function to get the latest version +function Get-Version { + param ( + [string]$repo, + [string]$searchTerm, + [string]$githubTokenHeader + ) + + $page = 1 + $version = null + + while (-not $version) { + if ($githubTokenHeader) { + $response = Invoke-WebRequest -Uri "https://api.github.com/repos/$repo/releases?per_page=100&page=$page" -Headers @{ 'Authorization' = $githubTokenHeader } -UseBasicParsing -ErrorAction SilentlyContinue + } else { + $response = Invoke-WebRequest -Uri "https://api.github.com/repos/$repo/releases?per_page=100&page=$page" -UseBasicParsing -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 (!$version) { - $q = (Invoke-WebRequest -Uri "$versionURL" -UseBasicParsing @webRequestOptions) | ConvertFrom-Json - $version = $q.tag_name +if (-not $version) { + $version = Get-Version -repo $repo -searchTerm "cadence-v1.0.0" -githubTokenHeader $githubToken } Write-Output("Installing version {0} ..." -f $version) @@ -67,7 +105,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 +118,7 @@ if ($addToPath -and $existingPaths -notcontains $directory) { [System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User) } -Write-Output "Done." +Write-Output "\nSuccessfully 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 From 2684ee44a00bab6a63726d3f7131d26626424ce2 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Thu, 15 Feb 2024 17:59:12 -0800 Subject: [PATCH 2/3] fix null --- install.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.ps1 b/install.ps1 index 23c9b223b..8340ba99f 100644 --- a/install.ps1 +++ b/install.ps1 @@ -48,7 +48,7 @@ function Get-Version { ) $page = 1 - $version = null + $version = $null while (-not $version) { if ($githubTokenHeader) { From f9069aab248e6a5c0d51babb94020ab8c76be760 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Thu, 15 Feb 2024 22:14:42 -0800 Subject: [PATCH 3/3] fix webrequest options --- install.ps1 | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/install.ps1 b/install.ps1 index 8340ba99f..9d8c67d88 100644 --- a/install.ps1 +++ b/install.ps1 @@ -39,24 +39,26 @@ $repo = "onflow/flow-cli" $versionURL = "https://api.github.com/repos/$repo/releases/latest" $assetsURL = "https://github.com/$repo/releases/download" +# Add the GitHub token to the web request headers if it was provided +$webRequestOptions = if ($githubToken) { + @{ 'Headers' = @{ 'Authorization' = "Bearer $githubToken" } } +} else { + @{} +} + # Function to get the latest version function Get-Version { param ( [string]$repo, [string]$searchTerm, - [string]$githubTokenHeader + [hashtable]$webRequestOptions ) $page = 1 $version = $null while (-not $version) { - if ($githubTokenHeader) { - $response = Invoke-WebRequest -Uri "https://api.github.com/repos/$repo/releases?per_page=100&page=$page" -Headers @{ 'Authorization' = $githubTokenHeader } -UseBasicParsing -ErrorAction SilentlyContinue - } else { - $response = Invoke-WebRequest -Uri "https://api.github.com/repos/$repo/releases?per_page=100&page=$page" -UseBasicParsing -ErrorAction SilentlyContinue - } - + $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) { @@ -86,7 +88,7 @@ function Get-Version { } if (-not $version) { - $version = Get-Version -repo $repo -searchTerm "cadence-v1.0.0" -githubTokenHeader $githubToken + $version = Get-Version -repo $repo -searchTerm "cadence-v1.0.0" -webRequestOptions $webRequestOptions } Write-Output("Installing version {0} ..." -f $version) @@ -118,7 +120,8 @@ if ($addToPath -and $existingPaths -notcontains $directory) { [System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User) } -Write-Output "\nSuccessfully installed Flow CLI $version" +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