AppHandling/Copy-AlSourceFiles.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<#
 .Synopsis
  Copy AL Source Files
 .Description
  This function will copy all .al and .xlf files from Path to Destination.
  Information about each file will be sent to the alFileStructure function to determine the relative location of the file.
 .Parameter Path
  Source Path from where you want to copy all AL source files
 .Parameter Destination
  Destination Path
 .Parameter Recurse
  Copy source in subfolders
 .Parameter alFileStructure
  Specify a function, which will determine the location of the individual al source files
 .Example
  Copy-AlSourceFiles -Path "$sourceFolder\*" -Destination $destinationFolder -Recurse -alFileStructure $TypeFolders
 
#>

function Copy-AlSourceFiles {

    Param(
        [string] $Path,
        [string] $Destination,
        [switch] $Recurse,
        [ScriptBlock] $alFileStructure
    )

    Write-Host "Copying Al Source Files from $Path to $Destination"

    if ($alFileStructure) {
        $types = @('enum', 'page', 'table', 'codeunit', 'report', 'query', 'xmlport', 'profile', 'dotnet', 'enumextension', 'pageextension', 'tableextension')
        $extensions = @(".al",".xlf",".lcl")

        $files = Get-ChildItem -Path $Path -Recurse:$Recurse
        $files | Where-Object { ($extensions.Contains($_.Extension.ToLowerInvariant())) -and !($_.Attributes.HasFlag([System.IO.FileAttributes]::Directory)) } | ForEach-Object {
    
            $filename = $_.Name
            $content = [System.IO.File]::ReadAllLines($_.FullName)

            try {
                if ($_.Extension -eq ".xlf" -or $_.Extension -eq ".lcl") {
                    $type = $_.Extension
                    $id = ''
                    $name = $_.BaseName
                } 
                else {
                    $found = $false
                    $content | ForEach-Object {
                        if (-not $found) {
                            $line = $_.Trim()
                            $idx = $line.IndexOf(' ')
                            if ($idx -lt 0) {
                                $type = $line
                            }
                            else {
                                $type = $line.SubString(0,$idx).ToLowerInvariant()
                            }
                            if ($types.Contains($type)) {
                                $found = $true
                            }
                        } 
                    }
    
                    if ($type -eq "dotnet") {
                        $id = ''
                        $name = $_.BaseName
                    }
                    else {
                        $line = $line.SubString($type.Length).TrimStart()
                        if ($type -eq "profile") {
                            $id = ''
                        }
                        else {
                            $id = $line.SubString(0,$line.IndexOf(' '))
                            $line = $line.SubString($id.Length).Trim()
                        }
                        if ($line.StartsWith('"')) {
                            $nameendidx = $line.IndexOf('"',1)
                            $name = $line.SubString(1,$nameendidx-1)
                        }
                        else {
                            $name = $line
                        }
                    }
                }
                if ($alFileStructure.Ast.ParamBlock.Parameters.Count -eq 3) {
                    $newFilename = "$($alFileStructure.Invoke($type, $id, $name))"
                }
                else {
                    $newFilename = "$($alFileStructure.Invoke($type, $id, $name, [ref] $content))"
                }
    
                if ($newFilename) {
                    $newFilename = $newFilename -replace '[~#%&*{}|:<>?/"]', ''
 
                    $destFilename = Join-Path $Destination $newFilename
                    $destPath = [System.IO.Path]::GetDirectoryName($destFilename)
                    $destName = [System.IO.Path]::GetFileName($destFilename)
                    
                    if (-not (Test-Path $destPath)) {
                        New-Item $destPath -ItemType Directory | Out-Null
                    }

                    if (Test-Path -Path $destFilename) {
                        Write-Warning "$destFilename already exists, adding sequence number"
                        $seq = 1
                        $dotIdx = $destName.IndexOf('.')
                        if ($dotIdx -lt 0) {
                            throw "Cannot add sequence number to $destName"
                        }
                        while (Test-Path -Path $destFilename) {
                            $seq++
                            $destFilename = Join-Path $destPath $destName.Insert($dotIdx,"$seq")
                        }
                    }

                    if ($type -eq "report") {
                        0..($content.Count-1) | % {
                            $line = $content[$_]
                            if ($line.Trim() -like "RDLCLayout = '*';" -or $line.Trim() -like "WordLayout = '*';") {
                                $startIdx = $line.IndexOf("'")+1
                                $endIdx = $line.LastIndexOf("'")
                                $layoutFilename = $line.SubString($startIdx, $endIdx-$startIdx)
                                $layoutFilename = $layoutFilename.SubString($layoutFilename.LastIndexOfAny(@('\','/'))+1)
                                $layoutFile = $Files | Where-Object { $_.name -eq $layoutFilename }
                                if ($layoutFile) {
                                    $layoutcontent = [System.IO.File]::ReadAllBytes($layoutFile.FullName)
        
                                    if ($alFileStructure.Ast.ParamBlock.Parameters.Count -eq 3) {
                                        $layoutNewFilename = "$($alFileStructure.Invoke($layoutFile.Extension, $id, $name))"
                                    }
                                    else {
                                        $layoutNewFilename = "$($alFileStructure.Invoke($layoutFile.Extension, $id, $name, [ref] $content))"
                                    }

                                    if ($layoutNewFilename) {
                                        if ($layoutNewFilename.StartsWith('*\')) {
                                            $layoutDestFilename = Join-Path $DestPath $layoutNewFilename.Substring(2)
                                            $layoutDestPath = [System.IO.Path]::GetDirectoryName($layoutDestFilename)
                                            $layoutDestName = [System.IO.Path]::GetFileName($layoutDestFilename)
                                            if ($layoutDestName.StartsWith('*.')) {
                                                $layoutDestName = [System.IO.Path]::GetFileNameWithoutExtension($destFileName) + $layoutDestName.Substring(1)
                                                $layoutDestFileName = Join-Path $layoutDestPath $layoutDestName
                                            }
                                        }
                                        elseif ($layoutNewFilename.StartsWith('*.')) {
                                            $layoutDestPath = $destPath
                                            $layoutDestName = [System.IO.Path]::GetFileNameWithoutExtension($destFileName) + $layoutNewFilename.Substring(1)
                                            $layoutDestFilename = Join-Path $layoutDestPath $layoutDestName
                                        }
                                        else {
                                            $layoutNewFilename = $layoutNewFilename -replace '[~#%&*{}|:<>?/"]', ''
    
                                            $layoutDestFilename = Join-Path $Destination $layoutNewFilename
                                            $layoutDestPath = [System.IO.Path]::GetDirectoryName($layoutDestFilename)
                                            $layoutDestName = [System.IO.Path]::GetFileName($layoutDestFilename)
                                        }
    
                                        if (-not (Test-Path $layoutDestPath)) {
                                            New-Item $layoutDestPath -ItemType Directory | Out-Null
                                        }
    
                                        if (Test-Path -Path $layoutDestFilename) {
                                            Write-Warning "$layoutDestFilename already exists, adding sequence number"
                                            $seq = 1
                                            $dotIdx = $layoutDestName.IndexOf('.')
                                            if ($dotIdx -lt 0) {
                                                throw "Cannot add sequence number to $layoutDestName"
                                            }
                                            while (Test-Path -Path $layoutDestFilename) {
                                                $seq++
                                                $layoutDestFilename = Join-Path $layoutDestPath $layoutDestName.Insert($dotIdx,"$seq")
                                            }
                                        }

                                        [System.IO.File]::WriteAllBytes($layoutDestFilename, $layoutcontent)

                                        $filename = (get-item $layoutDestFilename).FullName
                                        if ($filename.StartsWith($Destination,"InvariantCultureIgnoreCase")) {
                                            $layoutDestFilename = $filename
                                        }
            
                                        $content[$_] = $line.SubString(0,$startIdx) + $layoutDestFilename.Substring($destination.Length+1).Replace('\','/') + $line.SubString($endIdx)
                                    }
                                }
                                else {
                                    Write-Warning "Unable to find $layoutFilename"
                                }
                            }
                        }
                    }
        
                    [System.IO.File]::WriteAllLines($destFileName, $content)
                }
            }
            catch {
                Write-Warning "Unexpected error while handling $filename"
                throw
            }
        }
    }
    else {
        Copy-Item -Path $Path -Destination $Destination -Recurse:$Recurse -Force | Out-Null
    }
}
Export-ModuleMember -Function Copy-AlSourceFiles