diff --git a/docs/docs/install-pwsh.mdx b/docs/docs/install-pwsh.mdx index fc4f1e0d..6250c0fc 100644 --- a/docs/docs/install-pwsh.mdx +++ b/docs/docs/install-pwsh.mdx @@ -30,6 +30,13 @@ Import-Module oh-my-posh ``` ::: +After installation, import Oh My Posh in your `$PROFILE`. Restart your shell and Oh My Posh will start downloading +a compatible version of the executable (this is kept in sync on update). + +```powershell +Import-Module oh-my-posh +``` + ## List all themes To display every available theme in the current directory, use the following diff --git a/packages/powershell/oh-my-posh/deploy.ps1 b/packages/powershell/oh-my-posh/deploy.ps1 index 0739340c..655387cd 100644 --- a/packages/powershell/oh-my-posh/deploy.ps1 +++ b/packages/powershell/oh-my-posh/deploy.ps1 @@ -18,12 +18,6 @@ Param (Get-Content '.\oh-my-posh.psd1' -Raw).Replace('0.0.0.1', $ModuleVersion) | Out-File -Encoding 'UTF8' '.\oh-my-posh.psd1' # copy all themes into the module folder Copy-Item -Path "../../../themes" -Destination "./themes" -Recurse -# fetch all the binaries from the version's GitHub release -New-Item -Path "./" -Name "bin" -ItemType "directory" -"posh-windows-amd64.exe", "posh-windows-386.exe", "posh-windows-arm64.exe", "posh-darwin-amd64", "posh-linux-amd64", "posh-linux-arm", "posh-linux-arm64" | ForEach-Object -Process { - $download = "https://github.com/jandedobbeleer/oh-my-posh/releases/download/v$BinVersion/$_" - Invoke-WebRequest $download -Out "./bin/$_" -} # publish the module if ($RepositoryAPIKey) { Publish-Module -Path . -Repository $Repository -NuGetApiKey $RepositoryAPIKey -Verbose @@ -32,6 +26,4 @@ if ($RepositoryAPIKey) { } # reset module version (for local testing only as we don't want PR's with changed version numbers all the time) (Get-Content '.\oh-my-posh.psd1' -Raw).Replace($ModuleVersion, '0.0.0.1') | Out-File -Encoding 'UTF8' '.\oh-my-posh.psd1' -Remove-Item "./bin" -Recurse -Force Remove-Item "./themes" -Recurse -Force - diff --git a/packages/powershell/oh-my-posh/oh-my-posh.psm1 b/packages/powershell/oh-my-posh/oh-my-posh.psm1 index 09a8aada..80ff6480 100644 --- a/packages/powershell/oh-my-posh/oh-my-posh.psm1 +++ b/packages/powershell/oh-my-posh/oh-my-posh.psm1 @@ -17,48 +17,96 @@ https://ohmyposh.dev/docs/faq#powershell-running-in-constrainedlanguage-mode $env:POSH_CONSTRAINED_LANGUAGE = 1 } -function Get-PoshCommand { +function Get-PoshDownloadUrl { + param( + [Parameter(Mandatory = $true)] + [string] + $Version + ) + + $executable = "" if ($IsMacOS) { - return "$PSScriptRoot/bin/posh-darwin-amd64" + $executable = "posh-darwin-amd64" } - if ($IsLinux) { + elseif ($IsLinux) { # this is rather hacky but there's no other way for the time being $arch = uname -m if ($arch -eq 'aarch64') { - return "$PSScriptRoot/bin/posh-linux-arm64" + $executable = "posh-linux-arm64" } - if ($arch -eq 'armv7l') { - return "$PSScriptRoot/bin/posh-linux-arm" + elseif ($arch -eq 'armv7l') { + $executable = "posh-linux-arm" + } + else { + $executable = "posh-linux-amd64" } - return "$PSScriptRoot/bin/posh-linux-amd64" } - $arch = (Get-CimInstance -Class Win32_Processor -Property Architecture).Architecture - switch ($arch) { - 0 { return "$PSScriptRoot/bin/posh-windows-386.exe" } # x86 - 5 { return "$PSScriptRoot/bin/posh-windows-arm64.exe" } # ARM - 9 { return "$PSScriptRoot/bin/posh-windows-amd64.exe" } # x64 - 12 { return "$PSScriptRoot/bin/posh-windows-amd64.exe" } # x64 emulated on Surface Pro X + 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 + } } - throw "Oh My Posh: Unsupported architecture: $arch" + if ($executable -eq "") { + throw "oh-my-posh: Unsupported architecture: $arch" + } + return "https://github.com/jandedobbeleer/oh-my-posh/releases/download/v$Version/$executable" } -function Set-ExecutablePermissions { +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 } - - $executable = Get-PoshCommand - if (-Not (Test-Path $executable)) { - # This should only happen with a corrupt installation - Write-Warning "Executable at $executable was not found" - 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)/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 + function Set-PoshPrompt { param( [Parameter(Mandatory = $false)] @@ -192,8 +240,6 @@ function ThemeCompletion { ForEach-Object { New-CompletionResult -CompletionText $_ } } -Set-ExecutablePermissions - Register-ArgumentCompleter ` -CommandName Set-PoshPrompt ` -ParameterName Theme `