# This build should never run as CI or against a pull request.
name: $(BuildDefinitionName)_$(date:yyMM).$(date:dd)$(rev:rrr)
trigger: none
pr: none

resources:
  repositories:
  - repository: 1ESPipelineTemplates
    type: git
    name: 1ESPipelineTemplates/1ESPipelineTemplates
    ref: refs/tags/release

parameters:
  - name: buildConfigurations
    type: object
    default:
      - Release
  - name: buildPlatforms
    type: object
    default:
      - x64
      - arm64
  - name: versionNumber
    type: string
    default: '0.0.1'

extends:
  template: v1/1ES.Official.PipelineTemplate.yml@1ESPipelineTemplates
  parameters:
    customBuildTags:
    - 1ES.PT.ViaStartRight
    pool:
      name: SHINE-INT-S
      image: SHINE-VS17-Latest
      os: windows

    stages:
    - stage: build
      displayName: Build (Complete)
      pool:
        name: SHINE-INT-L
        image: SHINE-VS17-Latest
        os: windows
      jobs:
      - job: Build
        strategy:
          matrix:
            ${{ each config in parameters.buildConfigurations }}:
              ${{ each platform in parameters.buildPlatforms }}:
                ${{ config }}_${{ platform }}:
                  BuildConfiguration: ${{ config }}
                  BuildPlatform: ${{ platform }}
        templateContext:
          outputs:
          - output: pipelineArtifact
            artifactName: setup-$(BuildPlatform)
            targetPath: $(Build.ArtifactStagingDirectory)
          sdl:
            baseline:
              baselineFile: $(Build.SourcesDirectory)\.pipelines\sdl.gdnbaselines
        displayName: Build
        timeoutInMinutes: 240 # Some of the 1ES Pipeline stuff and Loc take a very long time
        cancelTimeoutInMinutes: 1
        variables:
          NUGET_RESTORE_MSBUILD_ARGS: /p:Platform=$(BuildPlatform) # Required for nuget to work due to self contained
          NODE_OPTIONS: --max_old_space_size=16384
          IsPipeline: 1 # The installer uses this to detect whether it should pick up localizations
          SkipCppCodeAnalysis: 1 # Skip the code analysis to speed up release CI. It runs on PR CI, anyway
          IsExperimentationLive: 1 # The build and installer use this to turn on experimentation
        steps:
        - checkout: self
          clean: true
          submodules: true
          persistCredentials: True

      # Sets versions for all PowerToy created DLLs
        - task: PowerShell@1
          displayName: Set Versions.Prop
          inputs:
            scriptName: .pipelines/versionSetting.ps1
            arguments: -versionNumber '${{ parameters.versionNumber }}' -DevEnvironment ''

      # ESRP needs 'Microsoft.NETCore.App', version '6.0.0' (x64)
        - task: UseDotNet@2
          displayName: 'Use .NET 6 SDK'
          inputs:
            packageType: sdk
            version: '6.x'

        - task: UseDotNet@2
          displayName: 'Use .NET 8 SDK'
          inputs:
            packageType: sdk
            version: '8.x'

        - task: PowerShell@2
          displayName: Verify and set latest VCToolsVersion usage
          inputs:
            filePath: '$(build.sourcesdirectory)\.pipelines\verifyAndSetLatestVCToolsVersion.ps1'
            pwsh: true

        - task: NuGetAuthenticate@1
            
        - task: NuGetToolInstaller@1
          displayName: Use NuGet Installer latest

      # this will restore the following nugets:
      # - main solution
      # - Bug report tool
      # - Webcam report tool
      # - Installer
      # - Bootstrapper Installer
        - task: PowerShell@2
          displayName: Download and install WiX 3.14 development build
          inputs:
            targetType: filePath
            filePath: '$(build.sourcesdirectory)\.pipelines\installWiX.ps1'

        - task: MicrosoftTDBuild.tdbuild-task.tdbuild-task.TouchdownBuildTask@1
          displayName: 'Download Localization Files -- PowerToys 37400'
          inputs:
            teamId: 37400
            authId: '$(TouchdownApplicationID)'
            authKey: '$(TouchdownApplicationKey)'
            resourceFilePath: |
             **\Resources.resx
             **\Resource.resx
             **\Resources.resw
            appendRelativeDir: true
            localizationTarget: false
            # pseudoSetting: Included

        - task: PowerShell@2
          displayName: Move Loc files into correct locations
          inputs:
            targetType: inline
            script: >-
              $VerbosePreference = "Continue"

              ./tools/build/move-and-rename-resx.ps1

              ./tools/build/move-uwp-resw.ps1
            pwsh: true
            
        - task: CmdLine@2
          displayName: Moving telem files
          inputs:
            script: |
              call nuget.exe restore -configFile .pipelines/release-nuget.config -PackagesDirectory . .pipelines/packages.config || exit /b 1
              move /Y "Microsoft.PowerToys.Telemetry.2.0.0\build\include\TraceLoggingDefines.h" "src\common\Telemetry\TraceLoggingDefines.h" || exit /b 1
              move /Y "Microsoft.PowerToys.Telemetry.2.0.0\build\include\TelemetryBase.cs" "src\common\Telemetry\TelemetryBase.cs" || exit /b 1

      ## ALL BUT INSTALLER BUILDING
        - task: VSBuild@1
          displayName: Build PowerToys main project
          inputs:
            solution: '**\PowerToys.sln'
            vsVersion: 17.0
            msbuildArgs: -restore -graph /p:RestorePackagesConfig=true /p:RestoreConfigFile="$(Build.SourcesDirectory)\.pipelines\release-nuget.config" /p:CIBuild=true /bl:$(Build.SourcesDirectory)\msbuild.binlog
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            clean: true
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Build BugReportTool
          inputs:
            solution: '**/tools/BugReportTool/BugReportTool.sln'
            vsVersion: 17.0
            msbuildArgs: -restore -graph /p:RestorePackagesConfig=true /p:RestoreConfigFile="$(Build.SourcesDirectory)\.pipelines\release-nuget.config" /p:CIBuild=true /bl:$(Build.SourcesDirectory)\msbuild.binlog
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            clean: true
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Build WebcamReportTool
          inputs:
            solution: '**/tools/WebcamReportTool/WebcamReportTool.sln'
            vsVersion: 17.0
            msbuildArgs: -restore -graph /p:RestorePackagesConfig=true /p:RestoreConfigFile="$(Build.SourcesDirectory)\.pipelines\release-nuget.config" /p:CIBuild=true /bl:$(Build.SourcesDirectory)\msbuild.binlog
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            clean: true
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Build StylesReportTool
          inputs:
            solution: '**/tools/StylesReportTool/StylesReportTool.sln'
            vsVersion: 17.0
            msbuildArgs: -restore -graph /p:RestorePackagesConfig=true /p:RestoreConfigFile="$(Build.SourcesDirectory)\.pipelines\release-nuget.config" /p:CIBuild=true /bl:$(Build.SourcesDirectory)\msbuild.binlog
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            clean: true
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Publish Settings for Packaging
          inputs:
            solution: 'src/settings-ui/Settings.UI/PowerToys.Settings.csproj'
            vsVersion: 17.0
            msbuildArgs: >-
              /target:Publish
              /graph
              /p:Configuration=$(BuildConfiguration);Platform=$(BuildPlatform);AppxBundle=Never
              /p:VCRTForwarders-IncludeDebugCRT=false
              /p:PowerToysRoot=$(Build.SourcesDirectory)
              /p:PublishProfile=InstallationPublishProfile.pubxml
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Publish Launcher for Packaging
          inputs:
            solution: 'src/modules/launcher/PowerLauncher/PowerLauncher.csproj'
            vsVersion: 17.0
            # The arguments should be the same as the ones for Settings; make sure they are.
            msbuildArgs: >-
              /target:Publish
              /graph
              /p:Configuration=$(BuildConfiguration);Platform=$(BuildPlatform);AppxBundle=Never
              /p:VCRTForwarders-IncludeDebugCRT=false
              /p:PowerToysRoot=$(Build.SourcesDirectory)
              /p:PublishProfile=InstallationPublishProfile.pubxml
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Publish Monaco Preview Handler for Packaging
          inputs:
            solution: 'src/modules/previewpane/MonacoPreviewHandler/MonacoPreviewHandler.csproj'
            vsVersion: 17.0
            # The arguments should be the same as the ones for Settings; make sure they are.
            msbuildArgs: >-
              /target:Publish
              /graph
              /p:Configuration=$(BuildConfiguration);Platform=$(BuildPlatform);AppxBundle=Never
              /p:VCRTForwarders-IncludeDebugCRT=false
              /p:PowerToysRoot=$(Build.SourcesDirectory)
              /p:PublishProfile=InstallationPublishProfile.pubxml
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Publish Markdown Preview Handler for Packaging
          inputs:
            solution: 'src/modules/previewpane/MarkdownPreviewHandler/MarkdownPreviewHandler.csproj'
            vsVersion: 17.0
            # The arguments should be the same as the ones for Settings; make sure they are.
            msbuildArgs: >-
              /target:Publish
              /graph
              /p:Configuration=$(BuildConfiguration);Platform=$(BuildPlatform);AppxBundle=Never
              /p:VCRTForwarders-IncludeDebugCRT=false
              /p:PowerToysRoot=$(Build.SourcesDirectory)
              /p:PublishProfile=InstallationPublishProfile.pubxml
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Publish Svg Preview Handler for Packaging
          inputs:
            solution: 'src/modules/previewpane/SvgPreviewHandler/SvgPreviewHandler.csproj'
            vsVersion: 17.0
            # The arguments should be the same as the ones for Settings; make sure they are.
            msbuildArgs: >-
              /target:Publish
              /graph
              /p:Configuration=$(BuildConfiguration);Platform=$(BuildPlatform);AppxBundle=Never
              /p:VCRTForwarders-IncludeDebugCRT=false
              /p:PowerToysRoot=$(Build.SourcesDirectory)
              /p:PublishProfile=InstallationPublishProfile.pubxml
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Publish Svg Thumbnail Provider for Packaging
          inputs:
            solution: 'src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.csproj'
            vsVersion: 17.0
            # The arguments should be the same as the ones for Settings; make sure they are.
            msbuildArgs: >-
              /target:Publish
              /graph
              /p:Configuration=$(BuildConfiguration);Platform=$(BuildPlatform);AppxBundle=Never
              /p:VCRTForwarders-IncludeDebugCRT=false
              /p:PowerToysRoot=$(Build.SourcesDirectory)
              /p:PublishProfile=InstallationPublishProfile.pubxml
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            maximumCpuCount: true

        - task: VSBuild@1
          displayName: Publish File Locksmith UI for Packaging
          inputs:
            solution: 'src/modules/FileLocksmith/FileLocksmithUI/FileLocksmithUI.csproj'
            vsVersion: 17.0
            # The arguments should be the same as the ones for Settings; make sure they are.
            msbuildArgs: >-
              /target:Publish
              /graph
              /p:Configuration=$(BuildConfiguration);Platform=$(BuildPlatform);AppxBundle=Never
              /p:VCRTForwarders-IncludeDebugCRT=false
              /p:PowerToysRoot=$(Build.SourcesDirectory)
              /p:PublishProfile=InstallationPublishProfile.pubxml
            platform: $(BuildPlatform)
            configuration: $(BuildConfiguration)
            maximumCpuCount: true

        # Check if deps.json files don't reference different dll versions.
        - task: PowerShell@2
          displayName: Audit deps.json files for all applications
          inputs:
            filePath: '.pipelines/verifyDepsJsonLibraryVersions.ps1'
            arguments: -targetDir '$(build.sourcesdirectory)\$(BuildPlatform)\$(BuildConfiguration)'
            pwsh: true

        # Check if asset files on the main application paths are playing nice and avoiding basic conflicts.
        - task: PowerShell@2
          displayName: Audit base applications path asset conflicts
          inputs:
            filePath: '.pipelines/verifyPossibleAssetConflicts.ps1'
            arguments: -targetDir '$(build.sourcesdirectory)\$(BuildPlatform)\$(BuildConfiguration)'
            pwsh: true

        - task: PowerShell@2
          displayName: Audit WinAppSDK applications path asset conflicts
          inputs:
            filePath: '.pipelines/verifyPossibleAssetConflicts.ps1'
            arguments: -targetDir '$(build.sourcesdirectory)\$(BuildPlatform)\$(BuildConfiguration)\WinUI3Apps'
            pwsh: true

      #### MAIN SIGNING AREA
      # reference https://dev.azure.com/microsoft/Dart/_git/AppDriver?path=/ESRPSigning.json&version=GBarm64-netcore&_a=contents for winappdriver
      # https://dev.azure.com/microsoft/Dart/_git/AppDriver?path=/CIPolicy.xml&version=GBarm64-netcore&_a=contents

        - task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@3
          displayName: Sign Core PT
          inputs:
            ConnectedServiceName: 'Terminal/Console/WinAppDriver Team Code Signing Connection'
            FolderPath: '$(BuildPlatform)/$(BuildConfiguration)' # Video conf uses x86 and x64.
            signType: batchSigning
            batchSignPolicyFile: '$(build.sourcesdirectory)\.pipelines\ESRPSigning_core.json'
            ciPolicyFile: '$(build.sourcesdirectory)\.pipelines\CIPolicy.xml'

        - task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@3
          displayName: Sign DSC Powershell files
          inputs:
            ConnectedServiceName: 'Terminal/Console/WinAppDriver Team Code Signing Connection'
            FolderPath: 'src/dsc/Microsoft.PowerToys.Configure'
            signType: batchSigning
            batchSignPolicyFile: '$(build.sourcesdirectory)\.pipelines\ESRPSigning_DSC.json'
            ciPolicyFile: '$(build.sourcesdirectory)\.pipelines\CIPolicy.xml'

        - task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@3
          displayName: Sign x86 directshow VCM
          inputs:
            ConnectedServiceName: 'Terminal/Console/WinAppDriver Team Code Signing Connection'
            FolderPath: 'x86/$(BuildConfiguration)' # Video conf uses x86 and x64.
            signType: batchSigning
            batchSignPolicyFile: '$(build.sourcesdirectory)\.pipelines\ESRPSigning_vcm.json'
            ciPolicyFile: '$(build.sourcesdirectory)\.pipelines\CIPolicy.xml'
      #### END SIGNING 
      ## END MAIN

        - pwsh: |-
            Move-Item msbuild.binlog "$(Build.ArtifactStagingDirectory)/"
          displayName: Stage binlog into artifact directory
          condition: always()

        - task: ComponentGovernanceComponentDetection@0
          displayName: Component Detection

        - task: CopyFiles@2
          displayName: Copying files for symbols
          inputs:
            contents: >-
              **/*.pdb
            flattenFolders: True
            targetFolder: $(Build.ArtifactStagingDirectory)/Symbols-$(BuildPlatform)/
         
        - task: PowerShell@2
          displayName: 'Remove unneeded files from ArtifactStagingDirectory'
          inputs:
            targetType: 'inline'
            script: |
              cd $(Build.ArtifactStagingDirectory)/Symbols-$(BuildPlatform)/
              Remove-Item vc143.pdb
              Remove-Item *test*

        - task: PublishSymbols@2
          displayName: Publish symbols path
          continueOnError: True
          inputs:
            SearchPattern: |
              $(Build.ArtifactStagingDirectory)/Symbols-$(BuildPlatform)/**/*.*
            IndexSources: false
            SymbolServerType: TeamServices
            
        - template: .pipelines/installer-steps.yml@self
          parameters:
            versionNumber: ${{ parameters.versionNumber }}
            perUserArg: "false"
            buildSubDir: "MachineSetup"
            installerPrefix: "PowerToysSetup"

        - task: PowerShell@2
          displayName: Clean installer dir before building per-user installer
          inputs:
            targetType: inline
            script: git clean -xfd  -e *exe -- .\installer\
            pwsh: true

        - template: .pipelines/installer-steps.yml@self
          parameters:
            versionNumber: ${{ parameters.versionNumber }}
            perUserArg: "true"
            buildSubDir: "UserSetup"
            installerPrefix: "PowerToysUserSetup"

        - task: CopyFiles@2
          displayName: Copying setup file over
          inputs:
            contents: "**/PowerToys*Setup-*.exe"
            flattenFolders: True
            targetFolder: $(Build.ArtifactStagingDirectory)

        - task: PowerShell@2
          displayName: 'Calculating SHA256 hash'
          inputs:
            targetType: 'inline'
            script: |
              $p = "$(System.ArtifactsDirectory)\";
              $staging = "$(Build.ArtifactStagingDirectory)\"
              $userHash = ((get-item $p\PowerToysUserSetup*.exe | Get-FileHash).Hash);
              $machineHash = ((get-item $p\PowerToysSetup*.exe | Get-FileHash).Hash);
              $userPlat = "hash_user_$(BuildPlatform).txt";
              $machinePlat = "hash_machine_$(BuildPlatform).txt";
              $combinedUserPath = $staging + $userPlat;
              $combinedMachinePath = $staging + $machinePlat;
              
              echo $p

              echo $userPlat
              echo $userHash
              echo $combinedUserPath

              echo $machinePlat
              echo $machineHash
              echo $combinedMachinePath
              
              $userHash | out-file -filepath $combinedUserPath
              $machineHash | out-file -filepath $combinedMachinePath
            pwsh: true

        # Publishing the GPO files
        - pwsh: |-
            New-Item "$(Build.ArtifactStagingDirectory)/gpo" -Type Directory
            Copy-Item src\gpo\assets\* "$(Build.ArtifactStagingDirectory)/gpo" -Recurse
          displayName: Stage the GPO files

...
