Scripts/ExportSolution.ps1

# ExportSolution.ps1
function Get-DataverseSolution {
    Param(
        [string] [Parameter(Mandatory = $true)] $StartPath,
        [string] [Parameter(Mandatory = $true)] $SelectedSolution
    )
    try {
        
        $SolutionName = $SelectedSolution
        Remove-Item (Join-Path $StartPath "\dataverse_*patch*") -Force -Recurse -ErrorAction Ignore        
        Remove-Item (Join-Path $StartPath "\pac_*patch*") -Force -Recurse -ErrorAction Ignore    
        ######################## EXPORT SOLUTION
        
        ##Update version file
        Set-DataverseSolutionVersion

        ##Export Patches if they Exist
        foreach ($PatchSolution in $PatchQuery.CrmRecords) {
            $SolutionName = $PatchSolution.uniquename
            # Trigger Clone or Sync
            Unpack-Solution "pac_$SolutionName\$SolutionName.cdsproj" "pac_$SolutionName"

            # Move items from SolutionPackage/Solution folder to SolutionPackage
            Remove-Item (Join-Path $StartPath "dataverse_$SolutionName") -Recurse -Force -ErrorAction Ignore
          
            Get-FlowsToBeDeployed "$StartPath" $SelectedSolution

            Get-ExportDataValid
        }
    
        ## No Patches
        If (!$PatchQuery.CrmRecords) {
            # Trigger Clone or Sync
            Unpack-Solution "$SolutionName.cdsproj" ""
         
            # Move items from SolutionPackage/Solution folder to SolutionPackage
            Remove-Item (Join-Path $StartPath "dataverse_$SolutionName") -Recurse -Force -ErrorAction Ignore                
            Remove-Item (Join-Path $StartPath "pac_$SolutionName") -Recurse -Force -ErrorAction Ignore
 
            Get-FlowsToBeDeployed "$StartPath" $SelectedSolution

            Get-ExportDataValid

        }
    
    }
    catch {
        Write-Host $_
        pause
    }
    finally {

    }
}

function Unpack-Solution {
    Param(
        [string] [Parameter(Mandatory = $true)] $cdsProjPathJoin,
        [string] [Parameter(Mandatory = $true)] $cdsProjFolderPathJoin
    )
    try {
        $pacexepath = "$env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe"
        $unpackfolderpath = (Join-Path $env:TEMP "ppdo")
        $cdsProjPath = (Join-Path $StartPath $cdsProjPathJoin)
        $cdsProjFolderPath = (Join-Path $StartPath $cdsProjFolderPathJoin)

        # If .cds project file exists (i.e., Clone performed already) trigger Sync
        if (Test-Path "$cdsProjPath") {
            Write-Host "Cloned solution available; Triggering Solution Sync"
            $cdsProjfolderPath = [System.IO.Path]::GetDirectoryName("$cdsProjPath")
            Write-Host "Pointing to cdsproj folder path - " $cdsProjfolderPath
            Set-Location -Path $cdsProjfolderPath
            $syncCommand = "solution sync --processCanvasApps true --packagetype Both --async"
            Write-Host "Triggering Sync - $syncCommand"
            & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe solution sync --processCanvasApps true --packagetype Both --async
            #Invoke-Expression -Command "$pacexepath $syncCommand"
        }
        else {
            # Trigger Clone
            $cloneCommand = "solution clone -n $SolutionName --processCanvasApps true --outputDirectory ""$unpackfolderpath"" --packagetype Both --async"
            Write-Host "Clone Command - $pacexepath $cloneCommand"
            & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe solution clone -n $SolutionName --processCanvasApps true --outputDirectory ""$unpackfolderpath"" --packagetype Both --async
            #Invoke-Expression -Command "$pacexepath $cloneCommand"
            Remove-Item $unpackfolderpath\$SolutionName\.gitignore -Force -ErrorAction SilentlyContinue
            Copy-Item "$unpackfolderpath\$SolutionName\." -Destination (Join-Path $StartPath "..\") -Recurse -Container -Force
            Remove-Item $unpackfolderpath -Force -Recurse -ErrorAction SilentlyContinue
        }
    }
    catch {
        Write-Host $_
        pause
    }
}

function Get-FlowsToBeDeployed {
    Param(
        [string] [Parameter(Mandatory = $true)] $StartPath,
        [string] [Parameter(Mandatory = $true)] $SelectedSolution
    )
    try {
        Write-Host "Generating Flows_Default.json to Support Flow Activation"
        $SolutionName = $SelectedSolution
        $SolutionPath = (Join-Path $StartPath "src\Workflows")
        $FlowJSON = @()

        $Workflows = Get-ChildItem -Path $SolutionPath -Filter *.json -ErrorAction SilentlyContinue
        if ($Workflows) {
            $Workflows | ForEach-Object {
                $FlowName = $_.BaseName.SubString(0, $_.BaseName.Length - 36)
                $FlowID = $_.BaseName.Replace($FlowName, '')
                $FlowJSON += @([ordered]@{FlowID = $FlowID; FlowName = $FlowName; ActivateAsUser = ""; })
            }
            ConvertTo-Json -Depth 3 $FlowJSON | Format-Json | Out-FileUtf8NoBom $StartPath\Flows_Default.json            
        }

    }
    catch {
        Write-Host $_
        pause
    }
}