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 c31bfa6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
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
34 changes: 27 additions & 7 deletions src/backends/winget.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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::cmd::{command_found, run_command};
use crate::prelude::*;

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
Expand Down Expand Up @@ -31,11 +33,29 @@ 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()?;
run_command(
["winget", "export", 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 +66,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 +79,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 c31bfa6

Please sign in to comment.