Documentation
name: release

on:
  push:
    branches:
    - release*
    tags:
    - 'v*'

env:
  # The NAME makes it easier to copy/paste snippets from other CI configs
  NAME: zbr

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
    - uses: actions/checkout@v4

    - id: release
      uses: ncipollo/release-action@v1
      if: ${{ startsWith(github.ref, 'refs/tags/v') }}
      with:
        artifactErrorsFailBuild: true
        body: "See [CHANGELOG.md](https://github.com/langston-barrett/${{ env.NAME }}/blob/main/CHANGELOG.md)."
        draft: true
        token: ${{ secrets.GITHUB_TOKEN }}

    - name: Publish to crates.io
      env:
        CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
        # Only push on actual release tags
        PUSH: ${{ startsWith(github.ref, 'refs/tags/v') }}
      run: |
        if [[ ${PUSH} == true ]]; then
          cargo publish --token ${CRATES_IO_TOKEN}
        else
          cargo publish --dry-run --token ${CRATES_IO_TOKEN}
        fi

  # Inspired by rustfmt:
  # https://github.com/rust-lang/rustfmt/blob/master/.github/workflows/upload-assets.yml
  artifacts:
    needs: release
    strategy:
      matrix:
        build: [linux-x86_64-gnu, linux-x86_64-musl, macos-x86_64]
        # build: [linux-x86_64-gnu, linux-x86_64-musl, macos-x86_64, windows-x86_64-gnu, windows-x86_64-msvc]
        include:
          - build: linux-x86_64-gnu
            os: ubuntu-latest
            rust: stable
            target: x86_64-unknown-linux-gnu
          - build: linux-x86_64-musl
            os: ubuntu-latest
            rust: stable
            target: x86_64-unknown-linux-musl
          # TODO(lb): Can these also be made stable?
          - build: macos-x86_64
            os: macos-latest
            rust: nightly
            target: x86_64-apple-darwin
          # TODO(lb): Possible?
          # - build: windows-x86_64-gnu
          #   os: windows-latest
          #   rust: nightly-x86_64-gnu
          #   target: x86_64-pc-windows-gnu
          # - build: windows-x86_64-msvc
          #   os: windows-latest
          #   rust: nightly-x86_64-msvc
          #   target: x86_64-pc-windows-msvc
    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v4

    - name: Install rustup
      shell: bash
      run: |
        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup-init.sh
        sh rustup-init.sh -y --default-toolchain none
        rustup target add ${{ matrix.target }}

    - name: Add mingw64 to path for x86_64-gnu
      run: echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH
      if: matrix.rust == 'nightly-x86_64-gnu'
      shell: bash

    - name: Deps
      if: matrix.target == 'x86_64-unknown-linux-musl'
      run: |
        sudo apt-get install -y musl-tools

    - name: Build executables
      shell: bash
      run: |
        cargo build \
          --bin ${{ env.NAME }} \
          --locked \
          --release \
          --target=${{ matrix.target }}

    - name: Upload binaries
      uses: ncipollo/release-action@v1
      if: ${{ startsWith(github.ref, 'refs/tags/v') }}
      with:
        allowUpdates: true
        artifactErrorsFailBuild: true
        replacesArtifacts: false
        artifacts: ${{ env.NAME }}_${{ matrix.target }}
        body: "See [CHANGELOG.md](https://github.com/langston-barrett/${{ env.NAME }}/blob/main/CHANGELOG.md)."
        draft: true
        token: ${{ secrets.GITHUB_TOKEN }}