Created
September 17, 2018 19:53
-
-
Save rjmholt/3b9916346af42d260eeeb34512ff5be7 to your computer and use it in GitHub Desktop.
Helper functions deleted from a PSES PR that might come in handy later
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Enable-WritingToPath { | |
param([string]$Path) | |
# This is not a problem on Windows | |
if (-not ($IsMacOS -or $IsLinux)) { | |
return | |
} | |
Write-Verbose "Setting permissions on path: $Path" | |
sudo chmod o+w $Path | |
$permissions = if ($IsLinux) { | |
stat -c "%a" $Path | |
} else { | |
stat -f "%A" $Path | |
} | |
Write-Verbose "Permissions set to $permissions" | |
} | |
function Invoke-WithPSCoreModulePath { | |
param( | |
[string]$NewModulePath, | |
[scriptblock]$ScriptBlock | |
) | |
$configBase = if ($IsLinux -or $IsMacOS) { | |
"$HOME/.config/powershell" | |
} else { | |
"$HOME\Documents\PowerShell" | |
} | |
$configPath = Join-Path $configBase 'powershell.config.json' | |
if (-not (Test-Path $configBase)) { | |
New-Item -ItemType Directory -Path $configPath | |
} elseif (Test-Path $configPath) { | |
$configBackupPath = Join-Path ([System.IO.Path]::GetTempPath()) 'powershell.config.backup.json' | |
Copy-Item -Path $configPath -Destination $configBackupPath -Force | |
} | |
try { | |
Enable-WritingToPath $configPath | |
$escapedPath = $NewModulePath -replace '\\', '\\' | |
New-Item -Path $configPath -Value "{ `"PSModulePath`": `"$escapedPath`" }" -Force | |
& $ScriptBlock | |
} finally { | |
Remove-Item -Path $configPath | |
if ($configBackupPath) { | |
Move-Item -Path $configBackupPath -Destination $configPath -Force | |
} | |
} | |
} | |
function Get-PSCoreModules { | |
param( | |
[ValidateNotNullOrEmpty()][string]$PSVersion = '6.1.0', | |
[string]$DestinationPath | |
) | |
$tmpPath = [System.IO.Path]::GetTempPath() | |
$relativeModulePath = 'Modules/' | |
if (-not $DestinationPath) { | |
$DestinationPath = Join-Path $tmpPath 'PSCoreModules' | |
} | |
if (Test-Path $DestinationPath) { | |
return Join-Path $DestinationPath $relativeModulePath | |
} | |
if ($IsMacOS) { | |
$os = 'osx' | |
$ext = 'tar.gz' | |
} elseif ($IsLinux) { | |
$os = 'linux' | |
$ext = 'tar.gz' | |
} else { | |
$os = 'win' | |
$ext = 'zip' | |
} | |
$uri = "https://github.com/PowerShell/PowerShell/releases/download/v$PSVersion/powershell-$PSVersion-$os-x64.$ext" | |
$downloadPath = Join-Path $tmpPath "pscoremodules.$ext" | |
if (-not (Test-Path $downloadPath)) { | |
Invoke-WebRequest -Uri $uri -OutFile $downloadPath | |
} | |
if ($IsMacOS -or $IsLinux) { | |
New-Item -ItemType Directory -Path $DestinationPath | |
tar xvf $downloadPath -C $DestinationPath | |
} else { | |
Expand-Archive -Path $downloadPath -DestinationPath $DestinationPath | |
} | |
return Join-Path $DestinationPath $relativeModulePath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment