Skip to content

Commit

Permalink
refactored indent_payload()
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod committed Oct 23, 2024
1 parent fcb68ab commit 1d7c891
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 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 @@ -35,6 +35,7 @@ shell-words = { version = "1.0.0", optional = true }
wasm-opt = "=0.116.1"
humantime = "2.1.0"
near-cli-rs = { version = "0.15.1", default-features = false }
indenter = "0.3"

[target.'cfg(target_os = "linux")'.dependencies]
nix = { version = "0.29.0", features = ["user", "process"], optional = true }
Expand Down
3 changes: 2 additions & 1 deletion cargo-near-build/src/cargo_native/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cargo_metadata::{Artifact, Message};
use eyre::{ContextCompat, WrapErr};
use std::io::BufRead;

use crate::pretty_print;
use crate::types::near::build::input::ColorPreference;
use crate::types::{cargo::manifest_path::ManifestPath, near::build::output::CompilationArtifact};

Expand Down Expand Up @@ -143,7 +144,7 @@ where
target: "near_teach_me",
parent: &tracing::Span::none(),
"Invoking cargo:\n{}",
near_cli_rs::common::indent_payload(&format!("{:#?}", cmd))
pretty_print::indent_payload(&format!("{:#?}", cmd))
);
tracing::info!("Invoking cargo: {:#?}", cmd);

Expand Down
6 changes: 4 additions & 2 deletions cargo-near-build/src/cargo_native/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{ffi::OsStr, io::BufRead, path::PathBuf, process::Command};

use eyre::WrapErr;

use crate::pretty_print;

pub const COMPILATION_TARGET: &str = "wasm32-unknown-unknown";

pub fn wasm32_exists() -> bool {
Expand Down Expand Up @@ -57,7 +59,7 @@ fn get_rustc_wasm32_unknown_unknown_target_libdir() -> eyre::Result<PathBuf> {
target: "near_teach_me",
parent: &tracing::Span::none(),
"Execution command:\n{}",
near_cli_rs::common::indent_payload(&format!("`{:?}`", command).replace("\"", ""))
pretty_print::indent_payload(&format!("`{:?}`", command).replace("\"", ""))
);

let output = command.output()?;
Expand Down Expand Up @@ -86,7 +88,7 @@ where
target: "near_teach_me",
parent: &tracing::Span::none(),
"Invoking rustup:\n{}",
near_cli_rs::common::indent_payload(&format!("`{}`", format!("{:?}", cmd).replace("\"", "")))
pretty_print::indent_payload(&format!("`{}`", format!("{:?}", cmd).replace("\"", "")))
);
tracing::info!(
"Invoking rustup: `{}`",
Expand Down
3 changes: 2 additions & 1 deletion cargo-near-build/src/env_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) const CARGO_NEAR_ABI_SCHEMA_VERSION: &str = "CARGO_NEAR_ABI_SCHEMA_VE
/// module contains variables, which are set to configure build with WASM reproducibility,
/// which correspond to some fields of `ContractSourceMetadata` in <https://github.com/near/NEPs/blob/master/neps/nep-0330.md>
pub mod nep330 {
use crate::pretty_print;
use std::collections::HashMap;

// ====================== NEP-330 1.2.0 - Build Details Extension ===========
Expand Down Expand Up @@ -52,7 +53,7 @@ pub mod nep330 {
target: "near_teach_me",
parent: &tracing::Span::none(),
"Variables, relevant for reproducible builds:\n{}",
near_cli_rs::common::indent_payload(&format!("{:#?}", env_map))
pretty_print::indent_payload(&format!("{:#?}", env_map))
);
}
}
3 changes: 2 additions & 1 deletion cargo-near-build/src/near/abi/generate/dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fs;
use camino::Utf8Path;

use crate::cargo_native::Dylib;
use crate::pretty_print;
use crate::types::near::build::output::CompilationArtifact;

