Skip to main content

canic_installer/release_set/
manifest.rs

1use serde::{Deserialize, Serialize};
2use std::{fs, path::Path};
3
4use super::{
5    build_release_set_entry, config_path, configured_release_roles, load_root_package_version,
6    resolve_artifact_root, root_manifest_path, root_release_set_manifest_path,
7    workspace_manifest_path,
8};
9
10///
11/// RootReleaseSetManifest
12///
13
14#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
15pub struct RootReleaseSetManifest {
16    pub release_version: String,
17    pub entries: Vec<ReleaseSetEntry>,
18}
19
20///
21/// ReleaseSetEntry
22///
23
24#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
25pub struct ReleaseSetEntry {
26    pub role: String,
27    pub template_id: String,
28    pub artifact_relative_path: String,
29    pub payload_size_bytes: u64,
30    pub payload_sha256_hex: String,
31    pub chunk_size_bytes: u64,
32    pub chunk_sha256_hex: Vec<String>,
33}
34
35// Build and persist the current root release-set manifest from built `.wasm.gz` artifacts.
36pub fn emit_root_release_set_manifest(
37    workspace_root: &Path,
38    dfx_root: &Path,
39    network: &str,
40) -> Result<std::path::PathBuf, Box<dyn std::error::Error>> {
41    let artifact_root = resolve_artifact_root(dfx_root, network)?;
42    let config_path = config_path(workspace_root);
43    let manifest_path = root_release_set_manifest_path(&artifact_root)?;
44    let release_version = load_root_package_version(
45        &root_manifest_path(workspace_root),
46        &workspace_manifest_path(workspace_root),
47    )?;
48    let entries = configured_release_roles(&config_path)?
49        .into_iter()
50        .map(|role_name| build_release_set_entry(dfx_root, &artifact_root, &role_name))
51        .collect::<Result<Vec<_>, _>>()?;
52    let manifest = RootReleaseSetManifest {
53        release_version,
54        entries,
55    };
56
57    fs::write(&manifest_path, serde_json::to_vec_pretty(&manifest)?)?;
58    Ok(manifest_path)
59}
60
61// Emit the root release-set manifest only once every required ordinary artifact exists.
62pub fn emit_root_release_set_manifest_if_ready(
63    workspace_root: &Path,
64    dfx_root: &Path,
65    network: &str,
66) -> Result<Option<std::path::PathBuf>, Box<dyn std::error::Error>> {
67    let artifact_root = resolve_artifact_root(dfx_root, network)?;
68    let roles = configured_release_roles(&config_path(workspace_root))?;
69
70    for role_name in roles {
71        let artifact_path = artifact_root
72            .join(&role_name)
73            .join(format!("{role_name}.wasm.gz"));
74        if !artifact_path.is_file() {
75            return Ok(None);
76        }
77    }
78
79    emit_root_release_set_manifest(workspace_root, dfx_root, network).map(Some)
80}
81
82// Load one previously emitted root release-set manifest from disk.
83pub fn load_root_release_set_manifest(
84    manifest_path: &Path,
85) -> Result<RootReleaseSetManifest, Box<dyn std::error::Error>> {
86    let source = fs::read(manifest_path)?;
87    Ok(serde_json::from_slice(&source)?)
88}