Private/_Menus.ps1


# _Menus.ps1

function Invoke-Menu() {    
    Param(
        [Parameter(Mandatory = $True)][String]$MenuTitle,
        [Parameter(Mandatory = $True)][array]$MenuOptions
    )
    if ($MenuOptions.Count -gt 0) {
        $MaxValue = $MenuOptions.Count - 1 
    }
    else {
        $MaxValue = 0
    }    
    $Selection = 0
    $EnterPressed = $False
    $Filter = ""
    $MasterMenuList = $MenuOptions
    
    if (!$VerbosePreference -eq "Continue") {
        Clear-Host    
    }

    While ($EnterPressed -eq $False) { 
        Write-Host "$MenuTitle"
        if ($Filter.Length -gt 0) {
            $MenuOptions = $MasterMenuList | Where-Object { $_ -match $Filter }
        }
        else {
            $MenuOptions = $MasterMenuList
        }
        For ($i = 0; $i -le $MaxValue; $i++) {            
            If ($i -eq $Selection) {
                Write-Host -BackgroundColor Cyan -ForegroundColor Black "[ $($MenuOptions[$i]) ]"
            }
            Else {
                Write-Host " $($MenuOptions[$i]) "
            }
        }
        $KeyInput = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown").virtualkeycode
        Switch ($KeyInput) {
            13 {
                $EnterPressed = $True
                $Selection = $MasterMenuList.IndexOf($MenuOptions[$selection])
                Return $Selection
                if (!$VerbosePreference -eq "Continue") {
                    Clear-Host    
                }
                break
            }
            38 {
                If ($Selection -eq 0) {
                    $Selection = $MaxValue
                }
                Else {
                    $Selection -= 1
                }
                if (!$VerbosePreference -eq "Continue") {
                    Clear-Host    
                }
                break
            }
            40 {
                If ($Selection -eq $MaxValue) {
                    $Selection = 0
                }
                Else {
                    $Selection += 1
                }
                if (!$VerbosePreference -eq "Continue") {
                    Clear-Host    
                }
                break
            }
            8 {
                if ($Filter.Length -gt 0) {
                    $Filter = $Filter.Substring(0, $Filter.Length - 1)
                    if (!$VerbosePreference -eq "Continue") {
                        Clear-Host    
                    }    
                }   
                else {
                    $Filter = ""
                    if (!$VerbosePreference -eq "Continue") {
                        Clear-Host    
                    }
                }             
            }
            Default {
                $Filter += [char]$KeyInput
                if (!$VerbosePreference -eq "Continue") {
                    Clear-Host    
                }
            }
        }
    }
}

