Skip to content

Commit

Permalink
refactor: Updated "feature flag" for near-cli-rs (ledger) (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod authored Jan 27, 2024
1 parent 7bf9e76 commit ee63db5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 157 deletions.
84 changes: 1 addition & 83 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@ derive_more = "0.99.9"
shell-words = "1.0.0"
interactive-clap = "0.2.8"
interactive-clap-derive = "0.2.8"
near-cli-rs = { version = "0.7.7" }
near-cli-rs = { version = "0.7.7", default-features = false }
dunce = "1"

[features]
default = ["ledger"]
ledger = ["near-cli-rs/ledger"]
55 changes: 23 additions & 32 deletions cargo-near/src/commands/abi_command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod abi;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = near_cli_rs::GlobalContext)]
#[interactive_clap(skip_default_from_cli)]
#[interactive_clap(input_context = near_cli_rs::GlobalContext)]
#[interactive_clap(output_context = AbiCommandlContext)]
pub struct AbiCommand {
/// Include rustdocs in the ABI file
#[interactive_clap(long)]
Expand All @@ -12,44 +12,35 @@ pub struct AbiCommand {
pub compact_abi: bool,
/// Copy final artifacts to this directory
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
pub out_dir: Option<crate::types::utf8_path_buf::Utf8PathBufInner>,
#[interactive_clap(skip_interactive_input)]
pub out_dir: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
/// Path to the `Cargo.toml` of the contract to build
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBufInner>,
#[interactive_clap(skip_interactive_input)]
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
/// Coloring: auto, always, never
#[interactive_clap(long)]
#[interactive_clap(value_enum)]
#[interactive_clap(skip_default_input_arg)]
#[interactive_clap(skip_interactive_input)]
pub color: Option<crate::common::ColorPreference>,
}

impl interactive_clap::FromCli for AbiCommand {
type FromCliContext = near_cli_rs::GlobalContext;
type FromCliError = color_eyre::eyre::Error;
fn from_cli(
optional_clap_variant: Option<<Self as interactive_clap::ToCli>::CliVariant>,
_context: Self::FromCliContext,
) -> interactive_clap::ResultFromCli<
<Self as interactive_clap::ToCli>::CliVariant,
Self::FromCliError,
>
where
Self: Sized + interactive_clap::ToCli,
{
let clap_variant = optional_clap_variant.unwrap_or_default();
let args = Self {
doc: clap_variant.doc,
compact_abi: clap_variant.compact_abi,
out_dir: clap_variant.out_dir.clone(),
manifest_path: clap_variant.manifest_path.clone(),
color: clap_variant.color.clone(),
#[derive(Debug, Clone)]
pub struct AbiCommandlContext;

impl AbiCommandlContext {
pub fn from_previous_context(
_previous_context: near_cli_rs::GlobalContext,
scope: &<AbiCommand as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
let args = AbiCommand {
doc: scope.doc,
compact_abi: scope.compact_abi,
out_dir: scope.out_dir.clone(),
manifest_path: scope.manifest_path.clone(),
color: scope.color.clone(),
};
if let Err(err) = self::abi::run(args) {
interactive_clap::ResultFromCli::Err(Some(clap_variant), color_eyre::eyre::eyre!(err))
} else {
interactive_clap::ResultFromCli::Ok(clap_variant)
}
self::abi::run(args)?;
Ok(Self)
}
}
59 changes: 25 additions & 34 deletions cargo-near/src/commands/build_command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod build;

#[derive(Debug, Default, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = near_cli_rs::GlobalContext)]
#[interactive_clap(skip_default_from_cli)]
#[interactive_clap(input_context = near_cli_rs::GlobalContext)]
#[interactive_clap(output_context = BuildCommandlContext)]
pub struct BuildCommand {
/// Build contract in release mode, with optimizations
#[interactive_clap(long)]
Expand All @@ -18,46 +18,37 @@ pub struct BuildCommand {
pub no_abi: bool,
/// Copy final artifacts to this directory
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
pub out_dir: Option<crate::types::utf8_path_buf::Utf8PathBufInner>,
#[interactive_clap(skip_interactive_input)]
pub out_dir: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
/// Path to the `Cargo.toml` of the contract to build
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBufInner>,
#[interactive_clap(skip_interactive_input)]
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
/// Coloring: auto, always, never
#[interactive_clap(long)]
#[interactive_clap(value_enum)]
#[interactive_clap(skip_default_input_arg)]
#[interactive_clap(skip_interactive_input)]
pub color: Option<crate::common::ColorPreference>,
}

impl interactive_clap::FromCli for BuildCommand {
type FromCliContext = near_cli_rs::GlobalContext;
type FromCliError = color_eyre::eyre::Error;
fn from_cli(
optional_clap_variant: Option<<Self as interactive_clap::ToCli>::CliVariant>,
_context: Self::FromCliContext,
) -> interactive_clap::ResultFromCli<
<Self as interactive_clap::ToCli>::CliVariant,
Self::FromCliError,
>
where
Self: Sized + interactive_clap::ToCli,
{
let clap_variant = optional_clap_variant.unwrap_or_default();
let args = Self {
release: clap_variant.release,
embed_abi: clap_variant.embed_abi,
doc: clap_variant.doc,
no_abi: clap_variant.no_abi,
out_dir: clap_variant.out_dir.clone(),
manifest_path: clap_variant.manifest_path.clone(),
color: clap_variant.color.clone(),
#[derive(Debug, Clone)]
pub struct BuildCommandlContext;

impl BuildCommandlContext {
pub fn from_previous_context(
_previous_context: near_cli_rs::GlobalContext,
scope: &<BuildCommand as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
let args = BuildCommand {
release: scope.release,
embed_abi: scope.embed_abi,
doc: scope.doc,
no_abi: scope.no_abi,
out_dir: scope.out_dir.clone(),
manifest_path: scope.manifest_path.clone(),
color: scope.color.clone(),
};
if let Err(err) = self::build::run(args).map(|_| ()) {
interactive_clap::ResultFromCli::Err(Some(clap_variant), color_eyre::eyre::eyre!(err))
} else {
interactive_clap::ResultFromCli::Ok(clap_variant)
}
self::build::run(args)?;
Ok(Self)
}
}
20 changes: 15 additions & 5 deletions cargo-near/src/types/utf8_path_buf.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use color_eyre::eyre::Context;

#[derive(
Expand All @@ -10,21 +12,29 @@ use color_eyre::eyre::Context;
derive_more::FromStr,
)]
#[as_ref(forward)]
pub struct Utf8PathBufInner(pub camino::Utf8PathBuf);
pub struct Utf8PathBuf(camino::Utf8PathBuf);

impl std::fmt::Display for Utf8PathBufInner {
impl std::fmt::Display for Utf8PathBuf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl interactive_clap::ToCli for Utf8PathBufInner {
type CliVariant = Utf8PathBufInner;
impl interactive_clap::ToCli for Utf8PathBuf {
type CliVariant = Utf8PathBuf;
}

impl Utf8PathBufInner {
impl Utf8PathBuf {
pub fn read_bytes(&self) -> color_eyre::Result<Vec<u8>> {
std::fs::read(self.0.clone().into_std_path_buf())
.wrap_err_with(|| format!("Error reading data from file: {:?}", self.0))
}

pub fn from_path_buf(path: PathBuf) -> Result<Self, PathBuf> {
Ok(camino::Utf8PathBuf::from_path_buf(path)?.into())
}

pub fn join(&self, path: impl AsRef<camino::Utf8Path>) -> Utf8PathBuf {
camino::Utf8Path::join(self.0.as_path(), path).into()
}
}
4 changes: 2 additions & 2 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ macro_rules! invoke_cargo_near {
doc: cmd.doc,
compact_abi: cmd.compact_abi,
out_dir: cmd.out_dir,
manifest_path: Some(cargo_near::types::utf8_path_buf::Utf8PathBufInner(cargo_path)),
manifest_path: Some(cargo_path.into()),
color: cmd.color,
};
cargo_near::commands::abi_command::abi::run(args)?;
Expand All @@ -69,7 +69,7 @@ macro_rules! invoke_cargo_near {
doc: cmd.doc,
no_abi: cmd.no_abi,
out_dir: cmd.out_dir,
manifest_path: Some(cargo_near::types::utf8_path_buf::Utf8PathBufInner(cargo_path)),
manifest_path: Some(cargo_path.into()),
color: cmd.color,
};
cargo_near::commands::build_command::build::run(args)?;
Expand Down

0 comments on commit ee63db5

Please sign in to comment.