multiversx_sc_meta/cmd/
local_deps.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::{
    cli::LocalDepsArgs,
    folder_structure::{
        dir_pretty_print, RelevantDirectories, CARGO_TOML_FILE_NAME, FRAMEWORK_CRATE_NAMES,
    },
};
use common_path::common_path_all;
use multiversx_sc_meta_lib::cargo_toml::CargoTomlContents;
use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, LinkedList},
    fs::File,
    io::Write,
    path::{Path, PathBuf},
};

#[derive(Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct LocalDeps {
    pub root: String,
    pub contract_path: String,
    pub common_dependency_path: Option<String>,
    pub dependencies: Vec<LocalDep>,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct LocalDep {
    pub path: String,
    pub depth: usize,
}

impl LocalDep {
    fn new(path: &Path, depth: usize) -> Self {
        LocalDep {
            path: path.to_string_lossy().to_string(),
            depth,
        }
    }
}

pub fn local_deps(args: &LocalDepsArgs) {
    let path = if let Some(some_path) = &args.path {
        some_path.as_str()
    } else {
        "./"
    };

    perform_local_deps(path, args.ignore.as_slice());
}

fn perform_local_deps(path: impl AsRef<Path>, ignore: &[String]) {
    let root_path = path.as_ref();
    let dirs = RelevantDirectories::find_all(root_path, ignore);
    dir_pretty_print(dirs.iter_contract_crates(), "", &|_| {});

    for contract_dir in dirs.iter_contract_crates() {
        let mut dep_map = BTreeMap::new();

        let output_dir_path = contract_dir.path.join("output");
        std::fs::create_dir_all(&output_dir_path).unwrap();

        expand_deps(root_path, contract_dir.path.clone(), &mut dep_map);

        let common_dependency_path = common_path_all(dep_map.keys().map(|pbuf| pbuf.as_path()));

        let deps_contents = LocalDeps {
            root: root_path.to_string_lossy().to_string(),
            contract_path: contract_dir.path.to_string_lossy().to_string(),
            common_dependency_path: common_dependency_path
                .map(|pbuf| pbuf.to_string_lossy().to_string()),
            dependencies: dep_map.values().cloned().collect(),
        };

        let mut deps_file = File::create(output_dir_path.join("local_deps.txt")).unwrap();
        writeln!(deps_file, "{}", serialize_local_deps_json(&deps_contents)).unwrap();
    }
}

fn expand_deps(
    root_path: &Path,
    starting_path: PathBuf,
    dep_map: &mut BTreeMap<PathBuf, LocalDep>,
) {
    let mut queue: LinkedList<PathBuf> = LinkedList::new();
    queue.push_back(starting_path);
    while let Some(parent) = queue.pop_front() {
        let cargo_toml_path = parent.join(CARGO_TOML_FILE_NAME);
        let cargo_toml_contents = CargoTomlContents::load_from_file(cargo_toml_path);
        let local_paths = cargo_toml_contents.local_dependency_paths(FRAMEWORK_CRATE_NAMES);
        for child in &local_paths {
            let full_path = parent.join(child).canonicalize().unwrap();
            let child_path = pathdiff::diff_paths(&full_path, root_path).unwrap();
            let parent_depth = if let Some(parent_dep) = dep_map.get(&parent) {
                parent_dep.depth
            } else {
                0
            };
            let child_depth = parent_depth + 1;
            if let Some(local_dep) = dep_map.get_mut(&full_path) {
                if child_depth < local_dep.depth {
                    local_dep.depth = child_depth;
                }
            } else {
                dep_map.insert(
                    full_path.clone(),
                    LocalDep::new(child_path.as_path(), child_depth),
                );
                queue.push_back(full_path);
            }
        }
    }
}

fn serialize_local_deps_json(deps_contents: &LocalDeps) -> String {
    let buf = Vec::new();
    let formatter = serde_json::ser::PrettyFormatter::with_indent(b"    ");
    let mut ser = serde_json::Serializer::with_formatter(buf, formatter);
    deps_contents.serialize(&mut ser).unwrap();
    let mut serialized = String::from_utf8(ser.into_inner()).unwrap();
    serialized.push('\n');
    serialized
}