From d9abdcc442181fdcbdcb580affc3466242a6c408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dj8yf0=CE=BCl?= Date: Thu, 3 Oct 2024 15:55:39 +0300 Subject: [PATCH] feat: add derive of `bon::Builder` for build and build_extended Opts --- Cargo.lock | 24 +++++++++++++++++++ cargo-near-build/Cargo.toml | 1 + cargo-near-build/src/lib.rs | 2 ++ .../types/near/build/input/implicit_env.rs | 8 ++++--- .../src/types/near/build/input/mod.rs | 11 ++++++++- .../types/near/build_extended/build_script.rs | 6 ++++- .../src/types/near/build_extended/mod.rs | 3 ++- 7 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ef436f2e..3d635996 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -311,6 +311,29 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bon" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97493a391b4b18ee918675fb8663e53646fd09321c58b46afa04e8ce2499c869" +dependencies = [ + "bon-macros", + "rustversion", +] + +[[package]] +name = "bon-macros" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2af3eac944c12cdf4423eab70d310da0a8e5851a18ffb192c0a5e3f7ae1663" +dependencies = [ + "darling", + "ident_case", + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "borsh" version = "1.5.1" @@ -484,6 +507,7 @@ dependencies = [ name = "cargo-near-build" version = "0.1.1" dependencies = [ + "bon", "bs58 0.5.1", "camino", "cargo_metadata", diff --git a/cargo-near-build/Cargo.toml b/cargo-near-build/Cargo.toml index e7f17bba..72135c60 100644 --- a/cargo-near-build/Cargo.toml +++ b/cargo-near-build/Cargo.toml @@ -23,6 +23,7 @@ near-abi = { version = "0.4.0", features = ["__chunked-entries"] } zstd = "0.13" schemars = "0.8" rustc_version = "0.4" +bon = "2.3.0" url = { version = "2.5.0", features = ["serde"], optional = true } serde = { version = "1.0.197", optional = true } git2 = { version = "0.19", optional = true } diff --git a/cargo-near-build/src/lib.rs b/cargo-near-build/src/lib.rs index cf730b9e..9e0ce7c3 100644 --- a/cargo-near-build/src/lib.rs +++ b/cargo-near-build/src/lib.rs @@ -16,6 +16,7 @@ //! //! 1. [camino] is re-exported, because it is used in [BuildOpts], and [BuildArtifact] as type of some of fields //! 2. [near_abi] is re-exported, because details of ABI generated depends on specific version of `near-abi` dependency +//! 3. [bon] is re-exported for the convenience of [bon::vec] helper macro //! //! ## Sample usage: //! @@ -111,5 +112,6 @@ pub mod docker { pub use crate::types::near::docker_build::Opts as DockerBuildOpts; } +pub use bon; pub use camino; pub use near_abi; diff --git a/cargo-near-build/src/types/near/build/input/implicit_env.rs b/cargo-near-build/src/types/near/build/input/implicit_env.rs index cea0846f..fd3e110c 100644 --- a/cargo-near-build/src/types/near/build/input/implicit_env.rs +++ b/cargo-near-build/src/types/near/build/input/implicit_env.rs @@ -1,13 +1,15 @@ /// additional argument of [build](crate::build) function, wrapped in an `Option` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, bon::Builder)] pub struct Opts { /// override value of [crate::env_keys::nep330::CONTRACT_PATH] environment variable + #[builder(into)] pub nep330_contract_path: Option, - /// override value of [crate::env_keys::nep330::CARGO_TARGET_DIR] environment variable, + /// override value of [crate::env_keys::CARGO_TARGET_DIR] environment variable, /// which is required to avoid deadlock in context of nested (cargo) build /// in build-script; /// - /// should best be a subfolder of [crate::env_keys::nep330::CARGO_TARGET_DIR] + /// should best be a subfolder of [crate::env_keys::CARGO_TARGET_DIR] /// of crate being built to work normally + #[builder(into)] pub cargo_target_dir: Option, } diff --git a/cargo-near-build/src/types/near/build/input/mod.rs b/cargo-near-build/src/types/near/build/input/mod.rs index ec1527a3..70c5a7f1 100644 --- a/cargo-near-build/src/types/near/build/input/mod.rs +++ b/cargo-near-build/src/types/near/build/input/mod.rs @@ -20,34 +20,43 @@ pub enum BuildContext { /// - `None` - for `Option`-s /// - empty vector - for `Vec` /// - delegates to [impl Default for CliDescription](struct.CliDescription.html#impl-Default-for-CliDescription) -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, bon::Builder)] pub struct Opts { /// disable implicit `--locked` flag for all `cargo` commands, enabled by default + #[builder(default)] pub no_locked: bool, /// Build contract in debug mode, without optimizations and bigger in size + #[builder(default)] pub no_release: bool, /// Do not generate ABI for the contract + #[builder(default)] pub no_abi: bool, /// Do not embed the ABI in the contract binary + #[builder(default)] pub no_embed_abi: bool, /// Do not include rustdocs in the embedded ABI + #[builder(default)] pub no_doc: bool, /// Copy final artifacts to this directory pub out_dir: Option, /// Path to the `Cargo.toml` of the contract to build pub manifest_path: Option, /// Set compile-time feature flags. + #[builder(into)] pub features: Option, /// Disables default feature flags. + #[builder(default)] pub no_default_features: bool, /// Coloring: auto, always, never; /// assumed to be auto when `None` pub color: Option, /// description of cli command, where [BuildOpts](crate::BuildOpts) are being used from, either real /// or emulated + #[builder(default)] pub cli_description: CliDescription, /// additional environment key-value pairs, that should be passed to underlying /// build commands + #[builder(default)] pub env: Vec<(String, String)>, } diff --git a/cargo-near-build/src/types/near/build_extended/build_script.rs b/cargo-near-build/src/types/near/build_extended/build_script.rs index b1a7fa76..5b1d98c0 100644 --- a/cargo-near-build/src/types/near/build_extended/build_script.rs +++ b/cargo-near-build/src/types/near/build_extended/build_script.rs @@ -1,22 +1,26 @@ -#[derive(Debug, Clone)] +#[derive(Debug, Clone, bon::Builder)] pub struct Opts { /// environment variable name to export result `*.wasm` path to with [`cargo::rustc-env=`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-env) /// instruction + #[builder(into)] pub result_env_key: Option, /// list of paths for [`cargo::rerun-if-changed=`](https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-changed) /// instruction /// /// if relative, it's relative to path of crate, where build.rs is compiled + #[builder(default)] pub rerun_if_changed_list: Vec, /// vector of key-value pairs of environment variable name and its value, /// when compilation should be skipped on a variable's value match; /// e.g. /// skipping emitting output `*.wasm` may be helpful when `PROFILE` is equal to `debug` /// for using `rust-analyzer/flycheck`, `cargo check`, `bacon` and other dev-tools + #[builder(default)] pub build_skipped_when_env_is: Vec<(String, String)>, /// path of stub file, where a placeholder empty `wasm` output is emitted to, when /// build is skipped due to match in [`Self::build_skipped_when_env_is`] /// /// if this path is relative, then the base is [`crate::extended::BuildOptsExtended::workdir`] + #[builder(into)] pub stub_path: Option, } diff --git a/cargo-near-build/src/types/near/build_extended/mod.rs b/cargo-near-build/src/types/near/build_extended/mod.rs index a9dba64f..87a840c0 100644 --- a/cargo-near-build/src/types/near/build_extended/mod.rs +++ b/cargo-near-build/src/types/near/build_extended/mod.rs @@ -3,8 +3,9 @@ use crate::{BuildImplicitEnvOpts, BuildOpts}; pub mod build_script; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, bon::Builder)] pub struct OptsExtended { + #[builder(into)] pub workdir: String, pub build_opts: BuildOpts, pub build_implicit_env_opts: BuildImplicitEnvOpts,