parameters:
  - name: VSCODE_CLI_ARTIFACTS
    type: object
    default: []

steps:
  - task: UseDotNet@2
    inputs:
      version: 2.x

  - task: EsrpClientTool@1
    displayName: Download ESRPClient

  - ${{ each target in parameters.VSCODE_CLI_ARTIFACTS }}:
    - task: DownloadPipelineArtifact@2
      displayName: Download ${{ target }}
      inputs:
        artifact: ${{ target }}
        path: $(Build.ArtifactStagingDirectory)/pkg/${{ target }}

  - script: |
      set -e
      node build/azure-pipelines/common/sign "$(esrpclient.toolpath)/$(esrpclient.toolname)" darwin-sign $(ESRP-PKI) $(esrp-aad-username) $(esrp-aad-password) $(Build.ArtifactStagingDirectory)/pkg "*.zip"
    displayName: Codesign

  - script: |
      set -e
      node build/azure-pipelines/common/sign "$(esrpclient.toolpath)/$(esrpclient.toolname)" darwin-notarize $(ESRP-PKI) $(esrp-aad-username) $(esrp-aad-password) $(Build.ArtifactStagingDirectory)/pkg "*.zip"
    displayName: Notarize

  - ${{ each target in parameters.VSCODE_CLI_ARTIFACTS }}:
    - script: |
        set -e
        ASSET_ID=$(echo "${{ target }}" | sed "s/unsigned_//")
        mv $(Build.ArtifactStagingDirectory)/pkg/${{ target }}/${{ target }}.zip $(Build.ArtifactStagingDirectory)/pkg/${{ target }}/$ASSET_ID.zip
        echo "##vso[task.setvariable variable=ASSET_ID]$ASSET_ID"
      displayName: Set asset id variable

    - publish: $(Build.ArtifactStagingDirectory)/pkg/${{ target }}/$(ASSET_ID).zip
      artifact: $(ASSET_ID)
