Skip to content

Commit

Permalink
Update to use PowerShell compatibility module (#457)
Browse files Browse the repository at this point in the history
- Also default to using PowerShellGet v3 which is a compatibility module that is a wrapper
  for the equivalent command in PSResourceGet.
- Now PSResourceGet always default to the latest released version if no
  specific version is configured or passed as parameter.
- 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.
  • Loading branch information
johlju authored Jan 13, 2024
1 parent 18a8ecb commit 5d47846
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 39 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
71 changes: 60 additions & 11 deletions Resolve-Dependency.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,15 @@ param

[Parameter()]
[System.String]
$PSResourceGetVersion
$PSResourceGetVersion,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$UsePowerShellGetCompatibilityModule,

[Parameter()]
[System.String]
$UsePowerShellGetCompatibilityModuleVersion
)

try
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'
}
Expand Down Expand Up @@ -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'
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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'
Expand Down
10 changes: 6 additions & 4 deletions Resolve-Dependency.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

<#
Expand All @@ -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'
}
71 changes: 60 additions & 11 deletions Sampler/Templates/Build/Resolve-Dependency.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,15 @@ param

[Parameter()]
[System.String]
$PSResourceGetVersion
$PSResourceGetVersion,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$UsePowerShellGetCompatibilityModule,

[Parameter()]
[System.String]
$UsePowerShellGetCompatibilityModuleVersion
)

try
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'
}
Expand Down Expand Up @@ -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'
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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'
Expand Down
10 changes: 6 additions & 4 deletions Sampler/Templates/Build/Resolve-Dependency.psd1.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

<#
Expand All @@ -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'
}

0 comments on commit 5d47846

Please sign in to comment.