name: Release
on:
  push:
    tags:
      - "*"

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        target:
          - x86_64-unknown-linux-gnu
          - x86_64-apple-darwin
          - x86_64-pc-windows-msvc
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            name: rumage-x86_64-unknown-linux-gnu.tar.gz
          - target: x86_64-apple-darwin
            os: macos-latest
            name: rumage-x86_64-apple-darwin.tar.gz
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            name: rumage-x86_64-pc-windows-msvc.zip
    runs-on: ${{ matrix.os }}
    steps:
      - name: Setup | Checkout
        uses: actions/checkout@v2

      - name: Setup | Cache Cargo
        uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Setup | Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          profile: minimal
          target: ${{ matrix.target }}

      - name: Build | Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Post Setup | Prepare artifacts [Windows]
        if: matrix.os == 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          strip rumage.exe
          7z a ../../../${{ matrix.name }} rumage.exe
          cd -
      - name: Post Setup | Prepare artifacts [-nix]
        if: matrix.os != 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          strip rumage
          tar czvf ../../../${{ matrix.name }} rumage
          cd -
      - name: Post Setup | Upload artifacts
        uses: actions/upload-artifact@v2
        with:
          name: ${{ matrix.name }}
          path: ${{ matrix.name }}

  publish:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Setup | Checkout
        uses: actions/checkout@v2

      - name: Setup | Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          override: true

      - name: Build | Publish
        run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}

  release:
    needs: publish
    runs-on: ubuntu-latest
    steps:
      - name: Setup | Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Setup | Artifacts
        uses: actions/download-artifact@v2

      - name: Setup | Checksums
        run: for file in rumage-*/rumage-*; do openssl dgst -sha256 -r "$file" | awk '{print $1}' > "${file}.sha256"; done

      - name: Build | Publish Pre-Release
        uses: softprops/action-gh-release@v1
        with:
          generate_release_notes: true
          files: rumage-*/rumage-*
          draft: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
