# This template contains a job which builds artifacts needed to release the app to the store and to
# Windows using Microsoft-internal systems. It relies on Microsoft-internal resources and will not
# work outside of Microsoft.
# Specifically, this job:
# - Signs the bundle using a secure system. If you want to build your own, use SignTool following
#   the example in the continuous integration pipeline.
# - Builds VPacks for including the app in the Windows OS build. Azure DevOps Universal Packages
#   offers similar capabilities.
# - Creates StoreBroker packages containing Microsoft Store assets. Although the Store assets for
#   this app are not open source, the StoreBroker tool is available at
#   https://github.com/Microsoft/StoreBroker.

jobs:
- job: WindowsInternalRelease
  dependsOn: Package
  pool:
    name: Package ES Lab E
  workspace:
    clean: outputs
  variables:
    skipComponentGovernanceDetection: true
    SBMediaRootPath: '$(TEMP)\SBMedia'
    SBPackagePath: '$(Build.ArtifactStagingDirectory)\storeBrokerPayload'
    SBLogPath: '$(SBPackagePath)\StoreBroker.log'
    FlightId: '161f0975-cb5f-475b-8ef6-26383c37621f'
    AppId: '9WZDNCRFHVN5'
    ProductId: '00009007199266248474'
  steps:
  - checkout: self
    clean: true

  # This must be the first task in the job definition, since it modifies the build environment
  # in ways other tasks would not expect (for example, it clears the artifacts directory).
  - task: PkgESSetupBuild@10
    displayName: Initialize Package ES
    inputs:
      productName: Calculator
      disableWorkspace: true
    env:
      XES_DISABLEPROV: true

  - task: NuGetToolInstaller@0
    displayName: Use NuGet 4.7.1
    inputs:
      versionSpec: 4.7.1
      checkLatest: true

  - task: DownloadBuildArtifacts@0
    displayName: Download appxBundle artifact
    inputs:
      artifactName: appxBundle

  - task: PkgESCodeSign@10
    displayName: Send bundle to Package ES code signing service
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    inputs:
      signConfigXml: build\config\SignConfig.xml
      inPathRoot: $(Build.ArtifactStagingDirectory)\appxBundle
      outPathRoot: $(Build.ArtifactStagingDirectory)\appxBundleSigned

  - task: PublishBuildArtifacts@1
    displayName: Publish AppxBundleSigned artifact
    inputs:
      pathtoPublish: $(Build.ArtifactStagingDirectory)\appxBundleSigned
      artifactName: AppxBundleSigned

  - task: CopyFiles@2
    displayName: Copy signed AppxBundle to vpack staging folder
    inputs:
      sourceFolder: $(Build.ArtifactStagingDirectory)\appxBundleSigned
      targetFolder: $(Build.ArtifactStagingDirectory)\vpack\appxBundle

  - task: PkgESVPack@10
    displayName: Create and push vpack for app
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    inputs:
      sourceDirectory: $(Build.ArtifactStagingDirectory)\vpack\appxBundle
      description: VPack for the Calculator Application
      pushPkgName: calculator.app
      version: $(versionMajor).$(versionMinor).$(versionBuild)
      owner: paxeeapps

  - task: PublishBuildArtifacts@1
    displayName: Publish vpack\app artifact with vpack manifest
    inputs:
      pathtoPublish: $(XES_VPACKMANIFESTDIRECTORY)\$(XES_VPACKMANIFESTNAME)
      artifactName: vpack\app

    # TODO (macool): create and push internal test packages and test config

  - task: UniversalPackages@0
    displayName: Download internals package
    inputs:
      command: download
      downloadDirectory: $(Build.SourcesDirectory)
      vstsFeed: WindowsApps
      vstsFeedPackage: calculator-internals
      vstsPackageVersion: 0.0.22

  - powershell: |
      # Just modify this line to indicate where your en-us PDP file is. Leave the other lines alone.
      $enUSPdpFilePath = "$(Build.SourcesDirectory)\PDP\en-US\PDP.xml"

      # This is going to save the release value from the PDP file to $(SBMediaReleaseVersion)
      # which you can then refer to in the UniversalPackages task.
      $release = ([xml](Get-Content $enUSPdpFilePath)).ProductDescription.Release.Trim()
      Write-Host "##vso[task.setvariable variable=SBMediaReleaseVersion;]$release"
    displayName: Determine the PDP Media release version from the en-us PDP file

  - task: UniversalPackages@0
    displayName: Download PDP media (screenshots, trailers) universal package
    inputs:
      command: download
      downloadDirectory: $(SBMediaRootPath)/$(SBMediaReleaseVersion)
      vstsFeed: WindowsInboxApps
      vstsFeedPackage: calculator-pdp-media
      vstsPackageVersion: $(SBMediaReleaseVersion)

  - task: MS-RDX-MRO.windows-store-publish-dev.package-task.store-package@2
    displayName: Create StoreBroker Payload
    inputs:
      serviceEndpoint: StoreBrokerProxy
      sbConfigPath: Tools/Build/StoreBroker/SBCalculatorConfig.json
      sourceFolder: $(Build.ArtifactStagingDirectory)/appxBundleSigned
      contents: Microsoft.WindowsCalculator_8wekyb3d8bbwe.appxbundle
      pdpPath: '$(Build.SourcesDirectory)\PDP'
      pdpInclude: PDP.xml
      pdpMediaPath: '$(SBMediaRootPath)'
      outSBPackagePath: '$(SBPackagePath)'
      outSBName: SBCalculator

  - task: PublishBuildArtifacts@1
    displayName: Publish StoreBroker Payload artifact
    inputs:
      pathtoPublish: '$(SBPackagePath)'
      artifactName: storeBrokerPayload

  - task: MS-RDX-MRO.windows-store-publish-dev.flight-task.store-flight@2
    displayName: 'Flight StoreBroker Payload to team ring'
    name: StoreBrokerFlight
    inputs:
      serviceEndpoint: StoreBrokerProxy
      appId: '$(AppId)'
      flightId: '$(FlightId)'
      inputMethod: JsonAndZip
      jsonPath: '$(SBPackagePath)\SBCalculator.json'
      zipPath: '$(SBPackagePath)\SBCalculator.zip'
      force: true
      skipPolling: true
      targetPublishMode: Immediate
      logPath: '$(SBLogPath)'

  - task: PkgESStoreBrokerAeroUpload@10
    displayName: Upload to Aero flighting dashboard
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
    inputs:
      ProductId: '$(ProductId)'
      FlightId: '$(FlightId)'
      SubmissionId: '$(StoreBrokerFlight.WS_SubmissionId)'
      SubmissionDataPath: '$(SBPackagePath)\SBCalculator.json'
      PackagePath: '$(SBPackagePath)\SBCalculator.zip'
      AeroEnvironment: Production

  - task: PkgESLateTasks@10
    displayName: Run PackageES LateTasks
    env:
      XES_DISABLEPROV: true
