parameters:
  - name: SDKType
    type: string
    default: bom
  - name: ServiceDirectory
    type: string
    default: 'not-specified' # Set a default that breaks in obvious ways.
  - name: Artifacts
    type: object
    default: []

jobs:
  - job: 'BuildAndValidate'
    displayName: 'Build and Validate'

    variables:
      - template: ../variables/globals.yml

    pool:
      name: azsdk-pool-mms-ubuntu-2004-general
      vmImage: MMSUbuntu20.04

    strategy:
      matrix:
        Client Libraries:
          ArtifactName: 'packages'

    steps:
      - template: /eng/common/pipelines/templates/steps/sparse-checkout.yml
        parameters:
          Paths:
            - 'sdk/${{ parameters.ServiceDirectory }}'
            - '**/*.xml'
            - '**/*.md'
            - '!sdk/**/test-recordings'
            - '!sdk/**/session-records'

      - script: |
          echo "##vso[build.addbuildtag]Scheduled"
        displayName: 'Tag scheduled builds'
        condition: and(succeeded(), eq(variables['Build.SourceBranchName'],'main'),eq(variables['Build.Reason'],'Schedule'))

      - task: UsePythonVersion@0
        displayName: 'Use Python $(PythonVersion)'
        inputs:
          versionSpec: $(PythonVersion)

      - template: /eng/common/pipelines/templates/steps/daily-dev-build-variable.yml

      - template: /eng/common/pipelines/templates/steps/verify-readme.yml
        parameters:
          ScanPath: $(Build.SourcesDirectory)/sdk/${{ parameters.ServiceDirectory }}

      - template: /eng/pipelines/templates/steps/generate-project-list-and-cache-maven-repository.yml
        parameters:
          JobType: 'Build and Validate'
          Artifacts: ${{ parameters.Artifacts }}

      # We `install` the code quality reports tooling into our local m2 cache separately from building the Maven project
      # reports. This means it is available as part of that, but also so that this is not documented in the project report.
      - template: /eng/pipelines/templates/steps/install-reporting-tools.yml

      - ${{ each artifact in parameters.Artifacts }}:
        - task: Maven@3
          displayName: 'Prepare POM: ${{artifact.groupId}}:${{artifact.name}}'
          inputs:
            mavenPomFile: sdk/${{parameters.ServiceDirectory}}/${{artifact.name}}/pom.xml
            goals: 'deploy'
            options: '$(DefaultOptions) -pl ${{artifact.groupId}}:${{artifact.name}} -DaltDeploymentRepository=id::default::file://$(System.DefaultWorkingDirectory)/build -Dgpg.skip'
            mavenOptions: '$(MemoryOptions) $(LoggingOptions)'
            javaHomeOption: 'JDKVersion'
            jdkVersionOption: $(JavaBuildVersion)
            jdkArchitectureOption: 'x64'
            publishJUnitResults: false

      - task: PowerShell@2
        displayName: 'Copy artifacts to staging'
        inputs:
          pwsh: true
          workingDirectory: $(Agent.BuildDirectory)
          filePath: eng/scripts/Stage-MavenPackageArtifacts.ps1
          arguments: >
            -SourceDirectory $(System.DefaultWorkingDirectory)/build
            -TargetDirectory $(Build.ArtifactStagingDirectory)
            -Artifacts ('${{ replace(convertToJson(parameters.Artifacts), '''', '`''') }}' | ConvertFrom-Json)
            -InformationAction Continue

      - publish: $(Build.ArtifactStagingDirectory)
        displayName: 'Publish Artifacts $(ArtifactName)'
        artifact: '$(ArtifactName)'

      - publish: $(System.DefaultWorkingDirectory)
        condition: and(eq(variables['CaptureRepositoryOnFailure'], 'true'), failed())
        displayName: 'Capture repo state for analysis'
        artifact: repository
