Skip to content

Commit

Permalink
feat(forc-pkg): Improve workspace build summary (FuelLabs#4188)
Browse files Browse the repository at this point in the history
*This PR is based on FuelLabs#4187 - will rebase to master once it lands.*

---

## Description

Previously when building a large workspace, the build would often end
followed by a bunch of lines saying "Bytecode size is ..." or "Bytecode
hash is ...", without any indication of which workspace member the
information was associated with.

This changes the behaviour to first output a "Finished" line, followed
by build output summary info clearly separated by the workspace member.

Closes FuelLabs#3779.

Here's an example of building our `examples/` workspace previously:

![Screenshot from 2023-02-26
21-48-18](https://user-images.githubusercontent.com/4587373/221406514-b4ee406b-7804-40e7-8710-a2febc902032.png)

And here's the output with this PR:
 
![Screenshot from 2023-02-26
21-49-14](https://user-images.githubusercontent.com/4587373/221406525-b709749d-22ba-4677-ad39-8eec15c5392d.png)

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] 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).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
mitchmindtree authored Feb 26, 2023
1 parent f357e24 commit 4582f67
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl BuiltPackage {
}
}

info!(" Bytecode size is {} bytes.", self.bytecode.len());
info!(" Bytecode size: {} bytes", self.bytecode.len());
// Additional ops required depending on the program type
match self.tree_type {
TreeType::Contract => {
Expand All @@ -458,15 +458,15 @@ impl BuiltPackage {
let root_file_name = format!("{}{}", &pkg_name, SWAY_BIN_ROOT_SUFFIX);
let root_path = output_dir.join(root_file_name);
fs::write(root_path, &root)?;
info!(" Predicate root: {}", root);
info!(" Predicate root: {}", root);
}
TreeType::Script => {
// hash the bytecode for scripts and store the result in a file in the output directory
let bytecode_hash = format!("0x{}", fuel_crypto::Hasher::hash(&self.bytecode));
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!(" Script bytecode hash: {}", bytecode_hash);
info!(" Bytecode hash: {}", bytecode_hash);
}
_ => (),
}
Expand Down Expand Up @@ -1971,6 +1971,7 @@ pub fn build_with_options(build_options: BuildOpts) -> Result<Built> {

// Build it!
let mut built_workspace = HashMap::new();
let build_start = std::time::Instant::now();
let built_packages = build(
&build_plan,
*build_target,
Expand All @@ -1979,7 +1980,11 @@ pub fn build_with_options(build_options: BuildOpts) -> Result<Built> {
const_inject_map,
)?;
let output_dir = pkg.output_directory.as_ref().map(PathBuf::from);

let finished = ansi_term::Colour::Green.bold().paint("Finished");
info!(" {finished} {profile_name} in {:?}", build_start.elapsed());
for (node_ix, built_package) in built_packages.into_iter() {
print_pkg_summary_header(&built_package);
let pinned = &graph[node_ix];
let pkg_manifest = manifest_map
.get(&pinned.id())
Expand Down Expand Up @@ -2009,6 +2014,18 @@ pub fn build_with_options(build_options: BuildOpts) -> Result<Built> {
}
}

fn print_pkg_summary_header(built_pkg: &BuiltPackage) {
let prog_ty_str = forc_util::program_type_str(&built_pkg.tree_type);
// The ansi_term formatters ignore the `std::fmt` right-align
// formatter, so we manually calculate the padding to align the program
// type and name around the 10th column ourselves.
let padded_ty_str = format!("{prog_ty_str:>10}");
let padding = &padded_ty_str[..padded_ty_str.len() - prog_ty_str.len()];
let ty_ansi = ansi_term::Colour::Green.bold().paint(prog_ty_str);
let name_ansi = ansi_term::Style::new().bold().paint(&built_pkg.pkg_name);
info!("{padding}{ty_ansi} {name_ansi}");
}

/// Returns the ContractId of a built_package contract with specified `salt`.
pub fn contract_id(built_package: &BuiltPackage, salt: &fuel_tx::Salt) -> ContractId {
// Construct the contract ID
Expand Down

0 comments on commit 4582f67

Please sign in to comment.