-
Notifications
You must be signed in to change notification settings - Fork 11
/
para_artifact.rs
94 lines (83 loc) · 2.81 KB
/
para_artifact.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
use std::path::{Path, PathBuf};
use provider::{
types::{GenerateFileCommand, GenerateFilesOptions, TransferedFile},
DynNamespace,
};
use support::fs::FileSystem;
use super::errors::GeneratorError;
use crate::ScopedFilesystem;
#[derive(Debug, Clone)]
pub(crate) enum ParaArtifactType {
Wasm,
State,
}
#[derive(Debug, Clone)]
pub(crate) enum ParaArtifactBuildOption {
Path(String),
Command(String),
}
/// Parachain artifact (could be either the genesis state or genesis wasm)
#[derive(Debug, Clone)]
pub struct ParaArtifact {
artifact_type: ParaArtifactType,
build_option: ParaArtifactBuildOption,
artifact_path: Option<PathBuf>,
}
impl ParaArtifact {
pub(crate) fn new(
artifact_type: ParaArtifactType,
build_option: ParaArtifactBuildOption,
) -> Self {
Self {
artifact_type,
build_option,
artifact_path: None,
}
}
pub(crate) fn artifact_path(&self) -> Option<&PathBuf> {
self.artifact_path.as_ref()
}
pub(crate) async fn build<'a, T>(
&mut self,
chain_spec_path: Option<impl AsRef<Path>>,
artifact_path: impl AsRef<Path>,
ns: &DynNamespace,
scoped_fs: &ScopedFilesystem<'a, T>,
) -> Result<(), GeneratorError>
where
T: FileSystem,
{
match &self.build_option {
ParaArtifactBuildOption::Path(path) => {
let t = TransferedFile {
local_path: PathBuf::from(path),
remote_path: artifact_path.as_ref().into(),
};
scoped_fs.copy_files(vec![&t]).await?;
},
ParaArtifactBuildOption::Command(cmd) => {
let generate_subcmd = match self.artifact_type {
ParaArtifactType::Wasm => "export-genesis-wasm",
ParaArtifactType::State => "export-genesis-state",
};
let mut args: Vec<String> = vec![generate_subcmd.into()];
if let Some(chain_spec_path) = chain_spec_path {
let full_chain_path = format!(
"{}/{}",
ns.base_dir().to_string_lossy(),
chain_spec_path.as_ref().to_string_lossy()
);
args.push("--chain".into());
args.push(full_chain_path)
}
let artifact_path_ref = artifact_path.as_ref();
let generate_command =
GenerateFileCommand::new(cmd.as_str(), artifact_path_ref).args(args);
let options = GenerateFilesOptions::new(vec![generate_command]);
ns.generate_files(options).await?;
self.artifact_path = Some(artifact_path_ref.into());
},
}
Ok(())
}
}