lx-ls 0.7.0

The file lister with personality! 🌟
name: Release
on:
  push:
    tags: ["v*"]

jobs:
  build:
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            cross: true
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: Install cross-compilation tools
        if: matrix.cross
        run: |
          sudo dpkg --add-architecture arm64
          # Pin existing sources to amd64 only (handles both .list and .sources formats).
          for f in /etc/apt/sources.list.d/*.sources; do
            [ -f "$f" ] && grep -q '^Architectures:' "$f" || sudo sed -i '/^Types:/a Architectures: amd64' "$f" || true
          done
          for f in /etc/apt/sources.list.d/*.list; do
            [ -f "$f" ] && sudo sed -i '/^deb \(http\|https\)/s|^deb |deb [arch=amd64] |' "$f" || true
          done
          # Add arm64 ports repository.
          echo 'deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports noble main restricted universe' | sudo tee /etc/apt/sources.list.d/arm64.list
          echo 'deb [arch=arm64] http://ports.ubuntu.com/ubuntu-ports noble-updates main restricted universe' | sudo tee -a /etc/apt/sources.list.d/arm64.list
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu zlib1g-dev:arm64
      - name: Set cross-linker
        if: matrix.cross
        run: |
          mkdir -p .cargo
          echo '[target.aarch64-unknown-linux-gnu]' >> .cargo/config.toml
          echo 'linker = "aarch64-linux-gnu-gcc"' >> .cargo/config.toml
      - run: cargo build --release --features jj --target ${{ matrix.target }}
      - name: Rename binary with target triple
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/lx dist/lx-${{ matrix.target }}
      - uses: actions/upload-artifact@v4
        with:
          name: lx-${{ matrix.target }}
          path: dist/lx-${{ matrix.target }}

  man-pages:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install pandoc
        run: sudo apt-get update && sudo apt-get install -y pandoc
      - name: Build man pages
        run: |
          pandoc man/lx.1.md -s -t man -o lx.1
          pandoc man/lxconfig.toml.5.md -s -t man -o lxconfig.toml.5
      - uses: actions/upload-artifact@v4
        with:
          name: man-pages
          path: |
            lx.1
            lxconfig.toml.5

  release:
    needs: [build, man-pages]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          merge-multiple: true
      - uses: softprops/action-gh-release@v2
        with:
          files: |
            lx-*
            lx.1
            lxconfig.toml.5
          generate_release_notes: true

  publish-crate:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_LX_LS }}

  update-homebrew:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          repository: wjv/homebrew-tap
          token: ${{ secrets.HOMEBREW_LX_LS }}
      - name: Update formula
        env:
          TAG: ${{ github.ref_name }}
        run: |
          VERSION="${TAG#v}"
          BASE="https://github.com/wjv/lx/releases/download/${TAG}"

          # Fetch checksums for release assets.
          for target in x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu x86_64-apple-darwin aarch64-apple-darwin; do
            sha=$(curl -sL "${BASE}/lx-${target}" | sha256sum | cut -d' ' -f1)
            eval "SHA_$(echo "$target" | tr '-' '_')=$sha"
          done
          SHA_MAN1=$(curl -sL "${BASE}/lx.1" | sha256sum | cut -d' ' -f1)
          SHA_MAN5=$(curl -sL "${BASE}/lxconfig.toml.5" | sha256sum | cut -d' ' -f1)

          cat > Formula/lx.rb <<RUBY
          class Lx < Formula
            desc "A modern file lister with personality"
            homepage "https://github.com/wjv/lx"
            version "${VERSION}"
            license "MIT"

            on_macos do
              if Hardware::CPU.arm?
                url "${BASE}/lx-aarch64-apple-darwin"
                sha256 "${SHA_aarch64_apple_darwin}"
              else
                url "${BASE}/lx-x86_64-apple-darwin"
                sha256 "${SHA_x86_64_apple_darwin}"
              end
            end

            on_linux do
              if Hardware::CPU.arm?
                url "${BASE}/lx-aarch64-unknown-linux-gnu"
                sha256 "${SHA_aarch64_unknown_linux_gnu}"
              else
                url "${BASE}/lx-x86_64-unknown-linux-gnu"
                sha256 "${SHA_x86_64_unknown_linux_gnu}"
              end
            end

            resource "man-lx" do
              url "${BASE}/lx.1"
              sha256 "${SHA_MAN1}"
            end

            resource "man-lxconfig" do
              url "${BASE}/lxconfig.toml.5"
              sha256 "${SHA_MAN5}"
            end

            def install
              bin.install Dir["lx-*"].first => "lx"
              man1.install resource("man-lx") => "lx.1"
              man5.install resource("man-lxconfig") => "lxconfig.toml.5"
            end

            test do
              assert_match "lx #{version}", shell_output("#{bin}/lx --version")
            end
          end
          RUBY

          # Remove leading whitespace from heredoc.
          sed -i 's/^          //' Formula/lx.rb
      - name: Commit and push
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/lx.rb
          git commit -m "Update lx to ${TAG#v}"
          git push