name: Rust

on: [push, pull_request]

jobs:
  build:
    name: Test rust ${{ matrix.rust }} on ${{ matrix.os }}

    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest, 'windows-latest']
        rust: [stable]

    steps:
    - uses: hecrj/setup-rust-action@v1
      with:
        rust-version: ${{ matrix.rust }}
    - uses: actions/checkout@v1
    - name: Build release
      run: cargo build --verbose --release
    - name: Upload unix binary
      uses: actions/upload-artifact@v1
      if: matrix.os != 'windows-latest'
      with:
        name: copycat-${{ matrix.os }}
        path: target/release/copycat
    - name: Upload windows binary
      uses: actions/upload-artifact@v1
      if: matrix.os == 'windows-latest'
      with:
        name: copycat-${{ matrix.os }}
        path: target/release/copycat.exe

  release:
    name: Release
    needs: build
    if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags/v')
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v1
    - uses: actions/download-artifact@v1
      with:
        name: copycat-ubuntu-latest
        path: dist/copycat-ubuntu-latest
    - uses: actions/download-artifact@v1
      with:
        name: copycat-macOS-latest
        path: dist/copycat-macOS-latest
    - uses: actions/download-artifact@v1
      with:
        name: copycat-windows-latest
        path: dist/copycat-windows-latest
    - name: List release files
      run: find dist/
    - name: Create release
      id: create_release
      uses: actions/create-release@v1.0.0
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}
        draft: false
        prerelease: false
    - name: Upload linux release asset
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 
        asset_path: ./dist/copycat-ubuntu-latest/copycat
        asset_name: copycat-linux-x86_64
        asset_content_type: application/x-elf
    - name: Upload macos release asset
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 
        asset_path: ./dist/copycat-macOS-latest/copycat
        asset_name: copycat-darwin-x86_64
        asset_content_type: application/x-mach-binary
    - name: Upload windows release asset
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 
        asset_path: ./dist/copycat-windows-latest/copycat.exe
        asset_name: copycat-windows-x86_64.exe
        asset_content_type: application/vnd.microsoft.portable-executable
