Skip to main content

wapm_cli/data/lock/
lockfile_module.rs

1use crate::abi::Abi;
2use crate::data::manifest::{Module, PACKAGES_DIR_NAME};
3use crate::util;
4use semver::Version;
5use std::path::{Path, PathBuf};
6
7/// legacy Lockfile module struct; which is only used to parse legacy lockfiles which get
8/// transformed into up to date ones (V1, V2)
9#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
10pub struct LockfileModuleV2 {
11    pub name: String,
12    pub package_version: String,
13    pub package_name: String,
14    pub source: String,
15    pub resolved: String,
16    pub abi: Abi,
17    pub entry: String,
18}
19
20/// legacy Lockfile module struct; which is only used to parse legacy lockfiles which get
21/// transformed into up to date ones (V3)
22#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
23pub struct LockfileModuleV3 {
24    pub name: String,
25    pub package_version: String,
26    pub package_name: String,
27    pub source: String,
28    pub resolved: String,
29    pub abi: Abi,
30    /// The entry is where the wasm module lives
31    pub entry: String,
32    /// The root is where the manifest lives
33    pub root: String,
34    /// The hash of the wasm module cached here for faster startup time
35    pub prehashed_module_key: Option<String>,
36}
37
38/// The latest Lockfile module struct (V4)
39/// It contains data relating to the Wasm module itself
40#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
41pub struct LockfileModule {
42    pub name: String,
43    pub package_version: String,
44    pub package_name: String,
45    pub package_path: String,
46    pub resolved: String,
47    pub resolved_source: String,
48    pub abi: Abi,
49    /// The source path is where the wasm module lives
50    pub source: String,
51    /// The hash of the wasm module cached here for faster startup time
52    pub prehashed_module_key: Option<String>,
53}
54
55pub type LockfileModuleV4 = LockfileModule;
56
57impl LockfileModule {
58    pub fn from_module(
59        manifest_base_dir_path: &Path,
60        name: &str,
61        version: &Version,
62        module: &Module,
63        download_url: &str,
64    ) -> Self {
65        // build the entry path
66        // this is path like /wapm_packages/_/lua@0.1.3/path/to/module/lua.wasm
67        let path = PathBuf::from(manifest_base_dir_path);
68
69        let source = {
70            let mut new_style = path.clone();
71            new_style.push(&module.source);
72            if new_style.exists() {
73                module.source.to_string_lossy().to_string()
74            } else {
75                // to prevent breaking packages published before this change (~2019/06/25)
76                module
77                    .source
78                    .file_name()
79                    .unwrap()
80                    .to_string_lossy()
81                    .to_string()
82            }
83        };
84
85        LockfileModule {
86            name: module.name.clone(),
87            package_version: version.to_string(),
88            package_name: name.to_string(),
89            package_path: format!("{}@{}", name, version),
90            resolved: download_url.to_string(),
91            resolved_source: format!("registry+{}", module.name),
92            abi: module.abi,
93            prehashed_module_key: util::get_hashed_module_key(&path.join(&source)),
94            source,
95        }
96    }
97
98    pub fn from_local_module(
99        manifest_base_dir_path: &Path,
100        name: &str,
101        version: &Version,
102        module: &Module,
103    ) -> Self {
104        let mut wasm_module_full_path = PathBuf::from(manifest_base_dir_path);
105        wasm_module_full_path.push(&module.source);
106
107        LockfileModule {
108            name: module.name.clone(),
109            package_version: version.to_string(),
110            package_name: name.to_string(),
111            package_path: format!("{}@{}", name, version),
112            resolved: "local".to_string(),
113            resolved_source: "local".to_string(),
114            abi: module.abi,
115            source: module.source.to_string_lossy().to_string(),
116            prehashed_module_key: util::get_hashed_module_key(&wasm_module_full_path),
117        }
118    }
119
120    /// Returns the full, absolute path to the WASM module
121    pub fn get_canonical_source_path_from_lockfile_dir(
122        &self,
123        mut lockfile_dir: PathBuf,
124    ) -> PathBuf {
125        lockfile_dir.push(PACKAGES_DIR_NAME);
126        lockfile_dir.push(&self.package_path);
127        lockfile_dir.push(&self.source);
128
129        lockfile_dir
130    }
131
132    /// Returns the Manifest path from the lockfile
133    ///
134    /// This method does extra logic to detect if the lockfile is global and adjusts accordingly.
135    ///
136    /// The `local_dep` flag should be passed when its known that the manifest we want to access
137    /// is not in the current directory and that we need to add `wapm_packages/...` to it.
138    pub fn get_canonical_manifest_path_from_lockfile_dir(
139        &self,
140        mut lockfile_dir: PathBuf,
141        local_dep: bool,
142    ) -> PathBuf {
143        if crate::config::Config::get_globals_directory().expect("Could not get globals direcotry")
144            == lockfile_dir
145            || local_dep
146        {
147            lockfile_dir.push(PACKAGES_DIR_NAME);
148            lockfile_dir.push(&self.package_path);
149
150            lockfile_dir
151        } else {
152            lockfile_dir
153        }
154    }
155}