RDG.Deployment.Utils.psm1

function Show-InfoBox {
  [CmdletBinding()]
  param (
    [Parameter(Mandatory=$true)]
    [string]$Title,
    [Parameter(Mandatory=$true)]
    [string[]]$Content
  )

  # Calculate the maximum line length in the content and title
  $maxTitleLength = $Title.Length
  $maxContentLength = ($Content | Measure-Object -Property Length -Maximum).Maximum

  # Determine the maximum length between title and content
  $maxLineLength = [math]::Max($maxTitleLength, $maxContentLength)

  # Calculate padding for the title and content rows
  $padding = " " * 2

  # Display the title aligned to the left within the box with padding
  $titleLineWithPadding = $padding + $Title.PadRight($maxLineLength) + $padding

  Write-Host ("╔" + "═" * ($maxLineLength + 4) + "╗")
  Write-Host ("║$titleLineWithPadding║")
  Write-Host ("╟" + "─" * ($maxLineLength + 4) + "╢")

  # Display the content box with consistent padding for each row
  foreach ($line in $Content) {
      $lineWithPadding = $padding + $line.PadRight($maxLineLength) + $padding
      Write-Host ("║$lineWithPadding║")
  }

  # Display the closing line of the box
  Write-Host ("╚" + "═" * ($maxLineLength + 4) + "╝")
}


function Get-Scripts {
  param(
  [bool]$delta,
  [string]$manifestPath
  )

  $manifest = Get-Content -Raw -Path $manifestPath | ConvertFrom-Json

  if ($delta) {
    $changedScripts = Get-Changed
    $scripts = $manifest.deploy.scripts | Where-Object { $_ -in $changedScripts }
  }
  else {
    $scripts = $manifest.deploy.scripts
  }

  return $scripts
}

function Get-Changed {
  param(
    [string]$type = 'deploy'
  )
  # Get the root folder of your Git repository
  $repoRoot = git rev-parse --show-toplevel

  # Get the commit hashes for the current and previous commits
  $currentCommit = git rev-parse HEAD
  $previousCommit = git rev-parse HEAD^

  # Get the list of modified files between the current and previous commits
  $changedFiles = git diff --name-only $previousCommit $currentCommit

  # Get the distinct folder paths from the changed files
  $changedFolders = $changedFiles | ForEach-Object { Split-Path -Parent $_ } | Get-Unique

  # Filter the changed folders to include only the ones containing deploy.ps1
  $folder = $changedFolders | Where-Object { Test-Path (Join-Path -Path $repoRoot -ChildPath ($_ + "\$type.ps1")) }

  # Get the relative paths of deploy.ps1 scripts within the deploy folders and their subfolders
  $changedScripts = $folder | Get-ChildItem -Recurse -Filter "$type.ps1" -File | ForEach-Object {
    $_.FullName.Substring($repoRoot.Length + 1)
  }

  return $changedScripts
}

function Get-LocationShort {
  param(
    [string]$location
  )

  $locations = @{
    westeurope = 'weu'
  }

  if (!$locations.ContainsKey($location)) {
    Write-Host "Location '$location' does not have a shortname available"
    exit 1
  }

  return $locations[$location];
}

function Show-Title {
  param(
    [string]$title = "",
    [string]$color = "Cyan",
    [string]$edge = "="
  )

  $line = $edge * 75
  Write-Host $line -ForegroundColor $color
  Write-Host " $title" -ForegroundColor $color
  Write-Host $line -ForegroundColor $color
}

function Block-Start {
  param(
    [string]$title = "",
    [string]$color = "Yellow",
    [string]$edge = "="
  )

  $separator = $edge * 75
  Write-Host $separator
  Write-Host " $title" -ForegroundColor $color
  Write-Host ""
}

function Block-End {
  param(
    [string]$title = "",
    [string]$color = "Green",
    [string]$edge = "="
  )

  $separator = $edge * 75
  Write-Host ""
  Write-Host " $title" -ForegroundColor $color
  Write-Host $separator
}

function Invoke-Script {
  param(
    [string]$scriptPath,
    [string]$location,
    [string]$stack,
    [string]$retailer,
    [string]$subscriptionId
  )

  Show-Title -title "🏃 Running $scriptPath ($retailer, $stack, $location)" -color "Yellow"

  try {
    & $scriptPath -location $location -stack $stack -retailer $retailer -subscriptionId $subscriptionId
    Show-Title -title "✅ Success $scriptPath" -edge "-" -color "Green"
  }
  catch {
    Show-Title -title "⛔️ Failed $scriptPath with error: $_" -color Red -edge "-"
    throw $_
  }
}