name: CI

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

jobs:
  Node:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x, 18.x]

    steps:
    - uses: actions/checkout@v4

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}

    - name: Install
      run: npm ci
      env:
        CI: true

    - name: Build 
      run: npm run build --if-present
      env:
        CI: true

    - name: Test
      run: npm run test
      env:
        CI: true

  Vala:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Install the dependencies
      run: |
        export DEBIAN_FRONTEND=noninteractive
        curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
        sudo apt-get update -y
        sudo apt-get install -y --no-install-recommends software-properties-common
        sudo add-apt-repository -y ppa:vala-team/daily
        sudo apt-get update -y
        sudo apt-get install \
        -y \
        --no-install-recommends \
        gcc \
        libgee-0.8-dev \
        git \
        libguestfs-gobject-dev \
        libvaladoc-0.58-dev \
        php \
        php-curl \
        sphinxsearch \
        unzip \
        valac \
        valadoc \
        wget \
        xsltproc \
        make \
        nodejs

    - name: Build the documentation
      run: |
        make build-docs GENERATOR_OPTS=""

    - name: Build search index
      run: |
        make configgen \
        && ./configgen ./valadoc.org/ \
        && mkdir ./sphinx/storage \
        && indexer --config ./sphinx.conf --all

    - name: Build website assets
      run: |
        npm ci && make build-data

    - name: Build image
      run: |
        docker build -t valalang/docs-tmp .
        CONTAINER_ID=$(sudo docker create -v "$(pwd)"/valadoc.org:/opt/valadoc.org --name valadoc valalang/docs-tmp:latest)
        docker start $CONTAINER_ID
        docker exec $CONTAINER_ID sh -c 'mv /opt/valadoc.org/* /var/www/html/'
        docker stop $CONTAINER_ID
        docker commit $CONTAINER_ID valalang/docs:latest
