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

feat(install): Add PIXI_NO_PATH_UPDATE for PATH update suppression #692

Merged
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
107 changes: 96 additions & 11 deletions install/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,91 @@
Specifies Pixi's home directory.
The default value is '$Env:USERPROFILE\.pixi'. You can also specify it by
setting the environment variable 'PIXI_HOME'.
.PARAMETER NoPathUpdate
If specified, the script will not update the PATH environment variable.
.LINK
https://pixi.sh
.LINK
https://github.com/prefix-dev/pixi
#>
param (
[string] $PixiVersion = 'latest',
[string] $PixiHome = "$Env:USERPROFILE\.pixi"
[string] $PixiHome = "$Env:USERPROFILE\.pixi",
[switch] $NoPathUpdate
)

Set-StrictMode -Version Latest

function Publish-Env {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool wasn't aware you could do this!

if (-not ("Win32.NativeMethods" -as [Type])) {
Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
}

$HWND_BROADCAST = [IntPtr] 0xffff
$WM_SETTINGCHANGE = 0x1a
$result = [UIntPtr]::Zero

[Win32.Nativemethods]::SendMessageTimeout($HWND_BROADCAST,
$WM_SETTINGCHANGE,
[UIntPtr]::Zero,
"Environment",
2,
5000,
[ref] $result
) | Out-Null
}

function Write-Env {
param(
[String] $name,
[String] $val,
[Switch] $global
)

$RegisterKey = if ($global) {
Get-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
} else {
Get-Item -Path 'HKCU:'
}

$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment', $true)
if ($null -eq $val) {
$EnvRegisterKey.DeleteValue($name)
} else {
$RegistryValueKind = if ($val.Contains('%')) {
[Microsoft.Win32.RegistryValueKind]::ExpandString
} elseif ($EnvRegisterKey.GetValue($name)) {
$EnvRegisterKey.GetValueKind($name)
} else {
[Microsoft.Win32.RegistryValueKind]::String
}
$EnvRegisterKey.SetValue($name, $val, $RegistryValueKind)
}
Publish-Env
}

function Get-Env {
param(
[String] $name,
[Switch] $global
)

$RegisterKey = if ($global) {
Get-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
} else {
Get-Item -Path 'HKCU:'
}

$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment')
$RegistryValueOption = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
$EnvRegisterKey.GetValue($name, $null, $RegistryValueOption)
}

if ($Env:PIXI_VERSION) {
$PixiVersion = $Env:PIXI_VERSION
}
Expand All @@ -31,6 +104,10 @@ if ($Env:PIXI_HOME) {
$PixiHome = $Env:PIXI_HOME
}

if ($Env:PIXI_NO_PATH_UPDATE) {
$NoPathUpdate = $true
}

# Repository name
$REPO = 'prefix-dev/pixi'
$ARCH = 'x86_64'
Expand All @@ -57,26 +134,34 @@ try {

# Create the install dir if it doesn't exist
if (!(Test-Path -Path $BinDir)) {
New-Item -ItemType directory -Path $BinDir
New-Item -ItemType Directory -Path $BinDir | Out-Null
}

$ZIP_FILE = $TEMP_FILE + ".zip"
Rename-Item -Path $TEMP_FILE -NewName $ZIP_FILE

# Extract pixi from the downloaded zip file
Expand-Archive -Path $ZIP_FILE -DestinationPath $BinDir -Force

# Add pixi to PATH if the folder is not already in the PATH variable
$PATH = [Environment]::GetEnvironmentVariable("Path", "User")
if ($PATH -notlike "*$BinDir*") {
Write-Output "Adding $BinDir to PATH`n"
[Environment]::SetEnvironmentVariable("Path", "$BinDir;" + [Environment]::GetEnvironmentVariable("Path", "User"), "User")
} else {
Write-Output "$BinDir is already in PATH`n"
}
} catch {
Write-Host "Error: '$DOWNLOAD_URL' is not available or failed to download"
exit 1
} finally {
Remove-Item -Path $ZIP_FILE
}

# Add pixi to PATH if the folder is not already in the PATH variable
if (!$NoPathUpdate) {
$PATH = Get-Env 'PATH'
if ($PATH -notlike "*$BinDir*") {
Write-Output "Adding $BinDir to PATH"
# For future sessions
Write-Env -name 'PATH' -val "$BinDir;$PATH"
# For current session
$Env:PATH = "$BinDir;$PATH"
Write-Output "You may need to restart your shell"
} else {
Write-Output "$BinDir is already in PATH"
}
} else {
Write-Output "You may need to update your PATH manually to use pixi"
}
3 changes: 3 additions & 0 deletions install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ update_shell() {
FILE=$1
LINE=$2

# shell update can be suppressed by `PIXI_NO_PATH_UPDATE` env var
[[ ! -z "$PIXI_NO_PATH_UPDATE" ]] && return

# Create the file if it doesn't exist
if [ -f "$FILE" ]; then
touch "$FILE"
Expand Down
Loading