From 58600883dd6ac69b8ffd1e51d7c5b00adcc09c33 Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 9 May 2023 09:30:30 +0800 Subject: [PATCH] feat(cli): add --use-cross command for building with `cross` (#1584) closes #1582 --- cli/codegen/commands.ts | 6 ++++++ cli/docs/build.md | 1 + cli/src/api/build.ts | 3 ++- cli/src/def/build.ts | 10 ++++++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cli/codegen/commands.ts b/cli/codegen/commands.ts index 463296c4..ba20d6ac 100644 --- a/cli/codegen/commands.ts +++ b/cli/codegen/commands.ts @@ -227,6 +227,12 @@ const BUILD_OPTIONS: CommandSchema = { '[experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform', short: 'x', }, + { + name: 'useCross', + type: 'boolean', + description: + '[experimental] use [cross](https://github.com/cross-rs/cross) instead of `cargo`', + }, { name: 'watch', type: 'boolean', diff --git a/cli/docs/build.md b/cli/docs/build.md index e3939b3a..34f58854 100644 --- a/cli/docs/build.md +++ b/cli/docs/build.md @@ -44,6 +44,7 @@ new NapiCli().build({ | bin | --bin | string | false | | Build only the specified binary | | package | --package,-p | string | false | | Build the specified library or the one at cwd | | crossCompile | --cross-compile,-x | boolean | false | | [experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform | +| useCross | --use-cross | boolean | false | | [experimental] use [cross](https://github.com/cross-rs/cross) instead of `cargo` | | watch | --watch,-w | boolean | false | | watch the crate changes and build continiously with `cargo-watch` crates | | features | --features,-F | string[] | false | | Space-separated list of features to activate | | allFeatures | --all-features | boolean | false | | Activate all available features | diff --git a/cli/src/api/build.ts b/cli/src/api/build.ts index 6fe7fbfd..0512c648 100644 --- a/cli/src/api/build.ts +++ b/cli/src/api/build.ts @@ -150,7 +150,8 @@ class Builder { const controller = new AbortController() const buildTask = new Promise((resolve, reject) => { - const buildProcess = spawn('cargo', this.args, { + const command = this.options.useCross ? 'cross' : 'cargo' + const buildProcess = spawn(command, this.args, { env: { ...process.env, ...this.envs, diff --git a/cli/src/def/build.ts b/cli/src/def/build.ts index 7e045e0d..f93ad6e0 100644 --- a/cli/src/def/build.ts +++ b/cli/src/def/build.ts @@ -97,6 +97,11 @@ export abstract class BaseBuildCommand extends Command { '[experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform', }) + useCross?: boolean = Option.Boolean('--use-cross', { + description: + '[experimental] use [cross](https://github.com/cross-rs/cross) instead of `cargo`', + }) + watch?: boolean = Option.Boolean('--watch,-w', { description: 'watch the crate changes and build continiously with `cargo-watch` crates', @@ -135,6 +140,7 @@ export abstract class BaseBuildCommand extends Command { bin: this.bin, package: this.package, crossCompile: this.crossCompile, + useCross: this.useCross, watch: this.watch, features: this.features, allFeatures: this.allFeatures, @@ -223,6 +229,10 @@ export interface BuildOptions { * [experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform */ crossCompile?: boolean + /** + * [experimental] use [cross](https://github.com/cross-rs/cross) instead of `cargo` + */ + useCross?: boolean /** * watch the crate changes and build continiously with `cargo-watch` crates */