Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added the ability to use the TEACH-ME mode #221

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
596ad4f
start
Sep 13, 2024
c90db6b
added a visual indicator for creating a new project
Sep 19, 2024
77efc98
added filter to "RUST_LOG" trace
Sep 20, 2024
1b14663
added "--teach-me" to execute_git_commands()
Sep 23, 2024
7586a1a
fixed filter for indicatif_layer
Sep 23, 2024
1d8c66c
refactored
Sep 24, 2024
2580b70
added "--teach-me" to track_usage()
Sep 26, 2024
286e48f
reverted a fix for track_usage()
Sep 26, 2024
e1fa0d0
added "--teach-me" to BuildCommand
Oct 1, 2024
f17072c
fixed
Oct 1, 2024
fa7ed9f
added "--teach-me" to subprocess_step
Oct 3, 2024
68159a6
formatted "Invoking cargo"
Oct 7, 2024
5cc4135
refactored indent_payload()
Oct 7, 2024
101cbcc
added --teach-me to NearArgs
Oct 11, 2024
c8ed557
refactored tracing
Oct 28, 2024
6efd6d7
updated README
Oct 29, 2024
dbcf698
Update README.md
FroVolod Oct 29, 2024
537d774
fixed
Nov 6, 2024
ec5a804
returned the fragment lost during the merge operation
Nov 7, 2024
3eb0d03
fixed
Nov 7, 2024
e2b74f3
updated to main fa934ed
Nov 8, 2024
7b1ee9e
Merge branch 'main' into teach-me
FroVolod Nov 8, 2024
7936c4b
fixed CI
Nov 8, 2024
39e6582
updated near-cli-rs (request-payload-for-broadcast-tx-commit)
Nov 10, 2024
da48e23
reverted stable version of near-cli-rs
Nov 11, 2024
8981d79
corrected spelling errors
Nov 13, 2024
e2b3dad
removed duplicate `--teach-me` flag
Nov 13, 2024
1c4d219
fixed output console command
Nov 13, 2024
f69b78c
deleted information about "Setting cargo working dir to ..." as redun…
Nov 13, 2024
6d19ed5
reverted to tracing::debug!
Nov 13, 2024
c0a48a2
changed "RUST_LOG=cargo_near=info" to "RUST_LOG=info"
Nov 13, 2024
7ffa162
reverted to {:#?} format for cargo_command
Nov 13, 2024
323a439
refactored print_env()
Nov 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ cargo near

Starts interactive mode that will allow to explore all the available commands.

---
Use `--teach-me` flag if you want to know "how it works".

```console
cargo near --teach-me
```

Starts an interactive mode with an explanation of the execution of the selected command.

---
Additionally depends on [Git](https://git-scm.com/) binary being installed, besides [cargo](https://github.com/rust-lang/cargo).

Expand Down
1 change: 1 addition & 0 deletions cargo-near-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tempfile = { version = "3.10.1", optional = true }
shell-words = { version = "1.0.0", optional = true }
wasm-opt = "=0.116.1"
humantime = "2.1.0"
indenter = "0.3"

[target.'cfg(target_os = "linux")'.dependencies]
nix = { version = "0.29.0", features = ["user", "process"], optional = true }
Expand Down
14 changes: 12 additions & 2 deletions 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 @@ -121,7 +122,11 @@ where

if let Some(path) = working_dir {
let path = crate::fs::force_canonicalize_dir(path.as_ref())?;
tracing::debug!("Setting cargo working dir to '{}'", path);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Setting cargo working dir to '{}'", path
);
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
cmd.current_dir(path);
}

Expand All @@ -134,7 +139,12 @@ where
ColorPreference::Never => cmd.args(["--color", "never"]),
};

tracing::info!("Invoking cargo: {:#?}", cmd);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Invoking cargo:\n{}",
pretty_print::indent_payload(&format!("{:#?}", cmd))
);

let mut child = cmd
// capture the stdout to return from this function as bytes
Expand Down
33 changes: 26 additions & 7 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 All @@ -11,12 +13,16 @@ pub fn wasm32_exists() -> bool {
Ok(wasm32_target_libdir_path) => {
if wasm32_target_libdir_path.exists() {
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Found {COMPILATION_TARGET} in {:?}",
wasm32_target_libdir_path
);
true
} else {
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Failed to find {COMPILATION_TARGET} in {:?}",
wasm32_target_libdir_path
);
Expand All @@ -38,16 +44,24 @@ pub fn wasm32_exists() -> bool {
}

fn get_rustc_wasm32_unknown_unknown_target_libdir() -> eyre::Result<PathBuf> {
let command = Command::new("rustc")
.args(["--target", COMPILATION_TARGET, "--print", "target-libdir"])
.output()?;
let mut command = Command::new("rustc");
command.args(["--target", COMPILATION_TARGET, "--print", "target-libdir"]);

tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Execution command:\n{}",
pretty_print::indent_payload(&format!("`{:?}`", command).replace("\"", ""))
);

if command.status.success() {
Ok(String::from_utf8(command.stdout)?.trim().into())
let output = command.output()?;

if output.status.success() {
Ok(String::from_utf8(output.stdout)?.trim().into())
} else {
eyre::bail!(
"Getting rustc's wasm32-unknown-unknown target wasn't successful. Got {}",
command.status,
output.status,
)
}
}
Expand All @@ -62,7 +76,12 @@ where
let mut cmd = Command::new(rustup);
cmd.args(args);

tracing::info!("Invoking rustup: {:?}", cmd);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Invoking rustup:\n{}",
pretty_print::indent_payload(&format!("`{}`", format!("{:?}", cmd).replace("\"", "")))
);

