DevOpsHandling/Publish-DevOpsArtifactsToFeed.ps1

function Publish-DevOpsArtifactsToFeed {
    Param(
        [Parameter(Mandatory=$false)]
        [string]$devOpsOrganization = "",
        [Parameter(Mandatory=$false)]
        [string]$devOpsFeed = "",
        [Parameter(Mandatory=$false)]
        [string]$devOpsArtifact = "",
        [Parameter(Mandatory=$true)]
        [string]$sourcePath,
        [Parameter(Mandatory=$false)]
        [string]$version = ""
    )

    if ($devOpsOrganization -eq "") {
        $devOpsOrganization = Get-EnvironmentKeyValue -KeyName "Organization"
    }
    if ($devOpsArtifact -eq "") {
        $devOpsArtifact = (((Get-AppKeyValue -KeyName 'name') -replace ' ', '_').ToLower())
    }
    if ($version -eq "") {
        $version = ((Get-AppKeyValue -KeyName 'version') -replace '(.*)\.(.*)','$1')
    }
    if ($devOpsFeed -eq "") {
        $dependencies = Get-EnvironmentKeyValue -KeyName "dependencies"
        if ($null -ne $dependencies) {
            if ($dependencies -ne "") {
                $devOpsFeed = $dependencies[0].devOpsFeed
            }
        }
    }

    if ($devOpsFeed -eq "") {
        $devOpsFeed = (Get-EnvironmentKeyValue -KeyName "devOpsFeed")
    }

    # publishing app
    Write-Host "Publishing $($devOpsArtifact)_app, version $version, in feed $devOpsFeed, organization $devOpsOrganization"

    foreach ($app in (Get-ChildItem (Join-Path $sourcePath "app"))) {
        $output = az artifacts universal publish --organization ("https://dev.azure.com/" + $devOpsOrganization) --feed "$devOpsFeed" --name "$($devOpsArtifact)_app" --version "$version" --path $app.FullName 
        if (!$output) {
            throw "Could not publish package $($devOpsArtifact)_app to $devOpsOrganization feed $devOpsFeed"
        }
        else {
            Write-Host "Published $($devOpsArtifact)_app version $($version)"
        }
    }

    # publishing runtime app
    Write-Host "Publishing $($devOpsArtifact)_runtime, version $version, in feed $devOpsFeed, organization $devOpsOrganization"

    foreach ($app in (Get-ChildItem (Join-Path $sourcePath "runtimeapp"))) {
        $output = az artifacts universal publish --organization ("https://dev.azure.com/" + $devOpsOrganization) --feed "$devOpsFeed" --name "$($devOpsArtifact)_runtime" --version "$version" --path $app.FullName 
        if (!$output) {
            throw "Could not publish package $($devOpsArtifact)_runtime to $devOpsOrganization feed $devOpsFeed"
        }
        else {
            Write-Host "Published $($devOpsArtifact)_runtimeapp version $($version)"
        }
    }

    # publishing tests app
    Write-Host "Publishing $($devOpsArtifact)_tests, version $version, in feed $devOpsFeed, organization $devOpsOrganization"

    foreach ($app in (Get-ChildItem (Join-Path $sourcePath "testapp"))) {
        $output = az artifacts universal publish --organization ("https://dev.azure.com/" + $devOpsOrganization) --feed "$devOpsFeed" --name "$($devOpsArtifact)_tests" --version "$version" --path $app.FullName 
        if (!$output) {
            throw "Could not publish package $($devOpsArtifact)_tests to $devOpsOrganization feed $devOpsFeed"
        }
        else {
            Write-Host "Published $($devOpsArtifact)_tests version $($version)"
        }
    }

    # publishing base objects
    if (Test-Path (Join-Path $sourcePAth "baseobjects")) {
        Write-Host "Publishing $($devOpsArtifact)_baseobjects, version $version, in feed $devOpsFeed, organization $devOpsOrganization"

        foreach ($app in (Get-ChildItem (Join-Path $sourcePath "baseobjects"))) {
            $output = az artifacts universal publish --organization ("https://dev.azure.com/" + $devOpsOrganization) --feed "$devOpsFeed" --name "$($devOpsArtifact)_baseobjects" --version "$version" --path $app.FullName 
            if (!$output) {
                throw "Could not publish package $($devOpsArtifact)_baseobjects to $devOpsOrganization feed $devOpsFeed"
            }
            else {
                Write-Host "Published $($devOpsArtifact)_baseobjects version $($version)"
            }
        }
    }
}
Export-ModuleMember Publish-DevOpsArtifactsToFeed