name: Build binary artifacts

on:
  push:
    tags:
      - weekly.**
      - 0.**

jobs:

  build-linux:
    runs-on: ubuntu-20.04
    env:
      CC: gcc
      ZIPNAME: v_linux.zip
    steps:
      - uses: actions/checkout@v1
      - name: Compile
        run: |
          make -j4
          ./v -cc $CC -prod -o v cmd/v
          ./v -cc $CC -prod cmd/tools/vup.v
          ./v -cc $CC -prod cmd/tools/vdoctor.v
      - name: Remove excluded
        run: |
           rm -rf .git
           rm -rf vc/
           rm -rf v_old
      - name: Create ZIP archive
        run: |
           cd ..
           zip -r9 --symlinks $ZIPNAME v/
           mv $ZIPNAME v/
           cd v/
      - name: Create artifact
        uses: actions/upload-artifact@v2
        with:
          name: linux
          path: v_linux.zip

  build-macos:
    runs-on: macos-latest
    env:
      CC: clang
      ZIPNAME: v_macos.zip
    steps:
      - uses: actions/checkout@v1
      - name: Compile
        run: |
          make -j4
          ./v -cc $CC -prod -o v cmd/v
          ./v -cc $CC -prod cmd/tools/vup.v
          ./v -cc $CC -prod cmd/tools/vdoctor.v
      - name: Remove excluded
        run: |
           rm -rf .git
           rm -rf vc/
           rm -rf v_old
      - name: Create ZIP archive
        run: |
           cd ..
           zip -r9 --symlinks $ZIPNAME v/
           mv $ZIPNAME v/
           cd v/
      - name: Create artifact
        uses: actions/upload-artifact@v2
        with:
          name: macos
          path: v_macos.zip

  build-windows:
    runs-on: windows-latest
    env:
      CC: msvc
      ZIPNAME: v_windows.zip
    steps:
      - uses: actions/checkout@v1
      - uses: msys2/setup-msys2@v2
      - name: Compile
        run: |
          .\make.bat -tcc
          .\v.exe cmd\tools\vup.v
          .\v.exe cmd\tools\vdoctor.v
      - name: Remove excluded
        shell: msys2 {0}
        run: |
           rm -rf .git
           rm -rf vc/
           rm -rf v_old.exe
      - name: Create archive
        shell: msys2 {0}
        run: |
           cd ..
           powershell Compress-Archive v $ZIPNAME
           mv $ZIPNAME v/
           cd v/
# NB: the powershell Compress-Archive line is from:
# https://superuser.com/a/1336434/194881
# It is needed, because `zip` is not installed by default :-|
      - name: Create artifact
        uses: actions/upload-artifact@v2
        with:
          name: windows
          path: v_windows.zip

  release:
    name: Create Github Release
    needs: [build-linux, build-windows, build-macos]
    runs-on: ubuntu-20.04
    steps:
      - name: Get short tag name
        uses: jungwinter/split@v1
        id: split
        with:
          msg: ${{ github.ref }}
          seperator: /
      - name: Create Release
        id: create_release
        uses: ncipollo/release-action@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ steps.split.outputs._2 }}
          name: ${{ steps.split.outputs._2 }}
          commit: ${{ github.sha }}
          draft: false
          prerelease: false

  publish:
    needs: [release]
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        version: [linux, macos, windows]
    steps:
      - uses: actions/checkout@v1
      - name: Fetch artifacts
        uses: actions/download-artifact@v1
        with:
          name: ${{ matrix.version }}
          path: ./${{ matrix.version }}
      - name: Get short tag name
        uses: jungwinter/split@v1
        id: split
        with:
          msg: ${{ github.ref }}
          seperator: /
      - name: Get release
        id: get_release_info
        uses: leahlundqvist/get-release@v1.3.1
        env:
          GITHUB_TOKEN: ${{ github.token }}
        with:
          tag_name: ${{ steps.split.outputs._2 }}
      - name: Upload Release Asset
        id: upload-release-asset
        uses: actions/upload-release-asset@v1.0.1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.get_release_info.outputs.upload_url }}
          asset_path: ${{ matrix.version }}/v_${{ matrix.version }}.zip
          asset_name: v_${{ matrix.version }}.zip
          asset_content_type: application/zip
