Skip to content

Commit dec0fb0

Browse files
🌟 [Feature]: Add Get-ModuleManifest function to retrieve module manifest details
1 parent b73de61 commit dec0fb0

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

‎scripts/Helpers/Helpers.psm1‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,125 @@ function Install-PSModule {
323323
return $codePath
324324
}
325325
}
326+
327+
function Get-ModuleManifest {
328+
<#
329+
.SYNOPSIS
330+
Get the module manifest.
331+
332+
.DESCRIPTION
333+
Get the module manifest as a path, file info, content, or hashtable.
334+
335+
.EXAMPLE
336+
Get-PSModuleManifest -Path 'src/PSModule/PSModule.psd1' -As Hashtable
337+
#>
338+
[OutputType([string], [System.IO.FileInfo], [System.Collections.Hashtable], [System.Collections.Specialized.OrderedDictionary])]
339+
[CmdletBinding()]
340+
param(
341+
# Path to the module manifest file.
342+
[Parameter(Mandatory)]
343+
[string] $Path,
344+
345+
# The format of the output.
346+
[Parameter()]
347+
[ValidateSet('FileInfo', 'Content', 'Hashtable')]
348+
[string] $As = 'Hashtable'
349+
)
350+
351+
if (-not (Test-Path -Path $Path)) {
352+
Write-Warning 'No manifest file found.'
353+
return $null
354+
}
355+
Write-Verbose "Found manifest file [$Path]"
356+
357+
switch ($As) {
358+
'FileInfo' {
359+
return Get-Item -Path $Path
360+
}
361+
'Content' {
362+
return Get-Content -Path $Path
363+
}
364+
'Hashtable' {
365+
$manifest = [System.Collections.Specialized.OrderedDictionary]@{}
366+
$psData = [System.Collections.Specialized.OrderedDictionary]@{}
367+
$privateData = [System.Collections.Specialized.OrderedDictionary]@{}
368+
$tempManifest = Import-PowerShellDataFile -Path $Path
369+
if ($tempManifest.ContainsKey('PrivateData')) {
370+
$tempPrivateData = $tempManifest.PrivateData
371+
if ($tempPrivateData.ContainsKey('PSData')) {
372+
$tempPSData = $tempPrivateData.PSData
373+
$tempPrivateData.Remove('PSData')
374+
}
375+
}
376+
377+
$psdataOrder = @(
378+
'Tags'
379+
'LicenseUri'
380+
'ProjectUri'
381+
'IconUri'
382+
'ReleaseNotes'
383+
'Prerelease'
384+
'RequireLicenseAcceptance'
385+
'ExternalModuleDependencies'
386+
)
387+
foreach ($key in $psdataOrder) {
388+
if (($null -ne $tempPSData) -and ($tempPSData.ContainsKey($key))) {
389+
$psData.$key = $tempPSData.$key
390+
}
391+
}
392+
if ($psData.Count -gt 0) {
393+
$privateData.PSData = $psData
394+
} else {
395+
$privateData.Remove('PSData')
396+
}
397+
foreach ($key in $tempPrivateData.Keys) {
398+
$privateData.$key = $tempPrivateData.$key
399+
}
400+
401+
$manifestOrder = @(
402+
'RootModule'
403+
'ModuleVersion'
404+
'CompatiblePSEditions'
405+
'GUID'
406+
'Author'
407+
'CompanyName'
408+
'Copyright'
409+
'Description'
410+
'PowerShellVersion'
411+
'PowerShellHostName'
412+
'PowerShellHostVersion'
413+
'DotNetFrameworkVersion'
414+
'ClrVersion'
415+
'ProcessorArchitecture'
416+
'RequiredModules'
417+
'RequiredAssemblies'
418+
'ScriptsToProcess'
419+
'TypesToProcess'
420+
'FormatsToProcess'
421+
'NestedModules'
422+
'FunctionsToExport'
423+
'CmdletsToExport'
424+
'VariablesToExport'
425+
'AliasesToExport'
426+
'DscResourcesToExport'
427+
'ModuleList'
428+
'FileList'
429+
'HelpInfoURI'
430+
'DefaultCommandPrefix'
431+
'PrivateData'
432+
)
433+
foreach ($key in $manifestOrder) {
434+
if ($tempManifest.ContainsKey($key)) {
435+
$manifest.$key = $tempManifest.$key
436+
}
437+
}
438+
if ($privateData.Count -gt 0) {
439+
$manifest.PrivateData = $privateData
440+
} else {
441+
$manifest.Remove('PrivateData')
442+
}
443+
444+
return $manifest
445+
}
446+
}
447+
}

0 commit comments

Comments
 (0)