diff --git a/CHANGELOG.md b/CHANGELOG.md index 59bcde1a..07058e4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,11 +14,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 for the built module and all required modules, to avoid returning duplicate modules when using `Get-Module -ListAvailable`. The templates already has this configuration. -- Update PSResourceGet to default to v1.0.1 if no version is passed in parameter - or specific version is configured. +- Now PSResourceGet always default to the latest released version if no + specific version is configured or passed as parameter. - Templates was changed to use PSResourceGet as the default method of resolving dependencies. It is possible to change to the method - PowerShellGet & PSDepend by changing the configuration. + PowerShellGet & PSDepend by changing the configuration. Also default to + using PowerShellGet v3 which is a compatibility module that is a wrapper + for the equivalent command in PSResourceGet. ### Fixed @@ -33,6 +35,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [NuGet version ranges](https://learn.microsoft.com/en-us/nuget/concepts/package-versioning#version-ranges) in _RequiredModules.psd1_, although then the file is not compatible with PSResourceGet or PSDepend (so no fallback can happen). +- Now it won't import legacy PowerShellGet and PackageManagement when + PSResourceGet or ModuleFast is used. +- Now it works saving PowerShellGet compatibility module when configured. +- Now if both ModuleFast and PowerShellGet compatibility module is configured + PSResourceGet is automatically added as a dependency. This is for example + needed for publishing built module to the gallery. ## [0.117.0] - 2023-09-29 diff --git a/README.md b/README.md index 72a730a7..43c8ca02 100644 --- a/README.md +++ b/README.md @@ -99,13 +99,13 @@ command line by passing the parameter `UsePSResourceGet` to the build script `build.ps1`, e.g. `.\build.ps1 -ResolveDependency -Tasks noop -UsePSResourceGet` If both PSResourceGet and ModuleFast is enabled then PSResource will be -preferred on Windows PowerShell and PowerShell 7.2 or lower. ModuleFast -will be preferred on PowerShell 7.3 or higher. +preferred on Windows PowerShell and PowerShell 7.1 or lower. ModuleFast +will be preferred on PowerShell 7.2 or higher. #### ModuleFast It is possible to use [ModuleFast](https://github.com/JustinGrote/ModuleFast) -to resolve dependencies. ModuleFast only works with PowerShell 7.3 or higher. +to resolve dependencies. ModuleFast only works with PowerShell 7.2 or higher. To use ModuleFast as a replacement for PowerShellGet it is possible to enable it in the configuration file `Resolve-Dependency.psd1`. It is also possible to allow the repository to use PowerShellGet as the @@ -114,12 +114,12 @@ the parameter `UseModuleFast` to the build script `build.ps1`, e.g. `.\build.ps1 -ResolveDependency -Tasks noop -UseModuleFast`. If both PSResourceGet and ModuleFast is enabled then ModuleFast will be -preferred on PowerShell 7.3 or higher. PSResource will be preferred -on Windows PowerShell and PowerShell 7.2 or lower. +preferred on PowerShell 7.2 or higher. PSResource will be preferred +on Windows PowerShell and PowerShell 7.1 or lower. When using ModuleFast as the only method there is more options to specify modules in the file RequiredModules.psd1. This syntax will limit resolving -dependencies to just ModuleFast (and PowerShell 7.3 or higher) as they are +dependencies to just ModuleFast (and PowerShell 7.2 or higher) as they are not supported by the other methods. See the comment-based help of the command [`Install-ModuleFast`](https://github.com/JustinGrote/ModuleFast/blob/main/ModuleFast.psm1) for more information of the available syntax. diff --git a/Resolve-Dependency.ps1 b/Resolve-Dependency.ps1 index ca266712..17cc98ec 100644 --- a/Resolve-Dependency.ps1 +++ b/Resolve-Dependency.ps1 @@ -135,7 +135,15 @@ param [Parameter()] [System.String] - $PSResourceGetVersion + $PSResourceGetVersion, + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $UsePowerShellGetCompatibilityModule, + + [Parameter()] + [System.String] + $UsePowerShellGetCompatibilityModuleVersion ) try @@ -198,21 +206,21 @@ if ($UseModuleFast -and $UsePSResourceGet) { Write-Information -MessageData 'Both ModuleFast and PSResourceGet is configured or/and passed as parameter.' -InformationAction 'Continue' - if ($PSVersionTable.PSVersion -ge '7.3') + if ($PSVersionTable.PSVersion -ge '7.2') { $UsePSResourceGet = $false - Write-Information -MessageData 'PowerShell 7.3 or higher being used, prefer ModuleFast over PSResourceGet.' -InformationAction 'Continue' + Write-Information -MessageData 'PowerShell 7.2 or higher being used, prefer ModuleFast over PSResourceGet.' -InformationAction 'Continue' } else { $UseModuleFast = $false - Write-Information -MessageData 'Windows PowerShell or PowerShell <=7.2 is being used, prefer PSResourceGet since ModuleFast is not supported on this version of PowerShell.' -InformationAction 'Continue' + Write-Information -MessageData 'Windows PowerShell or PowerShell <=7.1 is being used, prefer PSResourceGet since ModuleFast is not supported on this version of PowerShell.' -InformationAction 'Continue' } } -# Only bootstrao ModuleFast if it is not already imported. +# Only bootstrap ModuleFast if it is not already imported. if ($UseModuleFast -and -not (Get-Module -Name 'ModuleFast')) { try @@ -227,6 +235,11 @@ if ($UseModuleFast -and -not (Get-Module -Name 'ModuleFast')) } elseif($ModuleFastVersion) { + if ($ModuleFastVersion -notmatch 'v') + { + $ModuleFastVersion = 'v{0}' -f $ModuleFastVersion + } + Write-Information -MessageData ('ModuleFast is configured to use version {0}.' -f $ModuleFastVersion) -InformationAction 'Continue' $moduleFastBootstrapScriptBlockParameters.Release = $ModuleFastVersion @@ -279,13 +292,17 @@ if ($UsePSResourceGet) { if (-not $PSResourceGetVersion) { - # Default version to use if none is passed in parameter or specified in configuration. - $PSResourceGetVersion = '1.0.1' + # Default to latest version if no version is passed in parameter or specified in configuration. + $psResourceGetUri = "https://www.powershellgallery.com/api/v2/package/$psResourceGetModuleName" + } + else + { + $psResourceGetUri = "https://www.powershellgallery.com/api/v2/package/$psResourceGetModuleName/$PSResourceGetVersion" } $invokeWebRequestParameters = @{ # TODO: Should support proxy parameters passed to the script. - Uri = "https://www.powershellgallery.com/api/v2/package/$psResourceGetModuleName/$PSResourceGetVersion" + Uri = $psResourceGetUri OutFile = "$PSDependTarget/$psResourceGetModuleName.nupkg" # cSpell: ignore nupkg ErrorAction = 'Stop' } @@ -497,8 +514,12 @@ if (-not ($UseModuleFast -or $UsePSResourceGet)) # Fail if the given PSGallery is not registered. $previousGalleryInstallationPolicy = (Get-PSRepository -Name $Gallery -ErrorAction 'Stop').Trusted + $updatedGalleryInstallationPolicy = $false + if ($previousGalleryInstallationPolicy -ne $true) { + $updatedGalleryInstallationPolicy = $true + # Only change policy if the repository is not trusted Set-PSRepository -Name $Gallery -InstallationPolicy 'Trusted' -ErrorAction 'Ignore' } @@ -743,6 +764,34 @@ try $modulesToSave += 'PowerShell-Yaml' } + if ($UsePowerShellGetCompatibilityModule) + { + Write-Debug -Message 'PowerShellGet compatibility module is configured to be used.' + + # This is needed to ensure that the PowerShellGet compatibility module works. + $psResourceGetModuleName = 'Microsoft.PowerShell.PSResourceGet' + + if ($PSResourceGetVersion) + { + $modulesToSave += ('{0}:[{1}]' -f $psResourceGetModuleName, $PSResourceGetVersion) + } + else + { + $modulesToSave += $psResourceGetModuleName + } + + $powerShellGetCompatibilityModuleName = 'PowerShellGet' + + if ($UsePowerShellGetCompatibilityModuleVersion) + { + $modulesToSave += ('{0}:[{1}]' -f $powerShellGetCompatibilityModuleName, $UsePowerShellGetCompatibilityModuleVersion) + } + else + { + $modulesToSave += $powerShellGetCompatibilityModuleName + } + } + foreach ($requiredModule in $requiredModules) { # If the RequiredModules.psd1 entry is an Hashtable then special handling is needed. @@ -997,10 +1046,10 @@ finally Register-PSRepository @registerPSRepositoryParameters } - # Only try to revert installation policy if the repository exist - if ((Get-PSRepository -Name $Gallery -ErrorAction 'SilentlyContinue')) + if ($updatedGalleryInstallationPolicy -eq $true -and $previousGalleryInstallationPolicy -ne $true) { - if ($previousGalleryInstallationPolicy -ne $true) + # Only try to revert installation policy if the repository exist + if ((Get-PSRepository -Name $Gallery -ErrorAction 'SilentlyContinue')) { # Reverting the Installation Policy for the given gallery if it was not already trusted Set-PSRepository -Name $Gallery -InstallationPolicy 'Untrusted' diff --git a/Resolve-Dependency.psd1 b/Resolve-Dependency.psd1 index 496c142b..f80e3727 100644 --- a/Resolve-Dependency.psd1 +++ b/Resolve-Dependency.psd1 @@ -41,7 +41,7 @@ <# Enable ModuleFast to be the default method of resolving dependencies by setting - UseModuleFast to the value $true. ModuleFast requires PowerShell 7.3 or higher. + UseModuleFast to the value $true. ModuleFast requires PowerShell 7.2 or higher. If UseModuleFast is not configured or set to $false then PowerShellGet (or PSResourceGet if enabled) will be used to as the default method of resolving dependencies. You can always use the parameter `-UseModuleFast` of the @@ -64,7 +64,7 @@ `-UseModuleFast` of the Resolve-Dependency.ps1 or build.ps1 script. #> #UseModuleFast = $true - #ModuleFastVersion = 'v0.1.0-rc1' + #ModuleFastVersion = '0.1.2' #ModuleFastBleedingEdge = $true <# @@ -74,6 +74,8 @@ #> UsePSResourceGet = $true PSResourceGetVersion = '1.0.1' - #UsePowerShellGetCompatibilityModule = $true - #UsePowerShellGetCompatibilityModuleVersion = '3.0.22-beta22' + + # PowerShellGet compatibility module only works when using PSResourceGet or ModuleFast. + UsePowerShellGetCompatibilityModule = $true + UsePowerShellGetCompatibilityModuleVersion = '3.0.23-beta23' } diff --git a/Sampler/Templates/Build/Resolve-Dependency.ps1 b/Sampler/Templates/Build/Resolve-Dependency.ps1 index ca266712..17cc98ec 100644 --- a/Sampler/Templates/Build/Resolve-Dependency.ps1 +++ b/Sampler/Templates/Build/Resolve-Dependency.ps1 @@ -135,7 +135,15 @@ param [Parameter()] [System.String] - $PSResourceGetVersion + $PSResourceGetVersion, + + [Parameter()] + [System.Management.Automation.SwitchParameter] + $UsePowerShellGetCompatibilityModule, + + [Parameter()] + [System.String] + $UsePowerShellGetCompatibilityModuleVersion ) try @@ -198,21 +206,21 @@ if ($UseModuleFast -and $UsePSResourceGet) { Write-Information -MessageData 'Both ModuleFast and PSResourceGet is configured or/and passed as parameter.' -InformationAction 'Continue' - if ($PSVersionTable.PSVersion -ge '7.3') + if ($PSVersionTable.PSVersion -ge '7.2') { $UsePSResourceGet = $false - Write-Information -MessageData 'PowerShell 7.3 or higher being used, prefer ModuleFast over PSResourceGet.' -InformationAction 'Continue' + Write-Information -MessageData 'PowerShell 7.2 or higher being used, prefer ModuleFast over PSResourceGet.' -InformationAction 'Continue' } else { $UseModuleFast = $false - Write-Information -MessageData 'Windows PowerShell or PowerShell <=7.2 is being used, prefer PSResourceGet since ModuleFast is not supported on this version of PowerShell.' -InformationAction 'Continue' + Write-Information -MessageData 'Windows PowerShell or PowerShell <=7.1 is being used, prefer PSResourceGet since ModuleFast is not supported on this version of PowerShell.' -InformationAction 'Continue' } } -# Only bootstrao ModuleFast if it is not already imported. +# Only bootstrap ModuleFast if it is not already imported. if ($UseModuleFast -and -not (Get-Module -Name 'ModuleFast')) { try @@ -227,6 +235,11 @@ if ($UseModuleFast -and -not (Get-Module -Name 'ModuleFast')) } elseif($ModuleFastVersion) { + if ($ModuleFastVersion -notmatch 'v') + { + $ModuleFastVersion = 'v{0}' -f $ModuleFastVersion + } + Write-Information -MessageData ('ModuleFast is configured to use version {0}.' -f $ModuleFastVersion) -InformationAction 'Continue' $moduleFastBootstrapScriptBlockParameters.Release = $ModuleFastVersion @@ -279,13 +292,17 @@ if ($UsePSResourceGet) { if (-not $PSResourceGetVersion) { - # Default version to use if none is passed in parameter or specified in configuration. - $PSResourceGetVersion = '1.0.1' + # Default to latest version if no version is passed in parameter or specified in configuration. + $psResourceGetUri = "https://www.powershellgallery.com/api/v2/package/$psResourceGetModuleName" + } + else + { + $psResourceGetUri = "https://www.powershellgallery.com/api/v2/package/$psResourceGetModuleName/$PSResourceGetVersion" } $invokeWebRequestParameters = @{ # TODO: Should support proxy parameters passed to the script. - Uri = "https://www.powershellgallery.com/api/v2/package/$psResourceGetModuleName/$PSResourceGetVersion" + Uri = $psResourceGetUri OutFile = "$PSDependTarget/$psResourceGetModuleName.nupkg" # cSpell: ignore nupkg ErrorAction = 'Stop' } @@ -497,8 +514,12 @@ if (-not ($UseModuleFast -or $UsePSResourceGet)) # Fail if the given PSGallery is not registered. $previousGalleryInstallationPolicy = (Get-PSRepository -Name $Gallery -ErrorAction 'Stop').Trusted + $updatedGalleryInstallationPolicy = $false + if ($previousGalleryInstallationPolicy -ne $true) { + $updatedGalleryInstallationPolicy = $true + # Only change policy if the repository is not trusted Set-PSRepository -Name $Gallery -InstallationPolicy 'Trusted' -ErrorAction 'Ignore' } @@ -743,6 +764,34 @@ try $modulesToSave += 'PowerShell-Yaml' } + if ($UsePowerShellGetCompatibilityModule) + { + Write-Debug -Message 'PowerShellGet compatibility module is configured to be used.' + + # This is needed to ensure that the PowerShellGet compatibility module works. + $psResourceGetModuleName = 'Microsoft.PowerShell.PSResourceGet' + + if ($PSResourceGetVersion) + { + $modulesToSave += ('{0}:[{1}]' -f $psResourceGetModuleName, $PSResourceGetVersion) + } + else + { + $modulesToSave += $psResourceGetModuleName + } + + $powerShellGetCompatibilityModuleName = 'PowerShellGet' + + if ($UsePowerShellGetCompatibilityModuleVersion) + { + $modulesToSave += ('{0}:[{1}]' -f $powerShellGetCompatibilityModuleName, $UsePowerShellGetCompatibilityModuleVersion) + } + else + { + $modulesToSave += $powerShellGetCompatibilityModuleName + } + } + foreach ($requiredModule in $requiredModules) { # If the RequiredModules.psd1 entry is an Hashtable then special handling is needed. @@ -997,10 +1046,10 @@ finally Register-PSRepository @registerPSRepositoryParameters } - # Only try to revert installation policy if the repository exist - if ((Get-PSRepository -Name $Gallery -ErrorAction 'SilentlyContinue')) + if ($updatedGalleryInstallationPolicy -eq $true -and $previousGalleryInstallationPolicy -ne $true) { - if ($previousGalleryInstallationPolicy -ne $true) + # Only try to revert installation policy if the repository exist + if ((Get-PSRepository -Name $Gallery -ErrorAction 'SilentlyContinue')) { # Reverting the Installation Policy for the given gallery if it was not already trusted Set-PSRepository -Name $Gallery -InstallationPolicy 'Untrusted' diff --git a/Sampler/Templates/Build/Resolve-Dependency.psd1.template b/Sampler/Templates/Build/Resolve-Dependency.psd1.template index d1d8a943..08ec2a75 100644 --- a/Sampler/Templates/Build/Resolve-Dependency.psd1.template +++ b/Sampler/Templates/Build/Resolve-Dependency.psd1.template @@ -43,7 +43,7 @@ <# Enable ModuleFast to be the default method of resolving dependencies by setting - UseModuleFast to the value $true. ModuleFast requires PowerShell 7.3 or higher. + UseModuleFast to the value $true. ModuleFast requires PowerShell 7.2 or higher. If UseModuleFast is not configured or set to $false then PowerShellGet (or PSResourceGet if enabled) will be used to as the default method of resolving dependencies. You can always use the parameter `-UseModuleFast` of the @@ -66,7 +66,7 @@ `-UseModuleFast` of the Resolve-Dependency.ps1 or build.ps1 script. #> #UseModuleFast = $true - #ModuleFastVersion = 'v0.1.0-rc1' + #ModuleFastVersion = '0.1.2' #ModuleFastBleedingEdge = $true <# @@ -76,6 +76,8 @@ #> UsePSResourceGet = $true PSResourceGetVersion = '1.0.1' - #UsePowerShellGetCompatibilityModule = $true - #UsePowerShellGetCompatibilityModuleVersion = '3.0.22-beta22' + + # PowerShellGet compatibility module only works when using PSResourceGet or ModuleFast. + UsePowerShellGetCompatibilityModule = $true + UsePowerShellGetCompatibilityModuleVersion = '3.0.23-beta23' }