DevOpsHandling/Get-DevOpsArtifactsFromFeed.ps1

<#
 .Synopsis
  Retrieves artifact for a specific artifact from a feed
 .Description
  Retrieves artifact for a specific artifact name
 .Parameter devOpsOrganization
  The DevOps organization that contains the artifacts
  .Parameter devOpsFeed
  The name of the DevOps artifact feed
  .Parameter devOpsArtifact
  The artifact name to get - it will get the _app, _runtimeapp, and _tests apps
  .Parameter destination
  The output directory that the artifacts will be saved to
  .Parameter version
  The version of the artifact that should be retrieved. If not specified, it will be read from the settings.json
  .Example
  Get-DevOpsArtifactsFromFeed -devOpsArtifact "artifact" -devOpsFeed "feed" -devOpsArtifact "artifact" -destination "C:\Install" -version "1.0.0"
#>

function Get-DevOpsArtifactsFromFeed {
    Param(
        [Parameter(Mandatory = $true)]
        [string]$devOpsOrganization,
        [Parameter(Mandatory = $true)]
        [string]$devOpsFeed,
        [Parameter(Mandatory = $true)]
        [string]$devOpsArtifact,
        [Parameter(Mandatory = $true)]
        [string]$destination,
        [Parameter(Mandatory = $true)]
        [string]$version,
        [Parameter(Mandatory = $false)]
        [string]$devOpsToken = ""
    )

    $appsList = [System.Collections.ArrayList]@()

    $filesBefore = (Get-ChildItem $destination -filter "*.app")

    if ($devOpsToken -eq "") {
        $devOpsToken = Get-DevopsToken -devOpsOrganization $devOpsOrganization
    }

    if ($version -ne "" -and $version -ne "*") {
        $version = Get-NormalizedVersion $version
    }

    $output = $null
    Write-Host "Get Artifact '$devOpsArtifact' version '$version' from '$devOpsFeed'"
    $results = (Get-ArtifactVersion -devOpsOrganization $devOpsOrganization -devOpsToken $devOpsToken -feed $devOpsFeed -packageName $devOpsArtifact)
    if ($null -eq $results) {
        Write-Host "No versions found"
        $version = "*"
    }
    else {
        Write-Host $results
        if ($version -eq "*" -or $version -eq "") {
            $version = ($results.versions | Where-Object { $_.isLatest -eq $true }).normalizedVersion
        }
        else {
            if (!(Get-Member -InputObject $results -Name "versions" -MemberType Properties)) {
                Write-Host "could not find property 'versions' on result"
                $version = "*"
            }
            else {
                $tempResult = ($results.versions | Where-Object { $_.normalizedVersion -ge (Get-NormalizedVersion -version $version)})
                if ($null -eq $tempResult) {
                    throw "Cannot find version of $devOpsArtifact that is equal or greater than $version"
                }
                else {
                    $version = $tempResult.normalizedVersion
                }
            }
        }
    }

    Write-Host "Downloading '$devOpsArtifact' with version '$version' from '$devOpsFeed'"
    $output = az artifacts universal download --organization ("https://dev.azure.com/{0}/" -f $devOpsOrganization) --feed "$devOpsFeed" --name "$devOpsArtifact" --version "$version" --path "$destination"
    if (!$output) {
        throw "Could not download package $devOpsArtifact from $devOpsOrganization feed $devOpsFeed"
    }
    Write-Host "Downloaded app"

    $files = (Get-ChildItem $destination -Filter "*.app")
    Write-Host "Files: $files"
    foreach ($file in $files) {
        $ok = ($null -eq $filesBefore)
        if (!(Get-Member -InputObject $file -Name "Name" -MemberType Properties)) {
            $ok = $false
        }
        else {
            if (!$ok) {
                $ok = (!$filesBefore.Name.Contains($file.Name))
            }
        }
        if ($ok) {
            if (!$file.Name.StartsWith('Microsoft')) {
                if (!$appsList.contains($file.FullName)) {
                    [void]$appsList.Add($file.FullName)
                }
            }
        }
    }
    Write-Host "after foreach"

    return $appsList
}