2022-02-27 10:42:54 -08:00
|
|
|
function Get-CachePath {
|
|
|
|
[CmdletBinding()]
|
|
|
|
param (
|
|
|
|
[Parameter()]
|
|
|
|
[string]
|
|
|
|
$Path
|
|
|
|
)
|
|
|
|
|
|
|
|
if (-Not (Test-Path -Path $Path)) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
$child = "oh-my-posh"
|
|
|
|
$cachePath = Join-Path -Path $Path -ChildPath $child
|
2022-02-28 03:51:32 -08:00
|
|
|
if (Test-Path -Path $cachePath) {
|
2022-02-27 10:42:54 -08:00
|
|
|
return $cachePath
|
|
|
|
}
|
2022-03-02 22:54:45 -08:00
|
|
|
$null = New-Item -Path $Path -Name $child -ItemType Directory
|
2022-02-27 10:42:54 -08:00
|
|
|
return $cachePath
|
|
|
|
}
|
|
|
|
|
2022-01-14 13:01:16 -08:00
|
|
|
function Set-PoshRootPath {
|
2022-02-27 10:42:54 -08:00
|
|
|
$path = Get-CachePath -Path $env:LOCALAPPDATA
|
|
|
|
if ($path) {
|
|
|
|
$env:POSH_PATH = $path
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$path = Get-CachePath -Path $env:XDG_CACHE_HOME
|
|
|
|
if ($path) {
|
|
|
|
$env:POSH_PATH = $path
|
|
|
|
return
|
|
|
|
}
|
|
|
|
$homeCache = Join-Path -Path $env:HOME -ChildPath ".cache"
|
|
|
|
$path = Get-CachePath -Path $homeCache
|
|
|
|
if ($path) {
|
|
|
|
$env:POSH_PATH = $path
|
2022-01-14 13:01:16 -08:00
|
|
|
return
|
|
|
|
}
|
2022-02-27 10:42:54 -08:00
|
|
|
$env:POSH_PATH = $env:HOME
|
2021-12-27 04:23:06 -08:00
|
|
|
}
|
|
|
|
|
2021-11-22 02:43:11 -08:00
|
|
|
function Get-PoshDownloadUrl {
|
|
|
|
param(
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[string]
|
|
|
|
$Version
|
|
|
|
)
|
|
|
|
|
|
|
|
$executable = ""
|
2020-09-11 23:42:26 -07:00
|
|
|
if ($IsMacOS) {
|
2021-11-22 02:43:11 -08:00
|
|
|
$executable = "posh-darwin-amd64"
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
2021-11-22 02:43:11 -08:00
|
|
|
elseif ($IsLinux) {
|
2021-03-11 10:14:41 -08:00
|
|
|
# this is rather hacky but there's no other way for the time being
|
|
|
|
$arch = uname -m
|
2021-09-14 13:30:42 -07:00
|
|
|
if ($arch -eq 'aarch64') {
|
2021-11-22 02:43:11 -08:00
|
|
|
$executable = "posh-linux-arm64"
|
|
|
|
}
|
|
|
|
elseif ($arch -eq 'armv7l') {
|
|
|
|
$executable = "posh-linux-arm"
|
2021-03-11 10:14:41 -08:00
|
|
|
}
|
2021-11-22 02:43:11 -08:00
|
|
|
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
|
2021-09-14 13:30:42 -07:00
|
|
|
}
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
2021-11-22 02:43:11 -08:00
|
|
|
if ($executable -eq "") {
|
|
|
|
throw "oh-my-posh: Unsupported architecture: $arch"
|
2021-03-03 10:12:29 -08:00
|
|
|
}
|
2021-11-22 02:43:11 -08:00
|
|
|
return "https://github.com/jandedobbeleer/oh-my-posh/releases/download/v$Version/$executable"
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
|
|
|
|
2021-11-22 02:43:11 -08:00
|
|
|
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."
|
|
|
|
}
|
2021-01-20 16:13:27 -08:00
|
|
|
# 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
|
|
|
|
}
|
2021-11-22 02:43:11 -08:00
|
|
|
chmod a+x $executable 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
function Get-PoshCommand {
|
|
|
|
if ($PSVersionTable.PSEdition -ne "Core" -or $IsWindows) {
|
2022-01-14 13:01:16 -08:00
|
|
|
return Join-Path -Path $env:POSH_PATH -ChildPath "oh-my-posh.exe"
|
2021-11-22 02:43:11 -08:00
|
|
|
}
|
2022-01-14 13:01:16 -08:00
|
|
|
return Join-Path -Path $env:POSH_PATH -ChildPath "oh-my-posh"
|
2021-11-22 02:43:11 -08:00
|
|
|
}
|
2021-01-20 16:13:27 -08:00
|
|
|
|
2021-11-28 05:29:33 -08:00
|
|
|
function Sync-PoshThemes {
|
|
|
|
param(
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[string]
|
|
|
|
$Version
|
|
|
|
)
|
|
|
|
|
|
|
|
Write-Host "Downloading oh-my-posh themes for $Version"
|
2021-11-29 21:21:06 -08:00
|
|
|
$tmp = New-TemporaryFile | Rename-Item -NewName { $_ -replace 'tmp$', 'zip' } -PassThru
|
2021-11-28 05:29:33 -08:00
|
|
|
$themesUrl = "https://github.com/jandedobbeleer/oh-my-posh/releases/download/v$Version/themes.zip"
|
|
|
|
Invoke-WebRequest -OutFile $tmp $themesUrl
|
|
|
|
$destination = $env:POSH_THEMES_PATH
|
2022-02-13 04:42:10 -08:00
|
|
|
$tmp | Microsoft.PowerShell.Archive\Expand-Archive -DestinationPath $destination -Force
|
2021-11-28 05:29:33 -08:00
|
|
|
$tmp | Remove-Item
|
|
|
|
}
|
|
|
|
|
|
|
|
function Sync-PoshArtifacts {
|
|
|
|
param(
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[string]
|
|
|
|
$Version
|
|
|
|
)
|
|
|
|
|
2020-09-11 23:42:26 -07:00
|
|
|
$executable = Get-PoshCommand
|
2021-11-22 02:43:11 -08:00
|
|
|
if (-not (Test-Path $executable)) {
|
2021-11-28 05:29:33 -08:00
|
|
|
Write-Host "Downloading oh-my-posh executable for $Version"
|
|
|
|
$url = Get-PoshDownloadUrl -Version $Version
|
2021-11-22 02:43:11 -08:00
|
|
|
Get-PoshExecutable -Url $url -Destination $executable
|
2021-11-28 05:29:33 -08:00
|
|
|
Sync-PoshThemes -Version $Version
|
2021-01-20 16:13:27 -08:00
|
|
|
return
|
|
|
|
}
|
2021-11-22 02:43:11 -08:00
|
|
|
$poshVersion = & $executable --version
|
2022-02-03 02:28:47 -08:00
|
|
|
if ($poshVersion -eq "development") {
|
|
|
|
Write-Warning "omp development version detected"
|
|
|
|
return
|
|
|
|
}
|
2021-11-28 05:29:33 -08:00
|
|
|
if ($poshVersion -eq $Version) {
|
2021-11-22 02:43:11 -08:00
|
|
|
return
|
|
|
|
}
|
2021-11-28 05:29:33 -08:00
|
|
|
Write-Host "Updating oh-my-posh executable to $Version"
|
|
|
|
$url = Get-PoshDownloadUrl -Version $Version
|
2021-11-22 02:43:11 -08:00
|
|
|
Get-PoshExecutable -Url $url -Destination $executable
|
2021-11-28 05:29:33 -08:00
|
|
|
Sync-PoshThemes -Version $Version
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
|
|
|
|
2022-01-14 13:01:16 -08:00
|
|
|
Set-PoshRootPath
|
|
|
|
$env:PATH = $env:POSH_PATH + [System.IO.Path]::PathSeparator + $env:PATH
|
|
|
|
$env:POSH_THEMES_PATH = Join-Path -Path $env:POSH_PATH -ChildPath "themes"
|
|
|
|
$moduleVersion = Split-Path -Leaf $MyInvocation.MyCommand.ScriptBlock.Module.ModuleBase
|
|
|
|
Sync-PoshArtifacts -Version $moduleVersion
|
2021-11-22 02:43:11 -08:00
|
|
|
|
2021-11-27 11:35:40 -08:00
|
|
|
# Legacy functions
|
|
|
|
|
2020-09-11 23:42:26 -07:00
|
|
|
function Set-PoshPrompt {
|
|
|
|
param(
|
2020-09-19 23:53:14 -07:00
|
|
|
[Parameter(Mandatory = $false)]
|
2020-09-11 23:42:26 -07:00
|
|
|
[string]
|
2020-12-27 08:53:58 -08:00
|
|
|
$Theme
|
2020-09-11 23:42:26 -07:00
|
|
|
)
|
|
|
|
|
2021-01-17 04:13:00 -08:00
|
|
|
$config = ""
|
2021-11-27 11:35:40 -08:00
|
|
|
if (Test-Path "$($env:POSH_THEMES_PATH)/$Theme.omp.json") {
|
|
|
|
$path = "$($env:POSH_THEMES_PATH)/$Theme.omp.json"
|
2021-05-20 23:23:12 -07:00
|
|
|
$config = (Resolve-Path -Path $path).ProviderPath
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
|
|
|
elseif (Test-Path $Theme) {
|
2021-05-20 23:23:12 -07:00
|
|
|
$config = (Resolve-Path -Path $Theme).ProviderPath
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
2020-09-20 10:39:25 -07:00
|
|
|
else {
|
2021-11-27 11:35:40 -08:00
|
|
|
$config = "$($env:POSH_THEMES_PATH)/jandedobbeleer.omp.json"
|
2020-09-20 10:39:25 -07:00
|
|
|
}
|
2020-09-11 23:42:26 -07:00
|
|
|
|
2021-04-13 15:15:32 -07:00
|
|
|
# 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
|
|
|
|
|
2021-01-17 04:13:00 -08:00
|
|
|
$poshCommand = Get-PoshCommand
|
2022-03-12 13:04:08 -08:00
|
|
|
(& $poshCommand prompt init pwsh --config="$config") | Invoke-Expression
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|