name: rust

env:
  create_github_release: ''
  ubuntu_extension: ''
  macos_extension: ''
  windows_extension: '.exe'
  ubuntu_content_type: 'application/x-elf'
  macos_content_type: 'application/x-mach-binary'
  windows_content_type: 'application/vnd.microsoft.portable-executable'
  ubuntu_install_dependencies: 'sudo apt-get install libxcb-shape0-dev libxcb-xfixes0-dev'
  macos_install_dependencies: ''
  windows_install_dependencies: ''
  ubuntu_channel: linux
  macos_channel: mac
  windows_channel: windows

on: [push, pull_request]
jobs:
  build:
    name: rust ${{ matrix.rust }} on ${{ matrix.os }} version
    #if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags/v')
    runs-on: ${{ matrix.os }}-latest

    strategy:
      matrix:
        os: [ubuntu, macos, windows]
        rust: [stable]

    steps:
    - uses: hecrj/setup-rust-action@v1
      with:
        rust-version: ${{ matrix.rust }}
    - uses: actions/checkout@v2
    - name: Get crate metadata
      id: crate-metadata
      run: echo "::set-output name=metadata::$(cargo metadata --no-deps --format-version 1)"
    - name: Get crate name
      id: crate-name
      run: echo "::set-output name=name::${{ fromJSON(steps.crate-metadata.outputs.metadata).packages[0].name }}"
    - name: Installing dependencies
      if: env[format('{0}_install_dependencies', matrix.os)] != ''
      run: ${{ env[format('{0}_install_dependencies', matrix.os)] }}
    - name: Run tests
      run: cargo test --verbose
    - name: Build release
      run: cargo build --verbose --release
    - name: Upload ${{ matrix.os }} binary
      uses: actions/upload-artifact@v1
      with:
        name: ${{ steps.crate-name.outputs.name }}-${{ env[format('{0}_channel', matrix.os)] }}
        path: target/release/${{ steps.crate-name.outputs.name }}${{ env[format('{0}_extension', matrix.os)] }}

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

    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Get crate metadata
      id: crate-metadata
      run: echo "::set-output name=metadata::$(cargo metadata --no-deps --format-version 1)"
    - name: Get crate name
      id: crate-name
      run: echo "::set-output name=name::${{ fromJSON(steps.crate-metadata.outputs.metadata).packages[0].name }}"
    - uses: actions/download-artifact@v2
      with:
        name: ${{ steps.crate-name.outputs.name }}-${{ env[format('{0}_channel', matrix.os)] }}
        path: dist/${{ matrix.os }}
    - name: List release files
      run: find dist/
    - name: Create release
      if: env.create_github_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 ${{ matrix.os }} release asset
      if: env.create_github_release != ''
      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/${{ matrix.os }}/${{ steps.crate-name.outputs.name }}${{ env[format('{0}_extension', matrix.os)] }}
        asset_name: ${{ steps.crate-name.outputs.name }}-${{ env[format('{0}_channel', matrix.os)] }}${{ env[format('{0}_extension', matrix.os)] }}
        asset_content_type: ${{ env[format('{0}_content_type', matrix.os)] }}
    - name: Rename ${{ matrix.os }} executable before upload
      run: mv dist/${{ matrix.os }}/${{ steps.crate-name.outputs.name }}${{ env[format('{0}_extension', matrix.os)] }} dist/${{ steps.crate-name.outputs.name }}-${{ env[format('{0}_channel', matrix.os)] }}${{ env[format('{0}_extension', matrix.os)] }}
    - name: Check Butler credentials
      run: echo 'checking butler credentials...'
      env:
        BUTLER_APIKEY: ${{ secrets.BUTLER_APIKEY }}
    - name: Upload ${{ matrix.os }} to itch
      if: env.BUTLER_APIKEY != ''
      uses: josephbmanley/butler-publish-itchio-action@master
      env:
        BUTLER_CREDENTIALS: ${{ env.BUTLER_APIKEY }}
        CHANNEL: ${{ env[format('{0}_channel', matrix.os)] }}
        ITCH_GAME: ${{ steps.crate-name.outputs.name }}
        ITCH_USER: matheuslessarodrigues
        PACKAGE: dist/${{ steps.crate-name.outputs.name }}-${{ env[format('{0}_channel', matrix.os)] }}${{ env[format('{0}_extension', matrix.os)] }}
        VERSION: ${{ fromJSON(steps.crate-metadata.outputs.metadata).packages[0].version }}
