# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# Downloads an artifact with the specified nugetArtifactName,
# pushes any .nupkg files to the specified artifactFeedID,
# and pushes any debugging symbols (.pdb files) in the artifact
# (except for those named *.Tests.pdb) to TeamServices.

parameters:
  nugetArtifactName: 'nuget' # The name of the artifact where the NuGet assets (.nupkg and .snupkg files) can be downloaded
  debugArtifactName: 'debug' # The name of the artifact where the .pdb files can be downloaded
  artifactFeedID: '' # The GUID of the Azure Artifacts NuGet feed
  testSymbolFilesConvention: '**/*.Tests*.pdb' # The glob pattern (within the debugArtifactName) where to look for test project symbols (.pdb) files, so they can be distinguished from other project file types.

steps:
- pwsh: |
    function EnsureNotNullOrEmpty([string]$param, [string]$nameOfParam) {
        if ([string]::IsNullOrEmpty($param)) {
            Write-Host "##vso[task.logissue type=error;]Missing template parameter \"$nameOfParam\""
            Write-Host "##vso[task.complete result=Failed;]"
        }
    }
    EnsureNotNullOrEmpty('${{ parameters.nugetArtifactName }}', 'nugetArtifactName')
    EnsureNotNullOrEmpty('${{ parameters.debugArtifactName }}', 'debugArtifactName')
    EnsureNotNullOrEmpty('${{ parameters.artifactFeedID }}', 'artifactFeedID')
    EnsureNotNullOrEmpty('${{ parameters.testSymbolFilesConvention }}', 'testSymbolFilesConvention')
  displayName: 'Validate Template Parameters'

- task: DownloadPipelineArtifact@0
  displayName: 'Download Build Artifacts: ${{ parameters.nugetArtifactName }}'
  inputs:
    artifactName: '${{ parameters.nugetArtifactName }}'
    targetPath: '$(System.DefaultWorkingDirectory)/${{ parameters.nugetArtifactName }}'

- task: DownloadPipelineArtifact@0
  displayName: 'Download Build Artifacts: ${{ parameters.debugArtifactName }}'
  inputs:
    artifactName: '${{ parameters.debugArtifactName }}'
    targetPath: '$(System.DefaultWorkingDirectory)/${{ parameters.debugArtifactName }}'

- task: NuGetCommand@2
  displayName: 'NuGet push'
  inputs:
    command: push
    packagesToPush: '${{ parameters.nugetArtifactName }}/*.nupkg;!${{ parameters.nugetArtifactName }}/*.symbols.nupkg'
    publishVstsFeed: '/${{ parameters.artifactFeedID }}'
    allowPackageConflicts: true

- task: PublishSymbols@2
  displayName: 'Publish symbols path'
  inputs:
    SymbolsFolder: '$(System.DefaultWorkingDirectory)/${{ parameters.debugArtifactName }}'
    SearchPattern: |
      **/bin/**/*.pdb
      !${{ parameters.testSymbolFilesConvention }}
    IndexSources: true
    PublishSymbols: true
    SymbolServerType: TeamServices