parameters:
  Env: ''
  GenerateArgs: ''
  Build: true
  BuildArgs: ''
  VcpkgArgs: ''
  ServiceDirectory: ''


steps:
  - script: mkdir build
    displayName: create working directory

  - script: cmake --version
    workingDirectory: build
    displayName: cmake --version

  - script: clang --version
    workingDirectory: build
    displayName: clang --version

  - script: |
      ${{ parameters.Env }} cmake ${{ parameters.VcpkgArgs }} ${{ parameters.GenerateArgs }} ..
    workingDirectory: build
    displayName: cmake generate
    env:
      VCPKG_BINARY_SOURCES: $(VCPKG_BINARY_SOURCES_SECRET)
      X_VCPKG_ASSET_SOURCES: $(X_VCPKG_ASSET_SOURCES_SECRET)

# Core should build all cmake targets
  - ${{ if and(eq(parameters.Build, true), eq(parameters.ServiceDirectory, 'core')) }}:
    - script: cmake --build . ${{ parameters.BuildArgs }}
      workingDirectory: build
      displayName: cmake build All

# Non-core services define the list of targets to build
  - ${{ if and(eq(parameters.Build, true) , ne(parameters.ServiceDirectory, 'core')) }}:
    - pwsh: |
        if (Test-Path ${{ parameters.ServiceDirectory }}-targets-build.txt) {
          cmake --build . ${{ parameters.BuildArgs }} --target (Get-Content ${{ parameters.ServiceDirectory }}-targets-build.txt)
        }
        else
        {
          echo "Skipping Build Target for service ${{parameters.ServiceDirectory}}"
        }
      workingDirectory: build
      displayName: cmake build Targets

