name: MacOS build

on: [push]

jobs:
  macos-build:
    name: MacOS build
    runs-on: macos-latest

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
      - run: brew update
      - run: brew install sdl2 freetype zlib libpng gnu-sed
      - run: make -j 3 ARCH=macos
      - run: make -j 3 ARCH=macos unit_test
      - run: alias sed=gsed && make -j 3 e2e_test
      - run: make -j 3 ARCH=macos macos_bundle

      # Only upload asset to a release when pushing a tag
      - uses: jonfriesen/get-release-by-tag@v0.0.11
        id: get_release_by_tag
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
      # Delete the asset if it already exists (should only be useful for latest)
      - uses: mknejp/delete-release-assets@v1
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        with:
          token: ${{ github.token }}
          tag: ${{ github.ref }}
          assets: |
            Caprice32.dmg
            cap32-macos.zip
          fail-if-no-assets: false
          fail-if-no-release: false
      - uses: actions/upload-release-asset@v1.0.1
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.get_release_by_tag.outputs.upload_url }}
          asset_path: 'release/cap32-macos-bundle/Caprice32.dmg'
          asset_name: 'Caprice32.dmg'
          asset_content_type: 'com.apple.disk-image'
      - uses: actions/upload-release-asset@v1.0.1
        if: ${{ startsWith(github.ref, 'refs/tags/') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.get_release_by_tag.outputs.upload_url }}
          asset_path: 'release/cap32-macos.zip'
          asset_name: 'cap32-macos.zip'
          asset_content_type: 'application/zip'