function Get-MainMenu {
    param (
      
    )
    Set-ToolVariables
    Set-ProjectVariables
    $sourceControlConfigMessage = "(Org : $($global:devops_projectFile.OrgName) | Project : $($global:devops_projectFile.Project) | git Repo : $($global:devops_projectFile.gitRepo))"
    $CICDConfigMessage = "(CI/CD Environment : $($global:devops_projectFile.CICDEnvironmentName) | CI/CD URL : $($global:devops_projectFile.CICDEnvironmentURL))"
    if (!$global:devops_projectFile) {
        $Title = "Power Platform DevOps (No Project Selected)"
    }
    else {
        $Title = "Power Platform DevOps ( $($global:devops_projectFile.OrgName) | $($global:devops_projectFile.gitRepo) )"
        $global:devops_projectTitle = $global:devops_projectFile.gitRepo
        Set-Location $global:devops_projectLocation
    }
    $global:devops_message = @"
 
    Welcome to the Power Platform DevOps script.
     
     ver: $global:devops_version (latest available version : $latestVersion)
     branch: $global:devops_Branch
     basebranch: $global:devops_BaseBranch
     isdocker : $global:devops_isDocker
     product : $global:devops_DevOpsProduct
      
     org: $global:devops_OrgName
     project: $global:devops_ProjectName
     repo : $global:devops_gitRepo
     projectlocation: $global:devops_projectLocation
      
     azure subscription : $global:devops_selectedSubscriptionName ($global:devops_selectedSubscription)
     azure keyvault : $global:devops_AzureKeyVault
     tenant id : $global:devops_TenantID
     client id : $global:devops_ClientID
     
"@

    [console]::ForegroundColor = "White"
    if (!$VerbosePreference -eq "Continue") {
        Clear-Host    
    }
    Write-Host $global:devops_logo -ForegroundColor Magenta
    Write-Host $global:devops_message -ForegroundColor White
    $Repeater = "=" * $Title.Length
    Write-Host "================ $Title ================" -ForegroundColor White
    
    Write-Host "1: Run Pre-requisite checks (Install / Update)." -ForegroundColor (Set-ColourOption $global:devops_configFile.PreReqsComplete)
    if (($global:devops_configFile.PreReqsComplete -eq "True")) {
        if (!$global:devops_projectFile.ProjectAdded) {
            $project = $false
            Write-Host "2: Create a New Project or Select an Existing One" -ForegroundColor (Set-ColourOption ($project))    
        }
        else {
            $project = $global:devops_projectFile.ProjectAdded            
            Write-Host "2: Create a New Project or Select an Existing One" -ForegroundColor (Set-ColourOption ($project))
            Write-Host "3: Configure Source Control (Azure DevOps / Github)" $sourceControlConfigMessage -ForegroundColor (Set-ColourOption $global:devops_projectFile.SourceControlConfigured)
            if ($global:devops_projectFile.SourceControlConfigured -eq "True") {
                if ($global:devops_projectFile.ClientId -eq "") {
                    Write-Host "3a: Configure Service Principal (Recommended)" -ForegroundColor Yellow
                }
                Write-Host "4: Configure Continuous Deployment" $CICDConfigMessage -ForegroundColor (Set-ColourOption $global:devops_projectFile.CICDEnvironmentName)   
                Write-Host "B: Git [B]ranch Management" -ForegroundColor Cyan
                Write-Host "S: Commit and [S]ync changes to Source Control" -ForegroundColor Cyan
                Write-Host "V: [V]iew current change log for Source Control" -ForegroundColor Cyan     
            }
            if (!$global:devops_isDocker -or ($host.Name -match 'Visual Studio Code Host')) {
                Write-Host "O: [O]pen Project in Visual Studio" -ForegroundColor Cyan   
                Write-Host "D: Open Project in Visual Studio Co[D]e" -ForegroundColor Cyan        
            }              
            Write-Host "A: [A]dd Dataverse Solutions" -ForegroundColor Cyan  
            if ($global:devops_DataverseSolutionsAdded) {
                Write-Host "M: [M]anage Dataverse Solutions (Export, Unpack, Config Data)" -ForegroundColor Cyan          
            }
            if ($global:devops_projectFile.CICDEnvironmentName -ne "False") {
                Write-Host "T: Add Additional Deployment Environment [T]arget" -ForegroundColor Cyan
            }
        }
        
    }
        
    Write-Host "C: Advanced [C]onfiguration Management" -ForegroundColor Yellow
    if ($global:devops_projectFile.CICDEnvironmentName) {
        Write-Host "R: [R]un Pipeline Deployment" -ForegroundColor Yellow
    }
    Write-Host "U: Check for [U]pdates to Capgemini.PowerPlatform.DevOps" -ForegroundColor Yellow
    Write-Host "Q: Press 'Q' to quit." -ForegroundColor White
    Write-Host "=================$Repeater=================" -ForegroundColor White
    Write-Host ""
    Write-Host "* (White items still need to be completed)" -ForegroundColor White
    Write-Host "* (Green items are successful)" -ForegroundColor Green
    Write-Host "* (Red items have Errors)" -ForegroundColor Red
    Write-Host "* (Purple items are optional)" -ForegroundColor Magenta
    Write-Host "* (Blue Items are used for Day-to-Day ALM)" -ForegroundColor Cyan
    Write-Host ""
    if ($latestVersion -gt $global:devops_version) {
        Write-Host "There is a newer version available, please run Update-Module Capgemini.PowerPlatform.DevOps to update" -ForegroundColor Yellow
        Write-Host ""
    }
}

