ObjectHandling/Create-MyDeltaFolder.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<#
 .Synopsis
  Create folder with delta files for my objects
 .Description
  Compare my objects with my base objects and create a folder with delta files.
  Modified objects will be stored as .delta files, new objects will be .txt files.
 .Parameter containerName
  Name of the container in which the Nav Development Cmdlets are to be executed
 .Parameter modifiedFolder
  Folder containing your modified objects
 .Parameter $myoriginalFolder
  Folder containing the original objects for your modified objects
 .Parameter myDeltaFolder
  Folder in which the delta files are created
 .Example
  Create-MyDeltaFolder -containerName test -modifiedFolder c:\programdata\navcontainerhelper\myobjects -myoriginalFolder c:\programdata\navcontainerhelper\myoriginalobjects -mydeltaFolder c:\programdata\navcontainerhelper\mydeltafiles
#>

function Create-MyDeltaFolder {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$containerName, 
        [Parameter(Mandatory=$true)]
        [string]$modifiedFolder, 
        [Parameter(Mandatory=$true)]
        [string]$myOriginalFolder, 
        [Parameter(Mandatory=$true)]
        [string]$myDeltaFolder,
        [switch]$useNewSyntax
    )

    AssumeNavContainer -containerOrImageName $containerName -functionName $MyInvocation.MyCommand.Name

    $containerModifiedFolder = Get-NavContainerPath -containerName $containerName -path $modifiedFolder -throw
    $containerMyOriginalFolder = Get-NavContainerPath -containerName $containerName -path $myOriginalFolder -throw
    $containerMyDeltaFolder = Get-NavContainerPath -containerName $containerName -path $myDeltaFolder -throw

    Invoke-ScriptInNavContainer -containerName $containerName -ScriptBlock { Param($modifiedFolder, $myOriginalFolder, $myDeltaFolder, $useNewSyntax)

        Write-Host "Compare modified objects with original objects in $myOriginalFolder and create Deltas in $myDeltaFolder (container paths)"
        if (Test-Path $myDeltaFolder -PathType Container) {
            Get-ChildItem -Path $myDeltaFolder -Include * | Remove-Item -Recurse -Force
        } else {
            New-Item -Path $myDeltaFolder -ItemType Directory -Force -ErrorAction Ignore | Out-Null
        }
        $params = @{}
        if ($useNewSyntax) {
            $params += @{ 'ExportToNewSyntax' = $true }
        }

        if ([System.Text.Encoding]::Default.BodyName -eq "utf-8") {
            $cp = (Get-Culture).TextInfo.OEMCodePage
            $encoding = [System.Text.Encoding]::GetEncoding($cp)
            
            Write-Host "Converting my modified objects from OEM($cp) to UTF8 before comparing"
            Get-ChildItem -Path (Join-Path $modifiedFolder "*.*") | ForEach-Object {
                $content = [System.IO.File]::ReadAllText($_.FullName, $encoding )
                [System.IO.File]::WriteAllText($_.FullName, $content, [System.Text.Encoding]::UTF8 )
            }
        }

        Compare-NAVApplicationObject @params -OriginalPath $myOriginalFolder -ModifiedPath $modifiedFolder -DeltaPath $myDeltaFolder | Out-Null

        if ([System.Text.Encoding]::Default.BodyName -eq "utf-8") {
            Write-Host "Converting files from UTF8 to OEM($cp) after comparing"
            
            $myDeltaFolder, $modifiedFolder | % {
                Get-ChildItem -Path (Join-Path $_ "*.*") | ForEach-Object {
                    $content = [System.IO.File]::ReadAllText($_.FullName,[System.Text.Encoding]::UTF8 )
                    [System.IO.File]::WriteAllText($_.FullName, $content, $encoding )
                }
            }
        }


        Write-Host "Rename new objects to .TXT"
        Get-ChildItem $myDeltaFolder | ForEach-Object {
            $Name = $_.Name
            if ($Name.ToLowerInvariant().EndsWith(".delta")) {
                $BaseName = $_.BaseName
                $OrgName = Join-Path $myOriginalFolder "${BaseName}.TXT"
                if (!(Test-Path -Path $OrgName)) {
                    Rename-Item -Path $_.FullName -NewName "${BaseName}.TXT"
                }
            }
        }
    } -ArgumentList $containerModifiedFolder, $containerMyOriginalFolder, $containerMyDeltaFolder, $useNewSyntax
}
Export-ModuleMember -Function Create-MyDeltaFolder