-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathAddExistingApp.ps1
210 lines (187 loc) · 8.38 KB
/
AddExistingApp.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
Param(
[Parameter(HelpMessage = "The GitHub actor running the action", Mandatory = $false)]
[string] $actor,
[Parameter(HelpMessage = "The GitHub token running the action", Mandatory = $false)]
[string] $token,
[Parameter(HelpMessage = "Project name if the repository is setup for multiple projects", Mandatory = $false)]
[string] $project = '.',
[Parameter(HelpMessage = "Direct Download Url of .app or .zip file", Mandatory = $true)]
[string] $url,
[Parameter(HelpMessage = "Set the branch to update", Mandatory = $false)]
[string] $updateBranch,
[Parameter(HelpMessage = "Direct Commit?", Mandatory = $false)]
[bool] $directCommit
)
function getfiles {
Param(
[string] $url
)
$path = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString()).app"
Download-File -sourceUrl $url -destinationFile $path
if (!(Test-Path -Path $path)) {
throw "could not download the file."
}
expandfile -path $path
Remove-Item $path -Force -ErrorAction SilentlyContinue
}
function expandfile {
Param(
[string] $path
)
if ([string]::new([char[]](Get-Content $path @byteEncodingParam -TotalCount 2)) -eq "PK") {
# .zip file
$destinationPath = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString())"
Expand-7zipArchive -path $path -destinationPath $destinationPath
$directoryInfo = Get-ChildItem $destinationPath | Measure-Object
if ($directoryInfo.count -eq 0) {
throw "The file is empty or malformed."
}
$appFolders = @()
if (Test-Path (Join-Path $destinationPath 'app.json')) {
$appFolders += @($destinationPath)
}
Get-ChildItem $destinationPath -Recurse | Where-Object { $_.PSIsContainer -and (Test-Path -Path (Join-Path $_.FullName 'app.json')) } | ForEach-Object {
if (!($appFolders -contains $_.Parent.FullName)) {
$appFolders += @($_.FullName)
}
}
$appFolders | ForEach-Object {
$newFolder = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString())"
write-Host "$_ -> $newFolder"
Move-Item -Path $_ -Destination $newFolder -Force
Write-Host "done"
$newFolder
}
if (Test-Path $destinationPath) {
Get-ChildItem $destinationPath -include @("*.zip", "*.app") -Recurse | ForEach-Object {
expandfile $_.FullName
}
Remove-Item -Path $destinationPath -Force -Recurse -ErrorAction SilentlyContinue
}
}
elseif ([string]::new([char[]](Get-Content $path @byteEncodingParam -TotalCount 4)) -eq "NAVX") {
$destinationPath = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString())"
Extract-AppFileToFolder -appFilename $path -appFolder $destinationPath -generateAppJson
$destinationPath
}
else {
throw "The provided url cannot be extracted. The url might be wrong or the file is malformed."
}
}
. (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve)
$serverUrl, $branch = CloneIntoNewFolder -actor $actor -token $token -updateBranch $updateBranch -DirectCommit $directCommit -newBranchPrefix 'add-existing-app'
$baseFolder = (Get-Location).path
DownloadAndImportBcContainerHelper -baseFolder $baseFolder
$type = "PTE"
Write-Host "Reading $RepoSettingsFile"
$settingsJson = Get-Content $RepoSettingsFile -Encoding UTF8 | ConvertFrom-Json
if ($settingsJson.PSObject.Properties.Name -eq "type") {
$type = $settingsJson.type
}
CheckAndCreateProjectFolder -project $project
$projectFolder = (Get-Location).path
$appNames = @()
getfiles -url $url | ForEach-Object {
$appFolder = $_
"?Content_Types?.xml", "MediaIdListing.xml", "navigation.xml", "NavxManifest.xml", "DocComments.xml", "SymbolReference.json" | ForEach-Object {
Remove-Item (Join-Path $appFolder $_) -Force -ErrorAction SilentlyContinue
}
$appJson = Get-Content (Join-Path $appFolder "app.json") -Encoding UTF8 | ConvertFrom-Json
$appNames += @($appJson.Name)
$ranges = @()
if ($appJson.PSObject.Properties.Name -eq "idRanges") {
$ranges += $appJson.idRanges
}
if ($appJson.PSObject.Properties.Name -eq "idRange") {
$ranges += @($appJson.idRange)
}
# Determine whether the app is PTE or AppSource App based on one of the id ranges (the first)
if ($ranges[0].from -lt 100000 -and $ranges[0].to -lt 100000) {
$ttype = "PTE"
}
else {
$ttype = "AppSource App"
}
if ($appJson.PSObject.Properties.Name -eq "dependencies") {
foreach($dependency in $appJson.dependencies) {
if ($dependency.PSObject.Properties.Name -eq "AppId") {
$id = $dependency.AppId
}
else {
$id = $dependency.Id
}
if ($testRunnerApps.Contains($id)) {
$ttype = "Test App"
}
}
}
if ($ttype -ne "Test App") {
foreach($appName in (Get-ChildItem -Path $appFolder -Filter "*.al" -Recurse).FullName) {
$alContent = (Get-Content -Path $appName -Encoding UTF8) -join "`n"
if ($alContent -like "*codeunit*subtype*=*test*[test]*") {
$ttype = "Test App"
}
}
}
if ($ttype -ne "Test App" -and $ttype -ne $type) {
OutputWarning -message "According to settings, repository is for apps of type $type. The app you are adding seams to be of type $ttype"
}
$appFolders = Get-ChildItem -Path $appFolder | Where-Object { $_.PSIsContainer -and (Test-Path (Join-Path $_.FullName 'app.json')) }
if (-not $appFolders) {
$appFolders = @($appFolder)
# TODO: What to do about the über app.json - another workspace? another setting?
}
$orgfolderName = $appJson.name.Split([System.IO.Path]::getInvalidFileNameChars()) -join ""
$folderName = GetUniqueFolderName -baseFolder $projectFolder -folderName $orgfolderName
if ($folderName -ne $orgfolderName) {
OutputWarning -message "$orgFolderName already exists as a folder in the repo, using $folderName instead"
}
Move-Item -Path $appFolder -Destination $projectFolder -Force
Rename-Item -Path ([System.IO.Path]::GetFileName($appFolder)) -NewName $folderName
$appFolder = Join-Path $projectFolder $folderName
Get-ChildItem $appFolder -Filter '*.*' -Recurse | ForEach-Object {
if ($_.Name.Contains('%20')) {
Rename-Item -Path $_.FullName -NewName $_.Name.Replace('%20', ' ')
}
}
$appFolders | ForEach-Object {
# Modify .AL-Go\settings.json
try {
$settingsJsonFile = Join-Path $projectFolder $ALGoSettingsFile
$SettingsJson = Get-Content $settingsJsonFile -Encoding UTF8 | ConvertFrom-Json
if (@($settingsJson.appFolders) + @($settingsJson.testFolders)) {
if ($ttype -eq "Test App") {
if ($SettingsJson.testFolders -notcontains $foldername) {
$SettingsJson.testFolders += @($folderName)
}
}
else {
if ($SettingsJson.appFolders -notcontains $foldername) {
$SettingsJson.appFolders += @($folderName)
}
}
$SettingsJson | Set-JsonContentLF -Path $settingsJsonFile
}
}
catch {
throw "$ALGoSettingsFile is malformed. Error: $($_.Exception.Message)"
}
# Modify workspace
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 $foldername })) {
$workspace.folders += @(@{ "path" = $foldername })
}
$workspace | Set-JsonContentLF -Path $workspaceFile
}
catch {
throw "$workspaceFileName is malformed.$([environment]::Newline) $($_.Exception.Message)"
}
}
}
}
Set-Location $baseFolder
CommitFromNewFolder -serverUrl $serverUrl -commitMessage "Add existing apps ($($appNames -join ', '))" -branch $branch | Out-Null