UpgradeTools/Get-NewTablesAndFieldsInBranch.ps1

function Get-NewTablesAndFieldsInBranch {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$BranchPath,
        [Parameter(Mandatory=$false)]
        [int]$ChangesetNo,
        [Parameter(Mandatory=$false)]
        [string]$VersionCode
    )

    $Tables = @()
    $Result = Create-AdditionDeltaFromBase -AdditionPath $BranchPath -ChangesetNo $ChangesetNo
    $Files = gci $Result.ReplacementsPath -Filter 'TAB*'
    foreach ($File in $Files) {
        $Table = New-Object System.Object        
        $Fields = @()
        $Fields += Get-FieldsFromObjectFile -Path $File.FullName -VersionCode $VersionCode        
        $Table | Add-Member -MemberType NoteProperty -Name TableId -Value $Fields[0].TableID
        $Table | Add-Member -MemberType NoteProperty -Name TableName -Value $Fields[0].TableName
        $Table | Add-Member -MemberType NoteProperty -Name Fields -Value $Fields
        $Table | Add-Member -MemberType NoteProperty -Name KeyDef -Value (Get-PrimaryKeyDefinition -Path $File.FullName)
        $Tables += $Table
    }

    $Files = gci $Result.DeltasPath -Filter 'TAB*'
    foreach ($File in $Files) {
        $BaseFile = Join-Path $Result.BasePath -ChildPath ($File.BaseName + '.TXT')
        $Table = New-Object System.Object
        $Fields = @()
        $Fields += Get-FieldsFromObjectFile -Path $File.FullName -VersionCode $VersionCode        
        $Table | Add-Member -MemberType NoteProperty -Name TableId -Value $Fields[0].TableID
        $Table | Add-Member -MemberType NoteProperty -Name TableName -Value $Fields[0].TableName
        if ($Fields.Code -ne $null){
            $PrimaryKeyFields = Get-PrimaryKey -Path $BaseFile -VersionCode $VersionCode -TableID $Table.TableID -TableName $Table.TableName
            $Table | Add-Member -MemberType NoteProperty -Name PrimaryKeyFields -Value $PrimaryKeyFields
            $Table | Add-Member -MemberType NoteProperty -Name Fields -Value $Fields
            $Table | Add-Member -MemberType NoteProperty -Name KeyDef -Value (Get-PrimaryKeyDefinition -Path $BaseFile)
            $Tables += $Table
        }
    }

    $Tables
}

function Get-FieldsFromObjectFile {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$Path,
        [Parameter(Mandatory=$false)]
        [string]$VersionCode
    )

    $Fields = @()

    $Content = gc $Path -Raw

    $Match = [Regex]::Match($Content,'Table \d+')
    $TableID = [Int]::Parse($Content.Substring($Match.Index + 6,$Match.Length - 6))
    $TableNameRegex = [Regex]::Match($Content,'Modification .*\(')
    if ($TableNameRegex.Success){
        $TableName = $TableNameRegex.Value.Substring(13,$TableNameRegex.Length-14).Trim('"')
    }else {
        $FirstLine = Get-Content $Path -First 1
        $TableNameRegex = [Regex]::Match($FirstLine,'\d.*')

        $TableName = $TableNameRegex.Value.Trim('1','2','3','4','5','6','7','8','9','0',' ')
    }

    $Field = New-Object System.Object
    foreach ($Match in [Regex]::Matches($Content,'{ \d+ *; *;[\w\W\d]*?;[\w\W\d]*?[\s;]')) {
        $Field = New-Object System.Object

        $Match = $Match.Value.Split(';')

        $Field | Add-Member -MemberType NoteProperty -Name Code -Value $VersionCode
        $Field | Add-Member -MemberType NoteProperty -Name TableID -Value $TableID
        $Field | Add-Member -MemberType NoteProperty -Name TableName -Value $TableName
        $Field | Add-Member -MemberType NoteProperty -Name FieldID -Value $Match[0].SubString(1).Trim()
        $Field | Add-Member -MemberType NoteProperty -Name Name -Value $Match[2].Trim()
        $Field | Add-Member -MemberType NoteProperty -Name Type -Value $Match[3].Trim()
        $Fields += $Field
    }

    $Fields
}


function Get-PrimaryKey {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$Path,
        [Parameter(Mandatory=$false)]
        [string]$VersionCode,
        [Parameter(Mandatory=$false)]
        [string]$TableID,
        [Parameter(Mandatory=$false)]
        [string]$TableName

    )

    $Fields = @()

    $Content = gc $Path -Raw

    $MatchKeys = [Regex]::Match($Content,'[ ]*KEYS[\w\W\n ]*?\}')
    $MatchKeys = $MatchKeys.Value.Split(';')[1]
    $Keys = $MatchKeys.Split(',')

     foreach($Key in $Keys){
        $Key = $Key.TrimEnd()
        $Match = [Regex]::Matches($Content,"{ \d+ *; *;($Key) *;[\w\W\d]*?[\s;]")
        $Match = $Match.Value.Split(';')


        $Field = New-Object System.Object

        $Field | Add-Member -MemberType NoteProperty -Name Code -Value $VersionCode
        $Field | Add-Member -MemberType NoteProperty -Name TableID -Value $TableID
        $Field | Add-Member -MemberType NoteProperty -Name TableName -Value $TableName
        $Field | Add-Member -MemberType NoteProperty -Name FieldID -Value $Match[0].SubString(1).Trim()
        $Field | Add-Member -MemberType NoteProperty -Name Name -Value $Match[2].Trim()
        $Field | Add-Member -MemberType NoteProperty -Name Type -Value $Match[3].Trim()
        $Fields += $Field
    }

    $Fields
}

function Get-PrimaryKeyDefinition{
    Param(
        [Parameter(Mandatory=$true)]
        [string]$Path
    )

    $Content = gc $Path -Raw

    $MatchKeys = [Regex]::Match($Content,'[ ]*KEYS[\w\W\n ]*?;[[\w\W]*?;')
    
    $KeyDefinition = (-join $MatchKeys.Value , '} }')
    $KeyDefinition
}

Export-ModuleMember -Function Get-NewTablesAndFieldsInBranch