name: Release Nonebot Plugin Docs

on:
  release:
    types: [ published ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.8
          architecture: "x64"

      - uses: Gr1N/setup-poetry@v7

      - uses: actions/cache@v2
        with:
          path: ~/.cache/pypoetry/virtualenvs
          key: ${{ runner.os }}-poetry-${{ hashFiles('**/poetry.lock') }}

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: "16"

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Cache Packages
        uses: actions/cache@v2
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}

      - name: Install and build
        run: |
          poetry install -E all
          poetry run sphinx-build -M markdown ./docs_build ./build
          cp -r ./build/markdown/* ./website/docs/api/
          yarn install
          yarn prettier
          yarn build:plugin --out-dir ../packages/nonebot-plugin-docs/nonebot_plugin_docs/dist

      - name: Publish Package
        run: |
          cd packages/nonebot-plugin-docs/
          poetry version $(poetry version -s)
          poetry build
          poetry publish -u ${{secrets.PYPI_USERNAME}} -p ${{secrets.PYPI_PASSWORD}}