name: Release @pybricks/ide-docs

on:
  push:
    tags:
    - 'v4.*'

jobs:
  publish_ide_docs:
    runs-on: ubuntu-22.04
    permissions:
      id-token: write
    steps:
    - name: Get version from tag
      run: |
        VERSION="${GITHUB_REF_NAME#v}"
        NPM_VERSION=$(echo "$VERSION" | sed 's/\([0-9]\)a\([0-9]\)/\1-alpha.\2/;s/\([0-9]\)b\([0-9]\)/\1-beta.\2/;s/\([0-9]\)rc\([0-9]\)/\1-rc.\2/')
        echo "VERSION=$VERSION" >> $GITHUB_ENV
        echo "NPM_VERSION=$NPM_VERSION" >> $GITHUB_ENV
    - name: Ubuntu packages
      run: |
        sudo apt-get update
        sudo apt-get install -y dvisvgm preview-latex-style texlive texlive-fonts-extra texlive-latex-extra
    - uses: actions/checkout@v6
      with:
        submodules: recursive
    - name: Set up Python 3.8
      uses: actions/setup-python@v5
      with:
        python-version: 3.8
    - name: Install dependencies
      run: |
        pip install poetry
        poetry run python -m pip install --upgrade pip
        poetry run python -m pip install --upgrade setuptools
        poetry install --only=doc
    - uses: actions/setup-node@v6
      with:
        node-version: '22.x'
        registry-url: 'https://registry.npmjs.org'
    - run: npm version --no-git-tag-version "$NPM_VERSION"
      working-directory: npm/ide-docs
    - run: yarn build
      working-directory: npm/ide-docs
    - run: npm publish
      working-directory: npm/ide-docs
