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
use crate::{sandbox::utils::OnDiskStateView, DEFAULT_BUILD_DIR};
use anyhow::Result;
use move_command_line_common::env::get_bytecode_version_from_env;
use move_package::{compilation::compiled_package::CompiledPackage, BuildConfig};
use std::path::{Path, PathBuf};
pub struct PackageContext {
package: CompiledPackage,
build_dir: PathBuf,
}
impl PackageContext {
pub fn new(path: &Option<PathBuf>, build_config: &BuildConfig) -> Result<Self> {
let path = path.as_deref().unwrap_or_else(|| Path::new("."));
let build_dir = build_config
.install_dir
.as_ref()
.unwrap_or(&PathBuf::from(DEFAULT_BUILD_DIR))
.clone();
let package = build_config
.clone()
.compile_package(path, &mut Vec::new())?;
Ok(PackageContext { package, build_dir })
}
pub fn prepare_state(&self, storage_dir: &Path) -> Result<OnDiskStateView> {
let bytecode_version = get_bytecode_version_from_env();
let state = OnDiskStateView::create(self.build_dir.as_path(), storage_dir)?;
let package = self.package();
let new_modules = package
.deps_compiled_units
.iter()
.map(|(_, unit)| match &unit.unit {
move_compiler::compiled_unit::CompiledUnitEnum::Module(m) => &m.module,
_ => unreachable!(),
})
.filter(|m| !state.has_module(&m.self_id()));
let mut serialized_modules = vec![];
for module in new_modules {
let self_id = module.self_id();
let mut module_bytes = vec![];
module.serialize_for_version(bytecode_version, &mut module_bytes)?;
serialized_modules.push((self_id, module_bytes));
}
state.save_modules(&serialized_modules)?;
Ok(state)
}
pub fn package(&self) -> &CompiledPackage {
&self.package
}
}
impl Default for PackageContext {
fn default() -> Self {
Self::new(&None, &BuildConfig::default()).unwrap()
}
}