parameters:
  extensionsToUnitTest: []

steps:
  - script: |
      mkdir -p .build
      echo -n $BUILD_SOURCEVERSION > .build/commit
      echo -n $VSCODE_QUALITY > .build/quality
    displayName: Prepare cache flag

  - task: 1ESLighthouseEng.PipelineArtifactCaching.RestoreCacheV1.RestoreCache@1
    inputs:
      keyfile: 'build/.cachesalt, .build/commit, .build/quality'
      targetfolder: '.build, out-build, out-vscode-min, out-vscode-reh-min, out-vscode-reh-web-min'
      vstsFeed: 'BuildCache'
      platformIndependent: true
      alias: 'Compilation'

  - script: |
      set -e
      exit 1
    displayName: Check RestoreCache
    condition: and(succeeded(), ne(variables['CacheRestored-Compilation'], 'true'))

  - task: NodeTool@0
    inputs:
      versionSpec: "12.13.0"

  - task: geeklearningio.gl-vsts-tasks-yarn.yarn-installer-task.YarnInstaller@3
    inputs:
      versionSpec: "1.x"

  - task: AzureKeyVault@1
    displayName: 'Azure Key Vault: Get Secrets'
    inputs:
      azureSubscription: 'ClientToolsInfra_670062 (88d5392f-a34f-4769-b405-f597fc533613)'
      KeyVaultName: ado-secrets
      SecretsFilter: 'github-distro-mixin-password'

  - script: |
      set -e
      cat << EOF > ~/.netrc
      machine github.com
      login azuredatastudio
      password $(github-distro-mixin-password)
      EOF

      git config user.email "andresse@microsoft.com"
      git config user.name "AzureDataStudio"
    displayName: Prepare tooling

  - script: |
      set -e
      git remote add distro "https://github.com/$(VSCODE_MIXIN_REPO).git"
      git fetch distro
      git merge $(node -p "require('./package.json').distro")
    displayName: Merge distro

  - task: 1ESLighthouseEng.PipelineArtifactCaching.RestoreCacheV1.RestoreCache@1
    inputs:
      keyfile: 'build/.cachesalt, .yarnrc, remote/.yarnrc, **/yarn.lock, !**/node_modules/**/yarn.lock, !**/.*/**/yarn.lock, !samples/**/yarn.lock'
      targetfolder: '**/node_modules, !**/node_modules/**/node_modules, !samples/**/node_modules'
      vstsFeed: 'BuildCache'

  - script: |
      set -e
      CHILD_CONCURRENCY=1 yarn --frozen-lockfile
    displayName: Install dependencies
    env:
      GITHUB_TOKEN: $(github-distro-mixin-password)
    condition: and(succeeded(), ne(variables['CacheRestored'], 'true'))

  - task: 1ESLighthouseEng.PipelineArtifactCaching.SaveCacheV1.SaveCache@1
    inputs:
      keyfile: 'build/.cachesalt, .yarnrc, remote/.yarnrc, **/yarn.lock, !**/node_modules/**/yarn.lock, !**/.*/**/yarn.lock, !samples/**/yarn.lock'
      targetfolder: '**/node_modules, !**/node_modules/**/node_modules, !samples/**/node_modules'
      vstsFeed: 'BuildCache'
    condition: and(succeeded(), ne(variables['CacheRestored'], 'true'))

  - script: |
      set -e
      yarn postinstall
    displayName: Run postinstall scripts
    condition: and(succeeded(), eq(variables['CacheRestored'], 'true'))

  - script: |
      set -e
      node build/azure-pipelines/mixin
    displayName: Mix in quality

  - script: |
      set -e
      yarn gulp vscode-linux-x64-min-ci
      # yarn gulp vscode-reh-linux-x64-min-ci
      # yarn gulp vscode-reh-web-linux-x64-min-ci
    displayName: Build
    env:
      VSCODE_MIXIN_PASSWORD: $(github-distro-mixin-password)

  - script: |
      set -e
      yarn gulp package-rebuild-extensions
      yarn gulp compile-extensions
      yarn gulp package-external-extensions
    displayName: Package External extensions

  - script: |
      set -e
      service xvfb start
    displayName: Start xvfb
    condition: and(succeeded(), eq(variables['RUN_TESTS'], 'true'))

  - script: |
      set -e
      DISPLAY=:10 ./scripts/test.sh --build --coverage --reporter mocha-junit-reporter --tfs "Unit Tests"
    displayName: Run unit tests (Electron)
    condition: and(succeeded(), eq(variables['RUN_TESTS'], 'true'))

  - script: |
      # Figure out the full absolute path of the product we just built
      # including the remote server and configure the integration tests
      # to run with these builds instead of running out of sources.
      set -e
      APP_ROOT=$(agent.builddirectory)/azuredatastudio-linux-x64
      APP_NAME=$(node -p "require(\"$APP_ROOT/resources/app/product.json\").applicationName")
      INTEGRATION_TEST_ELECTRON_PATH="$APP_ROOT/$APP_NAME" \
      VSCODE_REMOTE_SERVER_PATH="$(agent.builddirectory)/azuredatastudio-reh-linux-x64" \
      DISPLAY=:10 ./scripts/test-integration.sh --build --tfs "Integration Tests"
    displayName: Run integration tests (Electron)
    condition: and(succeeded(), eq(variables['RUN_TESTS'], 'true'))

  - ${{each extension in parameters.extensionsToUnitTest}}:
    - script: |
        set -e
        APP_ROOT=$(agent.builddirectory)/azuredatastudio-linux-x64
        APP_NAME=$(node -p "require(\"$APP_ROOT/resources/app/product.json\").applicationName")
        INTEGRATION_TEST_ELECTRON_PATH="$APP_ROOT/$APP_NAME" \
        DISPLAY=:10 node ./scripts/test-extensions-unit.js ${{ extension }}
      displayName: 'Run ${{ extension }} Stable Extension Unit Tests'
      condition: and(succeeded(), eq(variables['RUN_TESTS'], 'true'))

  - script: |
      set -e
      APP_ROOT=$(agent.builddirectory)/azuredatastudio-linux-x64
      APP_NAME=$(node -p "require(\"$APP_ROOT/resources/app/product.json\").applicationName")
      INTEGRATION_TEST_ELECTRON_PATH="$APP_ROOT/$APP_NAME" \
      DISPLAY=:10 ./scripts/test-extensions-unit-unstable.sh
    displayName: 'Run Unstable Extension Unit Tests'
    continueOnError: true
    condition: and(succeeded(), eq(variables['RUN_UNSTABLE_TESTS'], 'true'))

  - script: |
      set -e
      yarn gulp vscode-linux-x64-build-deb
    displayName: Build Deb

  - script: |
      set -e
      yarn gulp vscode-linux-x64-build-rpm
    displayName: Build Rpm

  - script: |
      set -e
      ./build/azure-pipelines/linux/createDrop.sh
    displayName: Create Drop

  - script: |
      set -e
      shopt -s globstar
      mkdir -p $(Build.ArtifactStagingDirectory)/test-results/coverage
      cp --parents -r $(Build.SourcesDirectory)/extensions/*/coverage/** $(Build.ArtifactStagingDirectory)/test-results/coverage
    displayName: Copy Coverage

  - task: PublishTestResults@2
    displayName: 'Publish Test Results test-results.xml'
    inputs:
      testResultsFiles: '*.xml'
      searchFolder: '$(Build.ArtifactStagingDirectory)/test-results'
    continueOnError: true
    condition: and(succeeded(), eq(variables['RUN_TESTS'], 'true'))

  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifact: drop'

  - task: ms.vss-governance-buildtask.governance-build-task-component-detection.ComponentGovernanceComponentDetection@0
    displayName: 'Component Detection'
    inputs:
      failOnAlert: true
