refactor(cli): --profile flag (#1604)

This commit is contained in:
LongYinan 2023-05-27 12:00:43 +08:00 committed by GitHub
parent 82c2113c24
commit c7d6ee6e4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 12 deletions

View file

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

View file

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

View file

@ -18,3 +18,7 @@ exclude = ["./testing"]
[profile.release]
lto = true
[profile.napi-rs-custom]
inherits = "dev"
codegen-units = 1024

View file

@ -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',

View file

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

View file

@ -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 {

View file

@ -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
*/