|
| 1 | +function Add-ModuleManifestData { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Add data to a module manifest file property |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + This function adds data to a module manifest file property. |
| 8 | + If the property doesn't exist, it will be created. |
| 9 | + If it does exist, the new data will be appended to the existing data. |
| 10 | +
|
| 11 | + .EXAMPLE |
| 12 | + Add-ModuleManifestData -Path 'MyModule.psd1' -RequiredModules 'pester', 'platyPS' |
| 13 | +
|
| 14 | + Adds the modules 'pester' and 'platyPS' to the RequiredModules property of the module manifest file 'MyModule.psd1'. |
| 15 | + #> |
| 16 | + [CmdletBinding()] |
| 17 | + param( |
| 18 | + [Parameter(Mandatory)] |
| 19 | + [ValidateScript({ Test-Path -Path $_ -PathType Leaf })] |
| 20 | + [string]$Path, |
| 21 | + |
| 22 | + # Modules that must be imported into the global environment prior to importing this module. |
| 23 | + [Parameter()] |
| 24 | + [Object[]] $RequiredModules, |
| 25 | + |
| 26 | + # Compatible editions of PowerShell. |
| 27 | + [Parameter()] |
| 28 | + [string[]] $CompatiblePSEditions, |
| 29 | + |
| 30 | + # Assemblies that must be loaded prior to importing this module. |
| 31 | + [Parameter()] |
| 32 | + [string[]] $RequiredAssemblies, |
| 33 | + |
| 34 | + # Script files (.ps1) that are run in the caller's environment prior to importing this module. |
| 35 | + [Parameter()] |
| 36 | + [string[]] $ScriptsToProcess, |
| 37 | + |
| 38 | + # Type files (.ps1xml) to be loaded when importing this module. |
| 39 | + [Parameter()] |
| 40 | + [string[]] $TypesToProcess, |
| 41 | + |
| 42 | + # Format files (.ps1xml) to be loaded when importing this module. |
| 43 | + [Parameter()] |
| 44 | + [string[]] $FormatsToProcess, |
| 45 | + |
| 46 | + # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess. |
| 47 | + [Parameter()] |
| 48 | + [Object[]] $NestedModules, |
| 49 | + |
| 50 | + # Functions to export from this module, for best performance, do not use wildcards and do not |
| 51 | + # delete the entry, use an empty array if there are no functions to export. |
| 52 | + [Parameter()] |
| 53 | + [string[]] $FunctionsToExport, |
| 54 | + |
| 55 | + # Cmdlets to export from this module, for best performance, do not use wildcards and do not |
| 56 | + # delete the entry, use an empty array if there are no cmdlets to export. |
| 57 | + [Parameter()] |
| 58 | + [string[]] $CmdletsToExport, |
| 59 | + |
| 60 | + # Variables to export from this module. |
| 61 | + [Parameter()] |
| 62 | + [string[]] $VariablesToExport, |
| 63 | + |
| 64 | + # Aliases to export from this module, for best performance, do not use wildcards and do not |
| 65 | + # delete the entry, use an empty array if there are no aliases to export. |
| 66 | + [Parameter()] |
| 67 | + [string[]] $AliasesToExport, |
| 68 | + |
| 69 | + # DSC resources to export from this module. |
| 70 | + [Parameter()] |
| 71 | + [string[]] $DscResourcesToExport, |
| 72 | + |
| 73 | + # List of all modules packaged with this module. |
| 74 | + [Parameter()] |
| 75 | + [Object[]] $ModuleList, |
| 76 | + |
| 77 | + # List of all files packaged with this module. |
| 78 | + [Parameter()] |
| 79 | + [string[]] $FileList, |
| 80 | + |
| 81 | + # Tags applied to this module. These help with module discovery in online galleries. |
| 82 | + [Parameter()] |
| 83 | + [string[]] $Tags, |
| 84 | + |
| 85 | + # External dependent modules of this module. |
| 86 | + [Parameter()] |
| 87 | + [string[]] $ExternalModuleDependencies |
| 88 | + ) |
| 89 | + |
| 90 | + $moduleManifest = Get-ModuleManifest -Path $Path |
| 91 | + $changes = @{} |
| 92 | + |
| 93 | + if ($RequiredModules) { |
| 94 | + $RequiredModules += $moduleManifest.RequiredModules |
| 95 | + $changes.RequiredModules = $RequiredModules |
| 96 | + } |
| 97 | + if ($RequiredAssemblies) { |
| 98 | + $RequiredAssemblies += $moduleManifest.RequiredAssemblies |
| 99 | + $changes.RequiredAssemblies = $RequiredAssemblies |
| 100 | + } |
| 101 | + if ($CompatiblePSEditions) { |
| 102 | + $CompatiblePSEditions += $moduleManifest.CompatiblePSEditions |
| 103 | + $changes.CompatiblePSEditions = $CompatiblePSEditions |
| 104 | + } |
| 105 | + if ($ScriptsToProcess) { |
| 106 | + $ScriptsToProcess += $moduleManifest.ScriptsToProcess |
| 107 | + $changes.ScriptsToProcess = $ScriptsToProcess |
| 108 | + } |
| 109 | + if ($TypesToProcess) { |
| 110 | + $TypesToProcess += $moduleManifest.TypesToProcess |
| 111 | + $changes.TypesToProcess = $TypesToProcess |
| 112 | + } |
| 113 | + if ($FormatsToProcess) { |
| 114 | + $FormatsToProcess += $moduleManifest.FormatsToProcess |
| 115 | + $changes.FormatsToProcess = $FormatsToProcess |
| 116 | + } |
| 117 | + if ($NestedModules) { |
| 118 | + $NestedModules += $moduleManifest.NestedModules |
| 119 | + $changes.NestedModules = $NestedModules |
| 120 | + } |
| 121 | + if ($FunctionsToExport) { |
| 122 | + $FunctionsToExport += $moduleManifest.FunctionsToExport |
| 123 | + $changes.FunctionsToExport = $FunctionsToExport |
| 124 | + } |
| 125 | + if ($CmdletsToExport) { |
| 126 | + $CmdletsToExport += $moduleManifest.CmdletsToExport |
| 127 | + $changes.CmdletsToExport = $CmdletsToExport |
| 128 | + } |
| 129 | + if ($VariablesToExport) { |
| 130 | + $VariablesToExport += $moduleManifest.VariablesToExport |
| 131 | + $changes.VariablesToExport = $VariablesToExport |
| 132 | + } |
| 133 | + if ($AliasesToExport) { |
| 134 | + $AliasesToExport += $moduleManifest.AliasesToExport |
| 135 | + $changes.AliasesToExport = $AliasesToExport |
| 136 | + } |
| 137 | + if ($DscResourcesToExport) { |
| 138 | + $DscResourcesToExport += $moduleManifest.DscResourcesToExport |
| 139 | + $changes.DscResourcesToExport = $DscResourcesToExport |
| 140 | + } |
| 141 | + if ($ModuleList) { |
| 142 | + $ModuleList += $moduleManifest.ModuleList |
| 143 | + $changes.ModuleList = $ModuleList |
| 144 | + } |
| 145 | + if ($FileList) { |
| 146 | + $FileList += $moduleManifest.FileList |
| 147 | + $changes.FileList = $FileList |
| 148 | + } |
| 149 | + if ($Tags) { |
| 150 | + $Tags += $moduleManifest.PrivateData.PSData.Tags |
| 151 | + $changes.Tags = $Tags |
| 152 | + } |
| 153 | + if ($ExternalModuleDependencies) { |
| 154 | + $ExternalModuleDependencies += $moduleManifest.PrivateData.PSData.ExternalModuleDependencies |
| 155 | + $changes.ExternalModuleDependencies = $ExternalModuleDependencies |
| 156 | + } |
| 157 | + |
| 158 | + foreach ($key in $changes.GetEnumerator().Name) { |
| 159 | + $changes[$key] = $changes[$key] | Sort-Object -Unique | Where-Object { -not [string]::IsNullOrEmpty($_) } |
| 160 | + } |
| 161 | + |
| 162 | + Set-ModuleManifest -Path $Path @changes |
| 163 | + |
| 164 | +} |
0 commit comments