Skip to content

Commit

Permalink
fix: winget commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Nov 14, 2024
1 parent d612d08 commit f8e0f10
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ All notable changes to this project will be documented in this file.

## Unreleased

## [0.2.3] - 2024-11-14

### Fixed

- Fixed WinGet Command not working (#49)

## [0.2.2] - 2024-11-10

### Added

- Fixed build Errors and Commands not being found on Windows (#44)
- Added the WinGet Package Manager (#44)
- Added the HomeBrew Package Manager (#41)
- Added new test to de-duplicate the codebase by pulling the example config and
group files directly from the README.md

### Fixed

- Fixed build Errors and Commands not being found on Windows (#44)
- Fixed the optional dependencies Install Option in Arch packages being
ignored (#39)
- Fixed Flatpak package runtimes not being detected (#40)
Expand Down
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ hostname = "0.4.0"
walkdir = "2.5.0"
toml_edit = "0.22.22"
which = '7.0.0'
tempfile = "3.14.0"

[dev-dependencies]
assert_cmd = "2.0.16"
Expand Down
37 changes: 31 additions & 6 deletions src/backends/winget.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::{BTreeMap, BTreeSet};
use std::io::Read;

use color_eyre::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::cmd::{command_found, run_command, run_command_for_stdout};
use crate::prelude::*;
Expand Down Expand Up @@ -31,11 +33,34 @@ impl Backend for WinGet {
return Ok(BTreeMap::new());
}

let explicit = run_command_for_stdout(["winget", "list", "--id"], Perms::Same)?;
//TODO: refactor if https://github.com/microsoft/winget-cli/issues/184 or https://github.com/microsoft/winget-cli/issues/4267 are ever fixed
let mut tempfile = tempfile::NamedTempFile::new()?;
let _ = run_command_for_stdout(
[
"winget",
"export",
"--nowarn",
tempfile.path().to_str().unwrap(),
],
Perms::Same,
)?;

Ok(explicit
.lines()
.map(|x| (x.to_string(), WinGetQueryInfo {}))
let mut export = String::new();
tempfile.read_to_string(&mut export)?;

let export: Value = serde_json::from_str(&export)?;

Ok(export["Sources"]
.as_array()
.unwrap()
.iter()
.flat_map(|x| x["Packages"].as_array().unwrap())
.map(|x| {
(
x["PackageIdentifier"].as_str().unwrap().to_string(),
WinGetQueryInfo {},
)
})
.collect())
}

Expand All @@ -46,7 +71,7 @@ impl Backend for WinGet {
) -> Result<()> {
if !packages.is_empty() {
run_command(
["winget", "install", "--id", "--exact"]
["winget", "install"]
.into_iter()
.chain(packages.keys().map(String::as_str)),
Perms::Same,
Expand All @@ -59,7 +84,7 @@ impl Backend for WinGet {
fn remove_packages(packages: &BTreeSet<String>, _: bool, _: &Config) -> Result<()> {
if !packages.is_empty() {
run_command(
["winget", "uninstall", "--id", "--exact"]
["winget", "uninstall"]
.into_iter()
.chain(packages.iter().map(String::as_str)),
Perms::Same,
Expand Down

0 comments on commit f8e0f10

Please sign in to comment.