name: Build EDMC for Windows

on:
  push:
    tags:
      - "Release/*"
  workflow_dispatch:

jobs:
  test:
    name: Build EDMC
    runs-on: windows-2019

    defaults:
      run:
        shell: powershell

    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true

      - uses: actions/setup-python@v4
        with:
          python-version-file: '.python-version'
          architecture: "x86"

      - name: Install python tools
        run: |
          pip install wheel
          pip install -r requirements-dev.txt

      - name: Download winsparkle
        run: |
          Invoke-Webrequest -UseBasicParsing https://github.com/vslavik/winsparkle/releases/download/v0.7.0/WinSparkle-0.7.0.zip -OutFile out.zip
          Expand-Archive out.zip
          Move-Item 'out\WinSparkle-0.7.0\Release\*' '.\'

      - name: Build EDMC
        run: |
          python Build-exe-and-msi.py

      - name: Upload build files
        uses: actions/upload-artifact@v3
        with:
          name: Built files
          path: EDMarketConnector_win*.msi

  release:
    name: Release new version
    runs-on: ubuntu-latest
    needs: test
    if: "${{ github.event_name != 'workflow_dispatch' }}"

    steps:
      - name: Download binary
        uses: actions/download-artifact@v3
        with:
          name: Built files
          path: ./
          
      - name: Hash files
        run: sha256sum EDMarketConnector_win*.msi > ./hashes.sum

      - name: Create Draft Release
        uses: "softprops/action-gh-release@v1"
        with:
          token: "${{secrets.GITHUB_TOKEN}}"
          draft: true
          prerelease: true
          discussion_category_name: "Announcement"
          files: |
            ./EDMarketConnector_win*.msi
            ./hashes.sum
