Skip to content

Support Module Manifests with constrained language scope #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
# License

Copyright 2023 Justin Grote @justinwgrote

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## Third Party Notices

### [PSResourceGet](https://github.com/powershell/psresourceget)

Copyright (c) Microsoft Corporation.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 33 additions & 5 deletions ModuleFast.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ function Install-ModuleFastHelper {
}

#TODO: Dedupe all import-powershelldatafile operations to a function ideally
$existingModuleMetadata = Import-PowerShellDataFile $existingManifestPath
$existingModuleMetadata = Import-ModuleManifest $existingManifestPath
$existingVersion = [NugetVersion]::new(
$existingModuleMetadata.ModuleVersion,
$existingModuleMetadata.privatedata.psdata.prerelease
Expand Down Expand Up @@ -933,6 +933,34 @@ function Install-ModuleFastHelper {
}
}

function Import-ModuleManifest {
<#
.SYNOPSIS
Imports a module manifest from a path, and can handle some limited dynamic module manifest formats.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)][string]$Path
)

try {
Import-PowerShellDataFile $Path -ErrorAction Stop
} catch [InvalidOperationException] {
if ($PSItem.Exception.Message -notlike '*Cannot generate a PowerShell object for a ScriptBlock evaluating dynamic expressions*') {throw}

Write-Debug "$Path is a Manifest with dynamic expressions. Attempting to safe evaluate..."
#Inspiration from: https://github.com/PowerShell/PSResourceGet/blob/0a1836a4088ab0f4f13a4638fa8cd0f571c24140/src/code/Utils.cs#L1219
$manifest = [ScriptBlock]::Create((Get-Content $Path -Raw))

$manifest.CheckRestrictedLanguage(
[list[string]]::new(),
[list[string]]@('PSEdition','PSScriptRoot'),
$true
)
return $manifest.InvokeReturnAsIs();
}
}

#endregion Private

#region Classes
Expand Down Expand Up @@ -1547,7 +1575,7 @@ function Find-LocalModule {
[string]$classicManifestPath = $classicManifestPaths[0]
if ($classicManifestPath) {
#NOTE: This does result in Import-PowerShellData getting called twice which isn't ideal for performance, but classic modules should be fairly rare and not worth optimizing.
[version]$classicVersion = (Import-PowerShellDataFile $classicManifestPath).ModuleVersion
[version]$classicVersion = (Import-ModuleManifest $classicManifestPath).ModuleVersion
Write-Debug "${ModuleSpec}: Found classic module $classicVersion at $moduleBaseDir"
$candidatePaths.Add([Tuple]::Create($classicVersion, $moduleBaseDir))
}
Expand Down Expand Up @@ -1708,7 +1736,7 @@ function Read-RequiredSpecFile ($RequiredSpecPath) {
#HACK: Cannot read PowerShell Data Files from a string, the interface is private, so we write to a temp file as a workaround.
$tempFile = [io.path]::GetTempFileName()
$content > $tempFile
return Import-PowerShellDataFile -Path $tempFile
return Import-ModuleManifest -Path $tempFile
} else {
$json = ConvertFrom-Json $content -Depth 5
return $json
Expand All @@ -1720,7 +1748,7 @@ function Read-RequiredSpecFile ($RequiredSpecPath) {
$extension = [Path]::GetExtension($resolvedPath)

if ($extension -eq '.psd1') {
$manifestData = Import-PowerShellDataFile -Path $resolvedPath
$manifestData = Import-ModuleManifest -Path $resolvedPath
if ($manifestData.ModuleVersion) {
[ModuleSpecification[]]$requiredModules = $manifestData.RequiredModules
Write-Debug 'Detected a Module Manifest, evaluating RequiredModules'
Expand Down Expand Up @@ -1773,7 +1801,7 @@ filter ConvertFrom-ModuleManifest {
$ErrorActionPreference = 'Stop'

$ManifestName = Split-Path -Path $ManifestPath -LeafBase
$manifestData = Import-PowerShellDataFile -Path $ManifestPath -ErrorAction stop
$manifestData = Import-ModuleManifest -Path $ManifestPath

[Version]$manifestVersionData = $null
if (-not [Version]::TryParse($manifestData.ModuleVersion, [ref]$manifestVersionData)) {
Expand Down
14 changes: 14 additions & 0 deletions ModuleFast.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ InModuleScope 'ModuleFast' {
}
}
}

Describe 'Import-ModuleManifest' {
It 'Reads Dynamic Manifest' {
$Mocks = "$PSScriptRoot/Test/Mocks"
$manifest = Import-ModuleManifest "$Mocks/Dynamic.psd1"
$manifest | Should -BeOfType [System.Collections.Hashtable]
$manifest.ModuleVersion | Should -Be '1.0.0'
$manifest.RootModule | Should -Be 'coreclr\PrtgAPI.PowerShell.dll'
}
}
}

Describe 'Get-ModuleFastPlan' -Tag 'E2E' {
Expand Down Expand Up @@ -551,6 +561,10 @@ Describe 'Install-ModuleFast' -Tag 'E2E' {
@{
Name = 'ScriptModule'
File = 'RequiresModule.psm1'
},
@{
Name = 'DynamicManifest'
File = 'Dynamic.psd1'
}
)

Expand Down
10 changes: 10 additions & 0 deletions Test/Mocks/Dynamic.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@{
ModuleVersion = '1.0.0'
RootModule = if ($true) {
'coreclr\PrtgAPI.PowerShell.dll'
} else {
# Desktop
'fullclr\PrtgAPI.PowerShell.dll'
}
RequiredModules = @('PrereleaseTest')
}