Types/OpenPackage/GetTree.ps1

<#
.SYNOPSIS
    Gets the package tree
.DESCRIPTION
    Gets the tree of all files in a package matching a pattern.
#>

[OutputType([Collections.IDictionary])]
param(
# The file pattern
[string]
$FilePattern = '.'
)

if (-not $this.getParts) { return }

# Get all of the files in the tree.
$filesInTree = @(
    foreach ($part in $this.GetParts()) {
        if ($part.Uri -match $filePattern) {
            $part
        }
    }
)

# If there were no files, return an empty directory.
$fileCount = $filesInTree.Length
if (-not $fileCount) { return [Ordered]@{} }

$fileTree = [Ordered]@{}
foreach ($filePart in $filesInTree) {
    $relativePath = $filePart.Uri
    $hierarchy = @($relativePath -replace '^/' -split '[\\/]' -ne '')
    $pointer = $fileTree
    for ($index = 0; $index -lt ($hierarchy.Length - 1); $index++) {
        $subdirectory = $hierarchy[$index]
        if (-not $pointer[$subdirectory]) {
            $pointer[$subdirectory] = [Ordered]@{}
        }
        $pointer = $pointer[$subdirectory]
    }
    $pointer[$hierarchy[-1]] = $filePart
}


return $fileTree