vmonitor 0.0.1-alpha.2

A simple and lightweight system monitor
name: Release CI/CD

on:
  push:
    branches: [ main, master ]
    tags:
      - 'v*'
  pull_request:
    branches: [ main, master ]

env:
  CARGO_TERM_COLOR: always

jobs:
  # Check code formatting and style
  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - name: Rustfmt Check
        run: cargo fmt --all -- --check
      - name: Clippy Check
        run: cargo clippy -- -D warnings

  # Tested on multiple platforms
  test:
    name: Test
    needs: lint
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        rust: [stable]
    steps:
      - uses: actions/checkout@v4
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.rust }}
      - name: Cargo Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Run Tests
        run: cargo test --all-features

  # Build binary files for different platforms.
  build:
    name: Build
    needs: test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            suffix: ''
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            suffix: '.exe'
          - os: macos-latest
            target: x86_64-apple-darwin
            suffix: ''
    steps:
      - uses: actions/checkout@v4
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Build Release Binary
        run: cargo build --verbose --release
      - name: Upload Build Artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ github.event.repository.name }}-${{ matrix.target }}
          path: target/release/${{ github.event.repository.name }}${{ matrix.suffix }}

  # Post to crates.io (only when tagged)
  publish:
    name: Publish to crates.io
    needs: build
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - uses: actions/checkout@v4
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Cargo Login
        run: cargo login ${{ secrets.CRATES_IO_TOKEN }}
      - name: Cargo Publish
        run: cargo publish

  # Publish GitHub Release (only when tagging)
  github_release:
    name: Create GitHub Release
    needs: build
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - name: Download Linux Build
        uses: actions/download-artifact@v4
        with:
          name: ${{ github.event.repository.name }}-x86_64-unknown-linux-gnu
          path: ./linux
      - name: Download Windows Build
        uses: actions/download-artifact@v4
        with:
          name: ${{ github.event.repository.name }}-x86_64-pc-windows-msvc
          path: ./windows
      - name: Download macOS Build
        uses: actions/download-artifact@v4
        with:
          name: ${{ github.event.repository.name }}-x86_64-apple-darwin
          path: ./macos
      - name: Prepare Assets
        run: |
          chmod +x ./linux/${{ github.event.repository.name }}
          chmod +x ./macos/${{ github.event.repository.name }}
          zip -j ${{ github.event.repository.name }}-linux.zip ./linux/${{ github.event.repository.name }}
          zip -j ${{ github.event.repository.name }}-windows.zip ./windows/${{ github.event.repository.name }}.exe
          zip -j ${{ github.event.repository.name }}-macos.zip ./macos/${{ github.event.repository.name }}
      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            ${{ github.event.repository.name }}-linux.zip
            ${{ github.event.repository.name }}-windows.zip
            ${{ github.event.repository.name }}-macos.zip
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}