Skip to content

Commit

Permalink
Merge pull request #31 from miniben-90/split-rs-napi
Browse files Browse the repository at this point in the history
[FEATURE] split napi-rs part and data recovery for Rust project
  • Loading branch information
miniben-90 authored Aug 6, 2024
2 parents d49d350 + 5abe502 commit cc9ae55
Show file tree
Hide file tree
Showing 54 changed files with 1,425 additions and 352 deletions.
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[target.x86_64-unknown-linux-musl]
rustflags = [ "-C", "target-feature=-crt-static"]
rustflags = ["-C", "target-feature=-crt-static", "-lm"]

[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-musl-gcc"
rustflags = ["-C", "target-feature=-crt-static"]
rustflags = ["-C", "target-feature=-crt-static", "-lm"]
25 changes: 25 additions & 0 deletions .github/docker-ubuntu-rust-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!bin/bash

target=${1:-''}

curl https://sh.rustup.rs -sSf | bash -s -- -y

. "$HOME/.cargo/env"

sudo apt update

sudo apt upgrade -y

sudo apt install musl-tools -y

sudo apt install libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev libxcb-shm0-dev pkg-config build-essential -y

rustup target add $target

curl -L -o /tmp/firefox.tar.bz2 "https://download.mozilla.org/?product=firefox-latest&os=linux64"

sudo tar xjf /tmp/firefox.tar.bz2 -C /opt/

/opt/firefox/firefox --safe-mode https://github.com/ &

sleep 1
22 changes: 19 additions & 3 deletions .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 @@ -67,6 +67,14 @@ jobs:
yarn build --target x86_64-unknown-linux-gnu &&
strip *.node
# Build for linux (using Ubuntu) (x64)
- host: ubuntu-latest
target: x86_64-unknown-linux-musl
build: |-
set -e &&
yarn build --target x86_64-unknown-linux-musl &&
strip *.node
# Disabled problem with lxcb
# # Build for linux (using Ubuntu) (arm64)
# - host: ubuntu-latest
Expand Down Expand Up @@ -149,6 +157,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 All @@ -169,7 +185,7 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: bindings-${{ matrix.settings.target }}
path: ${{ env.APP_NAME }}.*.node
path: ${{ github.workspace }}/${{ env.APP_NAME }}.*.node
if-no-files-found: error

test:
Expand Down Expand Up @@ -303,7 +319,7 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: bindings-universal-apple-darwin
path: ${{ env.APP_NAME }}.*.node
path: ${{ github.workspace }}/${{ env.APP_NAME }}.*.node
if-no-files-found: error

publish:
Expand Down
187 changes: 187 additions & 0 deletions .github/workflows/ci-rs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
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: ${{ github.workspace }}/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
image: miniben90/ubuntu-dummy-desktop:latest

# Build for linux (using Ubuntu) (x64)
- host: ubuntu-latest
target: x86_64-unknown-linux-musl
image: miniben90/ubuntu-dummy-desktop:latest

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@v4
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: 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

- name: Test (x86_64-unknown-linux-gnu & x86_64-unknown-linux-musl)
if: ${{ matrix.settings.target == 'x86_64-unknown-linux-gnu' || matrix.settings.target == 'x86_64-unknown-linux-musl' }}
uses: addnab/docker-run-action@v3
with:
image: ${{ matrix.settings.image }}
options: --user 0:0 -v ${{ github.workspace }}/.github/docker-ubuntu-rust-install.sh:/usr/local/docker-ubuntu-rust-install.sh -v ${{ github.workspace }}/x-win-rs:/work -w /work
run: |
Xvfb :0 &
sleep 1
gpg-agent --daemon
sleep 1
xfce4-session &
sleep 1
sh /usr/local/docker-ubuntu-rust-install.sh ${{ matrix.settings.target }}
. "$HOME/.cargo/env"
cp /work /x-win -Rf
cd /x-win
cargo test --target ${{ matrix.settings.target }}
- name: Build
run: cargo build --release --target ${{ matrix.settings.target }}

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

publish:
defaults:
run:
working-directory: ${{ github.workspace }}/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

# Download artifacts from build part
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: ${{ github.workspace }}/x-win-rs/target

# Setup node version from matrix.node and install deps
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 20
check-latest: true
cache: yarn

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

# Update Cargo.toml version
- name: Set version to package.json
run: node ${{ github.workspace }}/.scripts/before-publish.cjs x-win-rs

- 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
71 changes: 55 additions & 16 deletions .scripts/before-publish.cjs
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
const path = require('node:path');
const fs = require('node:fs');
const { argv } = require('node:process');

console.log('[BEFORE-PUBLISH]', 'Update package.json and cargo.toml version...');
function raplceCargoVersion(cargoToml, version) {
return cargoToml.replace(/version\s*=\s*".*?"/, `version = "${version}"`);
}

if (process.env.TAG) {
const version = process.env.TAG.startsWith('v') ? process.env.TAG.substring(1) : process.env.TAG;

const packageJsonPath = path.join(process.cwd(), 'package.json');
const cargoTomlPath = path.join(process.cwd(), 'Cargo.toml');
if (argv && argv.findIndex(v => v === 'x-win-rs') !== -1) {
console.log('[BEFORE-PUBLISH]', 'Update cargo.toml version to', version);

const packageJson = require(packageJsonPath);
const cargoTomlPath = path.join(process.cwd(), process.cwd().endsWith('x-win-rs') ? '' : 'x-win-rs', 'Cargo.toml');
const cargoToml = fs.readFileSync(cargoTomlPath, { encoding: 'utf8' });

if (packageJson && process.env.TAG) {
const version = process.env.TAG.startsWith('v') ? process.env.TAG.substring(1) : process.env.TAG;
console.log('[BEFORE-PUBLISH]', 'Update package.json version to', version);
packageJson.version = version;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, 2));

const cargoToml = fs.readFileSync(cargoTomlPath, { encoding: 'utf8' });
console.log('[BEFORE-PUBLISH]', 'Update cargo.toml version to', version);
const newCargoToml = cargoToml.replace(/version\s*=\s*".*?"/, `version = "${version}"`);
fs.writeFileSync(cargoTomlPath, newCargoToml);
console.log('[BEFORE-PUBLISH]', 'Updating cargo.toml');

const newCargoToml = raplceCargoVersion(cargoToml, version);
fs.writeFileSync(cargoTomlPath, newCargoToml);

console.log('[BEFORE-PUBLISH]', 'Cargo.toml updated.');
console.log('[BEFORE-PUBLISH]', 'Finished.');
} else {
console.log('[BEFORE-PUBLISH]', 'Update package.json and cargo.toml version to', version);

const packageJsonPath = path.join(process.cwd(), 'package.json');
const cargoTomlPath = path.join(process.cwd(), 'Cargo.toml');

if (!fs.existsSync(packageJsonPath)) {
console.error('package.json file not found!');
process.exit(1);
}

if (!fs.existsSync(cargoTomlPath)) {
console.error('package.json file not found!');
process.exit(1);
}

const packageJson = require(packageJsonPath);
console.log('[BEFORE-PUBLISH]', 'Updating package.json...');

packageJson.version = version;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, 2));

console.log('[BEFORE-PUBLISH]', 'package.json updated.');

const cargoToml = fs.readFileSync(cargoTomlPath, { encoding: 'utf8' });

console.log('[BEFORE-PUBLISH]', 'Updating cargo.toml');

const newCargoToml = raplceCargoVersion(cargoToml, version);
fs.writeFileSync(cargoTomlPath, newCargoToml);

console.log('[BEFORE-PUBLISH]', 'Cargo.toml updated.');
console.log('[BEFORE-PUBLISH]', 'Finished.');
}
} else {
console.error('package or TAG env not found!');
console.error('TAG env not found!');
process.exit(1);
}

console.log('[BEFORE-PUBLISH]', 'Finished.');
/** Exist process with success */
process.exit(0);
Loading

0 comments on commit cc9ae55

Please sign in to comment.