Skip to content

Commit

Permalink
feat: add derive of bon::Builder for build and build_extended Opts
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Oct 3, 2024
1 parent e135aaf commit db9ae5c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 12 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cargo-near-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 6 additions & 4 deletions cargo-near-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
//!
Expand Down Expand Up @@ -90,9 +91,9 @@ pub use build_exports::*;
/// rerun_if_changed_list: vec!["../another-contract".into(), "../Cargo.toml".into(), "../Cargo.lock".into()],
/// build_skipped_when_env_is: vec![
/// // shorter build for `cargo check`
/// ("PROFILE".into(), "debug".into()),
/// (cargo_near_build::env_keys::BUILD_RS_ABI_STEP_HINT.into(), "true".into()),
/// ],
/// ("PROFILE", "debug"),
/// (cargo_near_build::env_keys::BUILD_RS_ABI_STEP_HINT, "true"),
/// ].into(),
/// stub_path: Some("../target/stub.bin".into()),
/// },
/// };
Expand All @@ -101,7 +102,7 @@ pub use build_exports::*;
#[cfg(feature = "build_script")]
pub mod extended {
pub use crate::near::build_extended::run as build;
pub use crate::types::near::build_extended::build_script::Opts as BuildScriptOpts;
pub use crate::types::near::build_extended::build_script::{EnvPairs, Opts as BuildScriptOpts};
pub use crate::types::near::build_extended::OptsExtended as BuildOptsExtended;
}

Expand All @@ -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;
2 changes: 1 addition & 1 deletion cargo-near-build/src/near/build_extended/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ macro_rules! print_warn {
impl Opts {
pub(crate) fn should_skip(&self, version: &Version) -> bool {
let mut return_bool = false;
for (env_key, value_to_skip) in self.build_skipped_when_env_is.iter() {
for (env_key, value_to_skip) in self.build_skipped_when_env_is.0.iter() {
if let Ok(actual_value) = std::env::var(env_key) {
if actual_value == *value_to_skip {
return_bool = true;
Expand Down
8 changes: 5 additions & 3 deletions cargo-near-build/src/types/near/build/input/implicit_env.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
/// 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 <https://github.com/rust-lang/cargo/issues/8938> 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<String>,
}
11 changes: 10 additions & 1 deletion cargo-near-build/src/types/near/build/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<camino::Utf8PathBuf>,
/// Path to the `Cargo.toml` of the contract to build
pub manifest_path: Option<camino::Utf8PathBuf>,
/// Set compile-time feature flags.
#[builder(into)]
pub features: Option<String>,
/// Disables default feature flags.
#[builder(default)]
pub no_default_features: bool,
/// Coloring: auto, always, never;
/// assumed to be auto when `None`
pub color: Option<ColorPreference>,
/// 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)>,
}

Expand Down
26 changes: 24 additions & 2 deletions cargo-near-build/src/types/near/build_extended/build_script.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
#[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<String>,
/// 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<String>,
/// 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
pub build_skipped_when_env_is: Vec<(String, String)>,
#[builder(default, into)]
pub build_skipped_when_env_is: EnvPairs,
/// 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<String>,
}

/// utility type which can be initialized with vector of 2-element tuples of literal strings,
/// by using [core::convert::Into]
/// like so: `vec![("key1", "value1"), ("key2", "value2")].into()`
#[derive(Default, Debug, Clone)]
pub struct EnvPairs(pub Vec<(String, String)>);

impl From<Vec<(&str, &str)>> for EnvPairs {
fn from(value: Vec<(&str, &str)>) -> Self {
let vector = value
.into_iter()
.map(|(key, value)| ((key.into(), value.into())))
.collect();

Self(vector)
}
}
3 changes: 2 additions & 1 deletion cargo-near-build/src/types/near/build_extended/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit db9ae5c

Please sign in to comment.