name: "Publish Snapshot"

on:
    workflow_dispatch:
        inputs:
            snapshot_version:
                description: "The calver version of this snapshot: 2022.09.1 or 2022.09.1.RC1"
                type: string
                required: true

defaults:
    run:
        working-directory: ./core

jobs:
    publish-snapshot:
        runs-on: ubuntu-latest
        permissions:
            contents: read
            id-token: write
        steps:
            - name: Checkout
              uses: actions/checkout@v5

            - name: Install node
              uses: actions/setup-node@v5
              with:
                  node-version: 20.x

            - name: Python venv
              run: python -m venv env

            - name: Activate Python
              run: source env/bin/activate

            - name: Update pip
              run: pip install --upgrade pip

            - name: Install PyMinifier
              run: pip install --ignore-requires-python python-minifier

            - name: Install Setuptools
              run: pip install setuptools

            - name: Cache node modules
              uses: actions/cache@v4
              env:
                  cache-name: cache-node-modules
              with:
                  # npm cache files are stored in `~/.npm` on Linux/macOS
                  path: ~/.npm
                  key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
                  restore-keys: |
                      ${{ runner.os }}-build-${{ env.cache-name }}-
                      ${{ runner.os }}-build-
                      ${{ runner.os }}-

            - name: Install Dependencies
              run: npm install && npx playwright install chromium

            - name: Build Pyscript.core
              run: npm run build

            - name: Configure AWS credentials
              uses: aws-actions/configure-aws-credentials@v5
              with:
                  aws-region: ${{ secrets.AWS_REGION }}
                  role-to-assume: ${{ secrets.AWS_OIDC_RUNNER_ROLE }}

            - name: Generate index.html in snapshot
              working-directory: .
              run: sed -e 's#_PATH_#https://pyscript.net/snapshots/${{ inputs.snapshot_version }}/#' -e 's#_DOC_VERSION_#${{ inputs.snapshot_version }}#' -e 's#_TAG_VERSION_#/tag/${{ inputs.snapshot_version }}#' -e 's#_VERSION_#${{ inputs.snapshot_version }}#' ./public/index.html > ./core/dist/index.html

            - name: Copy to Snapshot
              run: >
                  aws s3 sync ./dist/ s3://pyscript.net/snapshots/${{ inputs.snapshot_version }}/
