node-cleaner 0.2.24

A tool for cleaning and managing node_modules directories
Documentation
name: Release

permissions:
  contents: write

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:  # For manual triggering

jobs:
  release:
    runs-on: ubuntu-latest
    env:
      BINARY_NAME: node-cleaner

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Important for GoReleaser to fetch all tags

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y jq

      - name: Add build targets
        run: |
          rustup target add x86_64-unknown-linux-gnu
          rustup target add x86_64-unknown-linux-musl
          rustup target add aarch64-unknown-linux-gnu
          rustup target add aarch64-unknown-linux-musl

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

      # Install cross-compilation tools
      - name: Install cross-compilation dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu musl-tools

      # Install Zig for cross-compilation
      - name: Install Zig
        run: |
          wget https://ziglang.org/download/0.10.0/zig-linux-x86_64-0.10.0.tar.xz
          tar -xf zig-linux-x86_64-0.10.0.tar.xz
          sudo mv zig-linux-x86_64-0.10.0 /opt/zig
          sudo ln -s /opt/zig/zig /usr/local/bin/zig
          rm -rf zig-linux-x86_64-0.10.0.tar.xz
          zig version

      - name: Install cargo-zigbuild
        run: cargo install cargo-zigbuild

      # Build static binaries
      - name: Build static binaries
        run: |
          # x86_64 static binary
          cargo zigbuild --release --target x86_64-unknown-linux-musl
          
          # aarch64 static binary
          cargo zigbuild --release --target aarch64-unknown-linux-musl
          
          # List built binaries for debugging
          find target/ -name "*" -type f -executable
          ls -la target/x86_64-unknown-linux-musl/release/
          ls -la target/aarch64-unknown-linux-musl/release/

      # Create directories for packaging
      - name: Prepare packaging directories
        run: |
          mkdir -p dist/bin
          mkdir -p dist/deb/DEBIAN
          mkdir -p dist/deb/usr/bin
          mkdir -p dist/rpm
          
          # Copy binaries
          cp target/x86_64-unknown-linux-musl/release/$BINARY_NAME dist/bin/$BINARY_NAME-x86_64-linux
          cp target/aarch64-unknown-linux-musl/release/$BINARY_NAME dist/bin/$BINARY_NAME-aarch64-linux
          
          # Make binaries executable
          chmod +x dist/bin/*

      # Create Debian package
      - name: Create Debian package
        run: |
          # Get version from tag or use Cargo.toml version
          if [[ $GITHUB_REF == refs/tags/v* ]]; then
            VERSION=${GITHUB_REF#refs/tags/v}
          else
            VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
          fi
          
          # Extract package info from Cargo.toml
          DESCRIPTION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].description')
          AUTHORS=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].authors[0]')
          REPOSITORY=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].repository')
          
          echo "Building Debian package for version: $VERSION"
          
          # Copy binary for deb package
          cp target/x86_64-unknown-linux-musl/release/${BINARY_NAME} dist/deb/usr/bin/
          
          # Create Debian control file
          cat > dist/deb/DEBIAN/control << EOF
          Package: ${BINARY_NAME}
          Version: ${VERSION}
          Section: utils
          Priority: optional
          Architecture: amd64
          Maintainer: ${AUTHORS} <noreply@github.com>
          Description: ${DESCRIPTION}
           ${DESCRIPTION}.
           This utility helps developers clean up disk space by finding and
           removing unused node_modules directories.
          EOF
          
          # Build the deb package
          dpkg-deb --build dist/deb dist/${BINARY_NAME}_${VERSION}_amd64.deb

      # Create RPM package
      - name: Create RPM package
        run: |
          # Install rpm tools
          sudo apt-get install -y rpm
          
          # Get version from tag or use Cargo.toml version
          if [[ $GITHUB_REF == refs/tags/v* ]]; then
            VERSION=${GITHUB_REF#refs/tags/v}
          else
            VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
          fi
          
          # Extract package info from Cargo.toml
          DESCRIPTION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].description')
          AUTHORS=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].authors[0]')
          REPOSITORY=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].repository')
          LICENSE=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].license')
          
          echo "Building RPM package for version: $VERSION"
          
          # Create RPM spec file
          mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
          
          cat > ~/rpmbuild/SPECS/${BINARY_NAME}.spec << EOF
          Name:           ${BINARY_NAME}
          Version:        ${VERSION}
          Release:        1%{?dist}
          Summary:        ${DESCRIPTION}
          
          License:        ${LICENSE}
          URL:            ${REPOSITORY}
          Source0:        %{name}-%{version}.tar.gz
          
          %description
          ${DESCRIPTION}.
          This utility helps developers clean up disk space by finding and
          removing unused node_modules directories.
          
          %prep
          
          %build
          
          %install
          mkdir -p %{buildroot}/usr/bin
          cp $GITHUB_WORKSPACE/target/x86_64-unknown-linux-musl/release/${BINARY_NAME} %{buildroot}/usr/bin/
          
          %files
          /usr/bin/${BINARY_NAME}
          
          %changelog
          * $(date +'%a %b %d %Y') ${AUTHORS} <noreply@github.com> - ${VERSION}-1
          - Automated build from version ${VERSION}
          EOF
          
          # Build RPM
          rpmbuild -bb ~/rpmbuild/SPECS/${BINARY_NAME}.spec
          cp ~/rpmbuild/RPMS/x86_64/${BINARY_NAME}-${VERSION}-1.*.rpm dist/

      # Upload artifacts and create release
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            dist/*.deb
            dist/*.rpm
            dist/bin/*
          generate_release_notes: true
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      # Publish to crates.io
      - name: Publish to crates.io
        if: startsWith(github.ref, 'refs/tags/v')
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}