# Based on https://github.com/tlambert03/napari/blob/master/.github/workflows/make_bundle.yml
on:
  pull_request:
  release:
    types:
      - created

name: Create Windows Installer

jobs:
  build:
    name: Windows installer
    runs-on: windows-latest
    strategy:
      matrix:
        build_type: ['Lite', 'Full']
    env:
      UNWANTED_PACKAGES: ${{github.event_name == 'pull_request' && 'pip spyder-kernels Rtree' || 'pip Rtree' }}
      SKIP_PACKAGES: 'bcrypt'
      ADD_PACKAGES: ${{github.event_name == 'pull_request' && 'spyder_kernels blib2to3 _black_version blackd rtree' || 'blib2to3 _black_version blackd rtree' }}
      PYNSIST_REQ: pynsist==2.5.1
      EXTRA_FLAG: ${{ matrix.build_type == 'Full' && github.event_name == 'pull_request' && '-ep installers/Windows/req-extras-pull-request.txt' || matrix.build_type == 'Lite' && github.event_name == 'pull_request' && '-ep installers/Windows/req-pull-request.txt' || matrix.build_type == 'Full' && '-ep installers/Windows/req-extras-release.txt' || '-ep installers/Windows/req-release.txt' }}
      SUFFIX_FLAG: ${{ matrix.build_type == 'Lite' && '-s lite' || '-s full' }}
      TEMPLATE_FAG: '-t installers/Windows/assets/nsist/spyder.nsi'
      EXE_NAME: ${{ matrix.build_type == 'Lite' && github.event_name == 'pull_request' && 'Spyder_64bit_lite.exe' || matrix.build_type == 'Lite' && 'Spyder_64bit_lite.exe' || 'Spyder_64bit_full.exe' }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Install Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.7.9
      - name: Install yarg
        shell: bash -l {0}
        run: |
          python -m pip install yarg
      - name: Run script
        shell: bash -l {0}
        run: |
          python installers/Windows/installer.py 3.7.9 64 setup.py spyder.app.start:main spyder img_src/spyder.ico LICENSE.txt ${EXTRA_FLAG} ${SUFFIX_FLAG} ${TEMPLATE_FAG}
      - name: Upload Artifact
        uses: actions/upload-artifact@v2
        with:
          path: dist/${{ env.EXE_NAME }}
          name: ${{ env.EXE_NAME }}
      - name: Get Release
        if: ${{github.event_name == 'release'}}
        id: get_release
        env:
          GITHUB_TOKEN: ${{ github.token }}
        uses: bruceadams/get-release@v1.2.0
      - name: Upload Release Asset
        if: ${{github.event_name == 'release'}}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ github.token }}
        with:
          upload_url: ${{ steps.get_release.outputs.upload_url }}
          asset_path: dist/${{ env.EXE_NAME }}
          asset_name: ${{ env.EXE_NAME }}
          asset_content_type: application/vnd.microsoft.portable-executable