From 019bd49978682fc2147b3edd9af3fb916849898e Mon Sep 17 00:00:00 2001 From: Brett Mayson Date: Tue, 22 Oct 2024 00:24:52 +0000 Subject: [PATCH 1/3] progress bar --- Cargo.lock | 26 ++++++++++++++++++++++++++ bin/Cargo.toml | 1 + bin/src/lib.rs | 1 + bin/src/modules/archive.rs | 34 ++++++++++++++++++++++++++++------ bin/src/modules/files.rs | 17 +++++++++++++---- bin/src/modules/pbo.rs | 5 ++++- bin/src/modules/rapifier.rs | 6 +++++- bin/src/modules/sqf.rs | 5 ++++- bin/src/progress.rs | 13 +++++++++++++ 9 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 bin/src/progress.rs diff --git a/Cargo.lock b/Cargo.lock index c55aa727..1bda85b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1416,6 +1416,7 @@ dependencies = [ "hemtt-sqf", "hemtt-stringtable", "hemtt-workspace", + "indicatif", "num_cpus", "paste", "rayon", @@ -1953,6 +1954,19 @@ dependencies = [ "hashbrown 0.15.0", ] +[[package]] +name = "indicatif" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "763a5a8f45087d6bcea4222e7b72c291a054edf80e4ef6efd2a4979878c7bea3" +dependencies = [ + "console", + "instant", + "number_prefix", + "portable-atomic", + "unicode-width", +] + [[package]] name = "inotify" version = "0.9.6" @@ -2687,6 +2701,12 @@ dependencies = [ "libc", ] +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + [[package]] name = "objc-sys" version = "0.3.5" @@ -3078,6 +3098,12 @@ dependencies = [ "miniz_oxide 0.8.0", ] +[[package]] +name = "portable-atomic" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" + [[package]] name = "powerfmt" version = "0.2.0" diff --git a/bin/Cargo.toml b/bin/Cargo.toml index 8be91117..d6c44a74 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -38,6 +38,7 @@ dirs = { workspace = true } fs_extra = "1.3.0" git2 = { workspace = true } glob = "0.3.1" +indicatif = "0.17.8" num_cpus = "1.16.0" paste = { workspace = true } rayon = "1.10.0" diff --git a/bin/src/lib.rs b/bin/src/lib.rs index c4c88a23..2065771a 100644 --- a/bin/src/lib.rs +++ b/bin/src/lib.rs @@ -11,6 +11,7 @@ pub mod executor; pub mod link; pub mod logging; pub mod modules; +mod progress; pub mod report; pub mod update; pub mod utils; diff --git a/bin/src/modules/archive.rs b/bin/src/modules/archive.rs index e6399730..479bc828 100644 --- a/bin/src/modules/archive.rs +++ b/bin/src/modules/archive.rs @@ -1,9 +1,17 @@ -use std::fs::{create_dir_all, File}; +use std::{ + fs::{create_dir_all, File}, + path::PathBuf, +}; use walkdir::WalkDir; use zip::{write::SimpleFileOptions, ZipWriter}; -use crate::{context::Context, error::Error, report::Report}; +use crate::{context::Context, error::Error, progress::progress_bar, report::Report}; + +enum Entry { + File(String, PathBuf), + Directory(String), +} /// Creates the release zips /// @@ -27,7 +35,7 @@ pub fn release(ctx: &Context) -> Result { let options = SimpleFileOptions::default().compression_level(Some(9)); debug!("creating release at {:?}", output.display()); - let mut zip = ZipWriter::new(File::create(&output)?); + let mut to_write = Vec::new(); for entry in WalkDir::new(ctx.build_folder().expect("build folder exists")) { let Ok(entry) = entry else { continue; @@ -48,7 +56,7 @@ pub fn release(ctx: &Context) -> Result { path.replace('\\', "/") ); trace!("zip: creating directory {:?}", dir); - zip.add_directory(dir, options)?; + to_write.push(Entry::Directory(dir)); continue; } let name = path @@ -60,9 +68,23 @@ pub fn release(ctx: &Context) -> Result { name.display().to_string().replace('\\', "/") ); trace!("zip: adding file {:?}", file); - zip.start_file(file, options)?; - std::io::copy(&mut File::open(path)?, &mut zip)?; + to_write.push(Entry::File(file, path.to_owned())); + } + let progress = progress_bar(to_write.len() as u64).with_message("Creating release"); + let mut zip = ZipWriter::new(File::create(&output)?); + for entry in to_write { + match entry { + Entry::File(file, path) => { + zip.start_file(file, options)?; + std::io::copy(&mut File::open(path)?, &mut zip)?; + } + Entry::Directory(dir) => { + zip.add_directory(dir, options)?; + } + } + progress.inc(1); } + progress.finish_and_clear(); zip.finish()?; info!("Created release: {}", output.display()); std::fs::copy(&output, { diff --git a/bin/src/modules/files.rs b/bin/src/modules/files.rs index 37b35973..582a8755 100644 --- a/bin/src/modules/files.rs +++ b/bin/src/modules/files.rs @@ -1,6 +1,6 @@ use std::fs::create_dir_all; -use crate::{context::Context, error::Error, report::Report}; +use crate::{context::Context, error::Error, progress::progress_bar, report::Report}; use super::Module; @@ -17,7 +17,7 @@ impl Module for Files { require_literal_separator: true, ..Default::default() }; - let mut copied = 0; + let mut to_copy = Vec::new(); let mut globs = Vec::new(); for file in ctx.config().files().include() { globs.push(glob::Pattern::new(file)?); @@ -47,10 +47,19 @@ impl Module for Files { if !folder.exists() { std::mem::drop(create_dir_all(folder)); } - debug!("copying {:?} => {:?}", entry.as_str(), d.display()); - std::io::copy(&mut entry.open_file()?, &mut std::fs::File::create(&d)?)?; + to_copy.push((entry, d)); + } + + let mut copied = 0; + let progress = progress_bar(to_copy.len() as u64).with_message("Copying files"); + for (source, dest) in to_copy { + debug!("copying {:?} => {:?}", source.as_str(), dest.display()); + progress.set_message(format!("Copying {}", source.as_str())); + std::io::copy(&mut source.open_file()?, &mut std::fs::File::create(&dest)?)?; copied += 1; + progress.inc(1); } + progress.finish_and_clear(); info!("Copied {} files", copied); Ok(Report::new()) } diff --git a/bin/src/modules/pbo.rs b/bin/src/modules/pbo.rs index b2ea24e1..033e3a7b 100644 --- a/bin/src/modules/pbo.rs +++ b/bin/src/modules/pbo.rs @@ -12,7 +12,7 @@ use hemtt_pbo::WritablePbo; use hemtt_workspace::addons::{Addon, Location}; use vfs::VfsFileType; -use crate::{context::Context, error::Error, report::Report}; +use crate::{context::Context, error::Error, progress::progress_bar, report::Report}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// Should the optional and compat PBOs be collapsed into the addons folder @@ -42,15 +42,18 @@ pub fn build(ctx: &Context, collapse: Collapse) -> Result { }) }; let counter = AtomicU16::new(0); + let progress = progress_bar(ctx.addons().to_vec().len() as u64).with_message("Building PBOs"); ctx.addons() .to_vec() .iter() .map(|addon| { internal_build(ctx, addon, collapse, &version, git_hash.as_ref())?; + progress.inc(1); counter.fetch_add(1, Ordering::Relaxed); Ok(()) }) .collect::, Error>>()?; + progress.finish_and_clear(); info!("Built {} PBOs", counter.load(Ordering::Relaxed)); Ok(Report::new()) } diff --git a/bin/src/modules/rapifier.rs b/bin/src/modules/rapifier.rs index 5ffe06d4..10928c35 100644 --- a/bin/src/modules/rapifier.rs +++ b/bin/src/modules/rapifier.rs @@ -9,7 +9,7 @@ use hemtt_workspace::{addons::Addon, WorkspacePath}; use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; use vfs::VfsFileType; -use crate::{context::Context, error::Error, report::Report}; +use crate::{context::Context, error::Error, progress::progress_bar, report::Report}; use super::Module; @@ -68,11 +68,14 @@ impl Module for Rapifier { }) .collect::, Error>>()?; + let progress = + progress_bar(ctx.addons().to_vec().len() as u64).with_message("Rapifying Configs"); let reports = entries .par_iter() .map(|(addon, entry)| { let report = rapify(addon, entry, ctx)?; counter.fetch_add(1, Ordering::Relaxed); + progress.inc(1); Ok(report) }) .collect::, Error>>()?; @@ -81,6 +84,7 @@ impl Module for Rapifier { report.merge(new_report); } + progress.finish_and_clear(); info!("Rapified {} addon configs", counter.load(Ordering::Relaxed)); Ok(report) } diff --git a/bin/src/modules/sqf.rs b/bin/src/modules/sqf.rs index 3aabd22e..a018fd43 100644 --- a/bin/src/modules/sqf.rs +++ b/bin/src/modules/sqf.rs @@ -12,7 +12,7 @@ use hemtt_sqf::{ use hemtt_workspace::reporting::{Code, CodesExt, Diagnostic, Severity}; use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; -use crate::{context::Context, error::Error, report::Report}; +use crate::{context::Context, error::Error, progress::progress_bar, report::Report}; use super::Module; @@ -69,6 +69,7 @@ impl Module for SQFCompiler { .as_ref() .expect("database not initialized") .clone(); + let progress = progress_bar(entries.len() as u64).with_message("Compiling SQF"); let reports = entries .par_iter() .map(|(addon, entry)| { @@ -91,6 +92,7 @@ impl Module for SQFCompiler { let mut out = entry.with_extension("sqfc")?.create_file()?; sqf.optimize().compile_to_writer(&processed, &mut out)?; counter.fetch_add(1, Ordering::Relaxed); + progress.inc(1); } for code in codes { report.push(code); @@ -121,6 +123,7 @@ impl Module for SQFCompiler { for new_report in reports { report.merge(new_report); } + progress.finish_and_clear(); info!("Compiled {} sqf files", counter.load(Ordering::Relaxed)); Ok(report) } diff --git a/bin/src/progress.rs b/bin/src/progress.rs new file mode 100644 index 00000000..eab9f96f --- /dev/null +++ b/bin/src/progress.rs @@ -0,0 +1,13 @@ +use indicatif::{ProgressBar, ProgressStyle}; + +#[allow(clippy::module_name_repetitions)] +pub fn progress_bar(size: u64) -> ProgressBar { + ProgressBar::new(size).with_style( + ProgressStyle::with_template(if std::env::var("CI").is_ok() { + "" + } else { + "{msg} [{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} " + }) + .expect("valid template"), + ) +} From 7509475baddb63fd0be04a00b1854f384d16c499 Mon Sep 17 00:00:00 2001 From: Brett Mayson Date: Tue, 22 Oct 2024 00:50:08 +0000 Subject: [PATCH 2/3] fix bad copy paste for configs --- bin/src/modules/rapifier.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bin/src/modules/rapifier.rs b/bin/src/modules/rapifier.rs index 10928c35..25b433c8 100644 --- a/bin/src/modules/rapifier.rs +++ b/bin/src/modules/rapifier.rs @@ -13,8 +13,6 @@ use crate::{context::Context, error::Error, progress::progress_bar, report::Repo use super::Module; -// type RapifyResult = (Vec<(String, Vec)>, Result<(), Error>); - #[derive(Default)] pub struct Rapifier; @@ -68,8 +66,7 @@ impl Module for Rapifier { }) .collect::, Error>>()?; - let progress = - progress_bar(ctx.addons().to_vec().len() as u64).with_message("Rapifying Configs"); + let progress = progress_bar(entries.len() as u64).with_message("Rapifying Configs"); let reports = entries .par_iter() .map(|(addon, entry)| { From 7fba99504f0af02d8288e0da55989243f91f72a2 Mon Sep 17 00:00:00 2001 From: Brett Mayson Date: Tue, 22 Oct 2024 00:56:20 +0000 Subject: [PATCH 3/3] disable with -v --- bin/src/progress.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bin/src/progress.rs b/bin/src/progress.rs index eab9f96f..38c44d64 100644 --- a/bin/src/progress.rs +++ b/bin/src/progress.rs @@ -3,11 +3,15 @@ use indicatif::{ProgressBar, ProgressStyle}; #[allow(clippy::module_name_repetitions)] pub fn progress_bar(size: u64) -> ProgressBar { ProgressBar::new(size).with_style( - ProgressStyle::with_template(if std::env::var("CI").is_ok() { - "" - } else { - "{msg} [{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} " - }) + ProgressStyle::with_template( + if std::env::var("CI").is_ok() + || std::env::args().any(|a| a.starts_with("-v") && a.ends_with('v')) + { + "" + } else { + "{msg} [{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} " + }, + ) .expect("valid template"), ) }