cargo-crapapp 0.4.2

Cargo subcommand for building CRAP app installers and self-contained app bundles
Documentation
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PayloadEntry {
    #[serde(default)]
    pub source: Option<String>,
    pub destination: String,
    pub executable: bool,
    #[serde(default)]
    pub offset: usize,
    #[serde(default)]
    pub len: usize,
    #[serde(skip, default)]
    pub bytes: &'static [u8],
}

impl PayloadEntry {
    pub fn source(&self) -> Result<&str, String> {
        self.source
            .as_deref()
            .ok_or_else(|| format!("payload source is missing for {}", self.destination))
    }

    pub fn with_range(mut self, offset: usize, len: usize) -> Self {
        self.source = None;
        self.offset = offset;
        self.len = len;
        self
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AssociatedFile {
    pub path: String,
    pub kind: AssociatedFileKind,
    #[serde(default)]
    pub eula_report: bool,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Shortcut {
    pub target: String,
    pub name: String,
    pub directory: Option<String>,
    pub icon: Option<String>,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum AssociatedFileKind {
    File,
    Directory,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Eula {
    pub name: String,
    pub text: String,
    #[serde(default = "default_true")]
    pub required: bool,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DisplayIcon {
    pub width: u32,
    pub height: u32,
    pub rgba: Vec<u8>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct InstallerConfig {
    pub app_name: String,
    pub app_version: String,
    pub display_name: Option<String>,
    pub publisher: Option<String>,
    #[serde(default)]
    pub bundled_at: String,
    /// Build target used only while authoring an MSI; never written to setup config.
    #[serde(skip, default)]
    pub bundle_target: String,
    #[serde(rename = "variables")]
    pub required_variables: Vec<String>,
    #[serde(default)]
    pub uninstaller_source: String,
    #[serde(skip, default)]
    pub uninstaller_bytes: &'static [u8],
    pub payload: Vec<PayloadEntry>,
    /// MSI directories to add to PATH, expressed relative to `$INSTALLPATH`.
    #[serde(default)]
    pub path_entries: Vec<String>,
    pub display_icon: Option<String>,
    pub display_icon_rgba: Option<DisplayIcon>,
    #[serde(default)]
    pub associated_files: Vec<AssociatedFile>,
    #[serde(default)]
    pub shortcuts: Vec<Shortcut>,
    #[serde(default)]
    pub eulas: Vec<Eula>,
}

fn default_true() -> bool {
    true
}

impl Default for InstallerConfig {
    fn default() -> Self {
        Self {
            app_name: "template-app".to_owned(),
            app_version: "0.0.0".to_owned(),
            display_name: None,
            publisher: None,
            bundled_at: String::new(),
            bundle_target: String::new(),
            required_variables: Vec::new(),
            uninstaller_source: String::new(),
            uninstaller_bytes: &[],
            payload: Vec::new(),
            path_entries: Vec::new(),
            display_icon: None,
            display_icon_rgba: None,
            associated_files: Vec::new(),
            shortcuts: Vec::new(),
            eulas: Vec::new(),
        }
    }
}

impl InstallerConfig {
    pub fn allows_install_path_selection(&self) -> bool {
        self.required_variables
            .iter()
            .any(|variable| variable == "INSTALLPATH")
    }

    pub fn install_app_name(&self) -> &str {
        let app_name = self.app_name.trim();

        if app_name.is_empty() {
            "windows-installer"
        } else {
            app_name
        }
    }

    pub fn publisher_name(&self) -> Option<&str> {
        self.publisher
            .as_deref()
            .map(str::trim)
            .filter(|publisher| !publisher.is_empty())
    }

    pub fn new(
        config: &'static [u8],
        payload: &'static [u8],
        uninstaller: &'static [u8],
    ) -> Result<Self, String> {
        let mut config = serde_json::from_slice::<Self>(config)
            .map_err(|error| format!("failed to read installer config: {error}"))?;

        for entry in &mut config.payload {
            let end = entry
                .offset
                .checked_add(entry.len)
                .ok_or_else(|| format!("payload offset overflow for {}", entry.destination))?;
            entry.bytes = payload
                .get(entry.offset..end)
                .ok_or_else(|| format!("payload bytes out of range for {}", entry.destination))?;
        }

        config.uninstaller_bytes = uninstaller;

        Ok(config)
    }
}