Publish/Copy-AppFilesToFolder.ps1

function CopyAppFilesToFolder {
    Param(
        $appFiles,
        [string] $folder
    )

    if ($appFiles -is [String]) {
        if (!(Test-Path $appFiles)) {
            $appFiles = @($appFiles.Split(',').Trim() | Where-Object { $_ })
        }
    }
    if (!(Test-Path $folder)) {
        New-Item -Path $folder -ItemType Directory | Out-Null
    }
    $appFiles | Where-Object{ $_ } | % {
        $appFile = $_
        if ($appFile -like "http://*" -or $appFile -like "https://*") {
            $appUrl = $appFile
            $appFile = Join-Path ([System.IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString())
            Download-File -sourceUrl $appUrl -destinationFile $appFile
            CopyAppFilesToFolder -appFile $appFile -folder $folder
            Remove-Item -Path $appFile -Force
        }
        elseif (Test-Path $appFile -PathType Container) {
            get-childitem $appFile -Filter '*.app' -Recurse | ForEach-Object {
                $destFile = Join-Path $folder $_.Name
                if (Test-Path $destFile) {
                    Write-Host -ForegroundColor Yellow "WARNING: $([System.IO.Path]::GetFileName($destFile)) already exists, it looks like you have multiple app files with the same name. App filenames must be unique."
                }
                Copy-Item -Path $_.FullName -Destination $destFile -Force
                $destFile
            }
        }
        elseif (Test-Path $appFile -PathType Leaf) {
            if ([string]::new([char[]](Get-Content $appFile -Encoding byte -TotalCount 2)) -eq "PK") {
                $tmpFolder = Join-Path ([System.IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString())
                $copied = $false
                try {
                    if ($appFile -notlike "*.zip") {
                        $orgAppFile = $appFile
                        $appFile = Join-Path ([System.IO.Path]::GetTempPath()) "$([System.IO.Path]::GetFileName($orgAppFile)).zip"
                        Copy-Item $orgAppFile $appFile
                        $copied = $true
                    }
                    Expand-Archive $appfile -DestinationPath $tmpFolder -Force
                    Get-ChildItem -Path $tmpFolder -Recurse | Where-Object { $_.Name -like "*.app" -or $_.Name -like "*.zip" } | % {
                        CopyAppFilesToFolder -appFile $_.FullName -folder $folder
                    }
                }
                finally {
                    Remove-Item -Path $tmpFolder -Recurse -Force
                    if ($copied) { Remove-Item -Path $appFile -Force }
                }
            }
            else {
                $destFile = Join-Path $folder "$([System.IO.Path]::GetFileNameWithoutExtension($appFile)).app"
                if (Test-Path $destFile) {
                    Write-Host -ForegroundColor Yellow "WARNING: $([System.IO.Path]::GetFileName($destFile)) already exists, it looks like you have multiple app files with the same name. App filenames must be unique."
                }
                Copy-Item -Path $appFile -Destination $destFile -Force
                $destFile
            }
        }
        else {
            Write-Host -ForegroundColor Red "File not found: $appFile"
        }
    }
}