Skip to content

Commit b5959b7

Browse files
authored
✨ Support Module Manifests with some constrained language scope
1 parent 6497928 commit b5959b7

4 files changed

Lines changed: 86 additions & 6 deletions

File tree

LICENSE

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1+
# License
2+
13
Copyright 2023 Justin Grote @justinwgrote
24

35
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:
46

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

7-
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.
9+
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.
10+
11+
## Third Party Notices
12+
13+
### [PSResourceGet](https://github.com/powershell/psresourceget)
14+
15+
Copyright (c) Microsoft Corporation.
16+
17+
MIT License
18+
19+
Permission is hereby granted, free of charge, to any person obtaining a copy
20+
of this software and associated documentation files (the "Software"), to deal
21+
in the Software without restriction, including without limitation the rights
22+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23+
copies of the Software, and to permit persons to whom the Software is
24+
furnished to do so, subject to the following conditions:
25+
26+
The above copyright notice and this permission notice shall be included in all
27+
copies or substantial portions of the Software.
28+
29+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35+
SOFTWARE.

ModuleFast.psm1

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ function Install-ModuleFastHelper {
825825
}
826826

827827
#TODO: Dedupe all import-powershelldatafile operations to a function ideally
828-
$existingModuleMetadata = Import-PowerShellDataFile $existingManifestPath
828+
$existingModuleMetadata = Import-ModuleManifest $existingManifestPath
829829
$existingVersion = [NugetVersion]::new(
830830
$existingModuleMetadata.ModuleVersion,
831831
$existingModuleMetadata.privatedata.psdata.prerelease
@@ -933,6 +933,34 @@ function Install-ModuleFastHelper {
933933
}
934934
}
935935

936+
function Import-ModuleManifest {
937+
<#
938+
.SYNOPSIS
939+
Imports a module manifest from a path, and can handle some limited dynamic module manifest formats.
940+
#>
941+
[CmdletBinding()]
942+
param(
943+
[Parameter(Mandatory, ValueFromPipeline)][string]$Path
944+
)
945+
946+
try {
947+
Import-PowerShellDataFile $Path -ErrorAction Stop
948+
} catch [InvalidOperationException] {
949+
if ($PSItem.Exception.Message -notlike '*Cannot generate a PowerShell object for a ScriptBlock evaluating dynamic expressions*') {throw}
950+
951+
Write-Debug "$Path is a Manifest with dynamic expressions. Attempting to safe evaluate..."
952+
#Inspiration from: https://github.com/PowerShell/PSResourceGet/blob/0a1836a4088ab0f4f13a4638fa8cd0f571c24140/src/code/Utils.cs#L1219
953+
$manifest = [ScriptBlock]::Create((Get-Content $Path -Raw))
954+
955+
$manifest.CheckRestrictedLanguage(
956+
[list[string]]::new(),
957+
[list[string]]@('PSEdition','PSScriptRoot'),
958+
$true
959+
)
960+
return $manifest.InvokeReturnAsIs();
961+
}
962+
}
963+
936964
#endregion Private
937965

938966
#region Classes
@@ -1547,7 +1575,7 @@ function Find-LocalModule {
15471575
[string]$classicManifestPath = $classicManifestPaths[0]
15481576
if ($classicManifestPath) {
15491577
#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.
1550-
[version]$classicVersion = (Import-PowerShellDataFile $classicManifestPath).ModuleVersion
1578+
[version]$classicVersion = (Import-ModuleManifest $classicManifestPath).ModuleVersion
15511579
Write-Debug "${ModuleSpec}: Found classic module $classicVersion at $moduleBaseDir"
15521580
$candidatePaths.Add([Tuple]::Create($classicVersion, $moduleBaseDir))
15531581
}
@@ -1708,7 +1736,7 @@ function Read-RequiredSpecFile ($RequiredSpecPath) {
17081736
#HACK: Cannot read PowerShell Data Files from a string, the interface is private, so we write to a temp file as a workaround.
17091737
$tempFile = [io.path]::GetTempFileName()
17101738
$content > $tempFile
1711-
return Import-PowerShellDataFile -Path $tempFile
1739+
return Import-ModuleManifest -Path $tempFile
17121740
} else {
17131741
$json = ConvertFrom-Json $content -Depth 5
17141742
return $json
@@ -1720,7 +1748,7 @@ function Read-RequiredSpecFile ($RequiredSpecPath) {
17201748
$extension = [Path]::GetExtension($resolvedPath)
17211749

17221750
if ($extension -eq '.psd1') {
1723-
$manifestData = Import-PowerShellDataFile -Path $resolvedPath
1751+
$manifestData = Import-ModuleManifest -Path $resolvedPath
17241752
if ($manifestData.ModuleVersion) {
17251753
[ModuleSpecification[]]$requiredModules = $manifestData.RequiredModules
17261754
Write-Debug 'Detected a Module Manifest, evaluating RequiredModules'
@@ -1773,7 +1801,7 @@ filter ConvertFrom-ModuleManifest {
17731801
$ErrorActionPreference = 'Stop'
17741802

17751803
$ManifestName = Split-Path -Path $ManifestPath -LeafBase
1776-
$manifestData = Import-PowerShellDataFile -Path $ManifestPath -ErrorAction stop
1804+
$manifestData = Import-ModuleManifest -Path $ManifestPath
17771805

17781806
[Version]$manifestVersionData = $null
17791807
if (-not [Version]::TryParse($manifestData.ModuleVersion, [ref]$manifestVersionData)) {

ModuleFast.tests.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ InModuleScope 'ModuleFast' {
7272
}
7373
}
7474
}
75+
76+
Describe 'Import-ModuleManifest' {
77+
It 'Reads Dynamic Manifest' {
78+
$Mocks = "$PSScriptRoot/Test/Mocks"
79+
$manifest = Import-ModuleManifest "$Mocks/Dynamic.psd1"
80+
$manifest | Should -BeOfType [System.Collections.Hashtable]
81+
$manifest.ModuleVersion | Should -Be '1.0.0'
82+
$manifest.RootModule | Should -Be 'coreclr\PrtgAPI.PowerShell.dll'
83+
}
84+
}
7585
}
7686

7787
Describe 'Get-ModuleFastPlan' -Tag 'E2E' {
@@ -551,6 +561,10 @@ Describe 'Install-ModuleFast' -Tag 'E2E' {
551561
@{
552562
Name = 'ScriptModule'
553563
File = 'RequiresModule.psm1'
564+
},
565+
@{
566+
Name = 'DynamicManifest'
567+
File = 'Dynamic.psd1'
554568
}
555569
)
556570

Test/Mocks/Dynamic.psd1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@{
2+
ModuleVersion = '1.0.0'
3+
RootModule = if ($true) {
4+
'coreclr\PrtgAPI.PowerShell.dll'
5+
} else {
6+
# Desktop
7+
'fullclr\PrtgAPI.PowerShell.dll'
8+
}
9+
RequiredModules = @('PrereleaseTest')
10+
}

0 commit comments

Comments
 (0)