-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathAppsUtils.psm1
206 lines (195 loc) · 7.36 KB
/
AppsUtils.psm1
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#Requires -Version 5.1
Set-StrictMode -Version 3.0
# A temp fix for https://github.com/ScoopInstaller/Scoop/pull/5066#issuecomment-1372087032
function Invoke-ExternalCommand2 {
[CmdletBinding(DefaultParameterSetName = 'Default')]
[OutputType([Boolean])]
param (
[Parameter(Mandatory = $true,
Position = 0)]
[Alias('Path')]
[ValidateNotNullOrEmpty()]
[String]
$FilePath,
[Parameter(Position = 1)]
[Alias('Args')]
[String[]]
$ArgumentList,
[Parameter(ParameterSetName = 'UseShellExecute')]
[Switch]
$RunAs,
[Parameter(ParameterSetName = 'UseShellExecute')]
[Switch]
$Quiet,
[Alias('Msg')]
[String]
$Activity,
[Alias('cec')]
[Hashtable]
$ContinueExitCodes,
[Parameter(ParameterSetName = 'Default')]
[Alias('Log')]
[String]
$LogPath
)
if ($Activity) {
Write-Host "$Activity " -NoNewline
}
$Process = New-Object System.Diagnostics.Process
$Process.StartInfo.FileName = $FilePath
$Process.StartInfo.UseShellExecute = $false
$redirectToLogFile = $false
if ($LogPath) {
if ($FilePath -match '^msiexec(.exe)?$') {
$ArgumentList += "/lwe `"$LogPath`""
} else {
$redirectToLogFile = $true
$Process.StartInfo.RedirectStandardOutput = $true
$Process.StartInfo.RedirectStandardError = $true
}
}
if ($RunAs) {
$Process.StartInfo.UseShellExecute = $true
$Process.StartInfo.Verb = 'RunAs'
}
if ($Quiet) {
$Process.StartInfo.UseShellExecute = $true
$Process.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
}
if ($ArgumentList.Length -gt 0) {
if ($FilePath -match '^((cmd|cscript|wscript|msiexec)(\.exe)?|.*\.(bat|cmd|js|vbs|wsf))$') {
$Process.StartInfo.Arguments = $ArgumentList -join ' '
} elseif ($Process.StartInfo.PSObject.Properties.Name -contains 'ArgumentList') {
# ArgumentList is supported in PowerShell 6.1 and later (built on .NET Core 2.1+)
# ref-1: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.argumentlist?view=net-6.0
# ref-2: https://docs.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell?view=powershell-7.2#net-framework-vs-net-core
$ArgumentList | ForEach-Object { $Process.StartInfo.ArgumentList.Add($_) }
} else {
# escape arguments manually in lower versions, refer to https://docs.microsoft.com/en-us/previous-versions/17w5ykft(v=vs.85)
$escapedArgs = $ArgumentList | ForEach-Object {
# escape N consecutive backslash(es), which are followed by a double quote, to 2N consecutive ones
$s = $_ -replace '(\\+)"', '$1$1"'
# escape N consecutive backslash(es), which are at the end of the string, to 2N consecutive ones
$s = $s -replace '(\\+)$', '$1$1'
# escape double quotes
$s = $s -replace '"', '\"'
# https://github.com/ScoopInstaller/Scoop/issues/5231#issuecomment-1295840608
$s
}
$Process.StartInfo.Arguments = $escapedArgs -join ' '
Write-Host $Process.StartInfo.Arguments
}
}
try {
[void]$Process.Start()
} catch {
if ($Activity) {
Write-Host 'error.' -ForegroundColor DarkRed
}
Write-Host $_.Exception.Message -ForegroundColor DarkRed
return $false
}
if ($redirectToLogFile) {
# we do this to remove a deadlock potential
# ref: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=netframework-4.5#remarks
$stdoutTask = $Process.StandardOutput.ReadToEndAsync()
$stderrTask = $Process.StandardError.ReadToEndAsync()
}
$Process.WaitForExit()
if ($redirectToLogFile) {
Out-UTF8File -FilePath $LogPath -Append -InputObject $stdoutTask.Result
Out-UTF8File -FilePath $LogPath -Append -InputObject $stderrTask.Result
}
if ($Process.ExitCode -ne 0) {
if ($ContinueExitCodes -and ($ContinueExitCodes.ContainsKey($Process.ExitCode))) {
if ($Activity) {
Write-Host 'done.' -ForegroundColor DarkYellow
}
Write-Host $ContinueExitCodes[$Process.ExitCode] -ForegroundColor DarkYellow
return $true
} else {
if ($Activity) {
Write-Host 'error.' -ForegroundColor DarkRed
}
Write-Host "Exit code was $($Process.ExitCode)!" -ForegroundColor DarkRed
return $false
}
}
if ($Activity) {
Write-Host 'done.' -ForegroundColor Green
}
return $true
}
function Out-UTF8File {
param(
[Parameter(Mandatory = $True, Position = 0)]
[Alias('Path')]
[String] $FilePath,
[Switch] $Append,
[Switch] $NoNewLine,
[Parameter(ValueFromPipeline = $True)]
[PSObject] $InputObject
)
process {
if ($Append) {
[System.IO.File]::AppendAllText($FilePath, $InputObject)
} else {
if (!$NoNewLine) {
# Ref: https://stackoverflow.com/questions/5596982
# Performance Note: `WriteAllLines` throttles memory usage while
# `WriteAllText` needs to keep the complete string in memory.
[System.IO.File]::WriteAllLines($FilePath, $InputObject)
} else {
# However `WriteAllText` does not add ending newline.
[System.IO.File]::WriteAllText($FilePath, $InputObject)
}
}
}
}
function Mount-ExternalRuntimeData {
<#
.SYNOPSIS
Mount external runtime data
.PARAMETER Source
The source path, which is the persist_dir
.PARAMETER Target
The target path, which is the actual path app uses to access the runtime data
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string] $Source,
[Parameter(Mandatory = $true, Position = 1)]
[string] $Target
)
if (Test-Path $Source) {
Remove-Item $Target -Force -Recurse -ErrorAction SilentlyContinue
} else {
New-Item -ItemType Directory $Source -Force | Out-Null
if (Test-Path $Target) {
Get-ChildItem $Target | Move-Item -Destination $Source -Force
Remove-Item $Target
}
}
New-Item -ItemType Junction -Path $Target -Target $Source -Force | Out-Null
}
function Dismount-ExternalRuntimeData {
<#
.SYNOPSIS
Unmount external runtime data
.PARAMETER Target
The target path, which is the actual path app uses to access the runtime data
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string] $Target
)
if (Test-Path $Target) {
Remove-Item $Target -Force -Recurse
}
}
Export-ModuleMember `
-Function `
Mount-ExternalRuntimeData, Dismount-ExternalRuntimeData, `
Invoke-ExternalCommand2