multiversx_sc_meta_lib/contract/
wasm_cargo_toml_generate.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use super::wasm_cargo_toml_data::WasmCargoTomlData;
use crate::cargo_toml::{change_from_base_to_adapter_path, CargoTomlContents, DependencyRawValue};

const WASM_ADAPTER: &str = "multiversx-sc-wasm-adapter";
const CDYLIB_CRATE_TYPE: &str = "cdylib";

pub fn generate_wasm_cargo_toml(
    cargo_toml_data: &WasmCargoTomlData,
    crate_name: &str,
) -> CargoTomlContents {
    let mut new_cargo = CargoTomlContents::new();

    //set cargo toml prepend auto generate status
    new_cargo.prepend_auto_generated_comment = true;

    //add package info
    new_cargo.add_package_info(
        &cargo_toml_data.name,
        "0.0.0".to_string(),
        cargo_toml_data.edition.clone(),
        false,
    );

    //add crate type
    new_cargo.add_crate_type(CDYLIB_CRATE_TYPE);

    //add profile
    new_cargo.add_contract_variant_profile(&cargo_toml_data.profile);

    //add deps
    add_wasm_crate_deps(
        &mut new_cargo,
        crate_name,
        &cargo_toml_data.framework_dependency,
    );

    //check features
    if !cargo_toml_data.contract_features.is_empty() {
        new_cargo.change_features_for_parent_crate_dep(
            cargo_toml_data.contract_features.as_slice(),
            cargo_toml_data.contract_default_features,
        );
    }

    //insert default workspace
    new_cargo.add_workspace(&["."]);

    new_cargo
}

fn add_wasm_crate_deps(
    cargo_toml_contents: &mut CargoTomlContents,
    crate_name: &str,
    framework_dependency: &DependencyRawValue,
) {
    let mut wasm_adapter_dep = framework_dependency.clone();
    if let Some(path) = &mut wasm_adapter_dep.path {
        *path = change_from_base_to_adapter_path(path);
    }

    cargo_toml_contents.insert_dependency_raw_value(
        crate_name,
        DependencyRawValue {
            path: Some("..".to_owned()),
            ..Default::default()
        },
    );

    cargo_toml_contents.insert_dependency_raw_value(WASM_ADAPTER, wasm_adapter_dep);
}