pub fn extract_abi_entries(
Expand Down Expand Up @@ -38,7 +39,7 @@ pub fn extract_abi_entries(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Detected NEAR ABI symbols:\n{}",
near_cli_rs::common::indent_payload(&format!("{:#?}", &near_abi_symbols))
pretty_print::indent_payload(&format!("{:#?}", &near_abi_symbols))
);
tracing::debug!("Detected NEAR ABI symbols: {:?}", &near_abi_symbols);

Expand Down
4 changes: 2 additions & 2 deletions cargo-near-build/src/near/build/export.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{env_keys, types::near::build::input::Opts};
use crate::{env_keys, pretty_print, types::near::build::input::Opts};

pub fn nep_330_build_command(args: &Opts) -> eyre::Result<()> {
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Compute `CARGO_NEAR_BUILD_COMMAND`, current executable:\n{}",
near_cli_rs::common::indent_payload(&format!("`{}`", std::env::args().collect::<Vec<String>>().join(" ")))
pretty_print::indent_payload(&format!("`{}`", std::env::args().collect::<Vec<String>>().join(" ")))
);
tracing::debug!(
"compute `CARGO_NEAR_BUILD_COMMAND`, current executable: {:?}",
Expand Down
5 changes: 3 additions & 2 deletions cargo-near-build/src/near/docker_build/subprocess_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{
use nix::unistd::{getgid, getuid};

use crate::env_keys;
use crate::pretty_print;
use crate::types::near::docker_build::subprocess::{container_paths, env_vars};
use crate::types::near::docker_build::{cloned_repo, metadata};

Expand Down Expand Up @@ -75,7 +76,7 @@ pub fn run(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Input device is a TTY:\n{}",
near_cli_rs::common::indent_payload(&stdin_is_terminal.to_string())
pretty_print::indent_payload(&stdin_is_terminal.to_string())
);
tracing::debug!("input device is a tty: {}", stdin_is_terminal);
if stdin_is_terminal
Expand All @@ -93,7 +94,7 @@ pub fn run(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Execution docker command:\n{}",
near_cli_rs::common::indent_payload(&format!("`{}`", docker_args.join(" ")))
pretty_print::indent_payload(&format!("`{}`", docker_args.join(" ")))
);
tracing::debug!("docker command : {:#?}", docker_args);
docker_args
Expand Down
11 changes: 11 additions & 0 deletions cargo-near-build/src/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ pub fn duration(start: Instant, activity: &str) {
humantime::format_duration(duration)
);
}

pub fn indent_payload(s: &str) -> String {
use std::fmt::Write;

let mut indented_string = String::new();
indenter::indented(&mut indented_string)
.with_str(" | ")
.write_str(s)
.ok();
indented_string
}
5 changes: 3 additions & 2 deletions cargo-near-build/src/types/cargo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cargo_metadata::{MetadataCommand, Package};
use colored::Colorize;
use eyre::{ContextCompat, WrapErr};

use crate::pretty_print;
use crate::types::near::build::buildtime_env;

use super::manifest_path::ManifestPath;
Expand Down Expand Up @@ -54,7 +55,7 @@ impl CrateMetadata {
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Crate metadata:\n{}", near_cli_rs::common::indent_payload(
"Crate metadata:\n{}", pretty_print::indent_payload(
"Metadata information is very large. So, if you need it, use the \"RUST_LOG=trace\" flag. \
For example, `RUST_LOG=trace cargo-near near build`"
)
Expand Down Expand Up @@ -111,7 +112,7 @@ fn get_cargo_metadata(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Execution command:\n{}",
near_cli_rs::common::indent_payload(&format!("`{:?}`", cmd.cargo_command()).replace("\"", ""))
pretty_print::indent_payload(&format!("`{:?}`", cmd.cargo_command()).replace("\"", ""))
);
tracing::debug!("metadata command: {:#?}", cmd.cargo_command());
let metadata = cmd.exec();
Expand Down

0 comments on commit 1d7c891

Please sign in to comment.