function Get-ConfigMenu {
    param (
        [string]$Title = 'Power Platform DevOps'
    )
    $devopsConfigMessage = "(ADO Org : $($global:devops_projectFile.OrgName) | ADO Project : $($global:devops_projectFile.Project) | git Repo : $($global:devops_projectFile.gitRepo))"
    $CICDConfigMessage = "(CI/CD Environment : $($global:devops_projectFile.CICDEnvironmentName) | CI/CD URL : $($global:devops_projectFile.CICDEnvironmentURL))"
    if (!$global:devops_projectFile) {
        $Title = "Power Platform DevOps (No Project Selected)"
    }
    else {
        $Title = "Power Platform DevOps ($($global:devops_projectFile.gitRepo))"
        $global:devops_projectTitle = $global:devops_projectFile.gitRepo
        Set-Location $global:devops_projectLocation
    }
    $global:devops_message = @"
 
    Power Platform DevOps Configuration.
     
     ver: $global:devops_version (latest available version : $latestVersion)
     isdocker: $global:devops_isDocker
     product : $global:devops_DevOpsProduct
     project: $global:devops_projectTitle
     projectlocation: $global:devops_projectLocation
     repo : $global:devops_gitRepo
     azure subscription : $global:devops_selectedSubscriptionName ($global:devops_selectedSubscription)
     azure keyvault : $global:devops_AzureKeyVault
     tenant id : $global:devops_TenantID
     client id : $global:devops_ClientID
     
"@

    [console]::ForegroundColor = "White"
    if (!$VerbosePreference -eq "Continue") {
        Clear-Host    
    }
    Write-Host $global:devops_logo -ForegroundColor Magenta
    Write-Host $global:devops_message -ForegroundColor White
    $Repeater = "=" * $Title.Length
    Write-Host "================ $Title ================" -ForegroundColor White
    
    Write-Host "G: Edit [G]lobal Config File" -ForegroundColor Yellow
    if ($global:devops_projectFile) {

        Write-Host "P: Edit [P]roject Config File" -ForegroundColor Yellow
        Write-Host "A: Setup [A]zure KeyVault" -ForegroundColor Cyan
        Write-Host "C: [C]onfigure Service Principal" -ForegroundColor Cyan
        Write-Host "L: Change Dataverse [L]ogin Account" -ForegroundColor Cyan
        Write-Host "R: [R]efresh Git Access Token" -ForegroundColor Yellow
        Write-Host "D: [D]elete Git Auth Config Header (Advanced)" -ForegroundColor Yellow        
    }    
    Write-Host "U: [U]pdate Tooling to latest versions" -ForegroundColor Yellow
    Write-Host "Z: Clear A[Z]ure Account Cache (Advanced)" -ForegroundColor Yellow
    Write-Host "Q: Press 'Q' to return to Main Menu." -ForegroundColor White
    Write-Host "=================$Repeater=================" -ForegroundColor White
    Write-Host ""
}

function Get-SolutionMenu {
    param (
        [string]$SolutionName = ''
    )
    [string]$Title = "Dataverse Solution Management ($SolutionName)"
    [console]::ForegroundColor = "White"
    if (!$VerbosePreference -eq "Continue") {
        Clear-Host    
    }
    Write-Host $global:devops_logo -ForegroundColor Magenta
    $Repeater = "=" * $Title.Length
    Write-Host "================ $Title ================" -ForegroundColor White
    
    Write-Host "E: [E]xport & Unpack Solution" -ForegroundColor Cyan
    Write-Host "C: [C]onfiguration Data Create/Edit" -ForegroundColor Yellow
    Write-Host "D: Export Configuration [D]ata" -ForegroundColor Cyan
    Write-Host "G: [G]enerate Types" -ForegroundColor Cyan
    Write-Host "P: Deploy [P]lugins" -ForegroundColor Cyan
    Write-Host "W: Deploy [W]eb Resources" -ForegroundColor Cyan
    Write-Host "Q: Press 'Q' to return to Main Menu." -ForegroundColor White
    Write-Host "=================$Repeater=================" -ForegroundColor White
    Write-Host ""
}

