AppFilesHandling/Import-AllAppsIntoBCContainer.ps1

<#
.SYNOPSIS
    Build and install all apps from selected folder into specified container
.DESCRIPTION
    Compile and install all apps in a selected folder into specified container, if any app matches already installed app, upgrade procedure will be started
.EXAMPLE
    Install-AppsLocally -containerName bccontainer -appsFolder ~/MyProject/Apps
.NOTES
    The command uses commands from BcContainerHelper to export and compile the objects.
 
    Paramters:
 
    -containerName: Container name that will be used to compile apps and install into
    -appsFolder: folder which contains apps to be compiled and installed into container
    -credential (optional): PSCredentials object that contains credentials you chose during container creation process
    -unpublish (optional): Switch to indicate that old versions of installing apps should be unpublish
#>


function Import-AllAppsIntoBCContainer {
    Param(
        [Parameter(Mandatory = $true)]
        [string] $containerName,
        [Parameter(Mandatory = $true)]
        [string] $appsFolder,
        [pscredential] $credentials = (Get-Credential -m "Provide your container/BC credentials that you chose during container creation process"),
        [switch] $unpublish
    )
    $appsToUnpublish = @{}

    $sharedFolder = NewContainerSharedTempDirectory($containerName)
    Copy-Item -Path $appsFolder -Destination $sharedFolder -Recurse
    $manifests = Get-ITIAppManifestFile -Path $sharedFolder
    $appProjectFolders = Sort-AppFoldersByDependencies -appFolders $manifests.Directory.FullName | Get-Item
    foreach ($folder in $appProjectFolders) {
        Compile-AppInNavContainer -containerName $containerName -credential $credential -appProjectFolder $folder.FullName
        $appFile = (Get-ChildItem $folder -File -Filter "output/*.app").FullName
        $appManifest = Get-Content (Join-Path $folder.FullName "app.json") | ConvertFrom-Json
        $currentVersion = $appManifest.version
        $appId = @(Get-BcContainerAppInfo -containerName $containerName | ? { $_.AppId -eq $appManifest.id } | Select-Object -ExpandProperty AppId)
        if ($appId) {
            $tempPackageId = @(Get-BcContainerAppInfo -containerName $containerName | Where-Object { $_.AppId -eq $appId[0] } | Select-Object -ExpandProperty PackageId)
            $tempVersion = @(Get-BcContainerAppInfo -containerName $containerName | Where-Object { $_.AppId -eq $appId[0] } | Select-Object -ExpandProperty Version)
            Publish-NavContainerApp -containerName $containerName -credential $Credential -appFile $appFile -skipVerification -sync -install -upgrade

            for ($i = 0; $i -lt $tempPackageId.Count; $i++) {

                if (([string]$tempVersion -split (" "))[$i] -ne $currentVersion) {

                    $appsToUnpublish.Add($tempPackageId[$i], ([string]$tempVersion -split (" "))[$i])
                }
            }
        }
        else {
            Publish-NavContainerApp -containerName $containerName -credential $Credential -appFile $appFile -skipVerification -sync -install
        }
    }
    if ($unpublish) {
        foreach ($id in $appsToUnpublish.Keys) {
            $appVersion = $appsToUnpublish.$id
            $appName = Get-BcContainerAppInfo -containerName $containerName | Where-Object { $_.PackageId -eq $id } | Select-Object -ExpandProperty Name
            Write-Host "Unpublishing " $appName " version " $appVersion -ForegroundColor DarkYellow
            UnPublish-BcContainerApp -containerName $containerName -name $appName -version $appVersion -force
            Sync-BcContainerApp -containerName $containerName -appName $appName -appVersion $appVersion -Mode Clean -Force
        }
    }

    Remove-Item $sharedFolder -Force -Recurse
}

Export-ModuleMember -Function Import-AllAppsIntoBCContainer