name: Continuous build and release

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

permissions:
  contents: write

jobs:
  test:
    name: Continuous Integration
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Install dependencies from Ubuntu sources
        run: sudo apt-get install cc65

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 'ruby'

      - name: Lint scripts on bin/
        run: |
            gem install rubocop
            rubocop bin/ --format github

      - name: Main task
        run: V=1 make release

      - name: Clean old release assets
        run: gh release delete latest --yes --cleanup-tag || true
        env:
            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Update releases page
        uses: softprops/action-gh-release@v2
        with:
          tag_name: latest
          name: Continuous Build
          files: out/*.nes
          overwrite_files: true
          body: |
            This release was automatically generated from commit ${{github.sha}}.
          draft: false
          prerelease: true
          make_latest: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