let child = cmd
.stdout(std::process::Stdio::piped())
Expand Down
15 changes: 13 additions & 2 deletions cargo-near-build/src/env_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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 ===========
/// NEP-330 1.2.0
Expand All @@ -35,12 +37,21 @@ pub mod nep330 {
}

pub(crate) fn print_env() {
tracing::info!("Variables, relevant for reproducible builds:");
let mut env_map: HashMap<&str, String> = HashMap::new();
for key in [BUILD_ENVIRONMENT, CONTRACT_PATH, SOURCE_CODE_SNAPSHOT] {
let value = std::env::var(key)
.map(|val| format!("'{}'", val))
.unwrap_or("unset".to_string());
tracing::info!("{}={}", key, value);
env_map.insert(
key,
value.replace("'", "").replace("\"", "").replace(",", " "),
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
);
}
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Variables, relevant for reproducible builds:\n{}",
pretty_print::indent_payload(&format!("{:#?}", env_map))
);
}
}
12 changes: 10 additions & 2 deletions 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 All @@ -12,7 +13,9 @@ pub fn extract_abi_entries(
let dylib_path: &Utf8Path = &artifact.path;
let dylib_file_contents = fs::read(dylib_path)?;
let object = symbolic_debuginfo::Object::parse(&dylib_file_contents)?;
tracing::debug!(
tracing::info!(
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
target: "near_teach_me",
parent: &tracing::Span::none(),
"A dylib was built at {:?} with format {} for architecture {}",
&dylib_path,
&object.file_format(),
Expand All @@ -26,7 +29,12 @@ pub fn extract_abi_entries(
if near_abi_symbols.is_empty() {
eyre::bail!("No NEAR ABI symbols found in the dylib");
}
tracing::debug!("Detected NEAR ABI symbols: {:?}", &near_abi_symbols);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Detected NEAR ABI symbols:\n{}",
pretty_print::indent_payload(&format!("{:#?}", &near_abi_symbols))
);

let mut entries = vec![];
unsafe {
Expand Down
15 changes: 13 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 @@ -71,7 +72,12 @@ pub fn run(
&container_paths.crate_path,
];
let stdin_is_terminal = std::io::stdin().is_terminal();
tracing::debug!("input device is a tty: {}", stdin_is_terminal);
tracing::info!(
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
target: "near_teach_me",
parent: &tracing::Span::none(),
"Input device is a TTY:\n{}",
pretty_print::indent_payload(&stdin_is_terminal.to_string())
);
if stdin_is_terminal
&& std::env::var(env_keys::nep330::nonspec::SERVER_DISABLE_INTERACTIVE).is_err()
{
Expand All @@ -83,7 +89,12 @@ pub fn run(
docker_args.extend(vec![&docker_image, "/bin/bash", "-c"]);

docker_args.push(&shell_escaped_cargo_cmd);
tracing::debug!("docker command : {:#?}", docker_args);
tracing::info!(
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
target: "near_teach_me",
parent: &tracing::Span::none(),
"Docker command:\n{}",
pretty_print::indent_payload(&format!("{:#?}", 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
}
19 changes: 17 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, OptionExt, WrapErr};

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

use super::manifest_path::ManifestPath;
Expand Down Expand Up @@ -67,7 +68,11 @@ impl CrateMetadata {
} else {
self.target_directory.clone()
};
tracing::debug!("resolved output directory: {}", result);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Resolved output directory: {}", result
);
Ok(result)
}

Expand Down Expand Up @@ -136,7 +141,11 @@ fn get_cargo_metadata(
no_locked: bool,
cargo_target_dir: Option<&buildtime_env::CargoTargetDir>,
) -> eyre::Result<(cargo_metadata::Metadata, Package)> {
tracing::info!("Fetching cargo metadata for {}", manifest_path.path);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Fetching cargo metadata for {}", manifest_path.path
);
let mut cmd = MetadataCommand::new();
if !no_locked {
cmd.other_options(["--locked".to_string()]);
Expand All @@ -147,6 +156,12 @@ fn get_cargo_metadata(
}
let cmd = cmd.manifest_path(&manifest_path.path);
tracing::debug!("metadata command: {:#?}", cmd.cargo_command());
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Execution command:\n{}",
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
pretty_print::indent_payload(&format!("`{:?}`", cmd.cargo_command()).replace("\"", ""))
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
);
let metadata = cmd.exec();
if let Err(cargo_metadata::Error::CargoMetadata { stderr }) = metadata.as_ref() {
if stderr.contains("remove the --locked flag") {
Expand Down
7 changes: 6 additions & 1 deletion cargo-near/src/commands/build_command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ fn get_env_key_vals(input: Vec<String>) -> Vec<(String, String)> {
let dedup_map: HashMap<String, String> = HashMap::from_iter(iterator);

let result = dedup_map.into_iter().collect();
tracing::trace!("passed additional environment pairs: {:#?}", result);
tracing::info!(
target: "near_teach_me",
parent: &tracing::Span::none(),
"Passed additional environment pairs:\n{}",
near_cli_rs::common::indent_payload(&format!("{:#?}", result))
);
result
}

Expand Down
Loading
Loading