use super::aapt2_tool;
use crate::error::*;
use std::path::{Path, PathBuf};
#[derive(Clone, Default)]
pub struct Aapt2Compile {
res_path: Option<PathBuf>,
compiled_res: PathBuf,
res_dir: Option<PathBuf>,
res_zip: Option<PathBuf>,
output_text_symbols: Option<String>,
pseudo_localize: bool,
no_crunch: bool,
legacy: bool,
preserve_visibility_of_styleables: bool,
visibility: Option<Visibility>,
verbose: bool,
trace_folder: Option<PathBuf>,
help: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Visibility {
Public,
Private,
Default,
}
impl std::fmt::Display for Visibility {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Self::Public => write!(f, "public"),
Self::Private => write!(f, "private"),
Self::Default => write!(f, "default"),
}
}
}
impl Aapt2Compile {
pub fn new(res_path: &Path, compiled_res: &Path) -> Self {
Self {
res_path: Some(res_path.to_owned()),
compiled_res: compiled_res.to_owned(),
..Default::default()
}
}
pub fn new_from_res_dir(res_dir: &Path, compiled_res: &Path) -> Self {
Self {
res_path: None,
compiled_res: compiled_res.to_owned(),
res_dir: Some(res_dir.to_owned()),
..Default::default()
}
}
pub fn new_from_res_zip(res_zip: &Path, compiled_res: &Path) -> Self {
Self {
res_path: None,
compiled_res: compiled_res.to_owned(),
res_dir: None,
res_zip: Some(res_zip.to_owned()),
..Default::default()
}
}
pub fn output_text_symbols(&mut self, output_text_symbols: String) -> &mut Self {
self.output_text_symbols = Some(output_text_symbols);
self
}
pub fn pseudo_localize(&mut self, pseudo_localize: bool) -> &mut Self {
self.pseudo_localize = pseudo_localize;
self
}
pub fn no_crunch(&mut self, no_crunch: bool) -> &mut Self {
self.no_crunch = no_crunch;
self
}
pub fn legacy(&mut self, legacy: bool) -> &mut Self {
self.legacy = legacy;
self
}
pub fn preserve_visibility_of_styleables(
&mut self,
preserve_visibility_of_styleables: bool,
) -> &mut Self {
self.preserve_visibility_of_styleables = preserve_visibility_of_styleables;
self
}
pub fn visibility(&mut self, visibility: Visibility) -> &mut Self {
self.visibility = Some(visibility);
self
}
pub fn verbose(&mut self, verbose: bool) -> &mut Self {
self.verbose = verbose;
self
}
pub fn trace_folder(&mut self, trace_folder: &Path) -> &mut Self {
self.trace_folder = Some(trace_folder.to_owned());
self
}
pub fn help(&mut self, help: bool) -> &mut Self {
self.help = help;
self
}
pub fn run(&self) -> Result<PathBuf> {
let mut aapt2 = aapt2_tool()?;
aapt2.arg("compile");
if let Some(res_path) = &self.res_path {
walkdir::WalkDir::new(res_path)
.into_iter()
.filter_map(|e| e.ok())
.for_each(|input| {
if input.file_type().is_file() {
println!("{}", input.path().to_string_lossy());
aapt2.arg(input.path());
}
});
}
aapt2.arg("-o");
aapt2.arg(&self.compiled_res);
if let Some(res_dir) = &self.res_dir {
aapt2.arg("--dir").arg(res_dir);
}
if let Some(visibility) = &self.visibility {
aapt2.arg("--visibility").arg(visibility.to_string());
}
if let Some(res_zip) = &self.res_zip {
aapt2.arg("--zip").arg(res_zip);
}
if let Some(output_text_symbols) = &self.output_text_symbols {
aapt2.arg("--output-text-symbols").arg(output_text_symbols);
}
if self.pseudo_localize {
aapt2.arg("--pseudo-localize");
}
if self.no_crunch {
aapt2.arg("--no-crunch");
}
if self.legacy {
aapt2.arg("--legacy");
}
if self.preserve_visibility_of_styleables {
aapt2.arg("--preserve-visibility-of-styleables");
}
if let Some(trace_folder) = &self.trace_folder {
aapt2.arg("--trace-folder").arg(trace_folder);
}
if self.verbose {
aapt2.arg("-v");
}
if self.help {
aapt2.arg("-h");
}
aapt2.output_err(true)?;
Ok(self.compiled_res.clone())
}
}