Skip to content
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

Create Cadence 1.0 pre-release installer #1410

Merged
merged 4 commits into from
Feb 16, 2024
Merged
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
52 changes: 47 additions & 5 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
jribbink marked this conversation as resolved.
Show resolved Hide resolved
}
}

$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)
Expand All @@ -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(';')
Expand All @@ -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
34 changes: 28 additions & 6 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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."
jribbink marked this conversation as resolved.
Show resolved Hide resolved
echo ""
}

main
Loading