Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 81ec8ba

Browse files
authored
[cli] Add installation script by CLI (#907)
1 parent 8bc51ba commit 81ec8ba

File tree

2 files changed

+446
-0
lines changed

2 files changed

+446
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# This script installs the Aptos CLI on Windows.
2+
# It will perform the following steps:
3+
# - Determine the system architecture
4+
# - Download the CLI
5+
# - Put it in an appropriate location
6+
7+
# ANSI color codes for PowerShell
8+
$ESC = [char]0x1B
9+
$RED = "${ESC}[31m"
10+
$GREEN = "${ESC}[32m"
11+
$YELLOW = "${ESC}[33m"
12+
$BLUE = "${ESC}[34m"
13+
$CYAN = "${ESC}[36m"
14+
$BOLD = "${ESC}[1m"
15+
$NC = "${ESC}[0m"
16+
$GREEN = "`e[32m"
17+
$YELLOW = "`e[33m"
18+
$BLUE = "`e[34m"
19+
$CYAN = "`e[36m"
20+
$BOLD = "`e[1m"
21+
$NC = "`e[0m"
22+
23+
# Default values
24+
$SCRIPT = "aptos.exe"
25+
$TEST_COMMAND = "$SCRIPT info"
26+
$BIN_DIR = "$env:USERPROFILE\.aptoscli\bin"
27+
$FORCE = $false
28+
$ACCEPT_ALL = $false
29+
$VERSION = ""
30+
31+
# Print colored message
32+
function Write-ColorMessage {
33+
param(
34+
[string]$Color,
35+
[string]$Message
36+
)
37+
Write-Host "$Color$Message$NC"
38+
}
39+
40+
# Print error and exit
41+
function Die {
42+
param([string]$Message)
43+
Write-ColorMessage -Color $RED -Message "Error: $Message"
44+
exit 1
45+
}
46+
47+
# Check if a command exists
48+
function Test-CommandExists {
49+
param([string]$Command)
50+
return [bool](Get-Command -Name $Command -ErrorAction SilentlyContinue)
51+
}
52+
53+
# Get the latest version from GitHub API
54+
function Get-LatestVersion {
55+
try {
56+
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/aptos-labs/aptos-core/releases?per_page=100"
57+
$latestRelease = $response | Where-Object { $_.tag_name -match 'aptos-cli-v' } | Select-Object -First 1
58+
return $latestRelease.tag_name -replace 'aptos-cli-v', ''
59+
}
60+
catch {
61+
Die "Failed to get latest version: $_"
62+
}
63+
}
64+
65+
# Determine the target platform
66+
function Get-Target {
67+
$arch = (Get-WmiObject -Class Win32_Processor).Architecture
68+
switch ($arch) {
69+
0 { return "Windows-x86_64" } # x86
70+
9 { return "Windows-x86_64" } # x64
71+
default { Die "Unsupported architecture: $arch" }
72+
}
73+
}
74+
75+
# Download and install the CLI
76+
function Install-CLI {
77+
param(
78+
[string]$Version,
79+
[string]$Target
80+
)
81+
82+
Write-ColorMessage -Color $CYAN -Message "Downloading Aptos CLI version $Version for $Target..."
83+
84+
# Create bin directory if it doesn't exist
85+
if (-not (Test-Path $BIN_DIR)) {
86+
New-Item -ItemType Directory -Path $BIN_DIR -Force | Out-Null
87+
}
88+
89+
# Download URL
90+
$url = "https://github.com/aptos-labs/aptos-core/releases/download/aptos-cli-v$Version/aptos-cli-$Version-$Target.zip"
91+
$zipPath = Join-Path $env:TEMP "aptos-cli.zip"
92+
93+
try {
94+
# Download the file
95+
# Check if curl is installed
96+
if (-not (Test-CommandExists "curl.exe")) {
97+
Invoke-WebRequest -Uri $url -OutFile $zipPath
98+
} else {
99+
# Use curl to download the file
100+
curl.exe -L $url -o $zipPath
101+
}
102+
103+
# Extract the zip file
104+
Expand-Archive -Path $zipPath -DestinationPath $BIN_DIR -Force
105+
106+
# Clean up
107+
Remove-Item $zipPath -Force
108+
}
109+
catch {
110+
Die "Failed to download or extract CLI: $_"
111+
}
112+
113+
Write-ColorMessage -Color $GREEN -Message "Aptos CLI installed successfully!"
114+
}
115+
116+
# Main installation process
117+
function Main {
118+
# Parse command line arguments
119+
for ($i = 0; $i -lt $args.Count; $i++) {
120+
switch ($args[$i]) {
121+
'-f' { $FORCE = $true }
122+
'--force' { $FORCE = $true }
123+
'-y' { $ACCEPT_ALL = $true }
124+
'--yes' { $ACCEPT_ALL = $true }
125+
'--bin-dir' {
126+
if ($i + 1 -lt $args.Count) {
127+
$BIN_DIR = $args[$i + 1]
128+
$i++
129+
}
130+
else {
131+
Die "No directory specified for --bin-dir"
132+
}
133+
}
134+
'--cli-version' {
135+
if ($i + 1 -lt $args.Count) {
136+
$VERSION = $args[$i + 1]
137+
$i++
138+
}
139+
else {
140+
Die "No version specified for --cli-version"
141+
}
142+
}
143+
default {
144+
Die "Unknown option: $($args[$i])"
145+
}
146+
}
147+
}
148+
149+
# Get version if not specified
150+
if (-not $VERSION) {
151+
$VERSION = Get-LatestVersion
152+
}
153+
154+
# Get target platform
155+
$target = Get-Target
156+
157+
# Check if CLI is already installed
158+
$cliPath = Join-Path $BIN_DIR $SCRIPT
159+
if ((Test-Path $cliPath) -and (-not $FORCE)) {
160+
$currentVersion = (& $cliPath --version | Select-String -Pattern '\d+\.\d+\.\d+').Matches.Value
161+
if ($currentVersion -eq $VERSION) {
162+
Write-ColorMessage -Color $YELLOW -Message "Aptos CLI version $VERSION is already installed."
163+
exit 0
164+
}
165+
}
166+
167+
# Install the CLI
168+
Install-CLI -Version $VERSION -Target $target
169+
170+
# Add to PATH if not already there
171+
$currentPath = [Environment]::GetEnvironmentVariable('Path', 'User')
172+
if (-not $currentPath.Contains($BIN_DIR)) {
173+
Write-ColorMessage -Color $YELLOW -Message "Adding $BIN_DIR to PATH..."
174+
[Environment]::SetEnvironmentVariable('Path', "$currentPath;$BIN_DIR", 'User')
175+
Write-ColorMessage -Color $GREEN -Message "Please restart your terminal to update your PATH."
176+
}
177+
178+
# Test the installation
179+
Write-ColorMessage -Color $CYAN -Message "Testing the installation..."
180+
if (& $cliPath --version) {
181+
Write-ColorMessage -Color $GREEN -Message "Aptos CLI is working correctly!"
182+
}
183+
else {
184+
Write-ColorMessage -Color $RED -Message "There was a problem with the installation."
185+
exit 1
186+
}
187+
}
188+
189+
# Run the main function
190+
Main $args

0 commit comments

Comments
 (0)