use std::cell::RefCell;
use anyhow::Context;
use clap::{Args, ValueEnum};
use crate::plugin::load_plugins;
use super::Submodule;
thread_local! {
pub static BUILD_STEP: RefCell<Step> = RefCell::new(Step::PRE);
}
#[derive(Clone, Args)]
pub struct BuildArgs {
pub step: Option<Step>,
}
pub struct Build {
pub args: BuildArgs,
}
#[derive(Clone, Copy, ValueEnum, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Step {
PRE,
AAPT,
COMPILE,
DEX,
BUNDLE,
POST,
}
impl Build {
pub fn new(args: &BuildArgs) -> Self {
Self { args: args.clone() }
}
}
impl Submodule for Build {
fn run(&mut self) -> anyhow::Result<()> {
let order: Vec<Step> = if let Some(step) = self.args.step {
vec![step]
} else {
vec![
Step::PRE,
Step::AAPT,
Step::COMPILE,
Step::DEX,
Step::BUNDLE,
Step::POST,
]
};
let map = load_plugins()?;
for step in order {
BUILD_STEP.with(|s| {
*s.borrow_mut() = step;
});
if let Some(plugins) = map.get(&step) {
for plugin in plugins {
let exe = plugin.load().context(format!(
"Error loading plugin: {}:{} at build step {:?}",
plugin.name, plugin.version, plugin.step
))?;
let chunk = exe.load().context(format!(
"Error loading lua code for {}:{} at build step {:?}",
plugin.name, plugin.version, plugin.step
))?;
chunk.exec().context(format!(
"Failed to execute plugin code {:?} for plugin {}:{} at build step {:?}",
plugin.path, plugin.name, plugin.version, plugin.step
))?;
}
}
}
Ok(())
}
}