name: CI
on:
  pull_request:
  push:

permissions:
  contents: read # to fetch code (actions/checkout)

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-22.04
            java: 21
            distribution: temurin
            jobtype: 1
          - os: ubuntu-latest
            java: 17
            distribution: zulu
            jobtype: 2
          - os: ubuntu-latest
            java: 25
            distribution: zulu
            jobtype: 3
          - os: ubuntu-latest
            java: 25
            distribution: zulu
            jobtype: 4
          - os: ubuntu-latest
            java: 25
            distribution: zulu
            jobtype: 5
          # - os: ubuntu-latest
          #   java: 8
          #   distribution: zulu
          #   jobtype: 6
          - os: ubuntu-latest
            java: 25
            distribution: zulu
            jobtype: 7
          - os: ubuntu-latest
            java: 17
            distribution: temurin
            jobtype: 10
          - os: ubuntu-latest
            java: 17
            distribution: temurin
            jobtype: 11
          - os: windows-latest
            java: 17
            distribution: temurin
            jobtype: 10
          - os: windows-latest
            java: 17
            distribution: zulu
            jobtype: 12
          # - os: ubuntu-latest
          #   java: 17
          #   distribution: temurin
          #   jobtype: 13
    runs-on: ${{ matrix.os }}
    timeout-minutes: 25
    env:
      JAVA_OPTS: -Xms800M -Xmx2G -Xss6M -XX:ReservedCodeCacheSize=128M -server -Dsbt.io.virtual=false -Dfile.encoding=UTF-8
      JVM_OPTS: -Xms800M -Xmx2G -Xss6M -XX:ReservedCodeCacheSize=128M -server -Dsbt.io.virtual=false -Dfile.encoding=UTF-8
      UTIL_TESTS: "utilControl/test utilInterface/test utilLogging/test utilPosition/test utilRelation/test utilScripted/test"
      SBT_LOCAL: false
      TEST_SBT_VER: 1.5.0
      SBT_ETC_FILE: $HOME/etc/sbt/sbtopts
      JDK11: adopt@1.11.0-9
      SPARK_LOCAL_IP: "127.0.0.1"
    steps:
      - name: Don't convert LF to CRLF during checkout
        if: runner.os == 'Windows'
        run: |
          git config --global core.autocrlf false
          git config --global core.eol lf
      - name: Checkout sbt/sbt
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          submodules: true
      - name: Checkout sbt/io
        uses: actions/checkout@v6
        with:
          repository: sbt/io
          ref: develop
          path: io
      - name: Checkout sbt/zinc
        uses: actions/checkout@v6
        with:
          repository: sbt/zinc
          ref: develop
          path: zinc
      - name: Cleanup SBT server and named pipes (Windows only)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          # Kill sbt processes if any are running
          Get-Process sbt -ErrorAction SilentlyContinue | Stop-Process -Force
          # Remove all SBT named pipes, suppress errors if none exist
          Remove-Item "\\.\pipe\sbt-*" -ErrorAction SilentlyContinue
      - name: Setup JDK
        uses: actions/setup-java@v5
        with:
          distribution: "${{ matrix.distribution }}"
          java-version: "${{ matrix.java }}"
          cache: "sbt"
      - name: Setup sbt
        uses: sbt/setup-sbt@v1
      - name: Set up Python 3.12
        uses: actions/setup-python@v6
        with:
          python-version: 3.12
      - name: Pre-test cleanup
        shell: bash
        run: |
          rm -rf "$HOME/.sbt/scripted/" || true
          rm -rf "$HOME/.ivy2/local" || true
          find "$HOME/.sbt/boot" -name "*-SNAPSHOT" -exec rm -rf {} + 2>/dev/null || true
      - name: Publish local Zinc
        if: ${{ matrix.jobtype == 6 }}
        shell: bash
        run: |
          ./sbt -v lowerUtils/publishLocal
          cd zinc/
          sbt publishLocal
      - name: Build and test (1)
        if: ${{ matrix.jobtype == 1 }}
        shell: bash
        run: |
          rm -rf "$HOME/.sbt/boot/" || true
          ./sbt -v --client mimaReportBinaryIssues
          ./sbt -v --client javafmtCheck
          ./sbt -v --client "Test/javafmtCheck"
          ./sbt -v --client scalafmtCheckAll
          ./sbt -v --client scalafmtSbtCheck
          ./sbt -v --client serverTestProj/scalafmtCheckAll
          # ./sbt -v --client headerCheck
          # ./sbt -v --client "Test/headerCheck"
          ./sbt -v --client "Test/compile"
          ./sbt -v --client doc
          ./sbt -v --client publishLocal
          ./sbt -v --client test
          ./sbt -v --client "serverTestProj/test"
          ./sbt -v --client "all $UTIL_TESTS"
      - name: Build and test (2)
        if: ${{ matrix.jobtype == 2 }}
        shell: bash
        run: |
          ./sbt -v "scripted actions/* apiinfo/* cache/* compiler-project/* ivy-deps-management/* reporter/* tests/* classloader-cache/* package/*"
          # ./sbt -v "scripted watch/*"
      - name: Build and test (3)
        if: ${{ matrix.jobtype == 3 }}
        shell: bash
        run: |
          ./sbt -v --client "scripted dependency-graph/*"
          ./sbt -v --client "scripted dependency-management/* project-load/* project-matrix/* java/* run/*"
          ./sbt -v --client "scripted plugins/*"
          ./sbt -v --client "scripted nio/*"
      - name: Build and test (4)
        if: ${{ matrix.jobtype == 4 }}
        shell: bash
        run: |
          ./sbt -v "repoOverrideTest:scripted force-proxy-repos-file/*"
          ./sbt -v "scripted source-dependencies/*"
      - name: Build and test (5)
        if: ${{ matrix.jobtype == 5 }}
        shell: bash
        run: |
          ./sbt -v "scripted project/*"
          ./sbt -v "scripted project1/*"
      # - name: Build and test (6)
      #  if: ${{ matrix.jobtype == 6 }}
      #  shell: bash
      #  run: |
      #    # build from fresh IO, LM, and Zinc
      #    BUILD_VERSION="1.5.0-SNAPSHOT"
      #    cd io
      #    sbt -v -Dsbt.build.version=${BUILD_VERSION} +publishLocal
      #    cd ../
      #    sbt -Dsbtzinc.path=$HOME/work/sbt/sbt/zinc -Dsbt.build.version=$BUILD_VERSION -Dsbt.build.fatal=false "+lowerUtils/publishLocal; {zinc}/publishLocal; upperModules/publishLocal"
      #    find "$HOME/.sbt/boot" -name "*-SNAPSHOT" -exec rm -rf {} + 2>/dev/null || true
      #    sbt -v -Dsbt.version=$BUILD_VERSION "++$SCALA_213; all $UTIL_TESTS; ++$SCALA_212; all $UTIL_TESTS; scripted actions/* source-dependencies/*1of3 dependency-management/*1of4 java/*"
      - name: Scripted (tests)
        if: ${{ matrix.jobtype == 7 }}
        shell: bash
        run: |
          ./sbt -v "scripted tests/*"
      - name: Build and test lm-coursier (10)
        if: ${{ matrix.jobtype == 10 }}
        shell: bash
        run: scripts/lm-coursier-ci.sh
      - name: Build and test contraband
        if: ${{ matrix.jobtype == 11 }}
        shell: bash
        run: |
          find **/src/main/contraband-scala -name "*.scala" -delete || true
          sbt generateContrabands
          git diff --exit-code
      - name: Build and test (12)
        if: ${{ matrix.jobtype == 12 }}
        shell: bash
        run: |
          ./sbt -v "scripted cache/*"
      # - name: Hash Benchmark
      #   if: ${{ matrix.jobtype == 13 }}
      #   shell: bash
      #   run: |
      #     ./sbt -v "hashBenchmark/Jmh/run -i 5 -wi 3 -f1 -t1"
