use std::path::Path;
use brush_core::{
CreateOptions, ExecutionParameters, ExecutionResult, Shell, ShellVariable,
builtins::simple_builtin,
};
use super::{
builtins::FancyMessage,
vars::{AsShellValue, PacstallVariables},
};
pub struct PacstallShell {
pub shell: Shell,
}
impl PacstallShell {
pub async fn new(version: &str) -> Result<Self, brush_core::Error> {
let options = CreateOptions {
shell_name: Some("pacstall_interpreter".to_string()),
shell_product_display_str: Some(format!("pacstall v{}", version)),
..Default::default()
};
let mut shell = Shell::new(&options).await?;
shell.register_builtin("fancy_message", simple_builtin::<FancyMessage>());
for (name, output) in PacstallVariables::generate() {
shell.set_env_global(name, ShellVariable::new(output.to_shellvalue()))?;
}
Ok(Self { shell })
}
pub async fn load_pacscript<P: AsRef<Path>>(
&mut self,
pacscript: P,
) -> Result<ExecutionResult, brush_core::Error> {
self.shell
.source_script(
pacscript.as_ref(),
std::iter::empty::<&str>(),
&ExecutionParameters::default(),
)
.await
}
pub fn has_variable(&self, var: &str) -> Option<&ShellVariable> {
self.shell.get_env_var(var)
}
}