diff --git a/CHANGELOG.md b/CHANGELOG.md index 30dd8d1f9..a26d5032f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed the regular expression `features?` from the GitVersion configuration. Before, if a fix commit mentioned the word feature but means a SQL Server feature GitVersion would bump minor instead of patch number. + - Update pipeline script that is used to resolve dependencies. - When running in Azure Pipelines any existing SqlServer module is removed before running integration tests, so the tests can update to latest version. - `Get-SqlDscAudit` diff --git a/Resolve-Dependency.ps1 b/Resolve-Dependency.ps1 index a8c6b4d10..f05a1ab12 100644 --- a/Resolve-Dependency.ps1 +++ b/Resolve-Dependency.ps1 @@ -167,19 +167,32 @@ catch Write-Progress -Activity 'Bootstrap:' -PercentComplete 0 -CurrentOperation 'NuGet Bootstrap' -# TODO: This should handle the parameter $AllowOldPowerShellGetModule. -$powerShellGetModule = Import-Module -Name 'PowerShellGet' -MinimumVersion '2.0' -ErrorAction 'SilentlyContinue' -PassThru +$importModuleParameters = @{ + Name = 'PowerShellGet' + MinimumVersion = '2.0' + ErrorAction = 'SilentlyContinue' + PassThru = $true +} + +if ($AllowOldPowerShellGetModule) +{ + $importModuleParameters.Remove('MinimumVersion') +} + +$powerShellGetModule = Import-Module @importModuleParameters # Install the package provider if it is not available. -$nuGetProvider = Get-PackageProvider -Name 'NuGet' -ListAvailable | Select-Object -First 1 +$nuGetProvider = Get-PackageProvider -Name 'NuGet' -ListAvailable -ErrorAction 'SilentlyContinue' | + Select-Object -First 1 if (-not $powerShellGetModule -and -not $nuGetProvider) { $providerBootstrapParameters = @{ - Name = 'nuget' + Name = 'NuGet' Force = $true ForceBootstrap = $true ErrorAction = 'Stop' + Scope = 'CurrentUser' } switch ($PSBoundParameters.Keys) @@ -196,7 +209,7 @@ if (-not $powerShellGetModule -and -not $nuGetProvider) 'Scope' { - $providerBootstrapParameters.Add('Scope', $Scope) + $providerBootstrapParameters.Scope = $Scope } } @@ -205,15 +218,15 @@ if (-not $powerShellGetModule -and -not $nuGetProvider) $providerBootstrapParameters.Add('AllowPrerelease', $true) } - Write-Information -MessageData 'Bootstrap: Installing NuGet Package Provider from the web (Make sure Microsoft addresses/ranges are allowed).' + Write-Information -MessageData 'Bootstrap: Installing NuGet Package Provider from the web (Make sure Microsoft addresses/ranges are allowed).' -InformationAction 'Continue' - $null = Install-PackageProvider @providerBootstrapParams + $null = Install-PackageProvider @providerBootstrapParameters $nuGetProvider = Get-PackageProvider -Name 'NuGet' -ListAvailable | Select-Object -First 1 $nuGetProviderVersion = $nuGetProvider.Version.ToString() - Write-Information -MessageData "Bootstrap: Importing NuGet Package Provider version $nuGetProviderVersion to current session." + Write-Information -MessageData "Bootstrap: Importing NuGet Package Provider version $nuGetProviderVersion to current session." -InformationAction 'Continue' $Null = Import-PackageProvider -Name 'NuGet' -RequiredVersion $nuGetProviderVersion -Force } @@ -237,47 +250,79 @@ try # Versions below 2.0 are considered old, unreliable & not recommended if (-not $powerShellGetVersion -or ($powerShellGetVersion -lt [System.Version] '2.0' -and -not $AllowOldPowerShellGetModule)) { - Write-Progress -Activity 'Bootstrap:' -PercentComplete 40 -CurrentOperation 'Installing newer version of PowerShellGet' - - $installPowerShellGetParameters = @{ - Name = 'PowerShellGet' - Force = $True - SkipPublisherCheck = $true - AllowClobber = $true - Scope = $Scope - Repository = $Gallery - } + Write-Progress -Activity 'Bootstrap:' -PercentComplete 40 -CurrentOperation 'Fetching newer version of PowerShellGet' - switch ($PSBoundParameters.Keys) + # PowerShellGet module not found, installing or saving it. + if ($PSDependTarget -in 'CurrentUser', 'AllUsers') { - 'Proxy' - { - $installPowerShellGetParameters.Add('Proxy', $Proxy) + Write-Debug -Message "PowerShellGet module not found. Attempting to install from Gallery $Gallery." + + Write-Warning -Message "Installing PowerShellGet in $PSDependTarget Scope." + + $installPowerShellGetParameters = @{ + Name = 'PowerShellGet' + Force = $True + SkipPublisherCheck = $true + AllowClobber = $true + Scope = $Scope + Repository = $Gallery } - 'ProxyCredential' + switch ($PSBoundParameters.Keys) { - $installPowerShellGetParameters.Add('ProxyCredential', $ProxyCredential) + 'Proxy' + { + $installPowerShellGetParameters.Add('Proxy', $Proxy) + } + + 'ProxyCredential' + { + $installPowerShellGetParameters.Add('ProxyCredential', $ProxyCredential) + } + + 'GalleryCredential' + { + $installPowerShellGetParameters.Add('Credential', $GalleryCredential) + } } - 'GalleryCredential' - { - $installPowerShellGetParameters.Add('Credential', $GalleryCredential) + Write-Progress -Activity 'Bootstrap:' -PercentComplete 60 -CurrentOperation 'Installing newer version of PowerShellGet' + + Install-Module @installPowerShellGetParameters + } + else + { + Write-Debug -Message "PowerShellGet module not found. Attempting to Save from Gallery $Gallery to $PSDependTarget" + + $saveModuleParameters = @{ + Name = 'PowerShellGet' + MaximumVersion = '2.99.99' + Repository = $Gallery + Path = $PSDependTarget + Force = $true } + + Write-Progress -Activity 'Bootstrap:' -PercentComplete 60 -CurrentOperation "Saving PowerShellGet from $Gallery to $Scope" + + Save-Module @saveModuleParameters } - Write-Progress -Activity 'Bootstrap:' -PercentComplete 60 -CurrentOperation 'Installing newer version of PowerShellGet' + Write-Debug -Message 'Removing previous versions of PowerShellGet and PackageManagement from session' + + Get-Module -Name 'PowerShellGet' -All | Remove-Module -Force -ErrorAction 'SilentlyContinue' + Get-Module -Name 'PackageManagement' -All | Remove-Module -Force - Install-Module @installPowerShellGetParameters + Write-Progress -Activity 'Bootstrap:' -PercentComplete 65 -CurrentOperation 'Loading latest version of PowerShellGet' - Remove-Module -Name 'PowerShellGet' -Force -ErrorAction 'SilentlyContinue' - Remove-Module -Name 'PackageManagement' -Force + Write-Debug -Message 'Importing latest PowerShellGet and PackageManagement versions into session' - $powerShellGetModule = Import-Module PowerShellGet -Force -PassThru + Import-Module -Name 'PackageManagement' -MinimumVersion '1.4.8.1' -Force + + $powerShellGetModule = Import-Module -Name 'PowerShellGet' -MinimumVersion '2.2.5' -Force -PassThru $powerShellGetVersion = $powerShellGetModule.Version.ToString() - Write-Information -MessageData "Bootstrap: PowerShellGet version loaded is $powerShellGetVersion" + Write-Information -MessageData "Bootstrap: PowerShellGet version loaded is $powerShellGetVersion" -InformationAction 'Continue' } # Try to import the PSDepend module from the available modules. @@ -343,7 +388,7 @@ try $saveModuleParameters.add('MinimumVersion', $MinimumPSDependVersion) } - Write-Progress -Activity 'Bootstrap:' -PercentComplete 75 -CurrentOperation "Saving & Importing PSDepend from $Gallery to $Scope" + Write-Progress -Activity 'Bootstrap:' -PercentComplete 75 -CurrentOperation "Saving PSDepend from $Gallery to $Scope" Save-Module @saveModuleParameters } @@ -386,7 +431,7 @@ try } else { - Write-Verbose "PowerShell-Yaml is already available" + Write-Verbose 'PowerShell-Yaml is already available' } Write-Progress -Activity 'Bootstrap:' -PercentComplete 88 -CurrentOperation 'Importing PowerShell module PowerShell-Yaml' @@ -396,7 +441,7 @@ try Write-Progress -Activity 'Bootstrap:' -PercentComplete 90 -CurrentOperation 'Invoke PSDepend' - Write-Progress -Activity "PSDepend:" -PercentComplete 0 -CurrentOperation "Restoring Build Dependencies" + Write-Progress -Activity 'PSDepend:' -PercentComplete 0 -CurrentOperation 'Restoring Build Dependencies' if (Test-Path -Path $DependencyFile) { @@ -409,13 +454,14 @@ try Invoke-PSDepend @psDependParameters } - Write-Progress -Activity "PSDepend:" -PercentComplete 100 -CurrentOperation "Dependencies restored" -Completed + Write-Progress -Activity 'PSDepend:' -PercentComplete 100 -CurrentOperation 'Dependencies restored' -Completed - Write-Progress -Activity 'Bootstrap:' -PercentComplete 100 -CurrentOperation "Bootstrap complete" -Completed + Write-Progress -Activity 'Bootstrap:' -PercentComplete 100 -CurrentOperation 'Bootstrap complete' -Completed } finally { # Reverting the Installation Policy for the given gallery Set-PSRepository -Name $Gallery -InstallationPolicy $previousGalleryInstallationPolicy - Write-Verbose -Message "Project Bootstrapped, returning to Invoke-Build" + + Write-Verbose -Message 'Project Bootstrapped, returning to Invoke-Build' }