packager/bootpkg/src/prelude.rs

69 lines
2 KiB
Rust

use std::str::FromStr;
use serde::{Deserialize, Serialize};
use toml;
use manifest::{self, Manifest};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PKGR {
pub bootstrap: Option<Bootstrap>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bootstrap {
pub check_installed_commands: Vec<String>,
pub commands: Vec<String>,
}
#[derive(Serialize, Deserialize)]
pub struct BadManifest {
pkgr: Option<PKGR>,
}
pub fn mani_from_str(s: &str) -> Manifest<Option<PKGR>> {
let mani = Manifest::from_str(s).unwrap();
let bmani = toml::from_str::<BadManifest>(s).unwrap();
Manifest::<Option<PKGR>> {
package: mani.package,
dependencies: mani.dependencies,
fs: mani.fs,
bin: mani.bin,
build: mani.build,
pkgr: bmani.pkgr,
}
}
pub fn run_bootstrap(mani: Manifest<Option<PKGR>>) -> bool {
if let Some(pkgr) = mani.pkgr {
if let Some(bootstrap) = pkgr.bootstrap {
fn run_command<S: Into<String>>(s: S) -> i32 {
std::process::Command::new("sh")
.arg("-c")
.arg(s.into())
.spawn()
.expect("Could not spawn process.")
.wait()
.expect("Could not wait for process.")
.code()
.expect("Could not fetch exit code.")
}
for command in &bootstrap.check_installed_commands {
if run_command(command) != 0 {
println!("!! Command failed: {}", command);
println!("!! Already installed.");
return false;
}
}
for command in &bootstrap.commands {
if run_command(command) != 0 {
println!("!! Command failed: {}", command);
println!("!! Bootstrap failed!!!");
return false;
}
}
}
}
return true;
}