oh-my-posh/packages/powershell/oh-my-posh/oh-my-posh.psm1
2021-11-28 15:24:48 +01:00

127 lines
4 KiB
PowerShell

$env:POSH_THEMES_PATH = "$PSScriptRoot/themes"
$env:PATH = "$((Get-Item $MyInvocation.MyCommand.ScriptBlock.Module.ModuleBase).Parent.FullName);$env:PATH"
function Get-PoshDownloadUrl {
param(
[Parameter(Mandatory = $true)]
[string]
$Version
)
$executable = ""
if ($IsMacOS) {
$executable = "posh-darwin-amd64"
}
elseif ($IsLinux) {
# this is rather hacky but there's no other way for the time being
$arch = uname -m
if ($arch -eq 'aarch64') {
$executable = "posh-linux-arm64"
}
elseif ($arch -eq 'armv7l') {
$executable = "posh-linux-arm"
}
else {
$executable = "posh-linux-amd64"
}
}
else {
$arch = (Get-CimInstance -Class Win32_Processor -Property Architecture).Architecture
switch ($arch) {
0 { $executable = "posh-windows-386.exe" } # x86
5 { $executable = "posh-windows-arm64.exe" } # ARM
9 { $executable = "posh-windows-amd64.exe" } # x64
12 { $executable = "posh-windows-amd64.exe" } # x64 emulated on Surface Pro X
}
}
if ($executable -eq "") {
throw "oh-my-posh: Unsupported architecture: $arch"
}
return "https://github.com/jandedobbeleer/oh-my-posh/releases/download/v$Version/$executable"
}
function Get-PoshExecutable {
param(
[Parameter(Mandatory = $true)]
[string]
$Url,
[Parameter(Mandatory = $true)]
[string]
$Destination
)
Invoke-WebRequest $Url -Out $Destination
if (-Not (Test-Path $executable)) {
# This should only happen with a corrupt installation
throw "Executable at $executable was not found, please try importing oh-my-posh again."
}
# Set the right binary to executable before doing anything else
# Permissions don't need to be set on Windows
if ($PSVersionTable.PSEdition -ne "Core" -or $IsWindows) {
return
}
chmod a+x $executable 2>&1
}
function Get-PoshCommand {
$extension = ""
if ($PSVersionTable.PSEdition -ne "Core" -or $IsWindows) {
$extension = ".exe"
}
return "$((Get-Item $MyInvocation.MyCommand.ScriptBlock.Module.ModuleBase).Parent.FullName)/oh-my-posh$extension"
}
function Sync-PoshExecutable {
$executable = Get-PoshCommand
$moduleVersion = Split-Path -Leaf $MyInvocation.MyCommand.ScriptBlock.Module.ModuleBase
if (-not (Test-Path $executable)) {
Write-Host "Downloading oh-my-posh executable"
$url = Get-PoshDownloadUrl -Version $moduleVersion
Get-PoshExecutable -Url $url -Destination $executable
return
}
$poshVersion = & $executable --version
if ($poshVersion -eq $moduleVersion) {
return
}
Write-Host "Updating oh-my-posh executable to $moduleVersion"
$url = Get-PoshDownloadUrl -Version $moduleVersion
Get-PoshExecutable -Url $url -Destination $executable
}
Sync-PoshExecutable
# Legacy functions
function Set-PoshPrompt {
param(
[Parameter(Mandatory = $false)]
[string]
$Theme
)
$config = ""
if (Test-Path "$($env:POSH_THEMES_PATH)/$Theme.omp.json") {
$path = "$($env:POSH_THEMES_PATH)/$Theme.omp.json"
$config = (Resolve-Path -Path $path).ProviderPath
}
elseif (Test-Path $Theme) {
$config = (Resolve-Path -Path $Theme).ProviderPath
}
else {
$config = "$($env:POSH_THEMES_PATH)/jandedobbeleer.omp.json"
}
# Workaround for get-location/push-location/pop-location from within a module
# https://github.com/PowerShell/PowerShell/issues/12868
# https://github.com/JanDeDobbeleer/oh-my-posh2/issues/113
$global:omp_global_sessionstate = $PSCmdlet.SessionState
$poshCommand = Get-PoshCommand
(& $poshCommand --init --shell=pwsh --config="$config") | Invoke-Expression
}
function Get-PoshThemes() {
Write-Host 'Get-PoshThemes is deprecated, please have a look at all the themes in the documentation: https://ohmyposh.dev/docs/themes'
}