AL/Download-Symbols.ps1

function Download-Symbols {
    Param(
        [Parameter(Mandatory=$false)]
        [string]$SourcePath = (Get-Location),
        [Parameter(Mandatory=$false)]
        [string]$ContainerName = (Get-ContainerFromLaunchJson),
        # optionally download the test symbols
        [Parameter(Mandatory=$false)]
        [switch]
        $includeTestSymbols
    )

    $PackagesPath = Join-Path $SourcePath '.alpackages'

    if (!(Test-Path $PackagesPath)) {
        Create-EmptyDirectory $PackagesPath
    }

    [version]$BCFallPlatform = '15.0.0.0'
    [version]$platform = Get-AppKeyValue -SourcePath $SourcePath -KeyName 'platform'
    if ($platform -ge $BCFallPlatform) {
        $ServerInstance = 'bc'
    }else{
        $ServerInstance = 'nav'
    }
    $SymbolsDownloaded = $false

    # since BC15, system layer can be defined in dependencies
    $Dependencies = Get-AppKeyValue -SourcePath $SourcePath -KeyName 'dependencies'
    foreach ($Dependency in $Dependencies) {
        if ($Dependency.publisher -eq 'Microsoft') {
            $Uri = 'http://{0}:7049/{1}/dev/packages?publisher={2}&appName={3}&versionText={4}&tenant=default' -f $ContainerName, $ServerInstance, $Dependency.publisher, $Dependency.name, $Dependency.version
            Write-Host $Uri
            Invoke-WebRequest -Uri $Uri -Headers (Create-Headers) -OutFile (Join-Path $PackagesPath ('{0}_{1}_{2}.app' -f $Dependency.publisher, $Dependency.name, $Dependency.version))
            $SymbolsDownloaded = $true
        }
    }

    if ($SymbolsDownloaded) {
        $Uri = 'http://{0}:7049/{1}/dev/packages?publisher=Microsoft&appName=System&versionText={2}&tenant=default' -f $ContainerName, $ServerInstance, (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'platform')
        Write-Host $Uri
        Invoke-WebRequest -Uri $Uri -Headers (Create-Headers) -OutFile (Join-Path $PackagesPath ('Microsoft_System_{0}.app' -f (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'platform')))

        $Uri = 'http://{0}:7049/{1}/dev/packages?publisher=Microsoft&appName=Application&versionText={2}&tenant=default' -f $ContainerName, $ServerInstance, (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'platform')
        Write-Host $Uri
        Invoke-WebRequest -Uri $Uri -Headers (Create-Headers) -OutFile (Join-Path $PackagesPath ('Microsoft_Application_{0}.app' -f (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'platform')))
    }
    # prior to BC15 and from BC17, system layer defined through properties in app.json
    else {
        $Uri = 'http://{0}:7049/{1}/dev/packages?publisher=Microsoft&appName=Application&versionText={2}&tenant=default' -f $ContainerName, $ServerInstance, (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'application')
        Write-Host $Uri
        Invoke-WebRequest -Uri $Uri -Headers (Create-Headers) -OutFile (Join-Path $PackagesPath ('Microsoft_Application_{0}.app' -f (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'application')))
        
        $Uri = 'http://{0}:7049/{1}/dev/packages?publisher=Microsoft&appName=System&versionText={2}&tenant=default' -f $ContainerName, $ServerInstance, (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'platform')
        Write-Host $Uri
        Invoke-WebRequest -Uri $Uri -Headers (Create-Headers) -OutFile (Join-Path $PackagesPath ('Microsoft_System_{0}.app' -f (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'platform')))

        if ($platform -ge $BCFallPlatform) {
            $Uri = 'http://{0}:7049/{1}/dev/packages?publisher=Microsoft&appName=System Application&versionText={2}&tenant=default' -f $ContainerName, $ServerInstance, (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'application')
            Write-Host $Uri
            Invoke-WebRequest -Uri $Uri -Headers (Create-Headers) -OutFile (Join-Path $PackagesPath ('Microsoft_System Application_{0}.app' -f (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'application')))
        
            $Uri = 'http://{0}:7049/{1}/dev/packages?publisher=Microsoft&appName=Base Application&versionText={2}&tenant=default' -f $ContainerName, $ServerInstance, (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'application')
            Write-Host $Uri
            Invoke-WebRequest -Uri $Uri -Headers (Create-Headers) -OutFile (Join-Path $PackagesPath ('Microsoft_Base Application_{0}.app' -f (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'application')))
        } else {
            if ($includeTestSymbols.IsPresent) {
                $Uri = 'http://{0}:7049/{1}/dev/packages?publisher=Microsoft&appName=Test&versionText={2}&tenant=default' -f $ContainerName, $ServerInstance, (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'test')
                Write-Host $Uri
                Invoke-WebRequest -Uri $Uri -Headers (Create-Headers) -OutFile (Join-Path $PackagesPath ('Microsoft_Test_{0}.app' -f (Get-AppKeyValue -SourcePath $SourcePath -KeyName 'test')))
            }
        }
    }
}

function Create-Headers
{
    $ba = '{0}:{1}' -f (Get-EnvironmentKeyValue -KeyName 'user'), (Get-EnvironmentKeyValue -KeyName 'password')
    $ba = [System.Text.Encoding]::UTF8.GetBytes($ba)
    $ba = [System.Convert]::ToBase64String($ba)
    $h = @{Authorization=("Basic {0}" -f $ba);'Accept-Encoding'='gzip,deflate'}   
    $h
}

Export-ModuleMember -Function Download-Symbols