name: JuliaCompileAndTest
on: [push, pull_request]
# needed to allow julia-actions/cache to delete old caches that it has created
permissions:
  actions: write
  contents: read
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        triplet: ['x86_64-linux-gnu-cxx11', 'aarch64-apple-darwin', 'x86_64-w64-mingw32-cxx11']
    steps:
      - uses: actions/checkout@v4
      - uses: julia-actions/setup-julia@v2
        with:
          version: "1.7"
          arch: x64
      - uses: julia-actions/cache@v2
      - run: |
          git fetch --tags
          echo "HIGHS_RELEASE=$(git describe --tags $(git rev-list --tags --max-count=1) | sed 's/^v//')" >> $GITHUB_ENV
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            echo "HIGHS_COMMIT=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
            echo "HIGHS_URL=${{ github.event.pull_request.head.repo.clone_url }}" >> $GITHUB_ENV
          else
            echo "HIGHS_COMMIT=${{ github.sha }}" >> $GITHUB_ENV
            echo "HIGHS_URL=https://github.com/${{ github.repository }}.git" >> $GITHUB_ENV
          fi
      - run: |
          julia --color=yes -e 'using Pkg; Pkg.add("BinaryBuilder")'
          julia --color=yes .github/julia/build_tarballs.jl ${{ matrix.triplet }} --verbose --deploy="local"
        env:
          BINARYBUILDER_AUTOMATIC_APPLE: true
      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.triplet }}
          path: ${{ github.workspace }}/products/*
      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.triplet }}-jll
          path: /home/runner/.julia/dev/HiGHS_jll
  test:
    runs-on: ${{ matrix.os }}
    needs: build
    strategy:
      fail-fast: false
      matrix:
        include:
          - triplet: 'x86_64-linux-gnu-cxx11'
            os: 'ubuntu-latest'
            arch: x64
          - triplet: 'aarch64-apple-darwin'
            os: 'macos-14'
            arch: aarch64
          - triplet: 'x86_64-w64-mingw32-cxx11'
            os: 'windows-latest'
            arch: x64
    steps:
      - uses: actions/checkout@v4
      - uses: julia-actions/setup-julia@v2
        with:
          version: "1.10"
          arch: ${{ matrix.arch }}
      # Download and setup the artifact
      - uses: actions/download-artifact@v4
        with:
          name: ${{ matrix.triplet }}
          path: ${{ github.workspace }}/products
      - shell: bash
        run: tar -xzf products/HiGHS.*.${{ matrix.triplet }}.tar.gz -C products
      # We also need to download the JLL package, because it contains the new
      # dependencies
      - uses: actions/download-artifact@v4
        with:
          name: ${{ matrix.triplet }}-jll
          path: ${{ github.workspace }}/HiGHS_jll
      # We need to update the [compat] section in HiGHS to support HiGHS_jll
      - shell: julia --color=yes {0}
        run: |
          using Pkg
          Pkg.develop("HiGHS")
          project_filename = joinpath(Pkg.devdir(), "HiGHS", "Project.toml")
          project = read(project_filename, String)
          write(
            project_filename,
            replace(project, r"HiGHS_jll = \"=.+?\"" => "HiGHS_jll = \"1\""),
          )
      # Now we need to update the Artifacts.toml in the HiGHS_jll that we've
      # downloaded. Otherwise Julia will try to download the default on install.
      - shell: julia --color=yes {0}
        run: |
          import Pkg
          file = joinpath(ENV["GITHUB_WORKSPACE"], "HiGHS_jll", "Artifacts.toml")
          m = match(r"git-tree-sha1 = \"(.+?)\"", read(file, String))
          dir = escape_string(joinpath(ENV["GITHUB_WORKSPACE"], "products"))
          write(
            joinpath(homedir(), ".julia", "artifacts", "Overrides.toml"),
            "$(m[1]) = \"$(dir)\"\n",
          )
          Pkg.develop(; path = joinpath(ENV["GITHUB_WORKSPACE"], "HiGHS_jll"))
      - shell: julia --color=yes {0}
        run: |
          import HiGHS_jll
          file = joinpath(ENV["GITHUB_WORKSPACE"], "check", "instances", "flugpl.mps")
          run(`$(HiGHS_jll.highs()) --solver=hipo $file`)
          run(`$(HiGHS_jll.highs()) $file`)
      - shell: julia --color=yes {0}
        run: |
          using Pkg
          Pkg.test("HiGHS")
