name: build

on:
  push:
    tags: ['v*', 'test*']

jobs:
  build_macos:
    name: Build for MacOS
    runs-on: macos-13
    steps:
    - name: "Checkout"
      uses: actions/checkout@v2
      with:
        submodules: 'recursive'
    - name: "Setup Go"
      uses: actions/setup-go@v2
      with:
        go-version: '^1.17'
    - name: Cache Go Modules
      uses: actions/cache@v2
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-
    - name: "Build"
      run: make build_macos
    - name: Upload
      uses: actions/upload-artifact@v2
      with:
        name: macos
        path: _output/macos/yarr.app

  build_windows:
    name: Build for Windows
    runs-on: windows-2022
    steps:
    - name: "Checkout"
      uses: actions/checkout@v2
      with:
        submodules: 'recursive'
    - name: "Setup Go"
      uses: actions/setup-go@v2
      with:
        go-version: '^1.17'
    - name: Cache Go Modules
      uses: actions/cache@v2
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-
    - name: "Build"
      run: make build_windows
    - name: Upload
      uses: actions/upload-artifact@v2
      with:
        name: windows
        path: _output/windows/yarr.exe

  build_linux:
    name: Build for Linux
    runs-on: ubuntu-22.04
    steps:
    - name: "Checkout"
      uses: actions/checkout@v2
      with:
        submodules: 'recursive'
    - name: "Setup Go"
      uses: actions/setup-go@v2
      with:
        go-version: '^1.17'
    - name: Cache Go Modules
      uses: actions/cache@v2
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-
    - name: "Build"
      run: make build_linux
    - name: Upload
      uses: actions/upload-artifact@v2
      with:
        name: linux
        path: _output/linux/yarr

  create_release:
    name: Create Release
    runs-on: ubuntu-latest
    if: ${{ !contains(github.ref, 'test') }}
    needs: [build_macos, build_windows, build_linux]
    steps:
    - name: Create Release
      uses: actions/create-release@v1
      id: create_release
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          draft: true
          prerelease: true
    - name: Download Artifacts
      uses: actions/download-artifact@v2
      with:
        path: .
    - name: Preparation
      run: |
        ls -R
        chmod u+x macos/Contents/MacOS/yarr
        chmod u+x linux/yarr

        mv macos yarr.app     && zip -r yarr-macos.zip yarr.app
        mv windows/yarr.exe . && zip yarr-windows.zip yarr.exe
        mv linux/yarr .       && zip yarr-linux.zip yarr
    - name: Upload MacOS
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./yarr-macos.zip
          asset_name: yarr-${{ github.ref }}-macos64.zip
          asset_content_type: application/zip
    - name: Upload Windows
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./yarr-windows.zip
          asset_name: yarr-${{ github.ref }}-windows64.zip
          asset_content_type: application/zip
    - name: Upload Linux
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./yarr-linux.zip
          asset_name: yarr-${{ github.ref }}-linux64.zip
          asset_content_type: application/zip
