-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
87 lines (76 loc) · 3.73 KB
/
install.ps1
File metadata and controls
87 lines (76 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[string]$InstallDir = (Join-Path $env:LOCALAPPDATA "Programs\MiniTaskbar"),
[string]$RunKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run",
[switch]$SkipBuildAndTest
)
$ErrorActionPreference = "Stop"
$builtExe = Join-Path $PSScriptRoot "build\mini_taskbar.exe"
$defaultInstallDir = [System.IO.Path]::GetFullPath((Join-Path $env:LOCALAPPDATA "Programs\MiniTaskbar"))
$resolvedInstallDir = [System.IO.Path]::GetFullPath($InstallDir)
$temporaryRoot = [System.IO.Path]::GetFullPath([System.IO.Path]::GetTempPath()).TrimEnd([System.IO.Path]::DirectorySeparatorChar)
$temporaryTestPrefix = Join-Path $temporaryRoot "MiniTaskbarInstallTests-"
$temporaryTestPattern = '^' + [System.Text.RegularExpressions.Regex]::Escape($temporaryTestPrefix) + '[0-9a-f]{32}\\install$'
$isDefaultInstallDir = [System.String]::Equals($resolvedInstallDir, $defaultInstallDir, [System.StringComparison]::OrdinalIgnoreCase)
$isTemporaryInstallDir = $resolvedInstallDir -match $temporaryTestPattern
$defaultRunKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
$isDefaultRunKey = [System.String]::Equals($RunKey, $defaultRunKey, [System.StringComparison]::OrdinalIgnoreCase)
$isTemporaryRunKey = $RunKey -match '^HKCU:\\Software\\MiniTaskbarTests\\[0-9a-f]{32}$'
$installedExe = Join-Path $resolvedInstallDir "mini_taskbar.exe"
$applied = $false
$startedNow = $false
function Copy-ItemWithRetry {
param(
[string]$Source,
[string]$Destination
)
for ($attempt = 0; $attempt -lt 40; $attempt++) {
try {
Copy-Item -LiteralPath $Source -Destination $Destination -Force
return
}
catch [System.IO.IOException] {
if ($attempt -eq 39) {
throw
}
Start-Sleep -Milliseconds 250
}
}
}
if (-not (($isDefaultInstallDir -and $isDefaultRunKey) -or ($isTemporaryInstallDir -and $isTemporaryRunKey))) {
throw "Refusing to install into an unexpected target: $resolvedInstallDir with $RunKey"
}
if ($SkipBuildAndTest -and -not $isTemporaryInstallDir) {
throw "SkipBuildAndTest is allowed only for temporary test installations."
}
if ($PSCmdlet.ShouldProcess($resolvedInstallDir, "Build, test, install MiniTaskbar and register per-user startup")) {
if (-not $SkipBuildAndTest) {
& (Join-Path $PSScriptRoot "build.ps1") | Out-Null
& (Join-Path $PSScriptRoot "test.ps1")
}
New-Item -ItemType Directory -Force -Path $resolvedInstallDir | Out-Null
if ($isDefaultInstallDir) {
$existingResidents = @(Get-Process -Name "mini_taskbar" -ErrorAction SilentlyContinue |
Where-Object { $_.Path -eq $installedExe })
if ($existingResidents.Count -gt 0) {
$existingResidents | Stop-Process -Force -ErrorAction SilentlyContinue
$existingResidents | Wait-Process -Timeout 10 -ErrorAction SilentlyContinue
}
}
Copy-ItemWithRetry -Source $builtExe -Destination $installedExe
New-Item -ItemType Directory -Force -Path $RunKey | Out-Null
New-ItemProperty -Path $RunKey -Name "MiniTaskbar" -PropertyType String -Value ('"' + $installedExe + '" --resident') -Force | Out-Null
if ($isDefaultInstallDir) {
Start-Process -FilePath $installedExe -ArgumentList "--resident" -WindowStyle Hidden
$startedNow = $true
}
$applied = $true
}
$runProperties = Get-ItemProperty -LiteralPath $RunKey -ErrorAction SilentlyContinue
$startupProperty = if ($runProperties) { $runProperties.PSObject.Properties["MiniTaskbar"] } else { $null }
[pscustomobject]@{
InstalledExe = $installedExe
StartupValue = if ($startupProperty) { $startupProperty.Value } else { $null }
Applied = $applied
AppliedNow = $startedNow
}