Module/DevOps/Initialize-BCSDevOpsRepository.ps1

<#
.SYNOPSIS
  Initialize a BrightCom Repository
 
.DESCRIPTION
  Updates a BrightCom App Repository with default values
  You will need
 
.PARAMETER projectName
  Name of the project where the repository is.
  
.PARAMETER projectNameShort
  Short name for the project 3-4 characters.
  
.PARAMETER appIdRangeFrom
  From ID ranges for the app
  
.PARAMETER appIdRangeTo
  From ID ranges for the app
 
.PARAMETER ciTenants
  Tenant names where the app will be deployed, upgraded and installed when the CI pipeline runs. Divide multiple tenants with comma (e.g. Tenant1, Tenant2)
 
.PARAMETER cdTenants
  Tenant names where the app will be deployed, upgraded and installed when the Release pipeline runs. Divide multiple tenants with comma (e.g. Tenant1, Tenant2)
 
.PARAMETER templateRepoUrl
  Url to template repository if different from the default repository
 
.PARAMETER sourcePat
  Url to template repository if different from the default repository
 
.EXAMPLE
  Initialize-BCSDevOpsRepository -projectName "MyProjectName" -projectNameShort "MPN" -repositoryUrl $repoUrl -appIdRangeFrom "50000" -appIdRangeTo "50099" -ciTenants "MyQATenant" -cdTenants "MyProdTenant"
 
.NOTES
    Author: Mathias Stjernfelt
    Website: http://www.brightcom.se
#>


function Initialize-BCSDevOpsRepository {
  Param (
    [Parameter(Mandatory = $false)]
    [string]$organisation = 'BrightComSolutions',
    [Parameter(Mandatory = $true)]
    [string]$projectName,
    [Parameter(Mandatory = $true)]
    [string]$projectNameShort,
    [Parameter(Mandatory = $true)]
    [string]$appIdRangeFrom,
    [Parameter(Mandatory = $true)]
    [string]$appIdRangeTo,
    [Parameter(Mandatory = $true)]
    [string]$ciTenants,
    [Parameter(Mandatory = $true)]
    [string]$cdTenants,
    [Parameter(Mandatory = $false)]
    [string]$templateRepoUrl
  )

  try {
    #Check if powershell-yaml is installed
    if (Get-Module -ListAvailable -Name "powershell-yaml") {
      Write-Host "powershell-yaml is installed."
    } 
    else {
      Write-Host "powershell-yaml is not installed. Installing it from Powershell Gallery."
      Install-Module -Name powershell-yaml -force
    }

    if ([string]::IsNullOrEmpty($templateRepoUrl)) {
      $templateRepoUrl = "https://$($organisation)@dev.azure.com/$($organisation)/BrightCom%20Solutions/_git/BCS%20AL%20Project%20Template"
    }
    
    $guid = New-Guid
    $templatePath = "$env:TEMP\$guid"
    & git clone $templateRepoUrl $templatePath
    Remove-Item -Path "$templatePath\.git" -Recurse -Force

    $oldLocation = Get-Location
    if (Test-Path -Path $env:TEMP\$projectName) {
      Remove-Item -Path $env:TEMP\$projectName -Recurse -Force
    }

    $repoUrl = [uri]::EscapeUriString("https://$organisation@dev.azure.com/$organisation/$projectName/_git/$projectName")

    $tempProjectPath = "$env:TEMP\$projectName"

    Invoke-Utility "git" "clone" "$repoUrl" "$tempProjectPath"

    try {
      Copy-Item -Path "$templatePath\*" -Destination $tempProjectPath -Recurse -ErrorAction Stop
      Set-Location "$tempProjectPath" -ErrorAction Stop
    }
    catch {
      throw "Error changing location to $tempProjectPath\test\"
    }

    # Update app\app.json
    $fileName = "App\app.json"
    $filecontent = Get-Content -Path $fileName | ConvertFrom-Json

    $filecontent.id = [guid]::NewGuid();
    $filecontent.name = $projectName;
    $filecontent.idRanges[0].from = $appIdRangeFrom;
    $filecontent.idRanges[0].to = $appIdRangeTo;
    $filecontent | ConvertTo-Json | Format-Json -Indentation 2 | out-file $filename

    #Rename Workspace file
    Move-Item -Path "BCS AL Project Template.code-workspace" -Destination "$projectName.code-workspace"

    # Update test\app.json
    if (Test-Path -Path "Test\app.json") {
      $fileName = "Test\app.json"
      $filecontent = Get-Content -Path $fileName | ConvertFrom-Json

      $filecontent.id = [guid]::NewGuid();
      $filecontent.name = "$projectName - Test";
      $filecontent.idRanges[0].from = $testappIdRangeFrom;
      $filecontent.idRanges[0].to = $testAppIdRangeTo;

      $filecontent | ConvertTo-Json | Format-Json -Indentation 2 | out-file $filename
    }

    # pte-qa-settings.json
    $fileName = ".azureDevOps\pte-qa-settings.json"
    $filecontent = Get-Content -Path $fileName | ConvertFrom-Json
    $filecontent.name = "$projectNameShort";
    $filecontent.deployments[0].DeployToTenants = $ciTenants.Split(',');
    $filecontent | ConvertTo-Json -Depth 3 | Format-Json -Indentation 2 | out-file $filename

    # pte-prod-settings.json
    $fileName = ".azureDevOps\pte-prod-settings.json"
    $filecontent = Get-Content -Path $fileName | ConvertFrom-Json
    $filecontent.name = "$projectNameShort";
    $filecontent.deployments[0].DeployToTenants = $cdTenants.Split(',');
    $filecontent | ConvertTo-Json -Depth 3 | Format-Json -Indentation 2 | out-file $filename

    # pte-prod-release.yml
    $fileName = ".azureDevOps\pte-prod-release.yml";
    $filecontent = Get-Content -Path $fileName;
    $content = '';

    foreach ($line in $fileContent) {
      $content = $content + "`n" + $line;
    }

    $yaml = ConvertFrom-YAML $content -Ordered;
    $yaml.resources.pipelines[0].source = "$projectName - CI";
    $yaml | ConvertTo-Yaml | out-file $filename;

    #Update DevOps Repo with new setup
    Invoke-Utility git add -A
    Invoke-Utility git checkout -b main
    Invoke-Utility git commit -m 'Initial commit'
    Invoke-Utility git push -u origin main

    Set-Location $oldLocation

    # if (Test-Path -Path $env:TEMP\$projectName) {
    # Remove-Item -Path $env:TEMP\$projectName -Recurse -Force
    # }

    # if (Test-Path -Path $templatePath) {
    # Remove-Item -Path $templatePath -Recurse -Force
    # }
  }
  catch {
    if (Test-Path -Path $env:TEMP\$projectName) {
      Remove-Item -Path $env:TEMP\$projectName -Recurse -Force
    }

    if (Test-Path -Path $templatePath) {
      Remove-Item -Path $templatePath -Recurse -Force
    }

    throw "An error occured: $_.Exception";
  }
}

Export-ModuleMember -Function Initialize-BCSDevOpsRepository