name: Build

on:
  push:
    branches: [master]

jobs:
  createrelease:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Create Version
      uses: paulhatch/semantic-version@v4.0.2
      id: create_version
      with:
        tag_prefix: "v"
        major_pattern: "(MAJOR)"
        minor_pattern: "(MINOR)"
        format: "${major}.${minor}.${patch}-prerelease${increment}"
    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ steps.create_version.outputs.version_tag }}
        release_name: Release ${{ steps.create_version.outputs.version_tag }}
        draft: false
        prerelease: false

    - name: Output Release URL File
      run: echo "${{ steps.create_release.outputs.upload_url }}" > release_url.txt

    - name: Save Release URL File for publish
      uses: actions/upload-artifact@v1
      with:
        name: release_url
        path: release_url.txt

  build:
    name: Build packages
    needs: createrelease
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: macos-latest
            TARGET: macos
            CMD_BUILD: pyinstaller --noconfirm --onefile --noconsole --icon "logo.ico" main.py && cd dist && zip -r9 main main.app/
            OUT_FILE_NAME: main.zip
            ASSET_NAME: app-macos.zip
            OS_DEP_CMD: echo no additional dependencies needed
            ASSET_MIME: application/zip
          - os: ubuntu-latest
            TARGET: linux
            OS_DEP_CMD: sudo apt-get install python3-pyqt5 pyqt5-dev-tools qttools5-dev-tools
            CMD_BUILD: pyinstaller --noconfirm --onefile --noconsole --icon "logo.ico" main.py
            OUT_FILE_NAME: main
            ASSET_NAME: main-linux
            ASSET_MIME: application/x-executable
          - os: windows-latest
            TARGET: windows
            OS_DEP_CMD: echo no additional dependencies needed
            CMD_BUILD: pyinstaller --noconfirm --onefile --noconsole --icon "logo.ico" main.py
            OUT_FILE_NAME: main.exe
            ASSET_NAME: main-windows.exe
            ASSET_MIME: application/vnd.microsoft.portable-executable
    steps:
    - uses: actions/checkout@v1

    - name: Set up Python 3.9
      uses: actions/setup-python@v2
      with:
        python-version: 3.9

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pyperclip requests PyQt5 pyqt5 pyqt5-sip pyinstaller

    - name: Install additional dependencies
      run: ${{matrix.OS_DEP_CMD}}

    - name: Build with pyinstaller for ${{matrix.TARGET}}
      run: ${{matrix.CMD_BUILD}}

    - name: Load Release URL File from release job
      uses: actions/download-artifact@v1
      with:
        name: release_url

    - name: Get Release File Name & Upload URL
      id: get_release_info
      shell: bash
      run: |
        value=`cat release_url/release_url.txt`
        echo ::set-output name=upload_url::$value

    - name: Upload Release Asset
      id: upload-release-asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.get_release_info.outputs.upload_url }}
        asset_path: ./dist/${{ matrix.OUT_FILE_NAME}}
        asset_name: ${{ matrix.ASSET_NAME}}
        asset_content_type: ${{ matrix.ASSET_MIME}}