jobs:
- job: CreateMSIXBundle
  displayName: Create .msixbundle file
  pool:
    vmImage: windows-latest
  variables:
    - group: msixTools
    - group: 'Azure Blob variable group'

  steps:
    - template: release-SetReleaseTagAndContainerName.yml

    - task: DownloadPipelineArtifact@2
      inputs:
        source: specific
        project: PowerShellCore
        pipeline: '696'
        preferTriggeringPipeline: true
        runVersion: latestFromBranch
        runBranch: '$(Build.SourceBranch)'
        artifact: finalResults
        patterns: '**/*.msix'
        path: '$(Pipeline.Workspace)\releasePipeline\msix'

    - pwsh: |
        $cmd = Get-Command makeappx.exe -ErrorAction Ignore
        if ($cmd) {
            Write-Verbose -Verbose 'makeappx available in PATH'
            $exePath = $cmd.Source
        } else {
            $toolsDir = '$(Pipeline.Workspace)\releasePipeline\tools'
            New-Item $toolsDir -Type Directory -Force > $null
            Invoke-RestMethod -Uri '$(makeappUrl)' -OutFile "$toolsDir\makeappx.zip"
            Expand-Archive "$toolsDir\makeappx.zip" -DestinationPath "$toolsDir\makeappx" -Force
            $exePath = "$toolsDir\makeappx\makeappx.exe"

            Write-Verbose -Verbose 'makeappx was installed:'
            Get-ChildItem -Path $toolsDir -Recurse
        }

        $vstsCommandString = "vso[task.setvariable variable=MakeAppxPath]$exePath"
        Write-Host "sending " + $vstsCommandString
        Write-Host "##$vstsCommandString"
      displayName: Install makeappx tool

    - pwsh: |
        $sourceDir = '$(Pipeline.Workspace)\releasePipeline\msix'
        $file = Get-ChildItem $sourceDir | Select-Object -First 1
        $prefix = ($file.BaseName -split "-win")[0]
        $pkgName = "$prefix.msixbundle"
        Write-Verbose -Verbose "Creating $pkgName"

        $makeappx = '$(MakeAppxPath)'
        $outputDir = "$sourceDir\output"
        New-Item $outputDir -Type Directory -Force > $null
        & $makeappx bundle /d $sourceDir /p "$outputDir\$pkgName"

        Get-ChildItem -Path $sourceDir -Recurse
        $vstsCommandString = "vso[task.setvariable variable=BundleDir]$outputDir"
        Write-Host "sending " + $vstsCommandString
        Write-Host "##$vstsCommandString"
      displayName: Create MsixBundle

    - pwsh: |
        ## We use AzCopy v8.1 in our release pipeline, see the documentation at:
        ## https://docs.microsoft.com/en-us/previous-versions/azure/storage/storage-use-azcopy

        $azcopy = "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy\AzCopy.exe"
        & $azcopy /Source:$(BundleDir) /Dest:https://$(StorageAccount).blob.core.windows.net/$(AzureVersion)-private /DestKey:$(StorageAccountKey) /Pattern:*.msixbundle /Y
      displayName: Upload MSIX Bundle package to Az Blob
