parameters:
  runAsPublic: false
  sourceIndexPackageVersion: 1.0.1-20240129.2
  sourceIndexPackageSource: https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json
  sourceIndexBuildCommand: powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "eng/common/build.ps1 -restore -build -binarylog -ci"
  preSteps: []
  binlogPath: artifacts/log/Debug/Build.binlog
  condition: ''
  dependsOn: ''
  pool: ''

jobs:
- job: SourceIndexStage1
  dependsOn: ${{ parameters.dependsOn }}
  condition: ${{ parameters.condition }}
  variables:
  - name: SourceIndexPackageVersion
    value: ${{ parameters.sourceIndexPackageVersion }}
  - name: SourceIndexPackageSource
    value: ${{ parameters.sourceIndexPackageSource }}
  - name: BinlogPath
    value: ${{ parameters.binlogPath }}
  - ${{ if and(eq(parameters.runAsPublic, 'false'), ne(variables['System.TeamProject'], 'public'), notin(variables['Build.Reason'], 'PullRequest')) }}:
    - group: source-dot-net stage1 variables
  - template: /eng/common/templates/variables/pool-providers.yml

  ${{ if ne(parameters.pool, '') }}:
    pool: ${{ parameters.pool }}
  ${{ if eq(parameters.pool, '') }}:
    pool:
      ${{ if eq(variables['System.TeamProject'], 'public') }}:
        name: $(DncEngPublicBuildPool)
        demands: ImageOverride -equals windows.vs2022.amd64.open
      ${{ if eq(variables['System.TeamProject'], 'internal') }}:
        name: $(DncEngInternalBuildPool)
        demands: ImageOverride -equals windows.vs2022.amd64

  steps:
  - ${{ each preStep in parameters.preSteps }}:
    - ${{ preStep }}

  - task: UseDotNet@2
    displayName: Use .NET 8 SDK
    inputs:
      packageType: sdk
      version: 8.0.x
      installationPath: $(Agent.TempDirectory)/dotnet
      workingDirectory: $(Agent.TempDirectory)

  - script: |
      $(Agent.TempDirectory)/dotnet/dotnet tool install BinLogToSln --version $(SourceIndexPackageVersion) --add-source $(SourceIndexPackageSource) --tool-path $(Agent.TempDirectory)/.source-index/tools
      $(Agent.TempDirectory)/dotnet/dotnet tool install UploadIndexStage1 --version $(SourceIndexPackageVersion) --add-source $(SourceIndexPackageSource) --tool-path $(Agent.TempDirectory)/.source-index/tools
    displayName: Download Tools
    # Set working directory to temp directory so 'dotnet' doesn't try to use global.json and use the repo's sdk.
    workingDirectory: $(Agent.TempDirectory)

  - script: ${{ parameters.sourceIndexBuildCommand }}
    displayName: Build Repository

  - script: $(Agent.TempDirectory)/.source-index/tools/BinLogToSln -i $(BinlogPath) -r $(Build.SourcesDirectory) -n $(Build.Repository.Name) -o .source-index/stage1output
    displayName: Process Binlog into indexable sln

  - ${{ if and(eq(parameters.runAsPublic, 'false'), ne(variables['System.TeamProject'], 'public'), notin(variables['Build.Reason'], 'PullRequest')) }}:
    - script: $(Agent.TempDirectory)/.source-index/tools/UploadIndexStage1 -i .source-index/stage1output -n $(Build.Repository.Name)
      displayName: Upload stage1 artifacts to source index
      env:
        BLOB_CONTAINER_URL: $(source-dot-net-stage1-blob-container-url)
