multiversx_sc_meta_lib/
print_util.rs

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