use crate::error::*;
use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Debug, PartialEq, PartialOrd)]
pub struct BuildBundle {
modules: Vec<PathBuf>,
output: PathBuf,
config: Option<PathBuf>,
metadata_file: Option<PathBuf>,
}
impl BuildBundle {
pub fn new(modules: &[PathBuf], output: &Path) -> Self {
Self {
modules: modules.to_vec(),
output: output.to_owned(),
config: None,
metadata_file: None,
}
}
pub fn config(&mut self, config: &Path) -> &mut Self {
self.config = Some(config.to_owned());
self
}
pub fn metadata_file(&mut self, metadata_file: &Path) -> &mut Self {
self.metadata_file = Some(metadata_file.to_owned());
self
}
pub fn run(&self) -> Result<()> {
let mut build_bundle = Command::new("java");
build_bundle.arg("-jar");
if let Ok(bundletool_path) = std::env::var("BUNDLETOOL_PATH") {
build_bundle.arg(bundletool_path);
} else {
return Err(Error::BundletoolNotFound);
}
build_bundle.arg("build-bundle");
build_bundle.arg("--modules");
build_bundle.arg(
self.modules
.iter()
.map(|v| v.to_string_lossy().to_string())
.collect::<Vec<String>>()
.join(","),
);
build_bundle.arg("--output").arg(&self.output);
if let Some(config) = &self.config {
build_bundle.arg("--config").arg(config);
}
if let Some(metadata_file) = &self.metadata_file {
build_bundle.arg("--metadata-file").arg(metadata_file);
}
build_bundle.output_err(true)?;
Ok(())
}
}