get generic backend to work (to some extent)

This commit is contained in:
steven-omaha
2023-01-06 13:31:50 +01:00
parent ef4d0bc723
commit 80fb9d7252
6 changed files with 218 additions and 120 deletions
+57 -23
View File
@@ -8,37 +8,71 @@ use std::process::Command;
use crate::Package;
pub use pacman::Pacman;
type Switches = &'static [&'static str];
type Text = &'static str;
pub use rust::Rust;
pub type Switches = &'static [&'static str];
pub type Text = &'static str;
#[derive(Debug)]
pub enum Backends {
Pacman,
Rust,
}
impl Backends {
pub fn iter() -> BackendIter {
BackendIter(Some(Self::Pacman))
}
}
pub struct BackendIter(Option<Backends>);
impl Iterator for BackendIter {
type Item = Box<dyn Backend>;
fn next(&mut self) -> Option<Self::Item> {
match self.0 {
Some(Backends::Pacman) => {
self.0 = Some(Backends::Rust);
Some(Box::new(Pacman::new()))
}
Some(Backends::Rust) => {
self.0 = None;
Some(Box::new(Rust::new()))
}
None => None,
}
}
}
impl Backends {
pub fn get(&self) -> Box<dyn Backend> {
match self {
Self::Pacman => Box::new(Pacman {
packages: HashSet::new(),
}),
Self::Rust => Box::new(Rust {
packages: HashSet::new(),
}),
}
}
}
pub trait Backend {
/// The name that introduces the section in the group file
const SECTION: Text;
/// The binary that should be called to run the associated package manager.
const BINARY: Text;
/// The switches that signals the `BINARY` that the packages should be installed.
const SWITCHES_INSTALL: Switches;
/// The switches that signals the `BINARY` that the packages should be removed.
const SWITCHES_REMOVE: Switches;
fn get_binary(&self) -> Text;
fn get_section(&self) -> Text;
fn get_switches_install(&self) -> Switches;
fn get_switches_remove(&self) -> Switches;
/// Get all packages that are installed in the system.
fn get_all_installed_packages() -> HashSet<Package>;
fn get_all_installed_packages(&self) -> HashSet<Package>;
/// Get all packages that were installed in the system explicitly.
fn get_explicitly_installed_packages() -> HashSet<Package>;
fn get_explicitly_installed_packages(&self) -> HashSet<Package>;
/// Install the specified packages.
fn install_packages(packages: Vec<Package>) {
let mut cmd = Command::new(Self::BINARY);
cmd.args(Self::SWITCHES_INSTALL);
fn install_packages(&self, packages: Vec<Package>) {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_install());
for p in packages {
cmd.arg(format!("{p}"));
}
@@ -46,9 +80,9 @@ pub trait Backend {
}
/// Remove the specified packages.
fn remove_packages(packages: Vec<Package>) {
let mut cmd = Command::new(Self::BINARY);
cmd.args(Self::SWITCHES_REMOVE);
fn remove_packages(&self, packages: Vec<Package>) {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_remove());
for p in packages {
cmd.arg(format!("{p}"));
}
@@ -56,10 +90,10 @@ pub trait Backend {
}
/// extract packages from its own section as read from group files
fn extract_packages_from_group_file_content(content: &str) -> HashSet<Package> {
fn extract_packages_from_group_file_content(&self, content: &str) -> HashSet<Package> {
content
.lines()
.skip_while(|line| !line.starts_with(&format!("[{}]", Self::SECTION)))
.skip_while(|line| !line.starts_with(&format!("[{}]", self.get_section())))
.skip(1)
.filter(|line| !line.starts_with('['))
.fuse()
+29 -9
View File
@@ -6,25 +6,43 @@ use alpm::PackageReason::Explicit;
use super::{Backend, Switches, Text};
use crate::Package;
pub struct Pacman(HashSet<Package>);
pub struct Pacman {
pub packages: HashSet<Package>,
}
const BINARY: Text = "paru";
const SECTION: Text = "pacman";
const SWITCHES_INSTALL: Switches = &["-S"];
const SWITCHES_REMOVE: Switches = &["-Rsn"];
impl Backend for Pacman {
const BINARY: Text = "paru";
const SECTION: Text = "pacman";
const SWITCHES_INSTALL: Switches = &["-S"];
const SWITCHES_REMOVE: Switches = &["-Rsn"];
fn get_binary(&self) -> Text {
BINARY
}
fn get_all_installed_packages() -> HashSet<Package> {
fn get_section(&self) -> Text {
SECTION
}
fn get_switches_install(&self) -> Switches {
SWITCHES_INSTALL
}
fn get_switches_remove(&self) -> Switches {
SWITCHES_REMOVE
}
fn get_all_installed_packages(&self) -> HashSet<Package> {
convert_to_pacdef_packages(get_all_installed_packages_from_alpm())
}
fn get_explicitly_installed_packages() -> HashSet<Package> {
fn get_explicitly_installed_packages(&self) -> HashSet<Package> {
convert_to_pacdef_packages(get_explicitly_installed_packages_from_alpm())
}
fn add_packages(&mut self, packages: HashSet<Package>) {
for p in packages {
self.0.insert(p);
self.packages.insert(p);
}
}
}
@@ -54,7 +72,9 @@ fn convert_to_pacdef_packages(packages: HashSet<String>) -> HashSet<Package> {
impl Pacman {
pub fn new() -> Self {
Self(HashSet::new())
Self {
packages: HashSet::new(),
}
}
}
+30 -10
View File
@@ -3,27 +3,45 @@ use std::{collections::HashSet, process::Command};
use super::{Backend, Switches, Text};
use crate::Package;
pub struct Rust(HashSet<Package>);
pub struct Rust {
pub packages: HashSet<Package>,
}
const BINARY: Text = "cargo";
const SECTION: Text = "rust";
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_REMOVE: Switches = &["uninstall"];
impl Backend for Rust {
const BINARY: Text = "cargo";
const SECTION: Text = "rust";
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_REMOVE: Switches = &["uninstall"];
fn get_binary(&self) -> Text {
BINARY
}
fn get_all_installed_packages() -> HashSet<Package> {
fn get_section(&self) -> Text {
SECTION
}
fn get_switches_install(&self) -> Switches {
SWITCHES_INSTALL
}
fn get_switches_remove(&self) -> Switches {
SWITCHES_REMOVE
}
fn get_all_installed_packages(&self) -> HashSet<Package> {
extract_packages_names(&run_cargo_install_list())
.map(Package::from)
.collect()
}
fn get_explicitly_installed_packages() -> HashSet<Package> {
Self::get_all_installed_packages()
fn get_explicitly_installed_packages(&self) -> HashSet<Package> {
self.get_all_installed_packages()
}
fn add_packages(&mut self, packages: HashSet<Package>) {
for p in packages {
self.0.insert(p);
self.packages.insert(p);
}
}
}
@@ -46,7 +64,9 @@ fn extract_packages_names(output: &str) -> impl Iterator<Item = String> + '_ {
impl Rust {
pub fn new() -> Self {
Self(HashSet::new())
Self {
packages: HashSet::new(),
}
}
}
+67 -78
View File
@@ -5,7 +5,7 @@ use anyhow::{bail, Context, Result};
use clap::ArgMatches;
use crate::action;
use crate::backend::{Backend, Pacman};
use crate::backend::{Backend, Backends, Pacman};
use crate::cmd::run_edit_command;
use crate::ui::get_user_confirmation;
use crate::Group;
@@ -13,62 +13,51 @@ use crate::Package;
pub struct Pacdef {
pub(crate) args: ArgMatches,
pub(crate) groups: Option<HashSet<Group>>,
// action: Box<dyn Fn(Self)>,
pub(crate) groups: HashSet<Group>,
}
impl Pacdef {
pub fn new(args: ArgMatches, groups: HashSet<Group>) -> Self {
Self {
args,
groups: Some(groups),
// action: Box::new(Self::install_packages),
}
Self { args, groups }
}
pub(crate) fn take_packages_as_set(&mut self) -> HashSet<Package> {
self.groups
.take()
.unwrap()
.into_iter()
.flat_map(|g| g.packages)
.collect()
}
// pub(crate) fn get_packages_to_install(&mut self) -> Vec<Package> {
// let managed = self.take_packages_as_set();
// let local_packages = Pacman::get_all_installed_packages();
// let mut diff: Vec<_> = managed
// .into_iter()
// .filter(|p| !local_packages.contains(p))
// .collect();
// diff.sort_unstable();
// diff
// }
pub(crate) fn get_packages_to_install(&mut self) -> Vec<Package> {
let managed = self.take_packages_as_set();
let local_packages = Pacman::get_all_installed_packages();
let mut diff: Vec<_> = managed
.into_iter()
.filter(|p| !local_packages.contains(p))
.collect();
diff.sort_unstable();
diff
}
pub(crate) fn install_packages(mut self) {
let diff = self.get_packages_to_install();
if diff.is_empty() {
println!("nothing to do");
exit(0);
pub(crate) fn install_packages(&self) {
for b in Backends::iter() {
println!("{}", b.get_binary());
}
println!("Would install the following packages:");
for p in &diff {
println!(" {p}");
}
crate::ui::get_user_confirmation();
// 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}");
// }
// crate::ui::get_user_confirmation();
Pacman::install_packages(diff);
// Pacman::install_packages(diff);
}
#[allow(clippy::unit_arg)]
pub fn run_action_from_arg(self) -> Result<()> {
match self.args.subcommand() {
Some((action::CLEAN, _)) => Ok(self.clean_packages()),
// Some((action::CLEAN, _)) => Ok(self.clean_packages()),
Some((action::EDIT, groups)) => self.edit_group_files(groups).context("editing"),
Some((action::GROUPS, _)) => Ok(self.show_groups()),
// Some((action::GROUPS, _)) => Ok(self.show_groups()),
Some((action::SYNC, _)) => Ok(self.install_packages()),
Some((action::UNMANAGED, _)) => Ok(self.show_unmanaged_packages()),
// Some((action::UNMANAGED, _)) => Ok(self.show_unmanaged_packages()),
Some((action::VERSION, _)) => Ok(self.show_version()),
_ => todo!(),
}
@@ -98,45 +87,45 @@ impl Pacdef {
println!("pacdef, version: {}", env!("CARGO_PKG_VERSION"))
}
pub(crate) fn show_unmanaged_packages(mut self) {
for p in &self.get_unmanaged_packages() {
println!("{p}");
}
}
// pub(crate) fn show_unmanaged_packages(mut self) {
// for p in &self.get_unmanaged_packages() {
// println!("{p}");
// }
// }
/// Returns a `Vec` of alphabetically sorted unmanaged packages.
pub(crate) fn get_unmanaged_packages(&mut self) -> Vec<Package> {
let managed = self.take_packages_as_set();
let explicitly_installed = Pacman::get_explicitly_installed_packages();
let mut result: Vec<_> = explicitly_installed
.into_iter()
.filter(|p| !managed.contains(p))
.collect();
result.sort_unstable();
result
}
// /// Returns a `Vec` of alphabetically sorted unmanaged packages.
// pub(crate) fn get_unmanaged_packages(&mut self) -> Vec<Package> {
// let managed = self.take_packages_as_set();
// let explicitly_installed = Pacman::get_explicitly_installed_packages();
// let mut result: Vec<_> = explicitly_installed
// .into_iter()
// .filter(|p| !managed.contains(p))
// .collect();
// result.sort_unstable();
// result
// }
pub(crate) 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);
}
}
// pub(crate) 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);
// }
// }
fn clean_packages(mut self) {
let unmanaged = self.get_unmanaged_packages();
if unmanaged.is_empty() {
println!("nothing to do");
return;
}
// fn clean_packages(mut self) {
// let unmanaged = self.get_unmanaged_packages();
// if unmanaged.is_empty() {
// println!("nothing to do");
// return;
// }
println!("Would remove the following packages and their dependencies:");
for p in &unmanaged {
println!(" {p}");
}
get_user_confirmation();
Pacman::remove_packages(unmanaged);
}
// println!("Would remove the following packages and their dependencies:");
// for p in &unmanaged {
// println!(" {p}");
// }
// get_user_confirmation();
// Pacman::remove_packages(unmanaged);
// }
}