#![doc = docs_self!()]
use async_trait::async_trait;
use indoc::indoc;
use once_cell::sync::Lazy;
use tap::prelude::*;
use super::{DryRunStrategy, NoCacheStrategy, Pm, PmHelper, PmMode, PromptStrategy, Strategy};
use crate::{config::Config, error::Result, exec::Cmd};
macro_rules! docs_self {
() => {
indoc! {"
The [Homebrew Package Manager](https://brew.sh/).
"}
};
}
#[doc = docs_self!()]
#[derive(Debug)]
pub struct Brew {
cfg: Config,
}
static STRAT_PROMPT: Lazy<Strategy> = Lazy::new(|| Strategy {
prompt: PromptStrategy::CustomPrompt,
..Strategy::default()
});
static STRAT_INSTALL: Lazy<Strategy> = Lazy::new(|| Strategy {
prompt: PromptStrategy::CustomPrompt,
no_cache: NoCacheStrategy::Scc,
..Strategy::default()
});
impl Brew {
#[must_use]
#[allow(missing_docs)]
pub const fn new(cfg: Config) -> Self {
Self { cfg }
}
}
#[async_trait]
impl Pm for Brew {
fn name(&self) -> &str {
"brew"
}
fn cfg(&self) -> &Config {
&self.cfg
}
async fn q(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
if kws.is_empty() {
self.run(Cmd::new(["brew", "list"]).flags(flags)).await
} else {
self.qs(kws, flags).await
}
}
async fn qc(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["brew", "log"]).kws(kws).flags(flags))
.await
}
async fn qi(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.si(kws, flags).await
}
async fn qii(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["brew", "uses", "--installed"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run(cmd))
.await
}
async fn ql(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["brew", "list"]).kws(kws).flags(flags))
.await
}
async fn qs(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.search_regex(Cmd::new(["brew", "list", "--formula"]).flags(flags), kws)
.await?;
if cfg!(target_os = "macos") {
self.search_regex(Cmd::new(["brew", "list", "--cask"]).flags(flags), kws)
.await?;
}
Ok(())
}
async fn qu(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["brew", "outdated"]).kws(kws).flags(flags))
.await
}
async fn r(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["brew", "uninstall"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &STRAT_PROMPT))
.await
}
async fn rn(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["brew", "uninstall", "--zap", "-f"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &STRAT_PROMPT))
.await
}
async fn rns(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.rn(kws, flags).await?;
Cmd::new(["brew", "autoremove"])
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &STRAT_PROMPT))
.await
}
async fn rs(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.r(kws, flags).await?;
Cmd::new(["brew", "autoremove"])
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &STRAT_PROMPT))
.await
}
async fn s(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(if self.cfg.needed {
["brew", "install"]
} else {
["brew", "reinstall"]
})
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &STRAT_INSTALL))
.await
}
async fn sc(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
let strat = Strategy {
dry_run: DryRunStrategy::with_flags(["--dry-run"]),
prompt: PromptStrategy::CustomPrompt,
..Strategy::default()
};
Cmd::new(["brew", "cleanup"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &strat))
.await
}
async fn scc(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
let strat = Strategy {
dry_run: DryRunStrategy::with_flags(["--dry-run"]),
prompt: PromptStrategy::CustomPrompt,
..Strategy::default()
};
Cmd::new(["brew", "cleanup", "-s"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &strat))
.await
}
async fn sccc(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
let strat = Strategy {
dry_run: DryRunStrategy::with_flags(["--dry-run"]),
prompt: PromptStrategy::CustomPrompt,
..Strategy::default()
};
Cmd::new(["brew", "cleanup", "--prune=all"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &strat))
.await
}
async fn si(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["brew", "info"]).kws(kws).flags(flags))
.await
}
async fn sii(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["brew", "uses", "--eval-all"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run(cmd))
.await
}
async fn ss(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["brew", "search"]).kws(kws).flags(flags))
.await
}
async fn su(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["brew", "upgrade"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &STRAT_INSTALL))
.await
}
async fn suy(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.sy(&[], flags).await?;
self.su(kws, flags).await
}
async fn sw(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new(["brew", "fetch"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, PmMode::default(), &STRAT_PROMPT))
.await
}
async fn sy(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new(["brew", "update"]).flags(flags)).await?;
if !kws.is_empty() {
self.s(kws, flags).await?;
}
Ok(())
}
}