#![doc = doc_self!()]
use std::sync::LazyLock;
use async_trait::async_trait;
use indoc::indoc;
use tap::prelude::*;
use which::which;
use super::{NoCacheStrategy, Pm, PmHelper, PromptStrategy, Strategy};
use crate::{config::Config, error::Result, exec::Cmd};
macro_rules! doc_self {
() => {
indoc! {"
The [Scoop CLI Installer](https://scoop.sh/).
"}
};
}
use doc_self;
#[doc = doc_self!()]
#[derive(Debug)]
pub struct Scoop {
cfg: Config,
shell: String,
}
static STRAT_PROMPT: LazyLock<Strategy> = LazyLock::new(|| Strategy {
prompt: PromptStrategy::CustomPrompt,
..Strategy::default()
});
static STRAT_INSTALL: LazyLock<Strategy> = LazyLock::new(|| Strategy {
prompt: PromptStrategy::CustomPrompt,
no_cache: NoCacheStrategy::Scc,
..Strategy::default()
});
impl Scoop {
#[must_use]
#[allow(missing_docs)]
pub fn new(cfg: Config) -> Self {
let shell = which("pwsh").and(Ok("pwsh")).unwrap_or("powershell");
Self::with_shell(cfg, shell)
}
#[must_use]
#[allow(missing_docs)]
pub fn with_shell(cfg: Config, shell: &str) -> Self {
Self {
cfg,
shell: shell.to_owned(),
}
}
}
#[async_trait]
impl Pm for Scoop {
fn name(&self) -> &'static str {
"scoop"
}
fn cfg(&self) -> &Config {
&self.cfg
}
async fn q(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
if kws.is_empty() {
self.run(Cmd::new([&self.shell, "-Command", "scoop", "list"]).flags(flags))
.await
} else {
self.qs(kws, flags).await
}
}
async fn qi(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.si(kws, flags).await
}
async fn qs(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new([&self.shell, "-Command", "scoop", "list"])
.flags(flags)
.pipe(|cmd| self.search_regex_with_header(cmd, kws, 4))
.await
}
async fn qu(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new([&self.shell, "-Command", "scoop", "status"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run(cmd))
.await
}
async fn r(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new([&self.shell, "-Command", "scoop", "uninstall"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn rn(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new([&self.shell, "-Command", "scoop", "uninstall", "--purge"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn s(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new([&self.shell, "-Command", "scoop", "install"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_INSTALL))
.await
}
async fn sc(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new([&self.shell, "-Command", "scoop", "cache", "rm"])
.kws(if kws.is_empty() { &["*"][..] } else { kws })
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_PROMPT))
.await
}
async fn scc(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.sc(kws, flags).await
}
async fn si(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new([&self.shell, "-Command", "scoop", "info"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run(cmd))
.await
}
async fn ss(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new([&self.shell, "-Command", "scoop", "search"])
.kws(kws)
.flags(flags)
.pipe(|cmd| self.run(cmd))
.await
}
async fn su(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
Cmd::new([&self.shell, "-Command", "scoop", "update"])
.kws(if kws.is_empty() { &["*"][..] } else { kws })
.flags(flags)
.pipe(|cmd| self.run_with(cmd, self.default_mode(), &STRAT_INSTALL))
.await
}
async fn suy(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.sy(&[], flags).await?;
self.su(kws, flags).await
}
async fn sy(&self, kws: &[&str], flags: &[&str]) -> Result<()> {
self.run(Cmd::new([&self.shell, "-Command", "scoop", "update"]).flags(flags))
.await?;
if !kws.is_empty() {
self.s(kws, flags).await?;
}
Ok(())
}
}