-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.ps1
106 lines (92 loc) · 3.46 KB
/
install.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
param (
[string]$version
)
# Function to get latest version
function Get-LatestVersion {
param (
[string]$packageName
)
$apiUrl = "https://api.hyphen.ai/api/downloads/${packageName}/versions?latest=true"
try {
$response = Invoke-RestMethod -Uri $apiUrl -UseBasicParsing
$version = $response.data | Where-Object { $_.latest -eq $true } | Select-Object -ExpandProperty version
if (-not $version) {
Write-Error "Failed to get latest version"
exit 1
}
return $version
} catch {
Write-Error "Failed to get latest version"
exit 1
}
}
# Function to create alias
function Create-Alias {
param (
[string]$aliasCommand
)
$escapedAliasCommand = [Regex]::Escape($aliasCommand)
$profilePath = $PROFILE
if (-not (Test-Path -Path $profilePath)) {
New-Item -ItemType File -Path $profilePath -Force
}
if (-not (Get-Content $profilePath | Select-String -Pattern $escapedAliasCommand)) {
Add-Content -Path $profilePath -Value "`n$aliasCommand"
Write-Output "Alias added. Please restart your PowerShell session to apply changes."
} else {
Write-Output "Alias already exists in $profilePath"
}
}
# Function to add directory to PATH
function Add-ToPath {
param (
[string]$dirPath
)
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($userPath -notlike "*$dirPath*") {
[Environment]::SetEnvironmentVariable("PATH", "$userPath;$dirPath", "User")
$env:PATH += ";$dirPath"
Write-Output "Added $dirPath to PATH. Please restart your PowerShell session to apply changes."
} else {
Write-Output "$dirPath is already in PATH"
}
}
# Main installation function
function Install-CLI {
param (
[string]$version
)
$packageName = "hyphen-cli"
$os = "windows" # Hardcoded to windows
if (-not $version) {
$version = Get-LatestVersion -packageName $packageName
}
$downloadUrl = "https://api.hyphen.ai/api/downloads/${packageName}/${version}?os=${os}"
$tempDir = New-Item -Type Directory -Path (Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString()))
$binaryName = "hyphen.exe"
$tempFilePath = Join-Path $tempDir $binaryName
Write-Output "Downloading ${packageName} version ${version} for ${os}..."
try {
Invoke-WebRequest -Uri $downloadUrl -OutFile $tempFilePath -UseBasicParsing -ErrorAction Stop
} catch {
Write-Error "Failed to download the binary from $downloadUrl. The specified version may not exist."
exit 1
}
# Create installation directory in user's home
$installDir = Join-Path $env:USERPROFILE ".hyphen"
if (-not (Test-Path $installDir)) {
New-Item -ItemType Directory -Path $installDir | Out-Null
}
$installPath = Join-Path $installDir $binaryName
Write-Output "Installing ${binaryName} to $installPath..."
Move-Item -Path $tempFilePath -Destination $installPath -Force
Write-Output "${binaryName} has been successfully installed!"
# Add installation directory to PATH
Add-ToPath -dirPath $installDir
# Add the alias
$aliasCommandHx = "Set-Alias -Name hx -Value `"$installPath`""
Create-Alias -aliasCommand $aliasCommandHx
Write-Output "Please restart your PowerShell session to apply changes to PATH and aliases. Then you can run 'hx' or 'hyphen' to use the CLI."
}
# Run the installation
Install-CLI -version $version