Deltas/Apply-DeltasFromBranches.ps1

Function Apply-DeltasFromBranches
{
    Param(
        [Parameter(Mandatory=$true)]
        [string]$BaseVersionBranch,
        [Parameter(Mandatory=$true)]
        $AdditionBranches,
        [Parameter(Mandatory=$true)]
        [string]$WorkingFolder
    )

    
    #create the base folder
    $BasePath = (Join-Path -Path $WorkingFolder -ChildPath 'Base')    
    $AdditionPath = (Join-Path $WorkingFolder -ChildPath 'Addition')
    $ResultPath = (Join-Path $WorkingFolder -ChildPath 'Result')
    $OutputPath =  (Join-Path $WorkingFolder -ChildPath 'Output')

    Create-EmptyDirectory -DirectoryPath $WorkingFolder
    Create-EmptyDirectory -DirectoryPath $BasePath
    Create-EmptyDirectory -DirectoryPath $OutputPath    

    foreach($AdditionBranch in $AdditionBranches)
    {
        #(re)create the addition folder
        Create-EmptyDirectory -DirectoryPath $AdditionPath
        Create-EmptyDirectory -DirectoryPath $ResultPath
        
        Get-ObjectsFromTFSBranch -BranchPath $AdditionBranch -DestinationPath $AdditionPath

        #download objects from the base branch which are being modified by the addition
        $DeltaFiles = Get-ChildItem -Path $AdditionPath -Filter '*.DELTA'

        foreach($DeltaFile in $DeltaFiles)
        {
            $ObjectFileName = $DeltaFile.Name.Substring(0,$DeltaFile.Name.LastIndexOf('.')) + '.TXT'
            $BaseFileName = Join-Path -Path $BasePath -ChildPath $ObjectFileName
            if(![IO.File]::Exists($BaseFileName))
            {
                $BranchFileName = $BaseVersionBranch + '/' + $ObjectFileName
                Get-ObjectsFromTFSBranch -BranchPath $BranchFileName -DestinationPath $BaseFileName -Type 'File'
            }
        }

        Apply-DeltaAndMergeVersionList -DeltaPath $AdditionPath -TargetPath $BasePath -ResultPath $ResultPath

        #overwrite the base object vesions with the result of the delta application
        $ResultFiles = Get-ChildItem -Path $ResultPath -Filter '*.TXT'

        foreach($ResultFile in $ResultFiles)
        {
            [IO.File]::Copy($ResultFile.FullName, (Join-Path -Path $BasePath -ChildPath $ResultFile.Name), $true)
            [IO.File]::Copy((Join-Path -Path $BasePath -ChildPath $ResultFile.Name), (Join-Path -Path $OutputPath -ChildPath $ResultFile.Name), $true)
        }
    }

    Join-NAVApplicationObjectFile -Source $OutputPath -Destination (Join-Path -Path $WorkingFolder -ChildPath 'Output.TXT')
}

Export-ModuleMember -Function Apply-DeltasFromBranches