Skip to content

Commit

Permalink
feat(split): Split code to publish on crates.io x-win package to make…
Browse files Browse the repository at this point in the history
… usable on rust project
  • Loading branch information
miniben-90 committed Aug 5, 2024
1 parent d49d350 commit 9b403a1
Show file tree
Hide file tree
Showing 47 changed files with 1,128 additions and 179 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/CI.yml → .github/workflows/ci-napi.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build & Test
name: Build & Test & publich node package "@miniben90/x-win"

env:
DEBUG: napi:*
Expand Down Expand Up @@ -149,6 +149,14 @@ jobs:
if: ${{ !matrix.settings.docker }}
run: yarn install

# Lint Rust code
- name: Ceheck lint Rust
run: cargo fmt -- --check

# Check clippy code
- name: Check clippy code
run: cargo clippy -- -D warnings

# Build the project
- name: Build
if: ${{ !matrix.settings.docker }}
Expand Down
150 changes: 150 additions & 0 deletions .github/workflows/ci-rs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
name: Build & Test & publich Rust package "x-win"

env:
APP_NAME: x-win
MACOSX_DEPLOYMENT_TARGET: '10.13'

on:
push:
branches:
- main
tags-ignore:
- '**'
paths-ignore:
- '**/*.md'
- LICENSE
- '**/*.gitignore'
- .editorconfig
- docs/**
pull_request: null
release:
types: [published]

jobs:
build-n-test:
defaults:
run:
working-directory: ./x-win-rs
strategy:
fail-fast: false
matrix:
settings:
# Build matrix for MacOS (x64)
- host: macos-latest
target: x86_64-apple-darwin

# Build matrix for MacOS (arm64)
- host: macos-latest
target: aarch64-apple-darwin

# Build matrix for Windows (x64)
- host: windows-latest
target: x86_64-pc-windows-msvc

# Build matrix for Windows (x32)
- host: windows-latest
target: i686-pc-windows-msvc
architecture: 'x86'

# Build matrix for Windows (arm64)
- host: windows-latest
target: aarch64-pc-windows-msvc

# Build for linux (using Ubuntu) (x64)
- host: ubuntu-latest
target: x86_64-unknown-linux-gnu

# Build for linux (using Ubuntu) (x64)
- host: ubuntu-latest
target: x86_64-unknown-linux-musl

name: stable - ${{ matrix.settings.target }}
runs-on: ${{ matrix.settings.host }}
steps:
# Add actions/checkout
- uses: actions/checkout@v4

# Install lib dev required for compilation of the project
- name: (Linux) Install libx11 & libxcb for building
if: ${{ matrix.settings.host == 'ubuntu-latest' }}
run: |
sudo apt-get update
sudo apt-get install -y libx11-dev libxcb-ewmh-dev libxcb-randr0-dev
# Install Rust version
- name: Install Rust
if: ${{ !matrix.settings.docker }}
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: ${{ matrix.settings.target }}

# Set Cache cargo
- name: Cache cargo
uses: actions/cache@v3
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
.cargo-cache
target/
key: ${{ matrix.settings.target }}-cargo-${{ matrix.settings.host }}

# Lint Rust code
- name: Lint
run: cargo fmt -- --check

# Check clippy code
- name: Clippy
run: cargo clippy -- -D warnings

- name: Build
run: cargo build --release --target ${{ matrix.settings.target }}

- name: Open finder to have an active window (MacOS)
if: ${{ matrix.settings.host == 'macos-latest' }}
run: open .

- name: Open explorer to have an active window (windows)
if: ${{ matrix.settings.host == 'windows-latest' }}
run: explorer .

- name: Test
if: ${{ matrix.settings.host != 'ubuntu-latest' }}
run: cargo test

# Upload artifact for the next steps (macos and test)
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: build-${{ matrix.settings.target }}
path: target
if-no-files-found: error



publish:
defaults:
run:
working-directory: ./x-win-rs
if: ${{ github.event_name == 'release' && github.ref_type == 'tag' }}
name: Publish ${{ github.ref_name }} (${{ github.ref }})
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
TAG: ${{ github.ref_name }}
needs:
- build-n-test
steps:
- uses: actions/checkout@v4

# Install Rust version
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable

- name: Publish package
run: cargo publish
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cd x-win-rs && cargo fmt -- --check && cargo clippy -- -D warnings && cargo test
cargo fmt -- --check
cargo clippy -- -D warnings
yarn test
50 changes: 8 additions & 42 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "x-win"
version = "1.6.0"
name = "x-win-napi"
version = "0.0.0"
authors = ["BENKHADRA Hocine <[email protected]>"]
keywords = ["window", "active", "current", "position", "title", "list", "open"]
description = "This package allows you to retrieve precise information about active and open windows on Windows, MacOS, and Linux. You can obtain the position, size, title, and other memory of windows."
Expand All @@ -12,48 +12,14 @@ crate-type = ["cdylib"]

[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.16.8", default-features = false, features = ["napi4", "async"] }
napi-derive = "2.16.8"
napi = { version = "2.16.8", default-features = false, features = [
"napi4",
"async",
] }
napi-derive = "2.16.9"
once_cell = "1.19.0"
base64 = "0.22.1"

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.58.0", features = [
"Win32_Foundation",
"Win32_UI_WindowsAndMessaging",
"Win32_System_Threading",
"Win32_Storage_FileSystem",
"Win32_System_ProcessStatus",
"Win32_System_StationsAndDesktops",
"Win32_UI_Input_KeyboardAndMouse",
"Win32_Graphics_Dwm",
"UI_UIAutomation",
"Win32_System_Com",
"Win32_UI_Accessibility",
"Win32_System_Ole",
"Win32_UI_Shell_PropertiesSystem",
"Win32_UI_Shell_Common",
"Win32_System_Variant",
"Win32_Graphics_Gdi",
"Win32_Graphics_Imaging"
]}
png = "0.17.13"

[target.'cfg(target_os = "linux")'.dependencies]
xcb = { version = "1.4.0" }
x11 = { version = "2.21.0", features = ["xlib"], optional = true }
zbus = { version = "1.9.2" }
serde_json = { version = "1.0.117" }
image = "0.25.1"

[target.'cfg(target_os = "macos")'.dependencies]
cocoa = "0.25.0"
libc = "0.2.155"
cocoa-foundation = "0.1.2"
core-foundation = "0.9.4"
core-foundation-sys = "0.8.6"
core-graphics = "0.23.2"
objc = "0.2.7"
x-win = { path = "./x-win-rs" }

[build-dependencies]
napi-build = "2.1.3"
Expand Down
2 changes: 0 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
MIT License

Copyright (c) 2023 BENKHADRA Hocine

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @miniben90/x-win

[![CI](https://github.com/miniben-90/x-win/actions/workflows/CI.yml/badge.svg)](https://github.com/miniben-90/x-win/actions/workflows/CI.yml)
[![ci-rs](https://github.com/miniben-90/x-win/actions/workflows/ci-rs.yml/badge.svg)](https://github.com/miniben-90/x-win/actions/workflows/ci-rs.yml)
[![ci-napi](https://github.com/miniben-90/x-win/actions/workflows/ci-napi.yml/badge.svg)](https://github.com/miniben-90/x-win/actions/workflows/ci-napi.yml)
[![Node version](https://img.shields.io/node/v/@miniben90/x-win.svg)](https://www.npmjs.com/package/@miniben90/x-win)
![npm type definitions](https://img.shields.io/npm/types/@miniben90/x-win)
![NPM License](https://img.shields.io/npm/l/@miniben90/x-win)
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@
],
"devDependencies": {
"@napi-rs/cli": "^2.18.3",
"ava": "^6.1.3"
"ava": "^6.1.3",
"husky": "^9.1.4"
},
"ava": {
"timeout": "3m",
"failFast": true,
"files": ["__test__/**/*.mjs"]
"files": [
"__test__/**/*.mjs"
]
},
"engines": {
"node": ">= 14"
Expand All @@ -59,7 +62,8 @@
"prepublishOnly": "napi prepublish -t npm",
"test": "ava",
"universal": "napi universal",
"version": "napi version"
"version": "napi version",
"prepare": "husky"
},
"packageManager": "[email protected]"
}
3 changes: 1 addition & 2 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![deny(unused_imports)]

