name: Release Binary Assets

permissions:
  contents: write

on:
  release:
    types:
      - published
jobs:
  release:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
            binName: cargo-generate
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            cross: true
            binName: cargo-generate
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
            binName: cargo-generate
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            cross: true
            binName: cargo-generate
          - target: x86_64-apple-darwin
            os: macos-latest
            cross: false
            binName: cargo-generate
          - target: aarch64-apple-darwin
            os: macos-latest
            cross: true
            binName: cargo-generate
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            cross: false
            binName: cargo-generate.exe
    steps:
      - uses: actions/checkout@v6
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          target: ${{ matrix.target }}
          override: true
      - uses: mozilla-actions/sccache-action@v0.0.10
      - name: Build
        # todo: get rid of the deprecated actions-rs stuff..
        uses: actions-rs/cargo@v1
        with:
          command: build
          use-cross: ${{ matrix.cross }}
          args: --features vendored-openssl --release --target=${{ matrix.target }}
      # - name: Smoke Test
      #   uses: actions-rs/cargo@v1
      #   with:
      #     command: run
      #     use-cross: ${{ matrix.cross }}
      #     args: --features vendored-openssl --release --target=${{ matrix.target }} -- --version
      - name: Install cargo-deb and create a deb package
        if: matrix.os == 'ubuntu-latest'
        run: |
          cargo install cargo-deb
          cargo build --features vendored-openssl --release
          cargo deb --no-build
      - name: Upload the deb to release files
        if: matrix.os == 'ubuntu-latest'
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: target/debian/*
          tag: ${{ github.ref }}
          file_glob: true
          overwrite: true
      - name: Create Archive
        id: archive
        shell: bash
        env:
          TARGET: ${{ matrix.target }}
          TAG: ${{ github.event.release.tag_name }}
        run: |
          filename="cargo-generate-$TAG-$TARGET.tar.gz"
          tar -czvf "$filename" README.md LICENSE-MIT LICENSE-APACHE -C "target/$TARGET/release" "${{ matrix.binName }}"
          echo "::set-output name=filename::$filename"
      - name: Upload Archive
        uses: ncipollo/release-action@v1.20.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          allowUpdates: true
          artifactErrorsFailBuild: true
          artifacts: ${{ steps.archive.outputs.filename }}
          artifactContentType: application/octet-stream
          omitBodyDuringUpdate: true
          omitNameDuringUpdate: true
          omitPrereleaseDuringUpdate: true
