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

        ##Export Patches if they Exist
        foreach ($PatchSolution in $PatchQuery.CrmRecords) {
            $SolutionName = $PatchSolution.uniquename
            
            # Trigger Clone or Sync
            $pacexepath = "$env:APPDATA\Microsoft.PowerPlatform.DevOps\PACTools\tools\pac.exe"
            $unpackfolderpath = (Join-Path $env:TEMP "ppdo")
            $cdsProjPath = (Join-Path $StartPath "pac_$SolutionName\$SolutionName.cdsproj")
            $cdsProjFolderPath = (Join-Path $StartPath "pac_$SolutionName")
            # 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\Microsoft.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\Microsoft.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 "pac_$SolutionName") -Recurse -Container -Force
                Remove-Item $unpackfolderpath -Force -Recurse -ErrorAction SilentlyContinue
            }

            $gitStatus = iex "git status --porcelain"
            if ($gitStatus) {
                Set-DataverseSolutionVersion  

                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\Microsoft.PowerPlatform.DevOps\PACTools\tools\pac.exe solution sync --processCanvasApps true --packagetype Both --async
                #Invoke-Expression -Command "$pacexepath $syncCommand"
                
                # 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
            $pacexepath = "$env:APPDATA\Microsoft.PowerPlatform.DevOps\PACTools\tools\pac.exe"
            $unpackfolderpath = (Join-Path $env:TEMP "ppdo")
            $cdsProjPath = (Join-Path $StartPath "$SolutionName.cdsproj")
            $cdsProjFolderPath = (Join-Path $StartPath "")
            # 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\Microsoft.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\Microsoft.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
            }
         
            $gitStatus = iex "git status --porcelain"
            if ($gitStatus) {
                Set-DataverseSolutionVersion  
         
                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\Microsoft.PowerPlatform.DevOps\PACTools\tools\pac.exe solution sync --processCanvasApps true --packagetype Both --async
                #Invoke-Expression -Command "$pacexepath $syncCommand"

                # 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

                # git add -A
                # git commit -m "Updated unpack of solution $SolutionName"
                # $tagVersion = $global:devops_newVersion.Replace(".", "_")
                # git tag "$($SolutionName)_v_$tagVersion"
                # if ($global:devops_FullTool) {
                # git push origin "$($SolutionName)_v_$tagVersion"
                # }
            }
        
        }
    
    }
    catch {
        Write-Host $_
        pause
    }
    finally {

    }
}

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
    }
}