Skip to content

Commit

Permalink
progress bar (#811)
Browse files Browse the repository at this point in the history
* progress bar

* fix bad copy paste for configs

* disable with -v
  • Loading branch information
BrettMayson authored Oct 22, 2024
1 parent c3be35a commit d23d6e6
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 15 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 28 additions & 6 deletions bin/src/modules/archive.rs
Original file line number Diff line number Diff line change
@@ -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
///
Expand All @@ -27,7 +35,7 @@ pub fn release(ctx: &Context) -> Result<Report, Error> {
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;
Expand All @@ -48,7 +56,7 @@ pub fn release(ctx: &Context) -> Result<Report, Error> {
path.replace('\\', "/")
);
trace!("zip: creating directory {:?}", dir);
zip.add_directory(dir, options)?;
to_write.push(Entry::Directory(dir));
continue;
}
let name = path
Expand All @@ -60,9 +68,23 @@ pub fn release(ctx: &Context) -> Result<Report, Error> {
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, {
Expand Down
17 changes: 13 additions & 4 deletions bin/src/modules/files.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)?);
Expand Down Expand Up @@ -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())
}
Expand Down
5 changes: 4 additions & 1 deletion bin/src/modules/pbo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,15 +42,18 @@ pub fn build(ctx: &Context, collapse: Collapse) -> Result<Report, Error> {
})
};
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::<Result<Vec<_>, Error>>()?;
progress.finish_and_clear();
info!("Built {} PBOs", counter.load(Ordering::Relaxed));
Ok(Report::new())
}
Expand Down
7 changes: 4 additions & 3 deletions bin/src/modules/rapifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ 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;

// type RapifyResult = (Vec<(String, Vec<Annotation>)>, Result<(), Error>);

#[derive(Default)]
pub struct Rapifier;

Expand Down Expand Up @@ -68,11 +66,13 @@ impl Module for Rapifier {
})
.collect::<Result<Vec<_>, Error>>()?;

let progress = progress_bar(entries.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::<Result<Vec<Report>, Error>>()?;
Expand All @@ -81,6 +81,7 @@ impl Module for Rapifier {
report.merge(new_report);
}

progress.finish_and_clear();
info!("Rapified {} addon configs", counter.load(Ordering::Relaxed));
Ok(report)
}
Expand Down
5 changes: 4 additions & 1 deletion bin/src/modules/sqf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)| {
Expand All @@ -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);
Expand Down Expand Up @@ -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)
}
Expand Down
17 changes: 17 additions & 0 deletions bin/src/progress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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()
|| 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"),
)
}

0 comments on commit d23d6e6

Please sign in to comment.