add more code

This commit is contained in:
Dr. Matthias Ratajczak
2022-09-02 19:42:01 +02:00
parent 69d1034835
commit ef20514d1c
10 changed files with 759 additions and 61 deletions
+2
View File
@@ -1 +1,3 @@
flamegraph.svg
perf.data
/target /target
Generated
+517 -2
View File
@@ -44,6 +44,47 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bstr"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
dependencies = [
"lazy_static",
"memchr",
"regex-automata",
"serde",
]
[[package]]
name = "bumpalo"
version = "3.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"bitflags",
"textwrap 0.11.0",
"unicode-width",
]
[[package]] [[package]]
name = "clap" name = "clap"
version = "3.2.20" version = "3.2.20"
@@ -56,7 +97,7 @@ dependencies = [
"indexmap", "indexmap",
"strsim", "strsim",
"termcolor", "termcolor",
"textwrap", "textwrap 0.15.0",
] ]
[[package]] [[package]]
@@ -68,6 +109,121 @@ dependencies = [
"os_str_bytes", "os_str_bytes",
] ]
[[package]]
name = "criterion"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f"
dependencies = [
"atty",
"cast",
"clap 2.34.0",
"criterion-plot",
"csv",
"itertools",
"lazy_static",
"num-traits",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_cbor",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876"
dependencies = [
"cast",
"itertools",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "045ebe27666471bb549370b4b0b3e51b07f56325befa4284db65fc89c02511b1"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"once_cell",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51887d4adc7b564537b15adcfb307936f8075dfcd5f00dde9a9f1d29383682bc"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]]
name = "csv"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1"
dependencies = [
"bstr",
"csv-core",
"itoa 0.4.8",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "half"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.3" version = "0.12.3"
@@ -93,12 +249,103 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "itertools"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
[[package]]
name = "itoa"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
[[package]]
name = "js-sys"
version = "0.3.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "258451ab10b34f8af53416d1fdab72c22e805f0c92a1136d59470ec0b11138b2"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.132" version = "0.2.132"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5"
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "memoffset"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
dependencies = [
"autocfg",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "once_cell"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0"
[[package]]
name = "oorandom"
version = "11.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]] [[package]]
name = "os_str_bytes" name = "os_str_bytes"
version = "6.3.0" version = "6.3.0"
@@ -110,7 +357,8 @@ name = "pacdef"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"alpm", "alpm",
"clap", "clap 3.2.20",
"criterion",
] ]
[[package]] [[package]]
@@ -119,12 +367,173 @@ version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]]
name = "plotters"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "716b4eeb6c4a1d3ecc956f75b43ec2e8e8ba80026413e70a3f41fd3313d3492b"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "193228616381fecdc1224c62e96946dfbc73ff4384fba576e052ff8c1bea8142"
[[package]]
name = "plotters-svg"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a81d2759aae1dae668f783c308bc5c8ebd191ff4184aaa1b37f65a6ae5a56f"
dependencies = [
"plotters-backend",
]
[[package]]
name = "proc-macro2"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd99e5772ead8baa5215278c9b15bf92087709e9c1b2d1f97cdb5a183c933a7d"
dependencies = [
"autocfg",
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "258bcdb5ac6dad48491bb2992db6b7cf74878b0384908af124823d118c99683f"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
name = "regex"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
[[package]]
name = "regex-syntax"
version = "0.6.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
[[package]]
name = "serde_cbor"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5"
dependencies = [
"half",
"serde",
]
[[package]]
name = "serde_derive"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [
"itoa 1.0.3",
"ryu",
"serde",
]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.1.3" version = "1.1.3"
@@ -134,12 +543,118 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.15.0" version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "unicode-ident"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "662cd44805586bd52971b9586b1df85cdbbd9112e4ef4d8f41559c334dc6ac3f"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b260f13d3012071dfb1512849c033b1925038373aea48ced3012c09df952c602"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be8e654bdd9b79216c2929ab90721aa82faf65c48cdf08bdc4e7f51357b80da"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6598dd0bd3c7d51095ff6531a5b23e02acdc81804e30d8f07afb77b7215a140a"
[[package]]
name = "web-sys"
version = "0.3.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed055ab27f941423197eb86b2035720b1a3ce40504df082cac2ecc6ed73335a1"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
+11
View File
@@ -8,3 +8,14 @@ edition = "2021"
[dependencies] [dependencies]
alpm = "2.2.1" alpm = "2.2.1"
clap = "3.2.20" clap = "3.2.20"
[profile.release]
debug = true
[dev-dependencies]
criterion = "0.3.6"
[[bench]]
harness = false
name = "bench"
+24
View File
@@ -0,0 +1,24 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn get_all_installed_packages(c: &mut Criterion) {
c.bench_function("get_all_installed_packages", |b| {
b.iter(|| {
black_box(pacdef::db::get_all_installed_packages());
})
});
}
fn get_explicitly_installed_packages(c: &mut Criterion) {
c.bench_function("get_explicitly_installed_packages", |b| {
b.iter(|| {
black_box(pacdef::db::get_explicitly_installed_packages());
})
});
}
criterion_group!(
benches,
get_all_installed_packages,
get_explicitly_installed_packages
);
criterion_main!(benches);
+26 -2
View File
@@ -1,10 +1,34 @@
use clap::Arg;
use clap::Command; use clap::Command;
pub(crate) fn get_arg_parser() -> Command<'static> {
pub const EDIT: &str = "edit";
pub const GROUPS: &str = "groups";
pub const SYNC: &str = "sync";
pub const UNMANAGED: &str = "unmanaged";
pub const VERSION: &str = "version";
fn get_arg_parser() -> Command<'static> {
let result = Command::new("pacdef") let result = Command::new("pacdef")
.about("declarative package manager for Arch Linux") .about("declarative package manager for Arch Linux")
.version("1.0.0-alpha") .version("1.0.0-alpha")
.subcommand_required(true) .subcommand_required(true)
.arg_required_else_help(true) .arg_required_else_help(true)
.subcommand(Command::new("sync").about("install packages from all imported groups")); .subcommand(
Command::new(EDIT)
.about("edit one or more existing group files")
.arg_required_else_help(true)
.arg(Arg::new("group")),
)
.subcommand(Command::new(GROUPS).about("show names of imported groups"))
.subcommand(Command::new(SYNC).about("install packages from all imported groups"))
.subcommand(
Command::new(UNMANAGED)
.about("show explicitly installed packages not managed by pacdef"),
)
.subcommand(Command::new(VERSION).about("show version info"));
result result
} }
pub fn get_matched_arguments() -> clap::ArgMatches {
get_arg_parser().get_matches()
}
+35
View File
@@ -0,0 +1,35 @@
use crate::Package;
use alpm::Alpm;
use alpm::PackageReason::Explicit;
use std::collections::HashSet;
pub fn get_all_installed_packages() -> HashSet<Package> {
convert_to_pacdef_packages(get_all_installed_packages_alpm())
}
fn get_all_installed_packages_alpm() -> HashSet<String> {
let db = Alpm::new("/", "/var/lib/pacman").unwrap();
db.localdb()
.pkgs()
.iter()
.map(|p| p.name().to_string())
.collect()
}
pub fn get_explicitly_installed_packages() -> HashSet<Package> {
convert_to_pacdef_packages(get_explicitly_installed_packages_alpm())
}
fn get_explicitly_installed_packages_alpm() -> HashSet<String> {
let db = Alpm::new("/", "/var/lib/pacman").unwrap();
db.localdb()
.pkgs()
.iter()
.filter(|p| p.reason() == Explicit)
.map(|p| p.name().to_string())
.collect()
}
fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> {
packages.into_iter().map(Package::from).collect()
}
+5 -7
View File
@@ -5,16 +5,16 @@ use std::hash::Hash;
use std::io::BufRead; use std::io::BufRead;
use std::{io::BufReader, path::PathBuf}; use std::{io::BufReader, path::PathBuf};
const GROUPS_DIR: &str = "/home/ratajc72/.config/pacdef/groups"; pub const GROUPS_DIR: &str = "/home/ratajc72/.config/pacdef/groups";
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Group { pub struct Group {
pub(crate) name: String, pub name: String,
pub(crate) packages: HashSet<Package>, pub packages: HashSet<Package>,
} }
impl Group { impl Group {
pub(crate) fn load_from_dir() -> HashSet<Self> { pub fn load_from_dir() -> HashSet<Self> {
let mut result = HashSet::new(); let mut result = HashSet::new();
let path = PathBuf::from(GROUPS_DIR); let path = PathBuf::from(GROUPS_DIR);
for entry in path.read_dir().unwrap() { for entry in path.read_dir().unwrap() {
@@ -28,8 +28,6 @@ impl Group {
name: name.into_string().unwrap(), name: name.into_string().unwrap(),
packages, packages,
}); });
// let content = file.rea
} }
result result
} }
+7
View File
@@ -0,0 +1,7 @@
pub mod args;
pub mod db;
pub mod group;
pub mod package;
pub use group::Group;
pub use package::Package;
+130 -48
View File
@@ -1,48 +1,24 @@
use pacdef::db::get_all_installed_packages;
use pacdef::group::GROUPS_DIR;
use std::io::BufRead; use std::io::BufRead;
use std::io::Write; use std::io::Write;
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
use std::path::Path;
use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use std::{collections::HashSet, process::Command}; use std::{collections::HashSet, process::Command};
use alpm::Alpm; use clap::ArgMatches;
use args::get_arg_parser; use pacdef::args;
use group::Group; use pacdef::db::get_explicitly_installed_packages;
use package::Package; use pacdef::Group;
use pacdef::Package;
mod args;
pub(crate) mod group;
pub(crate) mod package;
fn main() { fn main() {
let args = get_arg_parser(); let args = args::get_matched_arguments();
let matches = args.get_matches();
match matches.subcommand() {
Some(("sync", _)) => install_pacdef_packages(),
s => panic!("{:#?}", s),
}
}
fn install_pacdef_packages() {
let groups = Group::load_from_dir(); let groups = Group::load_from_dir();
let packages_hs = groups let pacdef = Pacdef::new(args, groups);
.into_iter() pacdef.run_action_from_arg();
.flat_map(|g| g.packages)
.collect::<HashSet<_>>();
let local_packages = convert_to_pacdef_packages(get_alpm_packages());
let mut diff: Vec<_> = packages_hs.difference(&local_packages).collect();
diff.sort_unstable();
if diff.is_empty() {
exit(0);
}
println!("Would install the following packages:");
for p in &diff {
println!(" {p}");
}
println!();
get_user_confirmation();
install_packages(diff);
} }
fn get_user_confirmation() { fn get_user_confirmation() {
@@ -54,20 +30,16 @@ fn get_user_confirmation() {
} }
} }
fn get_alpm_packages() -> HashSet<String> { fn run_edit_command(files: &[&Path]) {
let db = Alpm::new("/", "/var/lib/pacman").unwrap(); let mut cmd = Command::new("nvim");
db.localdb() for f in files {
.pkgs() cmd.arg(f.to_string_lossy().to_string());
.iter() }
.map(|p| p.name().to_string()) dbg!(&cmd);
.collect() cmd.exec();
} }
fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> { fn run_install_command(diff: Vec<Package>) {
packages.into_iter().map(Package::from).collect()
}
fn install_packages(diff: Vec<&Package>) {
let mut cmd = Command::new("paru"); let mut cmd = Command::new("paru");
cmd.arg("-S"); cmd.arg("-S");
for p in diff { for p in diff {
@@ -76,3 +48,113 @@ fn install_packages(diff: Vec<&Package>) {
dbg!(&cmd); dbg!(&cmd);
cmd.exec(); cmd.exec();
} }
struct Pacdef {
args: ArgMatches,
groups: Option<HashSet<Group>>,
// action: Box<dyn Fn(Self)>,
}
impl Pacdef {
fn new(args: ArgMatches, groups: HashSet<Group>) -> Self {
Self {
args,
groups: Some(groups),
// action: Box::new(Self::install_packages),
}
}
fn take_packages_as_set(&mut self) -> HashSet<Package> {
self.groups
.take()
.unwrap()
.into_iter()
.flat_map(|g| g.packages)
.collect()
}
fn get_packages_to_install(&mut self) -> Vec<Package> {
let managed = self.take_packages_as_set();
let local_packages = get_all_installed_packages();
let mut diff: Vec<_> = managed
.into_iter()
.filter(|p| !local_packages.contains(p))
.collect();
diff.sort_unstable();
diff
}
fn install_packages(mut self) {
let diff = self.get_packages_to_install();
if diff.is_empty() {
println!("nothing to do");
exit(0);
}
println!("Would install the following packages:");
for p in &diff {
println!(" {p}");
}
println!();
get_user_confirmation();
run_install_command(diff);
}
fn run_action_from_arg(self) {
match self.args.subcommand() {
// Some((args::EDIT, groups)) => println!("{groups:#?}"),
Some((args::EDIT, groups)) => self.edit_group_files(groups),
Some((args::GROUPS, _)) => self.show_groups(),
Some((args::SYNC, _)) => self.install_packages(),
Some((args::UNMANAGED, _)) => self.show_unmanaged_packages(),
Some((args::VERSION, _)) => self.show_version(),
_ => todo!(),
}
}
fn edit_group_files(&self, groups: &ArgMatches) {
let files: Vec<_> = groups
.get_many::<String>("group")
.unwrap()
.map(|file| {
let mut buf = PathBuf::from(GROUPS_DIR);
buf.push(file);
buf
})
.collect();
for f in files {
println!("{f:#?}");
}
}
fn show_version(self) {
println!("pacdef, version: {}", env!("CARGO_PKG_VERSION"))
}
fn show_unmanaged_packages(mut self) {
for p in &self.get_unmanaged_packages() {
println!("{p}");
}
}
/// Returns a `Vec` of alphabetically sorted unmanaged packages.
fn get_unmanaged_packages(&mut self) -> Vec<Package> {
let managed = self.take_packages_as_set();
let explicitly_installed = get_explicitly_installed_packages();
let mut result: Vec<_> = explicitly_installed
.into_iter()
.filter(|p| !managed.contains(p))
.collect();
result.sort_unstable();
result
}
fn show_groups(mut self) {
let groups = self.groups.take().unwrap();
let mut vec: Vec<_> = groups.iter().collect();
vec.sort_unstable();
for g in vec {
println!("{}", g.name);
}
}
}
+2 -2
View File
@@ -7,8 +7,8 @@ use std::io::BufReader;
use std::io::Lines; use std::io::Lines;
#[derive(Debug, Eq, PartialOrd, Ord)] #[derive(Debug, Eq, PartialOrd, Ord)]
pub(crate) struct Package { pub struct Package {
pub(crate) name: String, pub name: String,
repo: Option<String>, repo: Option<String>,
} }