AppHandling/Create-AlProjectFolderFromNavContainer.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<#
 .Synopsis
  Create a VS Code AL Project Folder based on a Container
 .Description
  Export all objects from a container, convert them to AL and establish the necessary project files.
  The container needs to be started with -includeAL, which ensures that the .net used by the baseapp are available in a folder.
 .Parameter containerName
  Name of the container from which you want to create the AL Project folder
 .Parameter alProjectFolder
  The alProjectFolder will contain the AL project upon successful completion of this function.
  The content of the folder will be removed.
  This folder doesn't need to be shared with the container, but if you want to use Compile-AppInNavContainer, it might be a good idea to share it.
 .Parameter id
  This parameter specifies the ID of the AL app to be placed in app.json. Default is a new GUID.
 .Parameter name
  This parameter specifies the name of the AL app to be placed in app.json. Default is the container name.
 .Parameter publisher
  This parameter specifies the publisher of the AL app to be placed in app.json. Default is Default Publisher.
 .Parameter version
  This parameter specifies the version of the AL app to be placed in app.json. Default is 1.0.0.0.
 .Parameter addGIT
  Specify
 .Parameter useBaseLine
  Specify this switch if you want to use the AL BaseLine, which was created when creating the container with -includeAL.
  The baseline AL objects are added to "C:\ProgramData\NavContainerHelper\Extensions\Original-<version>-<country>-al" and will contain AL files for the C/AL objects in the container at create time.
 .Parameter alFileStructure
  Specify a function, which will determine the location of the individual al source files
 .Parameter runTxt2AlInContainer
  Specify a foreign container in which you want to run the txt2al tool
 .Parameter useBaseAppProperties
  Specify to retrieve app properties from base app actually installed in container
 .Parameter credential
  Credentials are needed to download the app if you do not use the baseline
 .Example
  $alProjectFolder = "C:\ProgramData\NavContainerHelper\AL\BaseApp"
  Create-AlProjectFolderFromNavContainer -containerName alContainer `
                                         -alProjectFolder $alProjectFolder `
                                         -name "myapp" `
                                         -publisher "Freddy Kristiansen" `
                                         -version "1.0.0.0" `
                                         -AddGIT `
                                         -useBaseLine
#>