pub mod thread;
pub mod x_win_struct;
pub mod api;
pub mod thread;
6 changes: 4 additions & 2 deletions src/common/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl ThreadManager {
let sender_ = sender.clone();

let handle = thread::spawn(move || {
work(receiver);
work(receiver);
});
threads_clone.lock().unwrap().insert(key, sender_);
let threads_clone_for_cleanup = Arc::clone(&self.threads);
Expand Down Expand Up @@ -63,7 +63,9 @@ impl ThreadManager {
pub fn stop_all_threads(&self) -> Result<(), String> {
let threads = self.threads.lock().unwrap();
for (_, sender) in threads.iter() {
sender.send(()).map_err(|_| "Failed to send stop signal.".to_string())?;
sender
.send(())
.map_err(|_| "Failed to send stop signal.".to_string())?;
}
Ok(())
}
Expand Down
20 changes: 20 additions & 0 deletions src/common/x_win_struct/icon_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@ impl IconInfo {
}
}
}

impl From<x_win::IconInfo> for IconInfo {
fn from(value: x_win::IconInfo) -> Self {
IconInfo {
data: value.data,
height: value.height,
width: value.width,
}
}
}

impl From<IconInfo> for x_win::IconInfo {
fn from(value: IconInfo) -> Self {
x_win::IconInfo {
data: value.data,
height: value.height,
width: value.width,
}
}
}
6 changes: 3 additions & 3 deletions src/common/x_win_struct/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(unused_imports)]

pub mod usage_info;
pub mod icon_info;
pub mod process_info;
pub mod window_position;
pub mod usage_info;
pub mod window_info;
pub mod icon_info;
pub mod window_position;
29 changes: 28 additions & 1 deletion src/common/x_win_struct/process_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,33 @@ pub struct ProcessInfo {

impl ProcessInfo {
pub fn new(process_id: u32, path: String, name: String, exec_name: String) -> Self {
Self { process_id, path, name, exec_name }
Self {
process_id,
path,
name,
exec_name,
}
}
}

impl From<x_win::ProcessInfo> for ProcessInfo {
fn from(value: x_win::ProcessInfo) -> Self {
ProcessInfo {
exec_name: value.exec_name,
name: value.name,
path: value.path,
process_id: value.process_id,
}
}
}

impl From<ProcessInfo> for x_win::ProcessInfo {
fn from(value: ProcessInfo) -> Self {
x_win::ProcessInfo {
exec_name: value.exec_name,
name: value.name,
path: value.path,
process_id: value.process_id,
}
}
}
Loading

0 comments on commit 9b403a1

Please sign in to comment.