multiversx_sc_meta_lib/
print_util.rs

1use std::{path::Path, process::Command};
2
3use colored::Colorize;
4
5/// Just for convenience, since we seem to be printing many things in green.
6///
7/// The argument is of type `String` because the argument is always a `format!` expression.
8pub fn println_green(s: String) {
9    println!("{}", s.green());
10}
11
12pub fn format_command(command: &Command) -> String {
13    let mut result = String::new();
14    for (key, opt_value) in command.get_envs() {
15        if let Some(value) = opt_value {
16            result +=
17                format!("{}=\"{}\" ", key.to_string_lossy(), value.to_string_lossy()).as_str();
18        }
19    }
20    result.push_str(command.get_program().to_string_lossy().as_ref());
21
22    for arg in command.get_args() {
23        result.push(' ');
24        result.push_str(arg.to_string_lossy().as_ref());
25    }
26
27    result
28}
29
30pub fn print_build_command(contract_name: String, command: &Command) {
31    let path = command
32        .get_current_dir()
33        .expect("missing command dir")
34        .canonicalize()
35        .expect("command dir canonicalization failed");
36    println!(
37        "{}\n{}",
38        format!("Building {} in {} ...", contract_name, path.display()).green(),
39        format_command(command).green(),
40    );
41}
42
43pub fn print_copy_contract(source_wasm_path: &str, output_wasm_path: &str) {
44    println!(
45        "{}",
46        format!("Copying {source_wasm_path} to {output_wasm_path} ...").green(),
47    );
48}
49
50pub fn print_call_wasm_opt(wasm_path: &str) {
51    println_green(format!("Calling wasm-opt on {wasm_path} ..."));
52}
53
54pub fn print_call_wasm2wat(wasm_path: &str, wat_path: &str) {
55    println_green(format!("Extracting wat from {wasm_path} to {wat_path} ..."));
56}
57
58pub fn print_pack_mxsc_file(output_mxsc_path: &str) {
59    println_green(format!("Packing {output_mxsc_path} ..."));
60}
61
62pub fn print_contract_size(size: usize) {
63    println!("{}", format!("Contract size: {size} bytes.").blue(),);
64}
65
66pub fn print_extract_imports(imports_path: &str) {
67    println_green(format!("Extracting imports to {imports_path} ..."));
68}
69
70pub fn print_check_ei(ei_version: &str) {
71    print!(
72        "{}",
73        format!("Checking EI version: {ei_version} ...").green(),
74    );
75}
76
77pub fn print_invalid_vm_hook(import_name: &str, ei_version: &str) {
78    print!(
79        "\n{}",
80        format!("WARNING! Import '{import_name}' is not available on EI version {ei_version}!")
81            .yellow(),
82    );
83}
84
85pub fn print_check_ei_ok() {
86    println!("{}", " OK".green(),);
87}
88
89pub fn print_ignore_ei_check() {
90    println!("{}", "EI version check explicitly ignored".yellow(),);
91}
92
93pub fn print_workspace_target_dir(target_path_str: &str) {
94    println_green(format!(
95        "Using workspace target directory: {target_path_str} ..."
96    ));
97}
98
99pub fn print_removing_wasm_crate(dir_name: &str) {
100    println!("{}", format!("Removing wasm crate: {dir_name}").red(),);
101}
102
103pub fn print_sc_config_main_deprecated(path: &Path) {
104    println!(
105        "{}",
106        format!(
107            "In {}: `main` field under `[settings]` is now deprecated",
108            path.display()
109        )
110        .yellow(),
111    );
112}
113
114pub fn print_proxy_error(path: &Path, error: String) {
115    println!(
116        "{}",
117        format!("Could not write proxy file {}: {error}", path.display()).red(),
118    );
119}