AL/Remove-ALTestDependencyList.ps1

<#
 .Synopsis
  removes test dependencies from app.json
 .Description
  removes all test dependencies from app.json
 .Parameter SourcePath
  Path to the current project
 .Example
  Remove-ALTestDependencyList
#>

function Remove-ALTestDependencyList {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact="low")]
    Param(
        [Parameter(Mandatory = $false)]
        [string] $sourcePath = (Get-Location)
    )

    $testapps = Get-EnvironmentKeyValue -SourcePath $sourcePath -KeyName "testapps"
    $dependencies = Get-AppKeyValue -SourcePath $sourcePath -KeyName "dependencies"

    [Version]$PlatformVersion = [Version]::Parse((Get-AppKeyValue -SourcePath (Get-Location) -KeyName 'platform'))
    $compiler = Get-AppKeyValue -SourcePath $TestPath -KeyName 'runtime'
    if ($PSCmdlet.ShouldProcess("app.json", "Removes test apps from app.json")) {
        if ($compiler -ge '4.3') {
            try {
                $dependencies = $dependencies | Where-Object { $testapps.appId -notcontains $_.Id }
                Set-AppKeyValue -SourcePath $sourcePath -KeyName "dependencies" -KeyValue $dependencies #(ConvertTo-Json $dependencies -Compress)
            }
            catch {
                throw "Could not remove test dependencies"
            }
        }
        else {
            if ($PlatformVersion -ge ([Version]::Parse('15.0.0.0'))) {
                try {
                    $dependencies = $dependencies | Where-Object { $testapps.appId -notcontains $_.appId }
                    Set-AppKeyValue -SourcePath $sourcePath -KeyName "dependencies" -KeyValue $dependencies
                }
                catch {
                    throw "Could not remove test dependencies"
                }
            }
            else {
                Set-AppKeyValue -SourcePath $sourcePath -KeyName test -KeyValue ''
            }
        }
    }
}
Export-ModuleMember Remove-ALTestDependencyList