name: build dev version
on:
  push:
    branches:
      - dev
  repository_dispatch:

jobs:
  build_dev:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@master
      - name: Set up Python 3.7
        uses: actions/setup-python@v1
        with:
          python-version: 3.7
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - name: Build frontend
        working-directory: ./webiojs
        run: |
          npm install
          gulp
          cp dist/pywebio.min.* ../pywebio/html/js
      - name: Build doc demos
        run: |
          pip3 install -e ".[all]"
          pip3 install -r requirements.txt
          cd docs && CODE_EXPORT_PATH=../demos/doc_demos make clean html
      - name: Set dev version
        run: python3 tools/build_dev_version.py
      - name: Release dev version
        run: |
          git config --global user.email "$(git log -n 1 --pretty=format:%ae)"
          git config --global user.name "${{ github.actor }}"
      
          # ref: https://stackoverflow.com/questions/8536732/can-i-hold-git-credentials-in-environment-variables
          git config --global credential.helper '!f() { sleep 1; echo "username=${{ github.actor }}"; echo "password=${GH_TOKEN}"; }; f'

          git fetch --unshallow origin
          git branch -D dev-release || true
          git checkout -b dev-release
          
          rm .gitignore
          git add pywebio/__version__.py
          git add pywebio/html/js
          git add demos/doc_demos

          git commit -m "Build at `date`"
          git push -f -u origin dev-release
        env:
          # This token is provided by Actions, you do not need to create your own token
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}