forked from microsoft/AL-Go-Actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppHelper.psm1
256 lines (231 loc) · 8.22 KB
/
AppHelper.psm1
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
<#
This module contains some useful functions for working with app manifests.
#>
. (Join-Path -path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve)
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$alTemplatePath = Join-Path -Path $here -ChildPath "AppTemplate"
$validRanges = @{
"PTE" = "50000..99999";
"AppSource App" = "100000..$([int32]::MaxValue)";
"Test App" = "50000..$([int32]::MaxValue)" ;
"Performance Test App" = "50000..$([int32]::MaxValue)" ;
};
<#
.SYNOPSIS
Check that the IdRange is valid for the template type.
#>
function ConfirmIdRanges([string] $templateType, [string]$idrange ) {
$validRange = $validRanges.$templateType.Replace('..', '-').Split("-")
$validStart = [int] $validRange[0]
$validEnd = [int] $validRange[1]
$ids = $idrange.Replace('..', '-').Split("-")
$idStart = [int] $ids[0]
$idEnd = [int] $ids[1]
if ($ids.Count -ne 2 -or ($idStart) -lt $validStart -or $idStart -gt $idEnd -or $idEnd -lt $validStart -or $idEnd -gt $validEnd -or $idStart -gt $idEnd) {
throw "IdRange should be formatted as fromId..toId, and the Id range must be in $($validRange[0]) and $($validRange[1])"
}
return $ids
}
function UpdateManifest
(
[string] $sourceFolder = $alTemplatePath,
[string] $appJsonFile,
[string] $name,
[string] $publisher,
[string] $version,
[string[]] $idrange,
[switch] $AddTestDependencies
)
{
#Modify app.json
$appJson = Get-Content (Join-Path $sourceFolder "app.json") -Encoding UTF8 | ConvertFrom-Json
$appJson.id = [Guid]::NewGuid().ToString()
$appJson.Publisher = $publisher
$appJson.Name = $name
$appJson.Version = $version
$appJson.Logo = ""
$appJson.url = ""
$appJson.EULA = ""
$appJson.privacyStatement = ""
$appJson.help = ""
"contextSensitiveHelpUrl" | ForEach-Object {
if ($appJson.PSObject.Properties.Name -eq $_) { $appJson.PSObject.Properties.Remove($_) }
}
$appJson.idRanges[0].from = [int]$idrange[0]
$appJson.idRanges[0].to = [int]$idrange[1]
if ($AddTestDependencies) {
$appJson.dependencies += @(
@{
"id" = "dd0be2ea-f733-4d65-bb34-a28f4624fb14"
"publisher" = "Microsoft"
"name" = "Library Assert"
"version" = $appJson.Application
},
@{
"id" = "e7320ebb-08b3-4406-b1ec-b4927d3e280b"
"publisher" = "Microsoft"
"name" = "Any"
"version" = $appJson.Application
}
)
}
$appJson | Set-JsonContentLF -path $appJsonFile
}
function UpdateALFile
(
[string] $sourceFolder = $alTemplatePath,
[string] $destinationFolder,
[string] $alFileName,
[int] $fromId = 50100,
[int] $toId = 50100,
[int] $startId
)
{
$al = Get-Content -Encoding UTF8 -Raw -path (Join-Path $sourceFolder $alFileName)
$fromId..$toId | ForEach-Object {
$al = $al.Replace("$_", $startId)
$startId++
}
$al | Set-ContentLF -Path (Join-Path $destinationFolder $alFileName)
}
<#
.SYNOPSIS
Creates a simple app.
#>
function NewSampleApp
(
[string] $destinationPath,
[string] $name,
[string] $publisher,
[string] $version,
[string[]] $idrange,
[bool] $sampleCode
)
{
Write-Host "Creating a new sample app in: $destinationPath"
New-Item -Path $destinationPath -ItemType Directory -Force | Out-Null
New-Item -Path "$($destinationPath)\.vscode" -ItemType Directory -Force | Out-Null
Copy-Item -path "$($alTemplatePath)\.vscode\launch.json" -Destination "$($destinationPath)\.vscode\launch.json"
UpdateManifest -appJsonFile "$($destinationPath)\app.json" -name $name -publisher $publisher -idrange $idrange -version $version
if ($sampleCode) {
UpdateALFile -destinationFolder $destinationPath -alFileName "HelloWorld.al" -startId $idrange[0]
}
}
<#
.SYNOPSIS
Creates a test app.
#>
function NewSampleTestApp
(
[string] $destinationPath,
[string] $name,
[string] $publisher,
[string] $version,
[string[]] $idrange,
[bool] $sampleCode
)
{
Write-Host "Creating a new test app in: $destinationPath"
New-Item -Path $destinationPath -ItemType Directory -Force | Out-Null
New-Item -Path "$($destinationPath)\.vscode" -ItemType Directory -Force | Out-Null
Copy-Item -path "$($alTemplatePath)\.vscode\launch.json" -Destination "$($destinationPath)\.vscode\launch.json"
UpdateManifest -appJsonFile "$($destinationPath)\app.json" -name $name -publisher $publisher -idrange $idrange -version $version -AddTestDependencies
if ($sampleCode) {
UpdateALFile -destinationFolder $destinationPath -alFileName "HelloWorld.Test.al" -startId $idrange[0]
}
}
<#
.SYNOPSIS
Creates a performance test app.
#>
function NewSamplePerformanceTestApp
(
[string] $destinationPath,
[string] $name,
[string] $publisher,
[string] $version,
[string[]] $idrange,
[bool] $sampleCode,
[bool] $sampleSuite,
[string] $appSourceFolder
)
{
Write-Host "Creating a new performance test app in: $destinationPath"
New-Item -Path $destinationPath -ItemType Directory -Force | Out-Null
New-Item -Path "$($destinationPath)\.vscode" -ItemType Directory -Force | Out-Null
New-Item -Path "$($destinationPath)\src" -ItemType Directory -Force | Out-Null
Copy-Item -path "$($alTemplatePath)\.vscode\launch.json" -Destination "$($destinationPath)\.vscode\launch.json"
UpdateManifest -sourceFolder $appSourceFolder -appJsonFile "$($destinationPath)\app.json" -name $name -publisher $publisher -idrange $idrange -version $version
if ($sampleCode) {
Get-ChildItem -Path "$appSourceFolder\src" -Recurse -Filter "*.al" | ForEach-Object {
Write-Host $_.Name
UpdateALFile -sourceFolder $_.DirectoryName -destinationFolder "$($destinationPath)\src" -alFileName $_.name -fromId 149100 -toId 149200 -startId $idrange[0]
}
}
if ($sampleSuite) {
UpdateALFile -sourceFolder $alTemplatePath -destinationFolder $destinationPath -alFileName bcptSuite.json -fromId 149100 -toId 149200 -startId $idrange[0]
}
}
<#
.SYNOPSIS
Update workspace file
#>
function UpdateWorkspaces
(
[string] $projectFolder,
[string] $appName
)
{
Get-ChildItem -Path $projectFolder -Filter "*.code-workspace" |
ForEach-Object {
try {
$workspaceFileName = $_.Name
$workspaceFile = $_.FullName
$workspace = Get-Content $workspaceFile -Encoding UTF8 | ConvertFrom-Json
if (-not ($workspace.folders | Where-Object { $_.Path -eq $appName })) {
$workspace.folders = AddNewAppFolderToWorkspaceFolders $workspace.folders $appName
}
$workspace | Set-JsonContentLF -Path $workspaceFile
}
catch {
throw "Updating the workspace file $workspaceFileName failed.$([environment]::Newline) $($_.Exception.Message)"
}
}
}
<#
.SYNOPSIS
Add new App Folder to Workspace file
#>
function AddNewAppFolderToWorkspaceFolders
(
[PSCustomObject[]] $workspaceFolders,
[string] $appFolder
)
{
$newAppFolder = [PSCustomObject]@{ "path" = $appFolder }
if (-not $workspaceFolders){
return @($newAppFolder)
}
$afterFolder = $workspaceFolders | Where-Object { $_.path -ne '.github' -and $_.path -ne '.AL-Go' } | Select-Object -Last 1
if ($afterFolder) {
$workspaceFolders = @($workspaceFolders | ForEach-Object {
$_
if ($afterFolder -and $_.path -eq $afterFolder.path) {
Write-Host "Adding new path to workspace folders after $($afterFolder.Path)"
$newAppFolder
$afterFolder = $null
}
})
}
else {
Write-Host "Inserting new path in workspace folders"
$workspaceFolders = @($newAppFolder) + $workspaceFolders
}
$workspaceFolders
}
Export-ModuleMember -Function NewSampleApp
Export-ModuleMember -Function NewSampleTestApp
Export-ModuleMember -Function NewSamplePerformanceTestApp
Export-ModuleMember -Function ConfirmIdRanges
Export-ModuleMember -Function UpdateWorkspaces
Export-ModuleMember -Function AddNewAppFolderToWorkspaceFolders