Skip to content

Commit

Permalink
feat: Add helper function for consistent forc output (#6208)
Browse files Browse the repository at this point in the history
## Description

Makes the indendation and formatting consistent for forc's action
messages.

### forc build

before

![image](https://github.com/FuelLabs/sway/assets/47993817/08d8a30a-82a0-48f5-92d5-1b6c7329ae57)

after

![image](https://github.com/FuelLabs/sway/assets/47993817/5dfd4b78-2f7c-47eb-92cf-5c3f518557ef)

### forc test

before

![image](https://github.com/FuelLabs/sway/assets/47993817/6eb2d6fe-416b-47ee-97b2-bcd2fabee4db)

after

![image](https://github.com/FuelLabs/sway/assets/47993817/68ee4bf3-7dba-4c26-b696-fd8d6ad50a98)

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.
  • Loading branch information
sdankel authored Jul 3, 2024
1 parent db8ba0b commit edff10d
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 71 deletions.
24 changes: 23 additions & 1 deletion Cargo.lock

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

15 changes: 7 additions & 8 deletions forc-pkg/src/lock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{pkg, source, DepKind, Edge};
use anyhow::{anyhow, Result};
use forc_tracing::{println_action_green, println_action_red};
use petgraph::{visit::EdgeRef, Direction};
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -351,10 +352,9 @@ where
true => format!(" {}", pkg.source),
false => String::new(),
};
tracing::info!(
" {} {}{src}",
ansi_term::Colour::Red.bold().paint("Removing"),
ansi_term::Style::new().bold().paint(&pkg.name)
println_action_red(
"Removing",
&format!("{}{src}", ansi_term::Style::new().bold().paint(&pkg.name)),
);
}
}
Expand All @@ -370,10 +370,9 @@ where
true => format!(" {}", pkg.source),
false => "".to_string(),
};
tracing::info!(
" {} {}{src}",
ansi_term::Colour::Green.bold().paint("Adding"),
ansi_term::Style::new().bold().paint(&pkg.name)
println_action_green(
"Adding",
&format!("{}{src}", ansi_term::Style::new().bold().paint(&pkg.name)),
);
}
}
Expand Down
27 changes: 17 additions & 10 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
BuildProfile,
};
use anyhow::{anyhow, bail, Context, Error, Result};
use forc_tracing::println_warning;
use forc_tracing::{println_action_green, println_warning};
use forc_util::{
default_output_directory, find_file_name, kebab_to_snake_case, print_compiling,
print_on_failure, print_warnings,
Expand Down Expand Up @@ -497,7 +497,7 @@ impl BuiltPackage {
let json_abi_path = output_dir.join(program_abi_stem).with_extension("json");
self.write_json_abi(&json_abi_path, minify)?;

info!(" Bytecode size: {} bytes", self.bytecode.bytes.len());
debug!(" Bytecode size: {} bytes", self.bytecode.bytes.len());
// Additional ops required depending on the program type
match self.tree_type {
TreeType::Contract => {
Expand Down Expand Up @@ -531,7 +531,7 @@ impl BuiltPackage {
let hash_file_name = format!("{}{}", &pkg_name, SWAY_BIN_HASH_SUFFIX);
let hash_path = output_dir.join(hash_file_name);
fs::write(hash_path, &bytecode_hash)?;
info!(" Bytecode hash: {}", bytecode_hash);
debug!(" Bytecode hash: {}", bytecode_hash);
}
_ => (),
}
Expand Down Expand Up @@ -713,7 +713,10 @@ impl BuildPlan {
cause,
);
}
info!(" Creating a new `Forc.lock` file. (Cause: {})", cause);
println_action_green(
"Creating",
&format!("a new `Forc.lock` file. (Cause: {})", cause),
);
let member_names = manifests
.iter()
.map(|(_, manifest)| manifest.project.name.to_string())
Expand All @@ -723,7 +726,7 @@ impl BuildPlan {
.map_err(|e| anyhow!("failed to serialize lock file: {}", e))?;
fs::write(lock_path, string)
.map_err(|e| anyhow!("failed to write lock file: {}", e))?;
info!(" Created new lock file at {}", lock_path.display());
debug!(" Created new lock file at {}", lock_path.display());
}

Ok(plan)
Expand Down Expand Up @@ -2171,11 +2174,13 @@ pub fn build_with_options(build_options: &BuildOpts) -> Result<Built> {
)?;
let output_dir = pkg.output_directory.as_ref().map(PathBuf::from);

let finished = ansi_term::Colour::Green.bold().paint("Finished");
info!(
" {finished} {} in {:.2}s",
profile_target_string(&build_profile.name, build_target),
build_start.elapsed().as_secs_f32()
println_action_green(
"Finished",
&format!(
"{} in {:.2}s",
profile_target_string(&build_profile.name, build_target),
build_start.elapsed().as_secs_f32()
),
);
for (node_ix, built_package) in built_packages {
print_pkg_summary_header(&built_package);
Expand Down Expand Up @@ -2299,6 +2304,8 @@ pub fn build(
let manifest = &plan.manifest_map()[&pkg.id()];
let program_ty = manifest.program_type().ok();

// TODO: Only print "Compiling" when the dependency is not already compiled.
// https://github.com/FuelLabs/sway/issues/6209
print_compiling(
program_ty.as_ref(),
&pkg.name,
Expand Down
14 changes: 8 additions & 6 deletions forc-pkg/src/source/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
source,
};
use anyhow::{anyhow, bail, Context, Result};
use forc_tracing::println_action_green;
use forc_util::git_checkouts_directory;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
Expand All @@ -15,7 +16,6 @@ use std::{
path::{Path, PathBuf},
str::FromStr,
};
use tracing::info;

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct Url {
Expand Down Expand Up @@ -204,11 +204,13 @@ impl source::Fetch for Pinned {
{
let _guard = lock.write()?;
if !repo_path.exists() {
info!(
" {} {} {}",
ansi_term::Color::Green.bold().paint("Fetching"),
ansi_term::Style::new().bold().paint(ctx.name),
self
println_action_green(
"Fetching",
&format!(
"{} {}",
ansi_term::Style::new().bold().paint(ctx.name),
self
),
);
fetch(ctx.fetch_id(), ctx.name(), self)?;
}
Expand Down
29 changes: 15 additions & 14 deletions forc-pkg/src/source/ipfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
source,
};
use anyhow::Result;
use forc_tracing::println_action_green;
use futures::TryStreamExt;
use ipfs_api::IpfsApi;
use ipfs_api_backend_hyper as ipfs_api;
Expand All @@ -14,7 +15,6 @@ use std::{
str::FromStr,
};
use tar::Archive;
use tracing::info;

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Cid(cid::Cid);
Expand Down Expand Up @@ -67,29 +67,30 @@ impl source::Fetch for Pinned {
{
let _guard = lock.write()?;
if !repo_path.exists() {
info!(
" {} {} {}",
ansi_term::Color::Green.bold().paint("Fetching"),
ansi_term::Style::new().bold().paint(ctx.name),
self
println_action_green(
"Fetching",
&format!(
"{} {}",
ansi_term::Style::new().bold().paint(ctx.name),
self
),
);
let cid = &self.0;
let ipfs_client = ipfs_client();
let dest = cache_dir();
futures::executor::block_on(async {
match ctx.ipfs_node() {
source::IPFSNode::Local => {
info!(
" {} with local IPFS node",
ansi_term::Color::Green.bold().paint("Fetching")
);
println_action_green("Fetching", "with local IPFS node");
cid.fetch_with_client(&ipfs_client, &dest).await
}
source::IPFSNode::WithUrl(ipfs_node_gateway_url) => {
info!(
" {} from {}. Note: This can take several minutes.",
ansi_term::Color::Green.bold().paint("Fetching"),
ipfs_node_gateway_url
println_action_green(
"Fetching",
&format!(
"from {}. Note: This can take several minutes.",
ipfs_node_gateway_url
),
);
cid.fetch_with_gateway_url(ipfs_node_gateway_url, &dest)
.await
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ repository.workspace = true
[dependencies]
anyhow = "1.0.65"
clap = { version = "4.5.4", features = ["derive"] }
colored = "2.0.0"
comrak = "0.16"
forc-pkg = { version = "0.61.1", path = "../../forc-pkg" }
forc-tracing = { version = "0.61.1", path = "../../forc-tracing" }
forc-util = { version = "0.61.1", path = "../../forc-util" }
horrorshow = "0.8.4"
include_dir = "0.7.3"
Expand Down
28 changes: 16 additions & 12 deletions forc-plugins/forc-doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ pub mod tests;

use anyhow::{bail, Result};
use cli::Command;
use colored::Colorize;
use doc::Documentation;
use forc_pkg as pkg;
use forc_pkg::{
manifest::{GenericManifestFile, ManifestFile},
PackageManifestFile,
};
use forc_tracing::println_action_green;
use forc_util::default_output_directory;
use render::RenderedDocumentation;
use std::{
Expand Down Expand Up @@ -77,11 +77,13 @@ pub fn compile_html(
}
fs::create_dir_all(&doc_path)?;

println!(
" {} {} ({})",
"Compiling".bold().yellow(),
pkg_manifest.project_name(),
manifest.dir().to_string_lossy()
println_action_green(
"Compiling",
&format!(
"{} ({})",
pkg_manifest.project_name(),
manifest.dir().to_string_lossy()
),
);

let member_manifests = manifest.member_manifests()?;
Expand Down Expand Up @@ -179,11 +181,13 @@ fn build_docs(
pkg_manifest,
} = program_info;

println!(
" {} documentation for {} ({})",
"Building".bold().yellow(),
pkg_manifest.project_name(),
manifest.dir().to_string_lossy()
println_action_green(
"Building",
&format!(
"documentation for {} ({})",
pkg_manifest.project_name(),
manifest.dir().to_string_lossy()
),
);

let raw_docs = Documentation::from_ty_program(
Expand All @@ -210,7 +214,7 @@ fn build_docs(

// write file contents to doc folder
write_content(rendered_docs, doc_path)?;
println!(" {}", "Finished".bold().yellow());
println_action_green("Finished", pkg_manifest.project_name());

Ok(raw_docs)
}
Expand Down
3 changes: 3 additions & 0 deletions forc-tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ repository.workspace = true
ansi_term = "0.12"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["ansi", "env-filter", "json"] }

[dev-dependencies]
tracing-test = "0.2"
Loading

0 comments on commit edff10d

Please sign in to comment.