name: CI

on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

permissions:
  contents: read

jobs:
  lint:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
    - uses: actions/checkout@v4
    - name: Set up Ruby 3.3
      uses: ruby/setup-ruby@master
      with:
        ruby-version: '3.3'
        bundler-cache: true
    - name: Build and test with Rake
      run: |
        STYLE_CHECKS=true bundle exec rubocop

  rbs:
    runs-on: ubuntu-latest
    name: RBS
    timeout-minutes: 20
    steps:
    - uses: actions/checkout@v4
    - name: Set up Ruby 3.0
      uses: ruby/setup-ruby@master
      with:
        ruby-version: '3.0'
        bundler-cache: true
    - name: Validate RBS signatures
      run: |
        bundle exec rake rbs:validate && bundle exec steep check

  # Seems that old imagemagick depend on ghostscript 9.x for handling fonts.
  # However, ubuntu 24.04 has ghostscript 10.x, which is too new for old imagemagick.
  test-linux-for-old-imagemagick:
    runs-on: ubuntu-22.04
    timeout-minutes: 20
    strategy:
      matrix:
        ruby-version: ['3.0']
        imagemagick-version:
          - { full: 6.8.9-10, major-minor: '6.8' }
          - { full: 7.0.11-14, major-minor: '7.0' }

    name: Linux, Ruby ${{ matrix.ruby-version }}, IM ${{ matrix.imagemagick-version.major-minor }}
    steps:
    - uses: actions/checkout@v4
    - name: Cache ImageMagick built objects
      uses: actions/cache@v4
      with:
        path: ./build-ImageMagick
        key: v1-${{ runner.os }}-imagemagick-${{ matrix.imagemagick-version.full }}
        restore-keys: |
          v1-${{ runner.os }}-imagemagick-${{ matrix.imagemagick-version.full }}
    - name: Update/Install packages
      run: |
        export IMAGEMAGICK_VERSION=${{ matrix.imagemagick-version.full }}
        ./before_install_linux.sh
    - name: Set up Ruby ${{ matrix.ruby-version }}
      uses: ruby/setup-ruby@master
      with:
        ruby-version: ${{ matrix.ruby-version }}
        bundler-cache: true
    - name: Build and test with Rake
      run: |
        bundle exec rake

  test-linux:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    strategy:
      matrix:
        ruby-version: ['3.0', '3.1', '3.2', '3.3', '3.4']
        imagemagick-version:
          - { full: 6.9.13-17, major-minor: '6.9' }
          - { full: 7.1.1-39, major-minor: '7.1' }

    name: Linux, Ruby ${{ matrix.ruby-version }}, IM ${{ matrix.imagemagick-version.major-minor }}
    steps:
    - uses: actions/checkout@v4
    - name: Cache ImageMagick built objects
      uses: actions/cache@v4
      with:
        path: ./build-ImageMagick
        key: v1-${{ runner.os }}-imagemagick-${{ matrix.imagemagick-version.full }}
        restore-keys: |
          v1-${{ runner.os }}-imagemagick-${{ matrix.imagemagick-version.full }}
    - name: Update/Install packages
      run: |
        export IMAGEMAGICK_VERSION=${{ matrix.imagemagick-version.full }}
        ./before_install_linux.sh
    - name: Set up Ruby ${{ matrix.ruby-version }}
      uses: ruby/setup-ruby@master
      with:
        ruby-version: ${{ matrix.ruby-version }}
        bundler-cache: true
    - name: Build and test with Rake
      run: |
        bundle exec rake

  test-macos:
    runs-on: macos-latest
    timeout-minutes: 20
    strategy:
      matrix:
        ruby-version: ['3.0', '3.1', '3.2', '3.3', '3.4']
        imagemagick-version:
          - { full: 6.9.13-17, major-minor: '6.9' }
          - { full: 7.1.1-39, major-minor: '7.1' }

    name: macOS, Ruby ${{ matrix.ruby-version }}, IM ${{ matrix.imagemagick-version.major-minor }}
    steps:
    - uses: actions/checkout@v4
    - name: Cache ImageMagick built objects
      uses: actions/cache@v4
      with:
        path: ./build-ImageMagick
        key: v1-${{ runner.os }}-imagemagick-${{ matrix.imagemagick-version.full }}
        restore-keys: |
          v1-${{ runner.os }}-imagemagick-${{ matrix.imagemagick-version.full }}
    - name: Update/Install packages
      run: |
        IMAGEMAGICK_VERSION=${{ matrix.imagemagick-version.full }} ./before_install_osx.sh
    - name: Set up Ruby ${{ matrix.ruby-version }}
      uses: ruby/setup-ruby@master
      with:
        ruby-version: ${{ matrix.ruby-version }}
        bundler-cache: true
    - name: Build and test with Rake
      run: |
        bundle exec rake

  test-windows:
    runs-on: windows-latest
    timeout-minutes: 20
    strategy:
      matrix:
        ruby-version: ['3.4']
        imagemagick-version:
          - { full: 6.9.13-17, major-minor: '6.9' }
          - { full: 7.1.1-39, major-minor: '7.1' }
    env:
      bundled_im_dir: C:\Program Files\ImageMagick-7.1.1-Q16-HDRI
      install_im_dir: D:\ImageMagick

    name: MSWin, Ruby ${{ matrix.ruby-version }}, IM ${{ matrix.imagemagick-version.major-minor }}
    steps:
    - uses: actions/checkout@v4
    - name: Setup environment variable
      # https://stackoverflow.com/questions/60169752/how-to-update-the-path-in-a-github-action-workflow-file-for-a-windows-latest-hos
      run: |
        Add-Content $env:GITHUB_PATH ${{ env.install_im_dir }}
    - name: Uninstall bundled ImageMagick
      # Bundled ImageMagick does not have C/C++ library and dll which required by rmagick.
      run: |
        Start-Process -FilePath "${{ env.bundled_im_dir }}\unins000.exe" -ArgumentList "/VERYSILENT /NORESTART"
    - name: Install ghostscript
      run: |
        choco install ghostscript
    - name: Install ImageMagick
      # https://github.com/rmagick/ImageMagick-binaries
      run: |
        $imagemagick_version = "${{ matrix.imagemagick-version.full }}"
        $installer_name = "ImageMagick-$($imagemagick_version)-Q16-x64-dll.exe"
        $url = "https://github.com/rmagick/ImageMagick-binaries/raw/main/$($installer_name)"
        Invoke-WebRequest -Uri $url -OutFile $installer_name
        Start-Process -FilePath $installer_name -ArgumentList "/DIR=${{ env.install_im_dir }} /VERYSILENT /NORESTART /TASKS=install_Devel"
    - name: Set up Ruby ${{ matrix.ruby-version }}
      uses: ruby/setup-ruby@master
      with:
        ruby-version: ${{ matrix.ruby-version }}
        bundler-cache: true
    - name: Build and test with Rake
      run: |
        cmd.exe /D /S /C "bundle exec rake"
