start reimplementing main functions again

This commit is contained in:
steven-omaha
2023-01-09 18:30:12 +01:00
parent ce98fa1cea
commit 4907787911
6 changed files with 107 additions and 46 deletions
+4
View File
@@ -16,5 +16,9 @@ macro_rules! impl_backend_constants {
fn get_switches_remove(&self) -> Switches { fn get_switches_remove(&self) -> Switches {
SWITCHES_REMOVE SWITCHES_REMOVE
} }
fn get_managed_packages(&self) -> &HashSet<Package> {
&self.packages
}
}; };
} }
+30 -6
View File
@@ -13,8 +13,8 @@ pub use rust::Rust;
pub(in crate::backend) type Switches = &'static [&'static str]; pub(in crate::backend) type Switches = &'static [&'static str];
pub(in crate::backend) type Text = &'static str; pub(in crate::backend) type Text = &'static str;
#[derive(Debug)] #[derive(Debug, Hash, PartialEq, Eq)]
pub enum Backends { pub(crate) enum Backends {
Pacman, Pacman,
Rust, Rust,
} }
@@ -24,7 +24,7 @@ impl Backends {
BackendIter(Some(Self::Pacman)) BackendIter(Some(Self::Pacman))
} }
pub fn get(&self) -> Box<dyn Backend> { fn get(&self) -> Box<dyn Backend> {
match self { match self {
Self::Pacman => Box::new(Pacman { Self::Pacman => Box::new(Pacman {
packages: HashSet::new(), packages: HashSet::new(),
@@ -36,7 +36,7 @@ impl Backends {
} }
} }
pub struct BackendIter(Option<Backends>); pub(crate) struct BackendIter(Option<Backends>);
impl Iterator for BackendIter { impl Iterator for BackendIter {
type Item = Box<dyn Backend>; type Item = Box<dyn Backend>;
@@ -56,11 +56,12 @@ impl Iterator for BackendIter {
} }
} }
pub trait Backend { pub(crate) trait Backend {
fn get_binary(&self) -> Text; fn get_binary(&self) -> Text;
fn get_section(&self) -> Text; fn get_section(&self) -> Text;
fn get_switches_install(&self) -> Switches; fn get_switches_install(&self) -> Switches;
fn get_switches_remove(&self) -> Switches; fn get_switches_remove(&self) -> Switches;
fn get_managed_packages(&self) -> &HashSet<Package>;
/// Get all packages that are installed in the system. /// Get all packages that are installed in the system.
fn get_all_installed_packages(&self) -> HashSet<Package>; fn get_all_installed_packages(&self) -> HashSet<Package>;
@@ -69,7 +70,11 @@ pub trait Backend {
fn get_explicitly_installed_packages(&self) -> HashSet<Package>; fn get_explicitly_installed_packages(&self) -> HashSet<Package>;
/// Install the specified packages. /// Install the specified packages.
fn install_packages(&self, packages: Vec<Package>) { fn install_packages(&self, packages: &[Package]) {
if packages.is_empty() {
return;
}
let mut cmd = Command::new(self.get_binary()); let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_install()); cmd.args(self.get_switches_install());
for p in packages { for p in packages {
@@ -100,5 +105,24 @@ pub trait Backend {
.collect() .collect()
} }
fn get_missing_packages_sorted(&self) -> Vec<Package> {
let installed = self.get_all_installed_packages();
let managed = self.get_managed_packages();
let mut diff: Vec<_> = managed.difference(&installed).cloned().collect();
diff.sort_unstable();
diff
}
fn add_packages(&mut self, packages: HashSet<Package>); fn add_packages(&mut self, packages: HashSet<Package>);
fn get_unmanaged_packages_sorted(&self) -> Vec<Package> {
let installed = self.get_explicitly_installed_packages();
let required = self.get_managed_packages();
let mut diff: Vec<_> = dbg!(installed)
.difference(dbg!(required))
.cloned()
.collect();
diff.sort_unstable();
diff
}
} }
+68 -32
View File
@@ -1,4 +1,4 @@
use std::collections::HashSet; use std::collections::{HashMap, HashSet};
use std::process::exit; use std::process::exit;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
@@ -21,33 +21,35 @@ impl Pacdef {
Self { args, groups } Self { args, groups }
} }
// 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(&self) { pub(crate) fn install_packages(&self) {
for b in Backends::iter() { let mut to_install = ToInstallPerBackend::new();
println!("{}", b.get_binary());
}
// 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); for b in Backends::iter() {
print!("{}: ", b.get_binary());
let diff = b.get_missing_packages_sorted();
if diff.is_empty() {
println!("nothing to do");
continue;
}
println!(" Would install the following packages:");
for p in &diff {
println!(" {p}");
}
to_install.push((b, diff));
println!();
}
if to_install.nothing_to_do_for_all_backends() {
return;
}
if !get_user_confirmation() {
return;
};
to_install.install_missing_packages()
} }
#[allow(clippy::unit_arg)] #[allow(clippy::unit_arg)]
@@ -57,7 +59,7 @@ impl Pacdef {
Some((action::EDIT, groups)) => self.edit_group_files(groups).context("editing"), 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::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()), Some((action::VERSION, _)) => Ok(self.show_version()),
_ => todo!(), _ => todo!(),
} }
@@ -87,11 +89,19 @@ impl Pacdef {
println!("pacdef, version: {}", env!("CARGO_PKG_VERSION")) println!("pacdef, version: {}", env!("CARGO_PKG_VERSION"))
} }
// pub(crate) fn show_unmanaged_packages(mut self) { pub(crate) fn show_unmanaged_packages(self) {
// for p in &self.get_unmanaged_packages() { for b in Backends::iter() {
// println!("{p}"); let unmanaged = b.get_unmanaged_packages_sorted();
// } if unmanaged.is_empty() {
// } continue;
}
println!("{}", b.get_section());
for p in unmanaged {
println!(" {p}");
}
}
}
// /// Returns a `Vec` of alphabetically sorted unmanaged packages. // /// Returns a `Vec` of alphabetically sorted unmanaged packages.
// pub(crate) fn get_unmanaged_packages(&mut self) -> Vec<Package> { // pub(crate) fn get_unmanaged_packages(&mut self) -> Vec<Package> {
@@ -129,3 +139,29 @@ impl Pacdef {
// Pacman::remove_packages(unmanaged); // Pacman::remove_packages(unmanaged);
// } // }
} }
struct ToInstallPerBackend(Vec<(Box<dyn Backend>, Vec<Package>)>);
impl ToInstallPerBackend {
fn new() -> Self {
Self(vec![])
}
fn push(&mut self, item: (Box<dyn Backend>, Vec<Package>)) {
self.0.push(item);
}
fn iter(&self) -> impl Iterator<Item = &(Box<dyn Backend>, Vec<Package>)> {
self.0.iter()
}
fn nothing_to_do_for_all_backends(&self) -> bool {
self.0.iter().all(|(_, diff)| diff.is_empty())
}
fn install_missing_packages(&self) {
self.0
.iter()
.for_each(|(backend, diff)| backend.install_packages(diff));
}
}
+2 -2
View File
@@ -22,9 +22,9 @@ impl Group {
let path = crate::path::get_pacdef_group_dir().context("getting pacdef group dir")?; let path = crate::path::get_pacdef_group_dir().context("getting pacdef group dir")?;
for entry in path.read_dir().context("reading group dir")? { for entry in path.read_dir().context("reading group dir")? {
let file = entry.context("getting a file")?; let file = entry.context("getting a file")?;
let name = file.file_name(); let path = file.path();
let group = Group::try_from(name)?; let group = Group::try_from(dbg!(path))?;
result.insert(group); result.insert(group);
} }
+1 -1
View File
@@ -2,7 +2,7 @@ use std::collections::HashSet;
use std::fmt::{Display, Write}; use std::fmt::{Display, Write};
use std::hash::Hash; use std::hash::Hash;
#[derive(Debug, Eq, PartialOrd, Ord)] #[derive(Debug, Eq, PartialOrd, Ord, Clone)]
pub struct Package { pub struct Package {
pub name: String, pub name: String,
repo: Option<String>, repo: Option<String>,
+2 -5
View File
@@ -1,11 +1,8 @@
use std::io::{BufRead, Write}; use std::io::{BufRead, Write};
use std::process::exit;
pub(crate) fn get_user_confirmation() { pub(crate) fn get_user_confirmation() -> bool {
print!("Continue? [Y/n] "); print!("Continue? [Y/n] ");
std::io::stdout().flush().unwrap(); std::io::stdout().flush().unwrap();
let reply = std::io::stdin().lock().lines().next().unwrap().unwrap(); let reply = std::io::stdin().lock().lines().next().unwrap().unwrap();
if !(reply.is_empty() || reply.to_lowercase().contains('y')) { !(reply.is_empty() || reply.to_lowercase().contains('y'))
exit(0)
}
} }