function Get-BranchMenu {
    [string]$Title = "Git Branch Management"
    [console]::ForegroundColor = "White"
    if (!$VerbosePreference -eq "Continue") {
        Clear-Host    
    }
    Write-Host $global:devops_logo -ForegroundColor Cyan
    $Repeater = "=" * $Title.Length
    Write-Host "================ $Title ================" -ForegroundColor White
    
    Write-Host "S: [S]elect Branch" -ForegroundColor Cyan
    Write-Host "C: [C]reate New Branch" -ForegroundColor Yellow
    Write-Host "U: [U]pdate from another branch" -ForegroundColor Yellow
    Write-Host "P: [P]ush branch upstream" -ForegroundColor Yellow
    Write-Host "L: Pull [L]atest changes from remote" -ForegroundColor Yellow
    Write-Host "Q: Press 'Q' to return to Main Menu." -ForegroundColor White
    Write-Host "=================$Repeater=================" -ForegroundColor White
    Write-Host ""
}

function Get-ToolingMenu {
    [string]$Title = "Tooling Management"
    [console]::ForegroundColor = "White"
    if (!$VerbosePreference -eq "Continue") {
        Clear-Host    
    }
    Write-Host $global:devops_logo -ForegroundColor Magenta
    $Repeater = "=" * $Title.Length
    Write-Host "================ $Title ================" -ForegroundColor White
    
    Write-Host "P: [P]ower Apps CLI" -ForegroundColor Yellow
    Write-Host "X: [X]rmContext" -ForegroundColor Yellow
    Write-Host "D: Xrm[D]efinitelyTyped" -ForegroundColor Yellow
    Write-Host "C: [C]onfiguration Migration Utility" -ForegroundColor Yellow
    Write-Host "Q: Press 'Q' to return to Main Menu." -ForegroundColor White
    Write-Host "=================$Repeater=================" -ForegroundColor White
    Write-Host ""
}

function Show-SolutionMenu {
    param (
        [string]$SolutionName = ''
    )
    Get-SolutionMenu -SolutionName $SolutionName
    $solutionSelection = Read-Host "Selection"
    try {
        switch ($solutionSelection) {
            'E' {
                try {              
                    Invoke-ExportSolution -StartPath $global:devops_projectLocation -SelectedSolution $selectedSolution
                }
                catch {
                    Write-Error $_
                    pause
                }  
            } 'C' {
                try {
                    Install-ConfigMigration
                    Write-Host "Launching Configuration Migration Utility..."
                    Write-Host "Please make sure to save the Schema file in $global:devops_projectLocation\$SolutionName\ReferenceData\schema.xml" -ForegroundColor Yellow
                    Write-Host "This path has been placed in your Clipboard to Paste in the Save File Dialog"
                    Set-Clipboard -Value "$global:devops_projectLocation\$SolutionName\ReferenceData\schema.xml"
                    . $env:APPDATA\Capgemini.PowerPlatform.DevOps\ConfigMigration\DataMigrationUtility.exe
                    pause
                }
                catch {
                    Write-Error $_ 
                    pause                                                
                }                    
                
            } 'D' {
                try {
                    Get-ExportDataValid
                }
                catch {
                    Write-Error $_ 
                    pause  
                }
                
            } 'G' {
                Get-EarlyTypes -StartPath $global:devops_projectLocation -SelectedSolution $selectedSolution
            } 'P' {
                try {
                    $CurrentLocation = Get-Location
                    Set-Location $global:devops_projectLocation\$selectedSolution\spkl\
                    . .\deploy-plugins.bat  
                    pause
 
                    Set-Location $CurrentLocation
                }
                catch {
                    Write-Host $_
                    pause
                }
                
            } 'W' {
                try {
                    $CurrentLocation = Get-Location
                    Set-Location $global:devops_projectLocation\$selectedSolution\spkl\
                    . .\deploy-webresources.bat    
                    Set-Location $CurrentLocation
                    pause
                }
                catch {
                    Write-Host $_
                    pause
                }
            }'Q' {
            }
        }            
    }
    catch {
        Write-Host $_
        pause
    }
}