Modules/businessdev.ALbuild.Containers/Public/Import-BcConfigurationPackage.ps1

function Import-BcConfigurationPackage {
    <#
    .SYNOPSIS
        Imports a RapidStart (.rapidstart) configuration package into a Business Central container.
 
    .DESCRIPTION
        Copies a RapidStart configuration package from the host into the container and imports it with
        Import-NAVConfigurationPackageFile. The package source is a .rapidstart file or a folder that
        contains one (e.g. the output of Get-BcUniversalPackage). Requires Windows + a running Docker
        engine. This is the native replacement for the V1 DownloadConfigPackageUniversalFeed import.
 
    .PARAMETER Name
        Container name.
 
    .PARAMETER Path
        A .rapidstart file, or a folder to search (recursively) for the first .rapidstart file.
 
    .PARAMETER ServerInstance
        Business Central server instance inside the container. Default 'BC'.
 
    .PARAMETER DockerExecutable
        The Docker executable to use. Default 'docker'.
 
    .EXAMPLE
        Import-BcConfigurationPackage -Name bld -Path '.\config\Setup.rapidstart'
 
    .EXAMPLE
        $pkg = Get-BcUniversalPackage -Organization org -Feed D365BC -Name cfg -OutputFolder $tmp
        Import-BcConfigurationPackage -Name bld -Path $pkg.OutputFolder
 
    .OUTPUTS
        PSCustomObject: Container, File.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [Alias('ContainerName')] [string] $Name,
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Path,
        [string] $ServerInstance = 'BC',
        [string] $DockerExecutable = 'docker'
    )

    if (-not (Test-Path -LiteralPath $Path)) { throw "Path '$Path' does not exist." }

    $file = if (Test-Path -LiteralPath $Path -PathType Container) {
        Get-ChildItem -LiteralPath $Path -Filter '*.rapidstart' -Recurse -File | Select-Object -First 1
    }
    else {
        Get-Item -LiteralPath $Path
    }
    if (-not $file) { throw "No .rapidstart configuration package found at '$Path'." }

    if (-not $PSCmdlet.ShouldProcess($Name, "Import configuration package '$($file.Name)'")) {
        return [PSCustomObject]@{ Container = $Name; File = $file.FullName }
    }

    $containerPath = "C:\run\$($file.Name)"
    Invoke-BcDocker -DockerExecutable $DockerExecutable -Quiet -Arguments @('cp', $file.FullName, "$($Name):$containerPath") | Out-Null

    $output = Invoke-BcContainerCommand -ContainerName $Name -DockerExecutable $DockerExecutable -Variables @{
        ServerInstance = $ServerInstance
        PackagePath    = $containerPath
    } -ScriptBlock {
        Import-NAVConfigurationPackageFile -ServerInstance $ServerInstance -Path $PackagePath -ErrorAction Stop
        Write-Output "Imported configuration package $PackagePath"
    }
    Write-ALbuildLog -Level Success "Imported configuration package '$($file.Name)' into '$Name'.`n$($output.Trim())"

    return [PSCustomObject]@{ Container = $Name; File = $file.FullName }
}