parameters:
  - name: ServiceDirectory
    type: string
  - name: Artifacts
    type: object
    default: []
  - name: AdditionalModules
    type: object
    default: []
  - name: CheckoutRecordings
    type: boolean
    default: false
  - name: SDKType
    type: string
    default: client
  - name: OSVmImage
    type: string
    default: $(OSVmImage)

steps:
  # Skip sparse checkout for the `azure-sdk-for-<lang>-pr` private mirrored repositories
  # as we require the GitHub service connection to be loaded.
  - ${{ if not(contains(variables['Build.DefinitionName'], 'java-pr')) }}:
    - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml
      parameters:
        Paths:
          - '**/*.xml'
          - ${{ if not(parameters.CheckoutRecordings) }}:
            - '!sdk/**/test-recordings'
            - '!sdk/**/session-records'

  - task: UsePythonVersion@0
    displayName: 'Use Python $(PythonVersion)'
    inputs:
      versionSpec: $(PythonVersion)

  - template: /eng/common/pipelines/templates/steps/verify-agent-os.yml
    parameters:
      AgentImage: ${{ parameters.OSVmImage }}

  - task: PythonScript@0
    displayName: 'Set versions for source build'
    inputs:
      scriptPath: 'eng/versioning/set_versions.py'
      arguments: '--build-type client --pst'
    condition: and(succeeded(), eq(variables['TestFromSource'], 'true'))

  - task: PythonScript@0
    displayName: 'Update versions for source build'
    inputs:
      scriptPath: 'eng/versioning/update_versions.py'
      arguments: '--update-type library --build-type client --sr'
    condition: and(succeeded(), eq(variables['TestFromSource'], 'true'))

  - template: /eng/pipelines/templates/steps/generate-project-list-and-cache-maven-repository.yml
    parameters:
      Artifacts: ${{ parameters.Artifacts }}
      AdditionalModules: ${{ parameters.AdditionalModules }}
      JobType: 'test'

  # This call is used generate the sparse checkout variables for non-FromSource runs.
  - task: PowerShell@2
    displayName: Generate directories variable for sparse checkout
    inputs:
      pwsh: true
      filePath: $(Build.SourcesDirectory)/eng/scripts/Generate-ServiceDirectories-From-Project-List.ps1
      arguments: >
        -SourcesDirectory $(Build.SourcesDirectory)
        -ProjectList $(ProjectList)
    condition: and(succeeded(), ne(variables['TestFromSource'], 'true'))

  # The only time generate_from_source_pom.py should be used to set the SparseCheckoutDirectories
  # is for FromSource runs or, in the case of code quality reports, a run that needs to build
  # everything using the latest source. It'll greedily set any service directories as it figures
  # out what libraries, their dependents and so on, that need to be in ClientFromSourcePom.xml
  - task: PythonScript@0
    displayName: 'Generate FromSource POM and directories for sparse checkout'
    inputs:
      scriptPath: 'eng/scripts/generate_from_source_pom.py'
      arguments: '--project-list $(ProjectList)'
      workingDirectory: '$(System.DefaultWorkingDirectory)'
    condition: and(succeeded(), eq(variables['TestFromSource'], 'true'))

  # Skip sparse checkout for the `azure-sdk-for-<lang>-pr` private mirrored repositories
  # as we require the GitHub service connection to be loaded.
  - ${{ if not(contains(variables['Build.DefinitionName'], 'java-pr')) }}:
    - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml
      parameters:
        SkipCheckoutNone: true
        Paths: $(SparseCheckoutDirectories)
