ObjectHandling/Convert-Txt2Al.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<#
 .Synopsis
  Convert txt and delta files to AL
 .Description
  Convert objects in myDeltaFolder to AL. Page and Table extensions are created as new objects using the startId as object Id offset.
  Code modifications and other things not supported in extensions will not be converted to AL.
  Manual modifications are required after the conversion.
 .Parameter containerName
  Name of the container in which the txt2al tool will be executed
 .Parameter myDeltaFolder
  Folder containing delta files
 .Parameter myAlFolder
  Folder in which the AL files are created
 .Parameter startId
  Starting offset for objects created by the tool (table and page extensions)
 .Example
  Convert-Txt2Al -containerName test -mydeltaFolder c:\programdata\navcontainerhelper\mydeltafiles -myAlFolder c:\programdata\navcontainerhelper\myAlFiles -startId 50100
#>

function Convert-Txt2Al {
    Param(
        [Parameter(Mandatory=$true)]
        [string] $containerName, 
        [Parameter(Mandatory=$true)]
        [string] $myDeltaFolder, 
        [Parameter(Mandatory=$true)]
        [string] $myAlFolder, 
        [int] $startId=50100,
        [string] $dotNetAddInsPackage
    )

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

    $containerMyDeltaFolder = Get-NavContainerPath -containerName $containerName -path $myDeltaFolder -throw
    $containerMyAlFolder = Get-NavContainerPath -containerName $containerName -path $myAlFolder -throw
    $containerDotNetAddInsPackage = ""
    if ($dotNetAddInsPackage) {
        $containerDotNetAddInsPackage = Get-NavContainerPath -containerName $containerName -path $dotNetAddInsPackage -throw
    }

    $navversion = Get-NavContainerNavversion -containerOrImageName $containerName
    $version = [System.Version]($navversion.split('-')[0])
    $ignoreSystemObjects = ($version.Major -ge 14)
    $platformVersion = Get-NavContainerPlatformVersion -containerOrImageName $containerName

    $dummy = Invoke-ScriptInNavContainer -containerName $containerName -ScriptBlock { Param($myDeltaFolder, $myAlFolder, $startId, $dotNetAddInsPackage, $platformVersion)
        
        $erroractionpreference = 'Continue'

        if (!($txt2al)) {
            throw "You cannot run Convert-Txt2Al on this Container"
        }
        Write-Host "Converting files in $myDeltaFolder to .al files in $myAlFolder with startId $startId (container paths)"
        Remove-Item -Path $myAlFolder -Recurse -Force -ErrorAction Ignore
        New-Item -Path $myAlFolder -ItemType Directory -ErrorAction Ignore | Out-Null

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

        $txt2alParameters = @("--source=""$myDeltaFolder""", "--target=""$myAlFolder""", "--rename", "--extensionStartId=$startId")
        if ($dotNetAddInsPackage) {
            $txt2alParameters += @("--dotNetAddInsPackage=""$dotNetAddInsPackage""")
        }

        if ($platformVersion) {
            $ver = [System.Version]($platformVersion)
            if (($ver.Major -eq 14 -and $ver.Build -ge 34429) -or ($ver.Major -eq 15 -and $ver.Build -ge 34399)) {
                Write-Host "Using Compiler CodeAnalysis to format documents"
                if (-not (Test-Path (Join-Path ([System.IO.Path]::GetDirectoryName($txt2al)) "Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.dll"))) {
                    Write-Host "Copying Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.dll from vsix"
                    if (!(Test-Path "c:\build" -PathType Container)) {
                        $tempZip = Join-Path $env:TEMP "alc.zip"
                        Copy-item -Path (Get-Item -Path "c:\run\*.vsix").FullName -Destination $tempZip
                        Expand-Archive -Path $tempZip -DestinationPath "c:\build\vsix"
                    }
                    $alcPath = 'C:\build\vsix\extension\bin'
                    Copy-Item -Path (Join-Path $alcPath "Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.dll") -Destination ([System.IO.Path]::GetDirectoryName($txt2al))
                }
            }
        }

        Write-Host "txt2al.exe $([string]::Join(' ', $txt2alParameters))"
        & $txt2al $txt2alParameters 2> $null

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

        $erroractionpreference = 'Stop'

    } -ArgumentList $containerMyDeltaFolder, $containerMyAlFolder, $startId, $containerDotNetAddInsPackage, $platformVersion
}
Export-ModuleMember -Function Convert-Txt2Al