schedules:
- cron: '0 0 * * *'
  displayName: Nightly dev build
  branches:
    include:
    - main
  always: true

trigger: none
pr: none

jobs:
  - job: Build

    timeoutInMinutes: 60

    variables:
      - template: /eng/pipelines/variables/globals.yml
      - template: /eng/pipelines/variables/image.yml

    pool:
      name: $(LINUXPOOL)
      image: $(LINUXVMIMAGE)
      os: linux

    steps:
      - checkout: self
        submodules: true

      - task: NodeTool@0
        displayName: 'Install Node.js $(NodeVersion)'
        inputs:
          versionSpec: '$(NodeVersion)'

      - template: /eng/pipelines/steps/cache-maven-repository.yml
        parameters:
          JobType: 'TypeSpec Nightly'

      - script: |
          npm install -g @typespec/compiler@next
          npx @azure-tools/typespec-bump-deps typespec-extension/package.json typespec-tests/package.json --add-npm-overrides
          echo "typespec-extension/package.json"
          cat typespec-extension/package.json
          echo "typespec-tests/package.json"
          cat typespec-tests/package.json
        displayName: 'Bump TypeSpec Dependencies to dev'

      - script: |
          npm install
        displayName: 'Install dev TypeSpec Dependencies'
        workingDirectory: ./typespec-extension

      - task: PowerShell@2
        retryCountOnTaskFailure: 1
        displayName: 'Generate Code'
        inputs:
          pwsh: true
          filePath: Generate-TypeSpec.ps1

      - script: |
          echo "Dependencies of typespec-java"
          cd typespec-extension
          npm ls --all
          echo "Dependencies of typespec-java-tests"
          cd ../typespec-tests
          npm ls --all
        displayName: 'Print Package Dependencies'
        condition: succeededOrFailed()

      - script: |
          git checkout package.json typespec-extension/package.json typespec-tests/package.json
        displayName: 'Revert package.json'

      - script: |
          git status
          git diff
        displayName: 'Git Diff'

#      - script: |
#          [ -z "`git status --porcelain`" ]
#        displayName: 'Check no Diff'

      - script: |
          npx --no-install tsp-spector server start ./node_modules/@typespec/http-specs/specs ./node_modules/@azure-tools/azure-http-specs/specs --coverageFile ./tsp-spector-coverage-java.json
        displayName: 'Start Spector Test Server'
        workingDirectory: ./typespec-tests

      - task: Maven@4
        displayName: 'Run TypeSpec Tests'
        inputs:
          mavenPomFile: pom.xml
          goals: 'clean test'
          options: '$(DefaultOptions) -PtestTsp -pl typespec-tests -DtrimStackTrace=false'
          mavenOptions: '$(MemoryOptions) $(LoggingOptions)'
          javaHomeOption: 'JDKVersion'
          jdkVersionOption: $(JavaVersion)
          jdkArchitectureOption: 'x64'
          publishJUnitResults: false

      - script: |
          npx --no-install tsp-spector server stop
        displayName: 'Stop Spector Test Server'
        workingDirectory: ./typespec-tests

      - template: /eng/pipelines/steps/cleanup-maven-local-cache.yml
