make cargo backend use .crates2.json file

This commit is contained in:
steven-omaha
2023-01-10 17:22:57 +01:00
parent c2072223c6
commit 487848e5bc
4 changed files with 30 additions and 54 deletions
Generated
+1
View File
@@ -349,6 +349,7 @@ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"criterion", "criterion",
"serde_json",
] ]
[[package]] [[package]]
+4
View File
@@ -8,6 +8,7 @@ alpm = "*"
anyhow = "*" anyhow = "*"
# clap 4 until (at least) 4.0.32 have scrapped actual color support. we stay on 3.x until that's fixed. # clap 4 until (at least) 4.0.32 have scrapped actual color support. we stay on 3.x until that's fixed.
clap = "3.*" clap = "3.*"
serde_json = "1.0.91"
[profile.release] [profile.release]
lto = "fat" lto = "fat"
@@ -21,3 +22,6 @@ criterion = "*"
harness = false harness = false
name = "bench" name = "bench"
[[bin]]
name = "pacdef"
path = "src/main.rs"
+24 -53
View File
@@ -1,4 +1,9 @@
use std::{collections::HashSet, process::Command}; use std::collections::HashSet;
use std::fs::read_to_string;
use std::path::PathBuf;
use anyhow::Result;
use serde_json::Value;
use super::{Backend, Switches, Text}; use super::{Backend, Switches, Text};
use crate::{impl_backend_constants, Group, Package}; use crate::{impl_backend_constants, Group, Package};
@@ -16,9 +21,10 @@ impl Backend for Rust {
impl_backend_constants!(); impl_backend_constants!();
fn get_all_installed_packages(&self) -> HashSet<Package> { fn get_all_installed_packages(&self) -> HashSet<Package> {
extract_packages_names(&run_cargo_install_list()) let file = get_crates_file().unwrap();
.filter_map(Package::try_from) let content = read_to_string(file).unwrap();
.collect() let json: Value = serde_json::from_str(&content).unwrap();
extract_packages(json)
} }
fn get_explicitly_installed_packages(&self) -> HashSet<Package> { fn get_explicitly_installed_packages(&self) -> HashSet<Package> {
@@ -26,20 +32,16 @@ impl Backend for Rust {
} }
} }
fn run_cargo_install_list() -> String { fn extract_packages(json: Value) -> HashSet<Package> {
let stdout = Command::new("cargo") json.get("installs")
.args(["install", "--list"])
.output()
.unwrap() .unwrap()
.stdout; .as_object()
String::from_utf8(stdout).unwrap() .unwrap()
} .into_iter()
.map(|(name, _)| name)
fn extract_packages_names(output: &str) -> impl Iterator<Item = String> + '_ { .map(|name| name.split_whitespace().next().unwrap())
output .filter_map(Package::try_from)
.lines() .collect()
.filter(|line| !line.starts_with(char::is_whitespace))
.map(|line| line.split_whitespace().next().unwrap().to_owned())
} }
impl Rust { impl Rust {
@@ -56,40 +58,9 @@ impl Default for Rust {
} }
} }
#[cfg(test)] fn get_crates_file() -> Result<PathBuf> {
mod tests { let mut result = crate::path::get_home_dir()?;
use super::extract_packages_names; result.push(".cargo");
result.push(".crates2.json");
#[test] Ok(result)
fn test_extract_packages() {
const OUTPUT: &str = "cargo-audit v0.17.4:
cargo-audit
cargo-cache v0.8.3:
cargo-cache
cargo-criterion v1.1.0:
cargo-criterion
cargo-update v11.1.1:
cargo-install-update
cargo-install-update-config
flamegraph v0.6.2:
cargo-flamegraph
flamegraph
topgrade v10.1.2 (/home/ratajc72/tmp/topgrade):
topgrade
wthrr v0.6.1:
wthrr";
let extracted: Vec<String> = extract_packages_names(OUTPUT).collect();
assert_eq!(
&extracted,
&[
"cargo-audit",
"cargo-cache",
"cargo-criterion",
"cargo-update",
"flamegraph",
"topgrade",
"wthrr"
]
);
}
} }
+1 -1
View File
@@ -24,6 +24,6 @@ fn get_xdg_config_home() -> Result<PathBuf> {
} }
} }
fn get_home_dir() -> Result<PathBuf> { pub(crate) fn get_home_dir() -> Result<PathBuf> {
Ok(env::var("HOME").context("getting $HOME variable")?.into()) Ok(env::var("HOME").context("getting $HOME variable")?.into())
} }