Skip to content

Commit 562df0a

Browse files
HeyItsGilbertCopilot
andcommitted
fix(PSGalleryModule): šŸ› improve dependency handling and parameter validation
* Refactored the extraction of `$Name` and `$Version` from `$Dependency` to ensure defaults are set correctly. * Enhanced the logic for determining `$Scope` based on `$Dependency.Target`. * Improved the handling of package provider installation and validation. * Streamlined conditional checks for command execution (`Save` vs `Install`). Co-authored-by: Copilot <copilot@github.com>
1 parent 9f3260f commit 562df0a

1 file changed

Lines changed: 64 additions & 80 deletions

File tree

ā€ŽPSDepend/PSDependScripts/PSGalleryModule.ps1ā€Ž

Lines changed: 64 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -114,44 +114,49 @@ param(
114114
)
115115

116116
# Extract data from Dependency
117-
$DependencyName = $Dependency.DependencyName
118-
$Name = $Dependency.Name
119-
if(-not $Name)
120-
{
121-
$Name = $DependencyName
122-
}
117+
$DependencyName = $Dependency.DependencyName
118+
$Name = $Dependency.Name
119+
if(-not $Name) {
120+
$Name = $DependencyName
121+
}
123122

124-
$Version = $Dependency.Version
125-
if(-not $Version)
126-
{
127-
$Version = 'latest'
128-
}
123+
$Version = $Dependency.Version
124+
if(-not $Version) {
125+
$Version = 'latest'
126+
}
129127

130-
# We use target as a proxy for Scope
131-
if(-not $Dependency.Target)
132-
{
133-
$Scope = 'AllUsers'
134-
}
135-
else
136-
{
137-
$Scope = $Dependency.Target
138-
}
128+
# We use target as a proxy for Scope
129+
if(-not $Dependency.Target) {
130+
$Scope = 'AllUsers'
131+
} else {
132+
$Scope = $Dependency.Target
133+
}
139134

140-
$Credential = $Dependency.Credential
135+
$Credential = $Dependency.Credential
141136