function Create-AlProjectFolderFromNavContainer {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $containerName, 
        [Parameter(Mandatory=$true)]
        [string] $alProjectFolder,
        [Parameter(ParameterSetName="AppProperties")]
        [string] $id = [GUID]::NewGuid().ToString(),
        [Parameter(ParameterSetName="AppProperties")]
        [string] $name = $containerName,
        [Parameter(ParameterSetName="AppProperties")]
        [string] $publisher = "Default Publisher",
        [Parameter(ParameterSetName="AppProperties")]
        [string] $version = "1.0.0.0",
        [switch] $AddGIT,
        [switch] $useBaseLine,
        [ScriptBlock] $alFileStructure,
        [string] $runTxt2AlInContainer = $containerName,
        [Parameter(ParameterSetName="BaseAppProperties")]
        [switch] $useBaseAppProperties,
        [PSCredential] $credential = $null
    )

    $navversion = Get-NavContainerNavversion -containerOrImageName $containerName
    $ver = [System.Version]($navversion.split('-')[0])
    $alFolder   = Join-Path $ExtensionsFolder "Original-$navversion-al"
    $dotnetAssembliesFolder = Join-Path $ExtensionsFolder "$containerName\.netPackages"

    if (!(Test-Path $alFolder -PathType Container) -or !(Test-Path $dotnetAssembliesFolder -PathType Container)) {
        throw "Container $containerName was not started with -includeAL (or -doNotExportObjectsAsText was specified)"
    }

    # Empty Al Project Folder
    if (Test-Path -Path $alProjectFolder -PathType Container) {
        Remove-Item -Path "$alProjectFolder\*" -Recurse -Force
    }
    else {
        New-Item -Path $AlProjectFolder -ItemType Directory | Out-Null
    }

    if ($useBaseLine) {
        Copy-AlSourceFiles -Path "$alFolder\*" -Destination $AlProjectFolder -Recurse -alFileStructure $alFileStructure
    }
    elseif ($ver.Major -ge 15) {
        $id = [Guid]::NewGuid().Guid
        $appFile = Join-Path $ExtensionsFolder "BaseApp-$id.app"
        $appFolder = Join-Path $ExtensionsFolder "BaseApp-$id"
        $myAlFolder = Join-Path $ExtensionsFolder "al-$id"
        try {
            $appName = "Base Application"
            if ($ver -lt [Version]("15.0.35659.0")) {
                $appName = "BaseApp"
            }
            $baseapp = Get-NavContainerAppInfo -containerName $containerName | Where-Object { $_.Name -eq $appName }
            Get-NavContainerApp -containerName $containerName `
                                -publisher $baseapp.Publisher `
                                -appName $baseapp.Name `
                                -appFile $appFile `
                                -credential $credential
        
            Extract-AppFileToFolder -appFilename $appFile -appFolder $appFolder
            'layout','src','translations' | ForEach-Object {
                if (Test-Path (Join-Path $appFolder $_)) {
                    Copy-Item -Path (Join-Path $appFolder $_) -Destination $myAlFolder -Recurse -Force
                }
            }

            Copy-AlSourceFiles -Path "$myAlFolder\*" -Destination $AlProjectFolder -Recurse -alFileStructure $alFileStructure
        }
        finally {    
            Remove-Item -Path $myAlFolder -Recurse -Force -ErrorAction SilentlyContinue
            Remove-Item -Path $appFolder -Recurse -Force -ErrorAction SilentlyContinue
            Remove-Item -Path $appFile -Force -ErrorAction SilentlyContinue
        }
    }
    else {
        Convert-ModifiedObjectsToAl -containerName $containerName -doNotUseDeltas -alProjectFolder $AlProjectFolder -alFileStructure $alFileStructure -runTxt2AlInContainer $runTxt2AlInContainer
    }

    $appJsonFile = Join-Path $AlProjectFolder "app.json"
    if ($useBaseLine -and $ver -ge [Version]("15.0.35528.0")) {
        $appJson = Get-Content "$alFolder\app.json" | ConvertFrom-Json

        if (-not $useBaseAppProperties) {
            $appJson.Id = $id
            $appJson.Name = $name
            $appJson.Publisher = $publisher
            $appJson.Version = $version
        }

    } elseif ($ver.Major -ge  15) {
        
        if ($useBaseAppProperties) {
            $appName = "Base Application"
            if ($ver -lt [Version]("15.0.35659.0")) {
                $appName = "BaseApp"
            }
            $baseapp = Get-NavContainerAppInfo -containerName $containerName | Where-Object { $_.Name -eq $appName }
            if ($baseapp) {
                $id = $baseapp.AppId
                $name = $baseapp.Name
                $publisher = $baseapp.Publisher
                $version = $baseapp.Version
            }
            else {
                throw "BaseApp not found"
            }
        }
        if ($ver -ge [Version]("15.0.35528.0")) {
            $sysAppVer = "$($ver.Major).0.0.0"
        }
        else {
            $sysAppVer = "1.0.0.0"
        }
        $appJson = @{ 
            "id" = $id
            "name" = $name
            "publisher" = $publisher
            "version" = $version
            "brief" = ""
            "description" = ""
            "privacyStatement" = ""
            "EULA" = ""
            "help" = ""
            "url" = ""
            "logo" = ""
            "dependencies" = @(@{
                "appId" = "63ca2fa4-4f03-4f2b-a480-172fef340d3f"
                "publisher" = "Microsoft"
                "name" = "System Application"
                "version" = $sysAppVer
            })
            "screenshots" = @()
            "platform" = "$($ver.Major).0.0.0"
            "idRanges" = @()
            "showMyCode" = $true
            "target" = "OnPrem"
        }
    }
    else {
        $appJson = @{ 
            "id" = $id
            "name" = $name
            "publisher" = $publisher
            "version" = $version
            "brief" = ""
            "description" = ""
            "privacyStatement" = ""
            "EULA" = ""
            "help" = ""
            "url" = ""
            "logo" = ""
            "dependencies" = @()
            "screenshots" = @()
            "platform" = "14.0.0.0"
            "idRanges" = @()
            "showMyCode" = $true
            "target" = "Internal"
        }
    }
    Set-Content -Path $appJsonFile -Value ($appJson | ConvertTo-Json)

    $dotnetPackagesFolder = Join-Path $AlProjectFolder ".netpackages"
    New-Item -Path $dotnetPackagesFolder -ItemType Directory -Force | Out-Null

    $alPackagesFolder = Join-Path $AlProjectFolder ".alpackages"
    New-Item -Path $alPackagesFolder -ItemType Directory -Force | Out-Null

    $vscodeFolder = Join-Path $AlProjectFolder ".vscode"
    New-Item -Path $vscodeFolder -ItemType Directory -Force | Out-Null

    $settingsJsonFile = Join-Path $vscodeFolder "settings.json"
    $settingsJson = @{
        "al.enableCodeAnalysis" = $false
        "al.enableCodeActions" = $false
        "al.incrementalBuild" = $true
        "al.packageCachePath" = ".alpackages"
        "al.assemblyProbingPaths" = @(".netpackages", $dotnetAssembliesFolder)
        "editor.codeLens" = $false
    }
    Set-Content -Path $settingsJsonFile -Value ($settingsJson | ConvertTo-Json)
    
    $launchJsonFile = Join-Path $vscodeFolder "launch.json"
    $config = Get-NavContainerServerConfiguration -ContainerName $containerName
    if ($config.DeveloperServicesSSLEnabled -eq "true") {
        $devserverUrl = "https://$containerName"
    }
    else {
        $devserverUrl = "http://$containerName"
    }
    if ($config.ClientServicesCredentialType -eq "Windows") {
        $authentication = "Windows"
    }
    else {
        $authentication = "UserPassword"
    }
    $launchJson = @{
        "version" = "0.2.0"
        "configurations" = @( @{
            "type" = "al"
            "request" = "launch"
            "name" = "$containerName"
            "server" = $devserverUrl
            "port" = [int]($config.DeveloperServicesPort)
            "serverInstance" = $config.ServerInstance
            "authentication" = $authentication
            "breakOnError" = $true
            "launchBrowser" = $true
        } )
    }
    Set-Content -Path $launchJsonFile -Value ($launchJson | ConvertTo-Json)

    if ($addGit) {
        Add-GitToAlProjectFolder -alProjectFolder $alProjectFolder -commitMessage $containerName
    }
    
    Write-Host -ForegroundColor Green "Al Project Folder Created"
}
Set-Alias -Name Create-AlProjectFolderFromBcContainer -Value Create-AlProjectFolderFromNavContainer
Export-ModuleMember -Function Create-AlProjectFolderFromNavContainer -Alias Create-AlProjectFolderFromBcContainer