-
Notifications
You must be signed in to change notification settings - Fork 50
/
mod.rs
42 lines (38 loc) · 1.5 KB
/
mod.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
use crate::error::ErrorKind;
/// Builds the cargo project located at `project_path` and returns the generated wasm file contents.
///
/// NOTE: This function does not check whether the resulting wasm file is a valid smart
/// contract or not.
/// NOTE: This function builds the project using default features
pub async fn compile_project(project_path: &str) -> crate::Result<Vec<u8>> {
let project_path = std::fs::canonicalize(project_path).map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => ErrorKind::Io.message(format!(
"Incorrect file supplied to compile_project('{}')",
project_path
)),
_ => ErrorKind::Io.custom(e),
})?;
// `no_abi` has become flipped true -> false
let cargo_opts = cargo_near_build::BuildOpts {
no_locked: true,
manifest_path: Some(
cargo_near_build::camino::Utf8PathBuf::from_path_buf(project_path.join("Cargo.toml"))
.map_err(|error_path| {
ErrorKind::Io.custom(format!(
"Unable to construct UTF-8 path from: {}",
error_path.display()
))
})?,
),
..Default::default()
};
let compile_artifact =
cargo_near_build::build(cargo_opts).map_err(|e| ErrorKind::Io.custom(e))?;
let file = compile_artifact
.path
.canonicalize()
.map_err(|e| ErrorKind::Io.custom(e))?;
tokio::fs::read(file)
.await
.map_err(|e| ErrorKind::Io.custom(e))
}