modules/devops/Write-CiPipeline.ps1

param(
    [string]$ProjectName,
    [string]$OutputPath,
    [string]$CiProvider = "GitHub"
)

. (Join-Path (Split-Path $MyInvocation.MyCommand.Path) "..\core\Utils.ps1")

if ($CiProvider -eq "None") { return }

if ($CiProvider -eq "GitHub") {
    $CiDir = Join-Path $OutputPath ".github\workflows"
    if ($Global:DryRun) {
        Write-Host " [PLAN] Create GitHub Actions at $CiDir" -ForegroundColor DarkGray
    } else {
        New-Item -ItemType Directory -Path $CiDir -Force | Out-Null
    }

    # ci.yml
    $ciYml = @"
name: CI
 
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
 
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Setup .NET
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 8.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
    - name: Test
      run: dotnet test --no-build --verbosity normal
"@


    # docker.yml
    $dockerYml = @"
name: Docker Build Check
 
on:
  push:
    branches: [ main ]
 
jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Build the stack
      run: docker compose build
"@


    if ($Global:DryRun) {
        Write-Host " [PLAN] Write ci.yml and docker.yml" -ForegroundColor DarkGray
    } else {
        [System.IO.File]::WriteAllText((Join-Path $CiDir "ci.yml"), $ciYml)
        [System.IO.File]::WriteAllText((Join-Path $CiDir "docker.yml"), $dockerYml)
    }
}
elseif ($CiProvider -eq "GitLab") {
    $ciFile = Join-Path $OutputPath ".gitlab-ci.yml"
    $gitlabYml = @"
image: mcr.microsoft.com/dotnet/sdk:8.0
 
stages:
  - build
  - test
 
build:
  stage: build
  script:
    - "dotnet restore"
    - "dotnet build --no-restore"
 
test:
  stage: test
  script:
    - "dotnet test --no-build --verbosity normal"
"@


    if ($Global:DryRun) {
        Write-Host " [PLAN] Write .gitlab-ci.yml" -ForegroundColor DarkGray
    } else {
        [System.IO.File]::WriteAllText($ciFile, $gitlabYml)
    }
}