OXYGENHUB
# 84f09ad45851 oxygenhub / packaging / windows / install.ps1
2.8 KiB · last changed oh works in PowerShell: ohub.exe and install.ps1, which respects the execution policy (closes #319); the images page tells peer-held images apart (refs #342); a queued job's reason names CPUs 9 days ago source blame history raw
1
# Install oh for this Windows user (oxygenhub#319). Run it from the
2
# unpacked zip:   powershell -ExecutionPolicy Bypass -File .\install.ps1
3
#
4
# - Copies oh.exe and ohub.exe to %LOCALAPPDATA%\Programs\oh and puts that
5
#   folder on this user's PATH.
6
# - PowerShell has a built-in alias `oh` (Out-Host) that hides oh.exe. Where
7
#   PowerShell runs scripts, this removes the alias in your profile; under
8
#   Windows' default policy (Restricted) a profile cannot run at all, so it
9
#   leaves that alone and you use `ohub`, which always works. It never
10
#   changes your execution policy.
11
# Nothing here needs administrator rights; run it again to update.
12
$ErrorActionPreference = 'Stop'
13
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
14
$dest = Join-Path $env:LOCALAPPDATA 'Programs\oh'
15
New-Item -ItemType Directory -Force -Path $dest | Out-Null
16
foreach ($exe in 'oh.exe', 'ohub.exe') {
17
    Copy-Item -Force (Join-Path $here $exe) (Join-Path $dest $exe)
18
}
19
20
$path = [Environment]::GetEnvironmentVariable('Path', 'User')
21
$parts = @($path -split ';' | Where-Object { $_ -ne '' })
22
if ($parts -notcontains $dest) {
23
    [Environment]::SetEnvironmentVariable('Path', (($parts + $dest) -join ';'), 'User')
24
    Write-Host "Added $dest to your PATH (new terminals see it)."
25
}
26
27
$marker = '# oh: PowerShell''s alias `oh` (Out-Host) hides oh.exe (oxygenhub#319)'
28
$line = 'Remove-Item Alias:oh -Force -ErrorAction SilentlyContinue'
29
$profilePath = $PROFILE.CurrentUserAllHosts
30
# A profile runs only where scripts may: under Restricted (Windows' default)
31
# or AllSigned (an unsigned profile) it would print an error in every new
32
# PowerShell instead, and the alias would stay.
33
# The policy a new PowerShell gets, not this one's (run with -ExecutionPolicy
34
# Bypass, this process may run anything): the first scope that sets one,
35
# else Restricted, the client default.
36
$policy = 'Restricted'
37
foreach ($scope in 'MachinePolicy', 'UserPolicy', 'CurrentUser', 'LocalMachine') {
38
    $p = Get-ExecutionPolicy -Scope $scope
39
    if ("$p" -ne 'Undefined') { $policy = "$p"; break }
40
}
41
if ($policy -in 'RemoteSigned', 'Unrestricted', 'Bypass') {
42
    if (-not (Test-Path $profilePath)) {
43
        New-Item -ItemType File -Force -Path $profilePath | Out-Null
44
    }
45
    if (-not (Select-String -Path $profilePath -SimpleMatch -Quiet -Pattern $line)) {
46
        Add-Content -Path $profilePath -Value "`r`n$marker`r`n$line"
47
        Write-Host "Your PowerShell profile ($profilePath) now removes the oh alias."
48
    }
49
    Remove-Item Alias:oh -Force -ErrorAction SilentlyContinue
50
    Write-Host "oh is installed: open a new terminal and run 'oh --version' (or 'ohub --version')."
51
} else {
52
    Write-Host "oh is installed: open a new terminal and run 'ohub --version'."
53
    Write-Host "PowerShell runs no scripts here (execution policy $policy), so its alias 'oh' (Out-Host) stays: use 'ohub', or 'oh.exe'. Command Prompt runs 'oh' as it is."
54
}