-
Notifications
You must be signed in to change notification settings - Fork 16
/
HelperFunctions.ps1
238 lines (225 loc) · 10.3 KB
/
HelperFunctions.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
$bcContainerHelperVersion = 'https://bccontainerhelper.blob.core.windows.net/public/preview.zip'
$tempName = Join-Path ([System.IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString())
Write-Host "Downloading BcContainerHelper developer version from $bcContainerHelperVersion"
$webclient = New-Object System.Net.WebClient
$webclient.DownloadFile($bcContainerHelperVersion, "$tempName.zip")
Expand-Archive -Path "$tempName.zip" -DestinationPath "$tempName"
Remove-Item "$tempName.zip"
$bcContainerHelperPath = (Get-Item -Path (Join-Path $tempName "*\BcContainerHelper.ps1")).FullName
. $bcContainerHelperPath
$bcContainerHelperConfig.DoNotUseCdnForArtifacts = $true
$ErrorActionPreference = "stop"
function GetRuntimeDependencyPackageId {
Param(
[string] $package
)
$nuspecFile = Join-Path $package 'manifest.nuspec'
$nuspec = [xml](Get-Content -Path $nuspecFile -Encoding UTF8)
$packageId = $nuspec.package.metadata.id
if ($packageId -match "^([^.]+)\.([^.]+)\..*$($appJson.id)`$") {
$publisherAndName = "$($Matches[1]).$($Matches[2])"
Write-Host "Publisher is $($Matches[1]) and name is $($Matches[2])"
}
else {
throw "Cannot determine publisher and name from the $packageId"
}
$runtimeDependencyPackageId = $nuspec.package.metadata.dependencies.dependency | Where-Object { $_.id -like "$($publisherAndName).runtime-*" } | Select-Object -ExpandProperty id
if (-not $runtimeDependencyPackageId) {
throw "Cannot determine dependency package id"
}
return $runtimeDependencyPackageId
}
function GetRuntimeDependencyPackageIds {
Param(
[string[]] $apps,
[string] $nuGetServerUrl,
[string] $nuGetToken
)
$runtimeDependencyPackageIds = @{}
$newPackage = $false
foreach($appFile in $apps) {
$appName = [System.IO.Path]::GetFileName($appFile)
$appJson = Get-AppJsonFromAppFile -appFile $appFile
# Test whether a NuGet package exists for this app?
$bcContainerHelperConfig.TrustedNuGetFeeds = @(
[PSCustomObject]@{ "url" = $nuGetServerUrl; "token" = $nuGetToken; "Patterns" = @("*.runtime.$($appJson.id)") }
)
$package = Get-BcNuGetPackage -packageName "runtime.$($appJson.id)" -version $appJson.version -select Exact
if (-not $package) {
# If just one of the apps doesn't exist as a nuGet package, we need to create a new indirect nuGet package and build all runtime versions of the nuGet
$package = Join-Path ([System.IO.Path]::GetTempPath()) ([GUID]::NewGuid().ToString())
New-BcNuGetPackage -appfile $appFile -isIndirectPackage -packageId "{publisher}.{name}.runtime.{id}" -runtimeDependencyId '{publisher}.{name}.runtime-{version}' -destinationFolder $package | Out-Null
$newPackage = $true
}
$runTimeDependencyPackageId = GetRuntimeDependencyPackageId -package $package
if ($newPackage) {
Remove-Item -Path $package -Recurse -Force
}
$runtimeDependencyPackageIds += @{ $appName = $runTimeDependencyPackageId }
}
return $runtimeDependencyPackageIds, $newPackage
}
function GetNuGetServerUrlAndRepository {
Param(
[string] $nuGetServerUrl
)
if ($nugetServerUrl -match '^https:\/\/github\.com\/([^\/]+)\/([^\/]+)$') {
$githubRepository = $nuGetServerUrl
$nuGetServerUrl = "https://nuget.pkg.github.com/$($Matches[1])/index.json"
}
else {
$githubRepository = ''
}
return $nuGetServerUrl, $githubRepository
}
function NormalizeVersionStr {
Param(
[string] $versionStr
)
$version = [System.version]$versionStr
if ($version.Build -eq -1) { $version = [System.Version]::new($version.Major, $version.Minor, 0, 0) }
if ($version.Revision -eq -1) { $version = [System.Version]::new($version.Major, $version.Minor, $version.Build, 0) }
return "$version"
}
# Find the highest application dependency for the apps in order to determine which BC Application version to use for runtime packages
function GetHighestApplicationDependency {
Param(
[string[]] $apps,
[string] $lowestVersion
)
if (-not $lowestVersion) { $lowestVersion = '1.0' }
$highestApplicationDependency = NormalizeVersionStr($lowestVersion)
foreach($appFile in $apps) {
$appJson = Get-AppJsonFromAppFile -appFile $appFile
# Determine Application Dependency for this app
if ($appJson.PSObject.Properties.Name -eq "Application") {
$applicationDependency = $appJson.application
}
else {
$baseAppDependency = $appJson.dependencies | Where-Object { $_.Name -eq "Base Application" -and $_.Publisher -eq "Microsoft" }
if ($baseAppDependency) {
$applicationDependency = $baseAppDependency.Version
}
else {
throw "Cannot determine application dependency for $appFile"
}
}
# Determine highest application dependency for all apps
if ([System.Version]$applicationDependency -gt [System.Version]$highestApplicationDependency) {
$highestApplicationDependency = $applicationDependency
}
}
return $highestApplicationDependency
}
function GetArtifactVersionsSince {
Param(
[string] $type,
[string] $country,
[string] $version,
[switch] $includeLatest
)
$artifactVersions = @()
$applicationVersion = [System.Version]$version
while ($true) {
$artifacturl = Get-BCArtifactUrl -type $type -country $country -version "$applicationVersion" -select Closest
if ($artifacturl) {
$artifactVersions += @([System.Version]($artifacturl.split('/')[4]))
if ($includeLatest) {
$latestArtifacturl = Get-BCArtifactUrl -type $type -country $country -version "$($applicationVersion.Major).$($applicationVersion.Minor).$([Int32]::MaxValue).$([Int32]::MaxValue)" -select Closest
if ($latestArtifacturl -ne $artifacturl) {
$artifactVersions += @([System.Version]($latestArtifacturl.split('/')[4]))
}
}
$applicationVersion = [System.Version]"$($applicationVersion.Major).$($applicationVersion.Minor+1).0.0"
}
elseif ($applicationVersion.Minor -eq 0) {
break
}
else {
$applicationVersion = [System.Version]"$($applicationVersion.Major+1).0.0.0"
}
}
return $artifactVersions
}
function GetArtifactVersionsNeeded {
Param(
[string[]] $apps,
[System.Version[]] $allArtifactVersions,
[hashtable] $runtimeDependencyPackageIds,
[string] $nuGetServerUrl,
[string] $nuGetToken
)
# Look for latest artifacts first
[Array]::Reverse($allArtifactVersions)
# Search for runtime nuGet packages for all apps
$artifactsNeeded = @()
foreach($appFile in $apps) {
$appName = [System.IO.Path]::GetFileName($appFile)
foreach($artifactVersion in $allArtifactVersions) {
$runtimeDependencyPackageId = $runtimeDependencyPackageIds."$appName"
$bcContainerHelperConfig.TrustedNuGetFeeds = @(
[PSCustomObject]@{ "url" = $nuGetServerUrl; "token" = $nuGetToken; "Patterns" = @($runtimeDependencyPackageId) }
)
$package = Get-BcNuGetPackage -packageName $runtimeDependencyPackageId -version "$artifactVersion" -select Exact
if ($package) {
break
}
else {
$artifactsNeeded += @($artifactVersion)
}
}
}
return ($artifactsNeeded | Select-Object -Unique)
}
function GenerateRuntimeAppFiles {
Param(
[string] $containerName,
[string] $type,
[string] $country,
[string[]] $additionalCountries,
[string] $artifactVersion,
[string[]] $apps,
[string[]] $dependencies,
[string] $licenseFileUrl
)
$artifacturl = Get-BCArtifactUrl -type $type -country $country -version $artifactVersion -select Closest
$global:runtimeAppFiles = @{}
$global:countrySpecificRuntimeAppFiles = @{}
Convert-BcAppsToRuntimePackages -containerName $containerName -artifactUrl $artifacturl -imageName '' -apps $apps -publishApps $dependencies -licenseFile $licenseFileUrl -skipVerification -afterEachRuntimeCreation { Param($ht)
if (-not $ht.runtimeFile) { throw "Could not generate runtime package" }
$appName = [System.IO.Path]::GetFileName($ht.appFile)
$global:runtimeAppFiles += @{ $appName = $ht.runtimeFile }
$global:countrySpecificRuntimeAppFiles += @{ $appName = @{} }
} | Out-Null
foreach($ct in $additionalCountries) {
$artifacturl = Get-BCArtifactUrl -type $type -country $ct -version $artifactVersion -select Closest
Convert-BcAppsToRuntimePackages -containerName $containerName -artifactUrl $artifacturl -imageName '' -apps $apps -publishApps $dependencies -licenseFile $licenseFileUrl -skipVerification -afterEachRuntimeCreation { Param($ht)
if (-not $ht.runtimeFile) { throw "Could not generate runtime package" }
$appName = [System.IO.Path]::GetFileName($ht.appFile)
$global:countrySpecificRuntimeAppFiles."$appName" += @{ $ct = $ht.runtimeFile }
} | Out-Null
}
return $global:runtimeAppFiles, $global:countrySpecificRuntimeAppFiles
}
function GetAppFile {
Param(
[string] $appFile,
[switch] $symbolsOnly
)
Write-Host "'$appFile'"
Write-Host $appFile.GetType()
if ($symbolsOnly) {
$symbolsFolder = Join-Path ([System.IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString())
New-Item -Path $symbolsFolder -ItemType Directory | Out-Null
$symbolsFile = Join-Path $symbolsFolder "$([System.IO.Path]::GetFileNameWithoutExtension(($appFile)))_symbols.app"
Write-Host "Creating symbols file $symbolsFile"
Create-SymbolsFileFromAppFile -appFile $appFile -symbolsFile $symbolsFile | Out-Null
if (-not (Test-Path $symbolsFile)) {
throw "Could not create symbols file from $appFile"
}
return $symbolsFile
}
else {
return $appFile
}
}