trigger:
- main

pool:
  name: 'SelfHost - Dedicated'

variables:
  SONARQUBE_HOST_URL: 'https://sonar.softplexity.com' # Replace with your SonarQube server URL
  SONARQUBE_SCANNER_MODE: 'CLI'
  SONARQUBE_PROJECT_KEY: 'SPX.Laravel_ear.ro_86247b4e-719f-4a54-a60a-3d65e8ec46ed'
  SONARQUBE_PROJECT_NAME: 'ear.ro'
  SONARQUBE_PROJECT_VERSION: '1.0'
  SONARQUBE_TOKEN: 'sqa_8143e47ad445416c8b5ea8bfef39252fbbeb37a0' 

jobs:
- job: Build
  displayName: 'Build and Analyze'
  pool:
    name: 'SelfHost - Dedicated'

  steps:
  - checkout: self
    fetchDepth: 0
  # - task: NodeTool@0
  #   inputs:
  #     versionSpec: '20.x'
  #   displayName: 'Install Node.js'

  # - script: |
  #     npm install -g npm@latest
  #     npm install
  #   displayName: 'Install NPM packages'
  - script: |
      sudo add-apt-repository ppa:ondrej/php -y
      sudo apt update
      sudo apt install php8.3 php8.3-cli php8.3-xml php8.3-mbstring php8.3-curl php8.3-zip php8.3-gd php8.3-sqlite3 -y
    displayName: 'Install PHP 8.3'

  # Install Composer
  - script: |
      EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
      php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
      ACTUAL_CHECKSUM="$(php -r 'echo hash_file("SHA384", "composer-setup.php");')"
      if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
        >&2 echo 'ERROR: Invalid installer checksum'
        rm composer-setup.php
        exit 1
      fi
      sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
      rm composer-setup.php
    displayName: 'Install Composer'

  - script: |
      composer install
    displayName: 'Install Composer dependencies'

  # - script: |
  #     php artisan test
  #   displayName: 'Run Laravel tests'
  
  - task: SonarQubePrepare@6
    inputs:
      SonarQube: "Sonar-Ear.ro"
      scannerMode: 'CLI'
      configMode: 'manual'
      cliProjectKey: $(SONARQUBE_PROJECT_KEY)

  - task: SonarQubeAnalyze@6
    inputs:
      jdkversion: 'JAVA_HOME_17_X64'

  - task: SonarQubePublish@6
    inputs:
      pollingTimeoutSec: '300'
