Last active
September 1, 2022 14:38
-
-
Save Jaykul/304e7874d629ef4848c9acd0cfd550d0 to your computer and use it in GitHub Desktop.
Print a module dependency tree for modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| filter Get-DependencyTree { | |
| <# | |
| .NOTES | |
| From https://gist.github.com/Jaykul/304e7874d629ef4848c9acd0cfd550d0 | |
| #> | |
| param( | |
| [Parameter(Mandatory, ValueFromPipeline)] | |
| [System.Management.Automation.PSModuleInfo[]]$Module, | |
| $Depth = 0, | |
| $Colors = @("Green", "DarkGreen", "Blue", "Cyan", "Yellow", "DarkYellow", "DarkRed", "Red", "Magenta") | |
| ) | |
| foreach($m in $Module) { | |
| Write-Host (("- " * $Depth) + $m.Name ) -ForegroundColor $Colors[$Depth] | |
| foreach($rm in @($m.RequiredModules)) { | |
| Get-Module $rm -ListAvailable | Select -First 1 | Get-DependencyTree -Depth ($Depth+1) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #requires -module DependsOn, Metadata | |
| function Optimize-ModuleList { | |
| <# | |
| .SYNOPSIS | |
| Sorts Modules based on dependency, so modules always appear after their RequiredModules | |
| .DESCRIPTION | |
| Should go in the RequiredModules module, since this supports in-place sorting of RequiredModules.psd1 | |
| However, it currently only works on installed modules, and requires DependsOn for the sorting | |
| .NOTES | |
| From https://gist.github.com/Jaykul/304e7874d629ef4848c9acd0cfd550d0 | |
| #> | |
| [CmdletBinding(DefaultParameterSetName = "ByName")] | |
| param( | |
| [Parameter(Mandatory, Position = 0, ParameterSetName = "ByName")] | |
| [string[]]$Name, | |
| [Parameter(Mandatory, Position = 0, ValueFromPipeline, ParameterSetName = "ModuleInfo")] | |
| [System.Management.Automation.PSModuleInfo[]]$Module, | |
| [Parameter(Mandatory, ParameterSetName = "RequiredModulesPath")] | |
| [string]$RequiredModulesPath | |
| ) | |
| begin { | |
| $Modules = [System.Collections.Generic.List[System.Management.Automation.PSModuleInfo]]::new() | |
| } | |
| process { | |
| if ($RequiredModulesPath) { | |
| $Metadata = Import-Metadata $RequiredModulesPath | |
| $Name = @($Metadata.Keys) | |
| } | |
| if ($Name) { | |
| $Module = GetRequiredModule $Name | |
| } | |
| $Modules.AddRange($Module) | |
| } | |
| end { | |
| $Ordered = $Modules | Resolve-DependencyOrder -Key { $_.Name } -DependsOn { $_.RequiredModules.Name } | |
| $Ordered | |
| if ($Metadata) { | |
| $RequiredModules = [Ordered]@{} | |
| # Output for RequiredModules ... | |
| foreach ($ModuleName in $Ordered.Name) { | |
| $VersionRange = $Metadata[$ModuleName] | |
| if (!$VersionRange) { | |
| # In case the version is 2 or 4 numbers, always use 3 | |
| $Version = $Ordered.Where({ $_.Name -eq $ModuleName }, "First", 1).Version | |
| $VersionRange = "[{0}.{1}.{2},]" -f [Math]::Max(0, $Version.Major), [Math]::Max(0, $Version.Minor), [Math]::Max(0, $Version.Build) | |
| } | |
| $RequiredModules[$ModuleName] = $VersionRange | |
| } | |
| Export-Metadata -InputObject $RequiredModules -Path $RequiredModulesPath | |
| } | |
| } | |
| } |
Author
Jaykul
commented
Nov 8, 2017

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment