Skip to content

Instantly share code, notes, and snippets.

@ThePSAdmin
Last active February 18, 2019 22:33
Show Gist options
  • Save ThePSAdmin/7dea5e76d2d490478cb994a0cebe79b2 to your computer and use it in GitHub Desktop.
Save ThePSAdmin/7dea5e76d2d490478cb994a0cebe79b2 to your computer and use it in GitHub Desktop.
$classFolder = "c:\dev\myproject\classes"
$files = Get-ChildItem $classFolder -Filter *.ps1 -Recurse
$typesList = [System.Collections.Generic.List[PsObject]]::New()
foreach ($file in $files) {
$ast = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$null, [ref]$null)
$typeNames = $ast.FindAll( {$args[0] -is [System.Management.Automation.Language.TypeDefinitionAst]}, $true) | Select-Object -ExpandProperty Name
$typesList.Add([PSCustomObject]@{
FilePath = $file.FullName
Types = $typeNames
Dependencies = [System.Collections.Generic.List[String]]::New()
Ast = $ast
}
)
}
foreach ($typeObj in $typesList) {
$typesToCheck = $typeObj.Ast.BaseTypes.TypeName
foreach ($t in $typesToCheck) {
if ($dependentType = ($typesList.Where{$_.Types -contains $t.Name})[0]) {
if ($dependentType.FilePath -ne $typeObj.FilePath -and
$typeObj.Dependencies -notcontains $dependentType.FilePath) {
$typeObj.Dependencies.Add($dependentType.FilePath)
}
}
}
}
while ($typesList.Count -gt 0) {
WRite-Verbose "$($typesList.Count)"
foreach ($typeObj in $typesList.Where{$_.Dependencies.Count -eq 0}) {
foreach ($t in $typesList) {
[void]$t.Dependencies.Remove($typeObj.FilePath)
}
[void]$typesList.Remove($typeObj)
$typeObj.FilePath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment