|
| 1 | +#!/usr/bin/env pwsh |
| 2 | + |
| 3 | +<# only supports windows, |
| 4 | +# `SUPPORTED_OS` array contains the supported linux and darwin for filtering only |
| 5 | +# `SUPPORTED_ARCH` array contains the supported architectures but only x86_64/amd64 are |
| 6 | +# available for now. both amd64 and x86_64 are there cause windows does report either |
| 7 | +# of them when querying for the architecture. |
| 8 | +#> |
| 9 | +[System.String[]]$SUPPORTED_ARCH = @("x86_64", "arm64", "amd64") |
| 10 | +[System.String[]]$SUPPORTED_OS = @("linux", "darwin", "win32nt") |
| 11 | + |
| 12 | +# Associate binaries with CPU architectures and operating systems |
| 13 | +[System.Collections.Hashtable]$BINARIES = @{ |
| 14 | + "x86_64-linux" = "Parseable_x86_64-unknown-linux-gnu" |
| 15 | + "arm64-linux" = "Parseable_aarch64-unknown-linux-gnu" |
| 16 | + "x86_64-darwin" = "Parseable_x86_64-apple-darwin" |
| 17 | + "arm64-darwin" = "Parseable_aarch64-apple-darwin" |
| 18 | + "amd64-win32nt" = "Parseable_x86_64-pc-windows-msvc.exe" |
| 19 | +} |
| 20 | + |
| 21 | +# util functions |
| 22 | +function Get-Env { |
| 23 | + param([String] $Key) |
| 24 | + |
| 25 | + $RegisterKey = Get-Item -Path 'HKCU:' |
| 26 | + $EnvRegisterKey = $RegisterKey.OpenSubKey('Environment') |
| 27 | + $EnvRegisterKey.GetValue($Key, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) |
| 28 | +} |
| 29 | + |
| 30 | +# These three environment functions are roughly copied from https://github.com/prefix-dev/pixi/pull/692 |
| 31 | +# They are used instead of `SetEnvironmentVariable` because of unwanted variable expansions. |
| 32 | +function Publish-Env { |
| 33 | + if (-not ("Win32.NativeMethods" -as [Type])) { |
| 34 | + <# dllimport should not be needed but still#> |
| 35 | + Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @" |
| 36 | +[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] |
| 37 | +public static extern IntPtr SendMessageTimeout( |
| 38 | + IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam, |
| 39 | + uint fuFlags, uint uTimeout, out UIntPtr lpdwResult); |
| 40 | +"@ |
| 41 | + } |
| 42 | + $HWND_BROADCAST = [IntPtr] 0xffff |
| 43 | + $WM_SETTINGCHANGE = 0x1a |
| 44 | + $result = [UIntPtr]::Zero |
| 45 | + [Win32.NativeMethods]::SendMessageTimeout($HWND_BROADCAST, |
| 46 | + $WM_SETTINGCHANGE, |
| 47 | + [UIntPtr]::Zero, |
| 48 | + "Environment", |
| 49 | + 2, |
| 50 | + 5000, |
| 51 | + [ref] $result |
| 52 | + ) | Out-Null |
| 53 | +} |
| 54 | +function Write-Env { |
| 55 | + param([String]$Key, [String]$Value) |
| 56 | + |
| 57 | + [Microsoft.Win32.RegistryKey]$RegisterKey = Get-Item -Path 'HKCU:' |
| 58 | + |
| 59 | + |
| 60 | + [Microsoft.Win32.RegistryKey]$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment', $true) |
| 61 | + if ($null -eq $Value) { |
| 62 | + $EnvRegisterKey.DeleteValue($Key) |
| 63 | + } |
| 64 | + else { |
| 65 | + $RegistryValueKind = if ($Value.Contains('%')) { |
| 66 | + [Microsoft.Win32.RegistryValueKind]::ExpandString |
| 67 | + } |
| 68 | + elseif ($EnvRegisterKey.GetValue($Key)) { |
| 69 | + $EnvRegisterKey.GetValueKind($Key) |
| 70 | + } |
| 71 | + else { |
| 72 | + [Microsoft.Win32.RegistryValueKind]::String |
| 73 | + } |
| 74 | + $EnvRegisterKey.SetValue($Key, $Value, $RegistryValueKind) |
| 75 | + } |
| 76 | + |
| 77 | + Publish-Env |
| 78 | +} |
| 79 | + |
| 80 | +function Get-Env { |
| 81 | + param([String] $Key) |
| 82 | + |
| 83 | + [Microsoft.Win32.RegistryKey]$RegisterKey = Get-Item -Path 'HKCU:' |
| 84 | + [Microsoft.Win32.RegistryKey]$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment') |
| 85 | + $EnvRegisterKey.GetValue($Key, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) |
| 86 | +} |
| 87 | + |
| 88 | +# Get the system's CPU architecture and operating system |
| 89 | +[String]$CPU_ARCH = [System.Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE").ToLower() |
| 90 | +[String]$OS = [System.Environment]::OSVersion.Platform.ToString().ToLower() |
| 91 | +[String]$INSTALLDIR = "${HOME}\.parseable\bin" |
| 92 | +[String]$BIN = "${INSTALLDIR}\parseable.exe" |
| 93 | + |
| 94 | +function Install-Parseable { |
| 95 | + Write-Output "OS: $OS" |
| 96 | + Write-Output "CPU arch: $CPU_ARCH" |
| 97 | + |
| 98 | + # Check if the CPU architecture is supported |
| 99 | + if ($SUPPORTED_ARCH -notcontains $CPU_ARCH) { |
| 100 | + Write-Error "Unsupported CPU architecture ($CPU_ARCH)." |
| 101 | + exit 1 |
| 102 | + } |
| 103 | + # Check if the OS is supported |
| 104 | + if ($SUPPORTED_OS -notcontains $OS) { |
| 105 | + Write-Error "Unsupported operating system ($OS)." |
| 106 | + exit 1 |
| 107 | + } |
| 108 | + |
| 109 | + Write-Output "Checking for existing installation..." |
| 110 | + if (Test-Path $BIN) { |
| 111 | + Write-Error "Parseable is already installed. Run 'parseable --version' to check the version." |
| 112 | + Write-Error "Consider removing the existing installation" |
| 113 | + exit 1 |
| 114 | + } |
| 115 | + |
| 116 | + Write-Output "No existing installation found" |
| 117 | + |
| 118 | + Write-Output "Fetching latest release..." |
| 119 | + # Get the latest release information using GitHub API |
| 120 | + $release = Invoke-RestMethod -Uri "https://api.github.com/repos/parseablehq/parseable/releases/latest" |
| 121 | + # Loop through binaries in the release and find the appropriate one |
| 122 | + foreach ($arch_os in "$CPU_ARCH-$OS") { |
| 123 | + $binary_name = $BINARIES[$arch_os] |
| 124 | + $download_url = ($release.assets | Where-Object { $_.name -like "*$binary_name*" }).browser_download_url |
| 125 | + if ($download_url) { |
| 126 | + break |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + mkdir -Force $INSTALLDIR |
| 131 | + |
| 132 | + Write-Output "Downloading Parseable Server..." |
| 133 | + # Download the binary using Invoke-WebRequest |
| 134 | + Invoke-WebRequest -Uri $download_url -OutFile $BIN |
| 135 | + |
| 136 | + # Make the binary executable (for Unix-like systems) |
| 137 | + if ($OS -eq "linux" -or $OS -eq "darwin") { |
| 138 | + Set-ItemProperty -Path $BIN -Name IsReadOnly -Value $false |
| 139 | + Set-ItemProperty -Path $BIN -Name IsExecutable -Value $true |
| 140 | + } |
| 141 | + |
| 142 | + Write-Output "Adding Parseable to PATH..." |
| 143 | + # Only try adding to path if there isn't already a bun.exe in the path |
| 144 | + $Path = (Get-Env -Key "Path") -split ';' |
| 145 | + if ($Path -notcontains $INSTALLDIR) { |
| 146 | + $Path += $INSTALLDIR |
| 147 | + Write-Env -Key 'Path' -Value ($Path -join ';') |
| 148 | + $env:PATH = $Path; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +Install-Parseable |
| 153 | + |
| 154 | +Write-Output "Parseable was downloaded successfully! at $INSTALLDIR" |
| 155 | +Write-Output "To get started, restart your terminal/editor, then type `"parseable`"`n" |
0 commit comments