Commands/Split-OpenPackage.ps1

function Split-OpenPackage {
    <#
    .SYNOPSIS
        Splits Open Packages
    .DESCRIPTION
        Splits Open Packages into multiple parts
    .EXAMPLE
        Get-Module OP |
            OP |
            Split-OpenPackage
    .LINK
        Group-Object
    .LINK
        Select-OpenPackage
    #>

    [CmdletBinding(PositionalBinding=$false)]
    [Alias('Split-OP','slop', 'slOpenPackage')]
    param(
    # One or more properties to group.
    [Parameter(ValueFromRemainingArguments)]
    [PSObject[]]
    $Property,
    <#
    Includes the specified parts.
    
    Enter a wildcard pattern, such as `*.txt`

    Wildcards are permitted.
    #>

    [ValidateNotNullOrEmpty()]
    [SupportsWildcards()]
    [string[]]
    $Include,
    
    <#
    Excludes the specified parts.
    
    Enter a wildcard pattern, such as `*.txt`
    
    Wildcards are permitted.
    #>

    [ValidateNotNullOrEmpty()]
    [SupportsWildcards()]
    [string[]]
    $Exclude,
    
    <#
    Includes the specified content types.
    
    Enter a wildcard pattern, such as `text/*`
    #>

    [ValidateNotNullOrEmpty()]
    [SupportsWildcards()]
    [string[]]
    $IncludeContentType,
    
    <#
    Excludes the specified content types.
    
    Enter a wildcard pattern, such as `text/*`
    #>

    [ValidateNotNullOrEmpty()]
    [SupportsWildcards()]
    [string[]]
    $ExcludeContentType,

    [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
    [Alias('Package')]
    [PSObject]
    $InputObject
    )

    begin {
        $selectOpenPackage = $ExecutionContext.SessionState.InvokeCommand.GetCommand('Select-OpenPackage','Function')
    }

    process {
        if ($InputObject -isnot [IO.Packaging.Package]) {
            return $InputObject
        }
        $selectSplat = [Ordered]@{InputObject=$InputObject}
        foreach ($key in $PSBoundParameters.Keys) {
            if ($selectOpenPackage.Parameters[$key]) {
                $selectSplat[$key] = $PSBoundParameters[$key]
            }
        }
        $selectedParts = Select-OpenPackage @selectSplat
        
        if (-not $property) {
            $Property = { @(@($_.Uri -split '/' -ne '')[-1] -split '\.')[-1] }
        }
        foreach ($group in $selectedParts | Group-Object -Property $Property) {
            # If the group has no name, it will be excluded
            if (-not $group.Name) { continue }
            $splitPackage = $inputObject | 
                Copy-OpenPackage  -Include $group.Group.Uri
            
            $splitPackage.Identifier =
                $splitPackage.Identifier -replace 
                    "(?:\p{P}$([Regex]::Escape($group.Name)))?$", ('.' + $group.Name)

            $splitPackage            
        }
    }
    end {

    }
}