trigger: none
pr: none

pool:
  vmImage: 'windows-latest'

parameters:
  - name: releaseBranch
    displayName: Release Branch
    type: string
    default: 'stable'
    values:
    - 'stable'
    - 'preview'
  - name: packageIdentityName
    displayName: Package Identity Name
    type: string
    default: 'Files'
    values:
    - 'Files'
    - 'FilesPreview'
  - name: packageDisplayName
    displayName: Package Display Name
    type: string
    default: 'Files'
    values:
    - 'Files'
    - 'Files - Preview'
  - name: iconVariant
    displayName: Icon Variant
    type: string
    default: 'Release'
    values:
    - 'Release'
    - 'Preview'

variables:
- group: BuildPipelineVariables
- name: solution
  value: '**/*.sln'
- name: buildPlatform
  value: 'x64|ARM64'
- name: storeBuildConfiguration
  value: 'Store'
- name: stableBuildConfiguration
  value: 'Stable'
- name: appxPackageDir
  value: '$(build.artifactStagingDirectory)\AppxPackages\\'

jobs:
### Store release ###
- job: StoreRelease
  timeoutInMinutes: 120

  steps:
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: |
        [xml]$xmlDoc = Get-Content '$(Build.SourcesDirectory)\src\Files.App (Package)\Package.appxmanifest'
        $xmlDoc.Package.Identity.Name="49306atecsolution.FilesUWP"
        $xmlDoc.Package.Identity.Publisher="CN=53EC4384-7F5B-4CF6-8C23-513FFE9D1AB7"
        $xmlDoc.Package.Properties.DisplayName="Files App"
        $xmlDoc.Package.Applications.Application.VisualElements.DisplayName="Files"
        $xmlDoc.Package.Applications.Application.VisualElements.DefaultTile.ShortName="Files"
        # Removes packageManagement from Store release
        $nsmgr = New-Object System.Xml.XmlNamespaceManager($xmlDoc.NameTable)
        $nsmgr.AddNamespace("pkg", "http://schemas.microsoft.com/appx/manifest/foundation/windows10")
        $nsmgr.AddNamespace("rescap", "http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities")
        $pm = $xmlDoc.SelectSingleNode("/pkg:Package/pkg:Capabilities/rescap:Capability[@Name='packageManagement']", $nsmgr)
        $xmlDoc.Package.Capabilities.RemoveChild($pm)
        $xmlDoc.Save('$(Build.SourcesDirectory)\src\Files.App (Package)\Package.appxmanifest')
      failOnStderr: true

 # This replaces references to the dev icon with the specified icon variant 
  - task: PowerShell@2
    displayName: 'Use Correct Logo'
    inputs:
      targetType: 'inline'
      script: |
        gci $(Build.SourcesDirectory)\src -Include *.csproj, *.appxmanifest, *.wapproj, *.xaml -recurse | ForEach -Process {
        (Get-Content $_ -Raw | ForEach -Process {$_ -replace "Assets\\AppTiles\\Dev", "Assets\AppTiles\Release"}) | Set-Content $_ -NoNewline
        }
      failOnStderr: true

# Adds the Bing maps token to the project
  - task: DownloadSecureFile@1
    name: mapsDevKey
    displayName: 'Download Bing Maps Dev Key'
    inputs:
      secureFile: 'BingMapsKey.txt'

  - task: CopyFiles@2
    inputs:
      SourceFolder: '$(Agent.TempDirectory)'
      Contents: '$(mapsDevKey.secureFilePath)'
      TargetFolder: '$(Build.SourcesDirectory)\src\Files.App\Resources'
      overWrite: true

# Injects the Sentry token to the project
  - task: PowerShell@2
    displayName: 'Inject Sentry token'
    inputs:
      targetType: 'inline'
      script: |
        gci $(Build.SourcesDirectory)\src -Include *.cs -recurse | ForEach -Process {
        (Get-Content $_ -Raw | ForEach -Process {$_ -replace "sentry.secret", "$(sentry.secret)"}) | Set-Content $_ -NoNewline
        }
      failOnStderr: true

# Injects the GitHub token to the project
  - task: PowerShell@2
    displayName: 'Inject GitHub token'
    inputs:
      targetType: 'inline'
      script: |
        gci $(Build.SourcesDirectory)\src -Include *.cs -recurse | ForEach -Process {
        (Get-Content $_ -Raw | ForEach -Process {$_ -replace "githubclientid.secret", "$(githubclientid.secret)"}) | Set-Content $_ -NoNewline
        }
      failOnStderr: true

  - task: UseDotNet@2
    inputs:
      packageType: sdk
      version: 8.0.303
      includePreviewVersions: false

  - task: CmdLine@2
    inputs:
      script: |
        for /f %%a in ('dir /b /a:d %localappdata%\Microsoft\VisualStudio\17*') do echo UsePreviews=True>%localappdata%\Microsoft\VisualStudio\%%a\sdk.txt

  - task: MSBuild@1
    inputs:
      platform: 'x64'
      solution: '$(solution)'
      configuration: '$(storeBuildConfiguration)'
      msbuildArguments: '/t:restore /p:Configuration="$(storeBuildConfiguration)";Platform="$(buildPlatform)";PublishReadyToRun=true'
      maximumCpuCount: true

  - task: MSBuild@1
    inputs:
      solution: '**/*.wapproj'
      platform: 'x64'
      configuration: '$(storeBuildConfiguration)'
      msbuildArguments: '/t:build;_GenerateAppxPackage /p:AppxBundlePlatforms="$(buildPlatform)" /p:AppxPackageDir="$(appxPackageDir)" /p:AppxBundle=Always /p:UapAppxPackageBuildMode=StoreUpload'
      maximumCpuCount: true

  - task: CopyFiles@2
    displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
    inputs:
      SourceFolder: '$(system.defaultworkingdirectory)'
      Contents: '**\bin\$(storeBuildConfiguration)\**'
      TargetFolder: '$(build.artifactstagingdirectory)'

  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifact: Store'
    inputs:
      PathtoPublish: '$(build.artifactstagingdirectory)'
      ArtifactName: 'Store'
      publishLocation: 'Container'

  - task: store-flight@0
    inputs:
      serviceEndpoint: 'Files'
      appId: '9NGHP3DX8HDX'
      flightName: 'Files - Preview'
      packagePath: '$(appxPackageDir)\**\*.msixupload'
      force: false
      skipPolling: false
      deletePackages: true
      numberOfPackagesToKeep: '5'
      isMandatoryUpdate: true
