From c7d6ee6e4be8f96791b8a5c7761fe1b1937ff54b Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 27 May 2023 12:00:43 +0800 Subject: [PATCH] refactor(cli): --profile flag (#1604) --- .github/workflows/cli-binary.yml | 6 +----- .github/workflows/test.yaml | 3 +++ Cargo.toml | 4 ++++ cli/codegen/commands.ts | 5 +++++ cli/docs/build.md | 1 + cli/src/api/build.ts | 15 ++++++++------- cli/src/def/build.ts | 9 +++++++++ 7 files changed, 31 insertions(+), 12 deletions(-) diff --git a/.github/workflows/cli-binary.yml b/.github/workflows/cli-binary.yml index c4e10631..c95a2ef9 100644 --- a/.github/workflows/cli-binary.yml +++ b/.github/workflows/cli-binary.yml @@ -47,10 +47,6 @@ jobs: run: | yarn workspace binary build ./examples/binary/napi-examples-binary + yarn workspace binary build --profile napi-rs-custom env: RUST_BACKTRACE: 1 - - - name: Clear the cargo caches - run: | - cargo install cargo-cache --no-default-features --features ci-autoclean - cargo-cache diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 66160187..431b3c4d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -73,3 +73,6 @@ jobs: run: | node ./node_modules/electron/install.js xvfb-run --auto-servernum yarn test:electron + + - name: Test build with profile + run: yarn workspace @examples/napi build --profile napi-rs-custom diff --git a/Cargo.toml b/Cargo.toml index 060f0091..9d3a6052 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,7 @@ exclude = ["./testing"] [profile.release] lto = true + +[profile.napi-rs-custom] +inherits = "dev" +codegen-units = 1024 diff --git a/cli/codegen/commands.ts b/cli/codegen/commands.ts index ba20d6ac..2bdba2a0 100644 --- a/cli/codegen/commands.ts +++ b/cli/codegen/commands.ts @@ -220,6 +220,11 @@ const BUILD_OPTIONS: CommandSchema = { description: 'Build the specified library or the one at cwd', short: 'p', }, + { + name: 'profile', + type: 'string', + description: 'Build artifacts with the specified profile', + }, { name: 'crossCompile', type: 'boolean', diff --git a/cli/docs/build.md b/cli/docs/build.md index 34f58854..0b02d9c4 100644 --- a/cli/docs/build.md +++ b/cli/docs/build.md @@ -43,6 +43,7 @@ new NapiCli().build({ | verbose | --verbose,-v | boolean | false | | Verbosely log build command trace | | bin | --bin | string | false | | Build only the specified binary | | package | --package,-p | string | false | | Build the specified library or the one at cwd | +| profile | --profile | string | false | | Build artifacts with the specified profile | | 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 | diff --git a/cli/src/api/build.ts b/cli/src/api/build.ts index ec47de22..57a51ca9 100644 --- a/cli/src/api/build.ts +++ b/cli/src/api/build.ts @@ -402,6 +402,10 @@ class Builder { this.args.push('--target-dir', this.options.targetDir) } + if (this.options.profile) { + this.args.push('--profile', this.options.profile) + } + if (this.options.cargoOptions?.length) { this.args.push(...this.options.cargoOptions) } @@ -460,13 +464,10 @@ class Builder { return } - let outputFolder = this.options.release ? 'release' : 'debug' - if (this.options.cargoOptions?.includes('--profile')) { - const args = this.options.cargoOptions - const profileIndex = args.indexOf('--profile') - outputFolder = args[profileIndex + 1] - } - const src = join(this.targetDir, this.target.triple, outputFolder, srcName) + const profile = + this.options.profile ?? (this.options.release ? 'release' : 'debug') + const src = join(this.targetDir, this.target.triple, profile, srcName) + debug(`Copy artifact from: [${src}]`) const dest = join(this.outputDir, destName) try { diff --git a/cli/src/def/build.ts b/cli/src/def/build.ts index f93ad6e0..823d1ae2 100644 --- a/cli/src/def/build.ts +++ b/cli/src/def/build.ts @@ -92,6 +92,10 @@ export abstract class BaseBuildCommand extends Command { description: 'Build the specified library or the one at cwd', }) + profile?: string = Option.String('--profile', { + description: 'Build artifacts with the specified profile', + }) + crossCompile?: boolean = Option.Boolean('--cross-compile,-x', { description: '[experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform', @@ -139,6 +143,7 @@ export abstract class BaseBuildCommand extends Command { verbose: this.verbose, bin: this.bin, package: this.package, + profile: this.profile, crossCompile: this.crossCompile, useCross: this.useCross, watch: this.watch, @@ -225,6 +230,10 @@ export interface BuildOptions { * Build the specified library or the one at cwd */ package?: string + /** + * Build artifacts with the specified profile + */ + profile?: string /** * [experimental] cross-compile for the specified target with `cargo-xwin` on windows and `cargo-zigbuild` on other platform */