parameters:
# assumption componentName and pipeline name the same
- name: componentName
  type: string
  default: ''
- name: pipelineId
  type: string
  default: ''
- name: branchName
  type: string
  default: '$(Build.SourceBranchName)' # for tagged builds
- name: fileFilter
  type: string
  default: ''
- name: extractAndDeleteFiles
  type: boolean
  default: true
# set to true if doing full build of ROCm stack
# and dependencies are pulled from same pipeline
- name: aggregatePipeline
  type: boolean
  default: false

steps:
- task: DownloadPipelineArtifact@2
  displayName: Download ${{ parameters.componentName }}
  retryCountOnTaskFailure: 3
  inputs:
    ${{ if eq(parameters.componentName, 'clr') }}:
      itemPattern: '**/*${{ parameters.componentName }}*${{ parameters.fileFilter }}*amd*' # filter out nvidia clr artifacts
    ${{ else }}:
      itemPattern: '**/*${{ parameters.componentName }}*${{ parameters.fileFilter }}*'
    targetPath: '$(Pipeline.Workspace)/d'
    allowPartiallySucceededBuilds: true
    ${{ if parameters.aggregatePipeline }}:
      buildType: 'current'
    ${{ else }}:
      buildType: 'specific'
      project: ROCm-CI
      specificBuildWithTriggering: true
      definition: ${{ parameters.pipelineId }}
      branchName: refs/heads/${{ parameters.branchName }}
      ${{ if eq(parameters.componentName, 'aomp') }}:
        buildVersionToDownload: latest # aomp trigger lives in ROCm/ROCm, so cannot use ROCm/aomp branch names
      ${{ else }}:
        buildVersionToDownload: latestFromBranch
- ${{ if eq(parameters.extractAndDeleteFiles, true) }}:
  - task: ExtractFiles@1
    displayName: Extract ${{ parameters.componentName }}
    inputs:
      archiveFilePatterns: '$(Pipeline.Workspace)/d/**/*.tar.gz'
      destinationFolder: '$(Agent.BuildDirectory)/rocm'
      cleanDestinationFolder: false
      overwriteExistingFiles: true
  - task: DeleteFiles@1
    displayName: Clean up Compressed ${{ parameters.componentName }}
    inputs:
      SourceFolder: '$(Pipeline.Workspace)/d'
      Contents: '**/*.tar.gz'
      RemoveDotFiles: true
