feat(pwsh): download executable from module

This commit is contained in:
Jan De Dobbeleer 2021-11-22 11:43:11 +01:00 committed by Jan De Dobbeleer
parent 9f200a8930
commit d422325f98
3 changed files with 78 additions and 33 deletions

View file

@ -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

View file

@ -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

View file

@ -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 `