# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- main

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: Build
  jobs:

  - job: SetupEnvironment
    displayName: "Setup Build Environment"
    steps:

    - task: PowerShell@2
      name: "diffStep"
      inputs:
        targetType: 'inline'
        script: |
          $diffResult = git diff --name-only HEAD~1 HEAD
          $folderFound = $false
          if ([string]::IsNullOrWhiteSpace($diffResult)) {
              Write-Host "No diff results found."
              Write-Host "##vso[task.setvariable variable=folderChanged;isOutput=true]$folderFound"
              exit 0  # Exit with success code
          }

          $diffResultEncoded = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($diffResult))
          Write-Host "##vso[task.setvariable variable=diffResultEncoded]$diffResultEncoded"

          $foldersToCheck = @('EnotaryoPH/EnotaryoPH.DbMigration/Scripts')
          foreach ($folder in $foldersToCheck) {
              if ($diffResult -match $folder) {
                  $folderFound = $true
                  Write-Host "Found changes in $folder"
                  break
              }
          }
          Write-Host "##vso[task.setvariable variable=folderChanged;isOutput=true]$folderFound"
      displayName: 'Check Git Diff for Specific Folders'

    - task: UseDotNet@2
      inputs:
        packageType: 'sdk'
        version: '9.0.x'

    - task: NodeTool@0
      inputs:
        versionSource: 'spec'
        versionSpec: '20.x'

  - job: LinuxBuild
    displayName: 'Build on Linux'
    dependsOn: SetupEnvironment
    variables:
      folderChanged: $[ dependencies.SetupEnvironment.outputs['diffStep.folderChanged'] ]
    pool:
      vmImage: 'ubuntu-latest'
    steps:

      - task: PowerShell@2
        inputs:
          filePath: 'make.ps1'
          arguments: '-OutputDir $(Build.ArtifactStagingDirectory)-linux'

      - task: ArchiveFiles@2
        inputs:
          rootFolderOrFile: '$(Build.ArtifactStagingDirectory)-linux/web'
          includeRootFolder: true
          archiveType: 'zip'
          archiveFile: '$(Build.ArtifactStagingDirectory)-linux/$(Build.BuildId)/web.zip'
          replaceExistingArchive: true

      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)-linux/$(Build.BuildId)/web.zip'
          ArtifactName: 'drop-linux-web'
          publishLocation: 'Container'

      - task: ArchiveFiles@2
        condition: eq(variables['folderChanged'], 'True')
        inputs:
          rootFolderOrFile: '$(Build.ArtifactStagingDirectory)-linux/migration'
          includeRootFolder: true
          archiveType: 'zip'
          archiveFile: '$(Build.ArtifactStagingDirectory)-linux/$(Build.BuildId)/migration.zip'
          replaceExistingArchive: true

      - task: PublishBuildArtifacts@1
        condition: eq(variables['folderChanged'], 'True')
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId)/migration.zip'
          ArtifactName: 'drop-linux-migration'
          publishLocation: 'Container'

  - job: WindowsBuild
    displayName: 'Build on Windows'
    dependsOn: SetupEnvironment
    variables:
      folderChanged: $[ dependencies.SetupEnvironment.outputs['diffStep.folderChanged'] ]
    pool:
      vmImage: 'windows-latest' # Specify the Windows agent
    steps:

      - task: PowerShell@2
        inputs:
          filePath: 'make.ps1'
          arguments: '-OutputDir $(Build.ArtifactStagingDirectory)-windows'

      - task: ArchiveFiles@2
        inputs:
          rootFolderOrFile: '$(Build.ArtifactStagingDirectory)-windows/web'
          includeRootFolder: true
          archiveType: 'zip'
          archiveFile: '$(Build.ArtifactStagingDirectory)-windows/$(Build.BuildId)/web.zip'
          replaceExistingArchive: true
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)-windows/$(Build.BuildId)/web.zip'
          ArtifactName: 'drop-windows-web'
          publishLocation: 'Container'

      - task: ArchiveFiles@2
        condition: eq(variables['folderChanged'], 'True')
        inputs:
          rootFolderOrFile: '$(Build.ArtifactStagingDirectory)-windows/migration'
          includeRootFolder: true
          archiveType: 'zip'
          archiveFile: '$(Build.ArtifactStagingDirectory)-windows/$(Build.BuildId)/migration.zip'
          replaceExistingArchive: true

      - task: PublishBuildArtifacts@1
        condition: eq(variables['folderChanged'], 'True')
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)-windows/$(Build.BuildId)/migration.zip'
          ArtifactName: 'drop-windows-migration'
          publishLocation: 'Container'




