-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
381 lines (327 loc) · 15.7 KB
/
install.ps1
File metadata and controls
381 lines (327 loc) · 15.7 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#Requires -Version 5.1
[CmdletBinding(DefaultParameterSetName="ReleaseInstall")]
param(
[Parameter(Mandatory=$true, ParameterSetName="SelfInstall")]
[switch]$Self,
[Parameter(Mandatory=$true, ParameterSetName="MainWebInstall")]
[switch]$Main,
[Parameter(Mandatory=$true, ParameterSetName="DevelopWebInstall")]
[switch]$Develop,
[Parameter(Mandatory=$false, ParameterSetName="ReleaseInstall")]
[string]$Version = "latest"
)
Function Write-StatusMessage {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, Position=0)]
[string]$Message,
[Parameter(Mandatory=$false)]
[string]$ForegroundColor = "Gray",
[Parameter(Mandatory=$false)]
[int]$Indent = 0,
[Parameter(Mandatory=$false)]
[ValidateSet("Default", "Verbose", "Debug", "Warning", "Error")]
[string]$Verbosity = "Default",
[Parameter(Mandatory=$false)]
[int]$Width = 0,
[Parameter(Mandatory=$false)]
[switch]$NoNewLine
)
if ($Indent -gt 0) {
$Message = "$(' ' * $Indent)$Message"
}
if ($Width -gt 0) {
if($Message.Length -gt $Width) {
$Message = $Message.Substring(0, $Width - 3) + "...";
} else {
$Message = $Message.PadRight($Width, " ");
}
}
$messageParams = @{ }
if($Verbosity -eq "Default") {
$messageParams.Object = $Message
$messageParams.ForegroundColor = $ForegroundColor
$messageParams.NoNewLine = $NoNewLine.IsPresent
} else {
$messageParams.Message = $Message
}
#$messageParams.Object = $Message
switch($Verbosity) {
"Verbose" {
Write-Verbose @messageParams
}
"Debug" {
Write-Debug @messageParams
}
"Warning" {
Write-Warning @messageParams
}
"Error" {
Write-Error @messageParams
}
"Default" {
Write-Host @messageParams
}
}
}
function Center-Text($text, $width) {
$text = "$text"
$pad = $width - $text.Length
if ($pad -le 0) { return $text }
$left = [math]::Floor($pad / 2)
$right = $pad - $left
(' ' * $left) + $text + (' ' * $right)
}
function Left-Text($text, $width) {
$text = " $text"
if ($text.Length -ge $width) { return $text }
return $text + (' ' * ($width - $text.Length))
}
function Right-Text($text, $width) {
$text = "$text "
if ($text.Length -ge $width) { return $text }
return (' ' * ($width - $text.Length)) + $text
}
$successCheck = [char]0x2713
Write-Host "DevSetup Module Installer" -ForegroundColor Cyan
Write-Host "=========================" -ForegroundColor Cyan
$Url = $null
$VersionToInstall = $null
if($PSBoundParameters.ContainsKey('Main')) {
# Install the main branch
$Url = "https://github.com/pwshdevs/devsetup/archive/main.zip"
$VersionToInstall = "main"
} elseif($PSBoundParameters.ContainsKey('Develop')) {
# Install the develop branch
$Url = "https://github.com/pwshdevs/devsetup/archive/develop.zip"
$VersionToInstall = "develop"
} elseif($PSBoundParameters.ContainsKey('Self')) {
# Install from the directory we are in
$Url = $null
} else {
# Download the the most current release and install that
if($Version -eq "latest") {
$Url = ((Invoke-WebRequest https://api.github.com/repos/pwshdevs/devsetup/releases/latest -usebasicparsing).Content | convertfrom-json).zipball_url
$VersionToInstall = "latest"
} else {
$Url = ((Invoke-WebRequest https://api.github.com/repos/pwshdevs/devsetup/releases -usebasicparsing) | convertfrom-json) | foreach-object { if($_.tag_name -eq "v$Version") { $_.zipball_url }}
if([string]::IsNullOrEmpty($Url)) {
Write-Error "Invalid version provided"
return
}
$VersionToInstall = $Version
}
}
$DevSetupModulePath = $null
$Archive = $null
$ExtractedFolder = $null
if($null -ne $Url) {
Write-StatusMessage "- Validating Installation Type..." -Width 60 -ForegroundColor Gray -NoNewLine
Write-StatusMessage (Right-Text "[$VersionToInstall]" 20) -ForegroundColor Green
$Archive = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "devsetup.zip"
$ExtractedFolder = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "devsetup"
Write-StatusMessage "- Downloading zipfile..." -Width 60 -ForegroundColor Gray -NoNewLine
Invoke-WebRequest $url -OutFile $Archive
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
Write-StatusMessage "- Extracting zipfile..." -Width 60 -ForegroundColor Gray -NoNewLine
Expand-Archive -Path $Archive -DestinationPath $ExtractedFolder -Force
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
$InstallerPath = (Get-ChildItem -Path $ExtractedFolder | Select-Object -First 1).FullName
$DevSetupModulePath = Join-Path -Path $InstallerPath -ChildPath "DevSetup"
} else {
$ScriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
$DevSetupModulePath = Join-Path -Path $ScriptDirectory -ChildPath "DevSetup"
}
try {
$ProgressPreference = 'SilentlyContinue'
# Get the script directory (where the DevSetup module should be)
Write-Debug "Script directory: $ScriptDirectory"
Write-Debug "DevSetup module path: $DevSetupModulePath"
# Verify the DevSetup module exists
if (-not (Test-Path $DevSetupModulePath)) {
throw "DevSetup module not found at: $DevSetupModulePath"
}
# Verify the module manifest exists
$ModuleManifest = Join-Path -Path $DevSetupModulePath -ChildPath "DevSetup.psd1"
if (-not (Test-Path $ModuleManifest)) {
throw "DevSetup module manifest not found at: $ModuleManifest"
}
Write-Debug "DevSetup module found and verified."
# Get the PSModulePath environment variable
$psModulePath = $Env:PSModulePath
# Split the string into an array of individual paths using the platform-specific path separator
$modulePaths = $psModulePath -split [System.IO.Path]::PathSeparator
# Determine the correct user modules path based on env:PSModulePath
$UserModulesPath = ($modulePaths | Select-Object -First 1)
if (-not $UserModulesPath) {
throw "Unable to determine user modules path for PowerShell version $($PSVersionTable.PSVersion)"
}
Write-Debug "Target user modules path: $UserModulesPath"
# Show current PSModulePath for debugging
Write-Debug "Current PSModulePath:"
($env:PSModulePath -split [IO.Path]::PathSeparator) | ForEach-Object { Write-Debug " $_" }
# Get the module version from the manifest
$ModuleVersion = $null
Write-Debug "Reading module version from manifest..."
try {
$ManifestData = Import-PowerShellDataFile -Path $ModuleManifest
$ModuleVersion = $ManifestData.ModuleVersion
if (-not $ModuleVersion) {
throw "ModuleVersion not found in manifest"
}
} catch {
Write-Warning "Failed to read module version from manifest: $_"
Write-Host "Using default version: 1.0.0" -ForegroundColor Yellow
$ModuleVersion = "1.0.0"
}
Write-StatusMessage "- Installing DevSetup Version..." -Width 60 -NoNewLine -ForegroundColor Gray
Write-StatusMessage (Right-Text "[$($ModuleVersion)]" 20) -ForegroundColor Green
Write-StatusMessage "- Checking PowerShell Version..." -Width 60 -NoNewLine -ForegroundColor Gray
Write-StatusMessage (Right-Text "[$($PSVersionTable.PSVersion)]" 20) -ForegroundColor Green
Write-StatusMessage "- Checking PowerShell Edition..." -Width 60 -NoNewLine -ForegroundColor Gray
Write-StatusMessage (Right-Text "[$($PSVersionTable.PSEdition)]" 20) -ForegroundColor Green
$nugetProvider = Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue -Force -ForceBootstrap
Write-StatusMessage "- Installing NuGet Package Provider..." -Width 60 -NoNewLine -ForegroundColor Gray
if ($nugetProvider) {
Write-StatusMessage (Right-Text "[$($nugetProvider.Version)]" 20) -ForegroundColor Green
} else {
Install-PackageProvider -Name NuGet -Force -ForceBootstrap
# Verify installation
$nugetProvider = Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue
if ($nugetProvider) {
Write-StatusMessage (Right-Text "[$($nugetProvider.Version)]" 20) -ForegroundColor Green
} else {
throw "Failed to install NuGet PackageProvider"
}
}
# Install required module dependencies
try {
$RequiredModules = $ManifestData.RequiredModules
if ($RequiredModules -and $RequiredModules.Count -gt 0) {
foreach ($RequiredModule in $RequiredModules) {
Write-StatusMessage "- Installing powershell dependency $RequiredModule..." -Width 60 -NoNewLine -ForegroundColor Gray
# Check if the module is already installed
$InstalledModule = Get-Module -ListAvailable -Name $RequiredModule -ErrorAction SilentlyContinue
if ($InstalledModule) {
Write-StatusMessage (Right-Text "[$($InstalledModule[0].Version)]" 20) -ForegroundColor Green
} else {
try {
Install-Module -Name $RequiredModule -Scope CurrentUser -Force -AllowClobber -ErrorAction Stop
$InstalledModule = Get-Module -ListAvailable -Name $RequiredModule -ErrorAction SilentlyContinue
Write-StatusMessage (Right-Text "[$($InstalledModule[0].Version)]" 20) -ForegroundColor Green
} catch {
Write-StatusMessage (Right-Text "[FAILED]" 20) -ForegroundColor Red
}
}
}
} else {
Write-StatusMessage "- No required modules specified in manifest..." -Width 60 -NoNewLine -ForegroundColor Gray
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
}
} catch {
Write-Warning "Failed to process required modules from manifest: $_"
Write-Host "- Continuing with installation..." -ForegroundColor Gray
}
# Create the user modules directory if it doesn't exist
if (-not (Test-Path $UserModulesPath)) {
Write-StatusMessage "- Creating user modules directory..." -Width 60 -NoNewLine -ForegroundColor Gray
New-Item -Path $UserModulesPath -ItemType Directory -Force | Out-Null
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
}
# Define the target installation path with version
$TargetModuleBasePath = Join-Path -Path $UserModulesPath -ChildPath "DevSetup"
$TargetModulePath = Join-Path -Path $TargetModuleBasePath -ChildPath $ModuleVersion
# Remove existing installation if it exists
if (Test-Path $TargetModuleBasePath) {
Write-StatusMessage "- Removing existing DevSetup module versions..." -Width 60 -NoNewLine -ForegroundColor Gray
Remove-Item -Path $TargetModuleBasePath -Recurse -Force | Out-Null
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
}
# Create the versioned directory structure
Write-StatusMessage "- Creating versioned directory structure..." -Width 60 -NoNewLine -ForegroundColor Gray
New-Item -Path $TargetModulePath -ItemType Directory -Force | Out-Null
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
# Copy the DevSetup module contents to the versioned path
Write-StatusMessage "- Installing DevSetup module..." -Width 60 -NoNewLine -ForegroundColor Gray
Copy-Item -Path "$DevSetupModulePath\*" -Destination $TargetModulePath -Recurse -Force | Out-Null
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
# Verify the installation
Write-StatusMessage "- Verifying installation..." -Width 60 -NoNewLine -ForegroundColor Gray
# Check if the module is now in PSModulePath
try {
$ModuleFound = Get-Module -ListAvailable -Name "DevSetup" -ErrorAction SilentlyContinue
} catch {
# Keep moving on
}
if ($ModuleFound) {
#Write-Host "- Installation Verified..." -ForegroundColor Gray
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
$ModuleFound | ForEach-Object {
Write-Debug " - Version: $($_.Version) at $($_.ModuleBase)"
}
} else {
Write-StatusMessage (Right-Text "[FAILED]" 20) -ForegroundColor Red
Write-Warning "DevSetup module not found in module search paths!"
Write-Host "- Manual verification - checking installation path..." -ForegroundColor Yellow
if (Test-Path (Join-Path $TargetModulePath "DevSetup.psd1")) {
Write-Host " - Module files exist at target path: $TargetModulePath" -ForegroundColor Green
} else {
Write-Error "Module files not found at target path!"
}
}
# Import the module to test it
try {
Import-Module -Name "DevSetup" -Force -ErrorAction SilentlyContinue
Write-Debug "DevSetup module imported successfully!"
# Test a basic function
if (Get-Command -Name "Use-DevSetup" -ErrorAction SilentlyContinue) {
Write-Debug "DevSetup functions are available."
} else {
Write-Warning "DevSetup functions may not be properly loaded."
}
# Show module information
$ModuleInfo = Get-Module -Name "DevSetup" -ErrorAction SilentlyContinue
if ($ModuleInfo) {
Write-Debug "Module Version: $($ModuleInfo.Version)"
Write-Debug "Module Path: $($ModuleInfo.ModuleBase)"
}
} catch {
#Write-Warning "Failed to import DevSetup module: $_"
}
if($Url) {
Write-StatusMessage "- Cleaning up temporary files..." -Width 60 -ForegroundColor Gray -NoNewLine
Remove-Item -Path $Archive -Force
Remove-Item -Path $ExtractedFolder -Recurse -Force
Write-StatusMessage (Right-Text "[$successCheck]" 20) -ForegroundColor Green
}
Write-Host "`nInstallation completed successfully!" -ForegroundColor Green
#Write-Host "Install Path:`n- $TargetModulePath`n" -ForegroundColor Gray
Write-Host "You can now use DevSetup commands in any PowerShell session." -ForegroundColor White
# Add the module to the current session's auto-import
Write-Debug "`nSetting up module auto-import for current session..."
if ($ModuleFound) {
# Force import in current session
try {
Import-Module DevSetup -Force -Global -ErrorAction SilentlyContinue
Write-Debug "DevSetup module loaded in current session."
} catch {
# Keep moving on
}
}
Write-Host "`nTo get started, run:" -ForegroundColor Cyan
#Write-Host " Use-DevSetup -Init" -ForegroundColor White
#Write-Host " # or use the alias:" -ForegroundColor Gray
Write-Host " devsetup -Init" -ForegroundColor White
Write-Host "`nNote: If the command isn't found in new sessions, run:" -ForegroundColor Yellow
Write-Host " Import-Module DevSetup" -ForegroundColor White
} catch {
Write-Error "Installation failed: $_"
Write-Host "`nTroubleshooting:" -ForegroundColor Yellow
Write-Host "1. Ensure you're running this script from the DevSetup directory" -ForegroundColor White
Write-Host "2. Check that the DevSetup folder and DevSetup.psd1 file exist" -ForegroundColor White
Write-Host "3. Verify you have write permissions to the user modules directory" -ForegroundColor White
exit 1
}
Write-Host ""
#Write-Host "`nPress any key to continue..." -ForegroundColor Gray
#$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")