Skip to content

Commit

Permalink
feat: Add the WinGet Package Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Nov 5, 2024
1 parent dc2bd42 commit d720d29
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ additional backends are welcome!
| `pipx` | `[pipx]` | |
| `cargo` | `[cargo]` | |
| `rustup` | `[rustup]` | |
| `winget` | `[winget]` | |
| `xbps` | `[xbps]` | |

## Config
Expand Down Expand Up @@ -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" }
Expand Down
4 changes: 4 additions & 0 deletions groups/example_group.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 2 additions & 1 deletion src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand Down
71 changes: 71 additions & 0 deletions src/backends/winget.rs
Original file line number Diff line number Diff line change
@@ -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<String, Self::InstallOptions>,
_: &Config,
) -> Result<BTreeMap<String, Self::InstallOptions>> {
Ok(packages)
}

fn query_installed_packages(_: &Config) -> Result<BTreeMap<String, Self::QueryInfo>> {
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<String, Self::InstallOptions>,
_: 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<String>, _: 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(())
}
}
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down

0 comments on commit d720d29

Please sign in to comment.