-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageManager.psm1
More file actions
286 lines (243 loc) · 8.98 KB
/
PackageManager.psm1
File metadata and controls
286 lines (243 loc) · 8.98 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#Requires -Version 5.1
Using module .\Logging.psm1
Using module .\Scope.psm1
Using module .\Exit.psm1
Using module .\Utils.psm1
enum PackageManager {
Chocolatey
Unsupported
}
# TODO :: Add support for other package managers.
[PackageManager]$Script:PackageManager = switch ($env:OS) {
'Windows_NT' { [PackageManager]::Chocolatey };
default { [PackageManager]::Unsupported };
};
[HashTable]$Script:PackageManagerDetails = switch ($Script:PackageManager) {
Chocolatey {
@{
Executable = "$($env:SystemDrive)\ProgramData\Chocolatey\bin\choco.exe";
Commands = @{
List = 'list';
Uninstall = 'uninstall';
Install = 'install';
Update = 'upgrade';
}
Options = @{
Common = @('--confirm', '--limit-output', '--no-progress', '--exact');
Force = '--force';
}
};
};
Unsupported {
Invoke-Error 'Could not find a supported package manager.';
$null;
}
};
[Boolean]$Script:CompletedSetup = $False;
function Local:Install-Requirement {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidUsingInvokeExpression',
'',
Justification = 'Required to install Chocolatey, there is no other way to do this.'
)]
[Compiler.Analyser.SuppressAnalyserAttribute(
'UseOfUndefinedFunction',
'refreshenv',
Justification = 'Will be defined by the imported module.'
)]
[CmdletBinding()]
param()
if ($Script:CompletedSetup) {
Invoke-Debug 'Setup already completed. Skipping...';
return;
}
if (-not (Test-NetworkConnection)) {
Invoke-Error 'No network connection detected. Skipping package manager installation.';
Invoke-FailedExit -ExitCode 9999;
}
@{
PSPrefix = '📦';
PSMessage = "Installing requirements for $Script:PackageManager...";
PSColour = 'Green';
} | Invoke-Write;
switch ($Script:PackageManager) {
Chocolatey {
if (Get-Command -Name 'choco' -ErrorAction SilentlyContinue) {
Invoke-Debug 'Chocolatey is already installed. Skipping installation.';
return
}
if (Test-Path -Path "$($env:SystemDrive)\ProgramData\chocolatey") {
Invoke-Debug 'Chocolatey files found, seeing if we can repair them...';
if (Test-Path -Path "$($env:SystemDrive)\ProgramData\chocolatey\bin\choco.exe") {
Invoke-Debug 'Chocolatey bin found, should be able to refreshenv!';
Invoke-Debug 'Refreshing environment variables...';
Import-Module "$($env:SystemDrive)\ProgramData\chocolatey\Helpers\chocolateyProfile.psm1" -Force;
refreshenv | Out-Null;
return;
} else {
Invoke-Warn 'Chocolatey bin not found, deleting folder and reinstalling...';
Remove-Item -Path "$($env:SystemDrive)\ProgramData\chocolatey" -Recurse -Force;
}
}
Invoke-Info 'Installing Chocolatey...';
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'));
}
Default {}
}
[Boolean]$Script:CompletedSetup = $True;
}
<#
.SYNOPSIS
Tests if a package is installed.
.PARAMETER PackageName
The name of the package to test.
#>
function Test-ManagedPackage {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$PackageName
)
begin { Enter-Scope; Install-Requirement; }
end { Exit-Scope -ReturnValue $Local:Installed; }
process {
@{
PSPrefix = '🔍';
PSMessage = "Checking if package '$PackageName' is installed...";
PSColour = 'Yellow';
} | Invoke-Write;
# if ($PackageVersion) {
# $Local:PackageArgs['Version'] = $PackageVersion;
# }
[Boolean]$Local:Installed = & $Script:PackageManagerDetails.Executable $Script:PackageManagerDetails.Commands.List $Script:PackageManagerDetails.Options.Common $PackageName;
Invoke-Verbose "Package '$PackageName' is $(if (-not $Local:Installed) { 'not ' })installed.";
return $Local:Installed;
}
}
<#
.SYNOPSIS
Installs a package using the system package manager.
.DESCRIPTION
This function installs a package using the detected system package manager (e.g., Chocolatey).
.PARAMETER PackageName
The name of the package to install.
.PARAMETER Sha256
The expected SHA256 hash of the package for validation.
.PARAMETER NoFail
If specified, allows the script to continue even if the installation fails.
#>
function Install-ManagedPackage {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$PackageName,
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]$Sha256,
[Parameter()]
[ValidateNotNullOrEmpty()]
[Switch]$NoFail
# [Parameter()]
# [ValidateNotNullOrEmpty()]
# [String]$PackageVersion
)
begin { Enter-Scope; Install-Requirement; }
end { Exit-Scope; }
process {
@{
PSPrefix = '📦';
PSMessage = "Installing package '$Local:PackageName'...";
PSColour = 'Green';
} | Invoke-Write;
# if ($PackageVersion) {
# $Local:PackageArgs['Version'] = $PackageVersion;
# }
[System.Diagnostics.Process]$Local:Process = Start-Process -FilePath $Script:PackageManagerDetails.Executable -ArgumentList (@($Script:PackageManagerDetails.Commands.Install) + $Script:PackageManagerDetails.Options.Common + @($PackageName)) -NoNewWindow -PassThru -Wait;
if ($Local:Process.ExitCode -ne 0) {
Invoke-Error "There was an issue while installing $Local:PackageName.";
Invoke-FailedExit -ExitCode $Local:Process.ExitCode -DontExit:$NoFail;
}
}
}
<#
.SYNOPSIS
Uninstalls a package using the system package manager.
.DESCRIPTION
This function uninstalls a package using the detected system package manager (e.g., Chocolatey).
.PARAMETER PackageName
The name of the package to uninstall.
.PARAMETER NoFail
If specified, allows the script to continue even if the uninstallation fails.
#>
function Uninstall-ManagedPackage {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$PackageName,
[Parameter()]
[Switch]$NoFail
)
begin { Enter-Scope; Install-Requirement; }
end { Exit-Scope; }
process {
if ($PSCmdlet.ShouldProcess($PackageName, "Uninstall package")) {
@{
PSPrefix = '🗑️';
PSMessage = "Uninstalling package '$PackageName'...";
PSColour = 'Yellow';
} | Invoke-Write;
try {
& $Script:PackageManagerDetails.Executable $Script:PackageManagerDetails.Commands.Uninstall $Script:PackageManagerDetails.Options.Common $PackageName | Out-Null;
if ($LASTEXITCODE -ne 0) {
throw "Error Code: $LASTEXITCODE";
}
} catch {
Invoke-Error "There was an issue while uninstalling $PackageName.";
Invoke-Error $_.Exception.Message;
if (-not $NoFail) {
Invoke-FailedExit -ExitCode $LASTEXITCODE;
}
}
}
}
}
<#
.SYNOPSIS
Updates a package using the system package manager.
.DESCRIPTION
This function updates a package to the latest version using the detected system package manager (e.g., Chocolatey).
.PARAMETER PackageName
The name of the package to update.
#>
function Update-ManagedPackage {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$PackageName
)
begin { Enter-Scope; Install-Requirement; }
end { Exit-Scope; }
process {
if ($PSCmdlet.ShouldProcess($PackageName, "Update package")) {
@{
PSPrefix = '🔄';
PSMessage = "Updating package '$PackageName'...";
PSColour = 'Blue';
} | Invoke-Write;
try {
& $Script:PackageManagerDetails.Executable $Script:PackageManagerDetails.Commands.Update $Script:PackageManagerDetails.Options.Common $PackageName | Out-Null;
if ($LASTEXITCODE -ne 0) {
throw "Error Code: $LASTEXITCODE";
}
} catch {
Invoke-Error "There was an issue while updating $PackageName.";
Invoke-Error $_.Exception.Message;
}
}
}
}
Export-ModuleMember -Function Test-ManagedPackage, Install-ManagedPackage, Uninstall-ManagedPackage, Update-ManagedPackage;