142-
if('AllUsers', 'CurrentUser' -notcontains $Scope)
143-
{
144-
$command = 'save'
145-
}
146-
else
147-
{
148-
$command = 'install'
137+
if('AllUsers', 'CurrentUser' -notcontains $Scope) {
138+
$command = 'save'
139+
} else {
140+
$command = 'install'
141+
}
142+
143+
$packageProviderSplat = @{
144+
Name = 'NuGet'
145+
ListAvailable = $true
146+
ErrorAction = 'SilentlyContinue'
147+
}
148+
if(-not (Get-PackageProvider @packageProviderSplat)) {
149+
Write-Debug "NuGet provider not found. Attempting to install NuGet provider with parameters: $($packageProviderSplat | Out-String)"
150+
# Bootstrap NuGet provider for Windows PowerShell 5.1 and PowerShell 7+.
151+
$installPackageProviderSplat = @{
152+
Name = 'NuGet'
153+
ForceBootstrap = $true
154+
Force = $true
155+
Scope = 'CurrentUser'
156+
ErrorAction = 'SilentlyContinue'
149157
}
150158

151-
if(-not (Get-PackageProvider -Name Nuget))
152-
{
153-
# Grab nuget bits.
154-
$null = Get-PackageProvider -Name NuGet -ForceBootstrap | Out-Null
159+
$null = Install-PackageProvider @installPackageProviderSplat
155160
}
156161

157162
Write-Verbose -Message "Getting dependency [$name] from PowerShell repository [$Repository]"
@@ -160,10 +165,10 @@ Write-Verbose -Message "Getting dependency [$name] from PowerShell repository [$
160165
# but allow to rely on all PS repos registered.
161166
if($Repository) {
162167
$validRepo = Get-PSRepository -Name $Repository -Verbose:$false -ErrorAction SilentlyContinue
163-
if (-not $validRepo) {
164-
Write-Error "[$Repository] has not been setup as a valid PowerShell repository."
165-
return
166-
}
168+
if (-not $validRepo) {
169+
Write-Error "[$Repository] has not been setup as a valid PowerShell repository."
170+
return
171+
}
167172
}
168173

169174
$params = @{
@@ -174,47 +179,40 @@ $params = @{
174179
Force = $True
175180
}
176181

177-
if($PSBoundParameters.ContainsKey('AllowPrerelease')){
182+
if($PSBoundParameters.ContainsKey('AllowPrerelease')) {
178183
$params.Add('AllowPrerelease', $AllowPrerelease)
179184
}
180185

181-
if($PSBoundParameters.ContainsKey('AcceptLicense')){
186+
if($PSBoundParameters.ContainsKey('AcceptLicense')) {
182187
$params.Add('AcceptLicense', $AcceptLicense)
183188
}
184189

185190
if($Repository) {
186191
$params.Add('Repository',$Repository)
187192
}
188193

189-
if($Version -and $Version -ne 'latest')
190-
{
194+
if($Version -and $Version -ne 'latest') {
191195
$Params.add('RequiredVersion', $Version)
192196
}
193197

194-
if($Credential)
195-
{
196-
$Params.add('Credential', $Credential)
198+
if($Credential) {
199+
$Params.add('Credential', $Credential)
197200
}
198201

199202
# This code works for both install and save scenarios.
200-
if($command -eq 'Save')
201-
{
203+
if($command -eq 'Save') {
202204
$ModuleName = Join-Path $Scope $Name
203205
$Params.Remove('AllowClobber')
204206
$Params.Remove('SkipPublisherCheck')
205-
}
206-
elseif ($Command -eq 'Install')
207-
{
207+
} elseif ($Command -eq 'Install') {
208208
$ModuleName = $Name
209209
}
210210

211211
# Only use "SkipPublisherCheck" (and other) parameter if "Install-Module" supports it
212212
$availableParameters = (Get-Command "Install-Module").Parameters
213213
$tempParams = $Params.Clone()
214-
foreach($thisParameter in $Params.Keys)
215-
{
216-
if(-Not ($availableParameters.ContainsKey($thisParameter)))
217-
{
214+
foreach($thisParameter in $Params.Keys) {
215+
if(-not ($availableParameters.ContainsKey($thisParameter))) {
218216
Write-Verbose -Message "Removing parameter [$thisParameter] from [Install-Module] as it is not available"
219217
$tempParams.Remove($thisParameter)
220218
}
@@ -226,33 +224,28 @@ Add-ToPsModulePathIfRequired -Dependency $Dependency -Action $PSDependAction
226224
$Existing = $null
227225
$Existing = Get-Module -ListAvailable -Name $ModuleName -ErrorAction SilentlyContinue
228226

229-
if($Existing)
230-
{
227+
if($Existing) {
231228
Write-Verbose "Found existing module [$Name]"
232229
# Thanks to Brandon Padgett!
233230
$ExistingVersion = $Existing | Measure-Object -Property Version -Maximum | Select-Object -ExpandProperty Maximum
234-
$FindModuleParams = @{Name = $Name}
231+
$FindModuleParams = @{Name = $Name }
235232
if($Repository) {
236233
$FindModuleParams.Add('Repository', $Repository)
237234
}
238-
if($Credential)
239-
{
235+
if($Credential) {
240236
$FindModuleParams.Add('Credential', $Credential)
241237
}
242-
if($AllowPrerelease)
243-
{
238+
if($AllowPrerelease) {
244239
$FindModuleParams.Add('AllowPrerelease', $AllowPrerelease)
245240
}
246241

247242
# Version string, and equal to current
248-
if($Version -and $Version -ne 'latest' -and $Version -eq $ExistingVersion)
249-
{
243+
if($Version -and $Version -ne 'latest' -and $Version -eq $ExistingVersion) {
250244
Write-Verbose "You have the requested version [$Version] of [$Name]"
251245
# Conditional import
252246
Import-PSDependModule -Name $ModuleName -Action $PSDependAction -Version $ExistingVersion
253247

254-
if($PSDependAction -contains 'Test')
255-
{
248+
if($PSDependAction -contains 'Test') {
256249
return $true
257250
}
258251
return $null
@@ -267,20 +260,17 @@ if($Existing)
267260
[System.Management.Automation.SemanticVersion]::TryParse($GalleryVersion, [ref]$parsedTempSemanticVersion)
268261
) {
269262
$GalleryVersion -le $parsedSemanticVersion
270-
}
271-
elseif ([System.Version]::TryParse($ExistingVersion, [ref]$parsedVersion)) {
263+
} elseif ([System.Version]::TryParse($ExistingVersion, [ref]$parsedVersion)) {
272264
$GalleryVersion -le $parsedVersion
273265
}
274266

275267
# latest, and we have latest
276-
if( $Version -and ($Version -eq 'latest' -or $Version -eq '') -and $isGalleryVersionLessEquals)
277-
{
268+
if( $Version -and ($Version -eq 'latest' -or $Version -eq '') -and $isGalleryVersionLessEquals) {
278269
Write-Verbose "You have the latest version of [$Name], with installed version [$ExistingVersion] and PSGallery version [$GalleryVersion]"
279270
# Conditional import
280271
Import-PSDependModule -Name $ModuleName -Action $PSDependAction -Version $ExistingVersion
281272

282-
if($PSDependAction -contains 'Test')
283-
{
273+
if($PSDependAction -contains 'Test') {
284274
return $True
285275
}
286276
return $null
@@ -289,24 +279,18 @@ if($Existing)
289279
}
290280

291281
#No dependency found, return false if we're testing alone...
292-
if( $PSDependAction -contains 'Test' -and $PSDependAction.count -eq 1)
293-
{
282+
if( $PSDependAction -contains 'Test' -and $PSDependAction.count -eq 1) {
294283
return $False
295284
}
296285

297-
if($PSDependAction -contains 'Install')
298-
{
299-
if('AllUsers', 'CurrentUser' -contains $Scope)
300-
{
286+
if($PSDependAction -contains 'Install') {
287+
if('AllUsers', 'CurrentUser' -contains $Scope) {
301288
Write-Verbose "Installing [$Name] with scope [$Scope]"
302289
Install-Module @params -Scope $Scope
303-
}
304-
else
305-
{
290+
} else {
306291
Write-Verbose "Saving [$Name] with path [$Scope]"
307292
Write-Verbose "Creating directory path to [$Scope]"
308-
if(-not (Test-Path $Scope -ErrorAction SilentlyContinue))
309-
{
293+
if(-not (Test-Path $Scope -ErrorAction SilentlyContinue)) {
310294
$Null = New-Item -ItemType Directory -Path $Scope -Force -ErrorAction SilentlyContinue
311295
}
312296
Save-Module @params -Path $Scope

0 commit comments

Comments
Ā (0)