From d720d293cd9db26616c0ab9c3e537b39bf181f9a Mon Sep 17 00:00:00 2001 From: ripytide Date: Tue, 5 Nov 2024 20:08:29 +0000 Subject: [PATCH] feat: Add the WinGet Package Manager --- CHANGELOG.md | 1 + README.md | 5 +++ groups/example_group.toml | 4 +++ src/backends/mod.rs | 3 +- src/backends/winget.rs | 71 +++++++++++++++++++++++++++++++++++++++ src/prelude.rs | 1 + 6 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/backends/winget.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index ad464a9..25a8d11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Added +- Added the WinGet Package Manager (#44) - Added the HomeBrew Package Manager (#41) ### Fixed diff --git a/README.md b/README.md index 4e191b4..a85de2a 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ additional backends are welcome! | `pipx` | `[pipx]` | | | `cargo` | `[cargo]` | | | `rustup` | `[rustup]` | | +| `winget` | `[winget]` | | | `xbps` | `[xbps]` | | ## Config @@ -169,6 +170,10 @@ rustup = [ # components: extra non-default components to install with this toolchain { package = "stable", components = ["rust-analyzer"] } ] +winget = [ + "metapac", + { package = "metapac" } +] xbps = [ "metapac", { package = "metapac" } diff --git a/groups/example_group.toml b/groups/example_group.toml index 133dddc..46a18cd 100644 --- a/groups/example_group.toml +++ b/groups/example_group.toml @@ -53,6 +53,10 @@ rustup = [ # components: extra non-default components to install with this toolchain { package = "stable", components = ["rust-analyzer"] } ] +winget = [ + "metapac", + { package = "metapac" } +] xbps = [ "metapac", { package = "metapac" } diff --git a/src/backends/mod.rs b/src/backends/mod.rs index a4b2124..dc64bf7 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -7,6 +7,7 @@ pub mod dnf; pub mod flatpak; pub mod pipx; pub mod rustup; +pub mod winget; pub mod xbps; use std::collections::{BTreeMap, BTreeSet}; @@ -17,7 +18,7 @@ use serde::{Deserialize, Serialize}; macro_rules! apply_public_backends { ($macro:ident) => { - $macro! { Arch, Apt, Brew, Cargo, Dnf, Flatpak, Pipx, Rustup, Xbps } + $macro! { Arch, Apt, Brew, Cargo, Dnf, Flatpak, Pipx, Rustup, WinGet, Xbps } }; } pub(crate) use apply_public_backends; diff --git a/src/backends/winget.rs b/src/backends/winget.rs new file mode 100644 index 0000000..165a36d --- /dev/null +++ b/src/backends/winget.rs @@ -0,0 +1,71 @@ +use std::collections::{BTreeMap, BTreeSet}; + +use color_eyre::Result; +use serde::{Deserialize, Serialize}; + +use crate::cmd::{command_found, run_command, run_command_for_stdout}; +use crate::prelude::*; + +#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] +pub struct WinGet; + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct WinGetQueryInfo {} + +#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct WinGetInstallOptions {} + +impl Backend for WinGet { + type QueryInfo = WinGetQueryInfo; + type InstallOptions = WinGetInstallOptions; + + fn map_managed_packages( + packages: BTreeMap, + _: &Config, + ) -> Result> { + Ok(packages) + } + + fn query_installed_packages(_: &Config) -> Result> { + if !command_found("winget") { + return Ok(BTreeMap::new()); + } + + let explicit = run_command_for_stdout(["winget", "list", "--id"], Perms::Same)?; + + Ok(explicit + .lines() + .map(|x| (x.to_string(), WinGetQueryInfo {})) + .collect()) + } + + fn install_packages( + packages: &BTreeMap, + _: bool, + _: &Config, + ) -> Result<()> { + if !packages.is_empty() { + run_command( + ["winget", "install", "--id", "--exact"] + .into_iter() + .chain(packages.keys().map(String::as_str)), + Perms::Same, + )?; + } + + Ok(()) + } + + fn remove_packages(packages: &BTreeSet, _: bool, _: &Config) -> Result<()> { + if !packages.is_empty() { + run_command( + ["winget", "uninstall", "--id", "--exact"] + .into_iter() + .chain(packages.iter().map(String::as_str)), + Perms::Same, + )?; + } + + Ok(()) + } +} diff --git a/src/prelude.rs b/src/prelude.rs index c91a802..bae83f0 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -10,6 +10,7 @@ pub use crate::backends::dnf::{Dnf, DnfInstallOptions, DnfQueryInfo}; pub use crate::backends::flatpak::{Flatpak, FlatpakInstallOptions, FlatpakQueryInfo}; pub use crate::backends::pipx::{Pipx, PipxInstallOptions, PipxQueryOptions}; pub use crate::backends::rustup::{Rustup, RustupInstallOptions, RustupQueryInfo}; +pub use crate::backends::winget::{WinGet, WinGetInstallOptions, WinGetQueryInfo}; pub use crate::backends::xbps::{Xbps, XbpsInstallOptions, XbpsQueryInfo}; pub use crate::backends::{Backend, StringPackageStruct}; pub use crate::cli::{