diff --git a/src/creator/mod.rs b/src/creator/mod.rs index 513dda8..7075bc2 100644 --- a/src/creator/mod.rs +++ b/src/creator/mod.rs @@ -26,7 +26,7 @@ pub enum Message { pub struct Creator { path: PathBuf, - entries: Vec>, + entries: Vec, specs: Specs, } @@ -35,7 +35,11 @@ impl Creator { let d = env::current_dir()?; env::set_current_dir(path)?; let path = path.to_path_buf(); - let entries = WalkDir::new(".").into_iter().collect::>(); + let entries = WalkDir::new(".") + .into_iter() + .filter(|x| x.is_ok()) + .map(|x| x.unwrap().path().to_path_buf()) + .collect::>(); env::set_current_dir(d)?; Ok(Self { path, @@ -44,6 +48,15 @@ impl Creator { }) } + fn from_list(list: &[&str], specs: Specs) -> Result { + let entries = list.iter().map(|x| Path::new(x).to_path_buf()).collect(); + Ok(Self { + path: env::current_dir().unwrap_or(Path::new("/").to_path_buf()), + entries, + specs, + }) + } + pub fn len(&self) -> usize { self.entries.len() } @@ -69,12 +82,10 @@ impl Creator { env::set_current_dir(&self.path)?; self.entries .par_iter() - .filter(|x| x.is_ok()) - .map(|x| x.as_ref().unwrap()) - .filter(|x| x.path() != Path::new(".")) + .filter(|x| x.as_path() != Path::new(".")) .for_each(|x| { let sender = sender.lock().unwrap().clone(); - if let Ok(item) = Item::try_create(x.path().to_path_buf().as_path()) { + if let Ok(item) = Item::try_create(x.as_path()) { if let Entry::File { path: _, sha256sum: _, @@ -107,7 +118,7 @@ impl Creator { sender .send(Message::Failure(format!( "Could not process DirEntry for {}", - x.path().display() + x.display() ))) .expect("could not send message"); } diff --git a/src/lib.rs b/src/lib.rs index cb634e8..e2b4dad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ pub use { db::Database, hooks::Hooks, item::Item, - package::{create_package, Dependency, Package, Specs}, + package::{Dependency, Package, Specs}, plist::*, repository::Repository, tar, diff --git a/src/package/mod.rs b/src/package/mod.rs index dfdd00b..bbec6d7 100644 --- a/src/package/mod.rs +++ b/src/package/mod.rs @@ -127,67 +127,3 @@ impl Package { || (self.version == other.version && self.release > other.release)) } } - -/// Creates a package archive given a path to the included files, a package `Specs` -/// file and an output directory -/// -/// ## Usage -/// ``` -/// use std::path::Path; -/// use hpk::Specs; -/// -/// let staging = Path::new("staged"); -/// let outdir = Path::new("package"); -/// let specs = Specs { -/// name: "foo".to_string(), -/// version: "0.42.0".parse().unwrap(), -/// description: "an example package".to_string(), -/// ..Default::default() -/// }; -/// let result = hpk::create_package(&staging, specs, &outdir); -pub fn create_package(path: &Path, specs: Specs, outdir: &Path) -> Result<(), Box> { - if !outdir.exists() { - fs::create_dir_all(outdir)?; - } - let outdir = outdir.canonicalize()?; - env::set_current_dir(path)?; - let mut items = WalkDir::new(".") - .into_iter() - .collect::>() - .par_iter() - .filter(|x| x.is_ok()) - .filter_map(|x| Item::try_create(x.as_ref().unwrap().path().to_path_buf().as_path()).ok()) - .collect::>(); - let mut plist = Plist::default(); - let mut archive = vec![]; - let mut totalsize = 0; - while let Some(item) = items.pop() { - if let Entry::File { - path: _, - sha256sum: _, - mode: _, - size, - } = &item.entry - { - totalsize += size; - } - plist.entries.push(item.entry); - archive.extend(item.data); - } - let mut package: Package = specs.into(); - package.plist = plist; - package.size = totalsize; - let node = package.save_ron_and_create_tar_node(&outdir)?.to_vec()?; - archive.extend(node); - let name = package.fullname(); - let mut path = PathBuf::from(&name); - path.set_extension("tar.zst"); - let mut outfile = outdir; - outfile.push(path); - let fd = File::create(&outfile)?; - let mut writer = Encoder::new(fd, 0)?; - writer.write_all(&archive)?; - let _fd = writer.finish()?; - println!("{} saved", outfile.display()); - Ok(()) -}