From 717e0cf60c2950cabe5083dda53821a55e8f2f0a Mon Sep 17 00:00:00 2001 From: LDprg Date: Tue, 7 May 2024 19:22:08 +0000 Subject: [PATCH] Rework progress bars --- Cargo.toml | 1 + src/bars.rs | 36 +++++++++++++ src/main.rs | 90 +++++++++++++++++---------------- test_packages/xz/SOMEBUILD.toml | 3 ++ 4 files changed, 86 insertions(+), 44 deletions(-) create mode 100644 src/bars.rs diff --git a/Cargo.toml b/Cargo.toml index e1cc8bc..98fefec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ tokio-stream = "0.1.15" tokio-tar = "0.3.1" tokio-util = "0.7.11" toml = "0.8.12" +url = "2.5.0" diff --git a/src/bars.rs b/src/bars.rs new file mode 100644 index 0000000..8532f04 --- /dev/null +++ b/src/bars.rs @@ -0,0 +1,36 @@ +use indicatif::MultiProgress; +use indicatif_log_bridge::LogWrapper; + +pub enum ProgressStyle { + Download, + Build, +} + +impl ProgressStyle { + pub fn value(&self) -> indicatif::ProgressStyle { + match *self { + Self::Download => indicatif::ProgressStyle::with_template( + "{spinner:.green} [{elapsed}] {wide_bar:.cyan/blue} {bytes}/{total_bytes} {bytes_per_sec} {msg} ({eta})", + ).unwrap(), + Self::Build => indicatif::ProgressStyle::with_template( + "{spinner:.green} [{elapsed}] {wide_bar:.cyan/blue} {pos}/{len} {msg}", + ).unwrap(), + }.progress_chars("#>-") + } +} + +pub fn create_multibar() -> MultiProgress { + let logger = + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")) + .format_timestamp(None) + .format_target(false) + .build(); + + let multibar = MultiProgress::new(); + + LogWrapper::new(multibar.clone(), logger) + .try_init() + .unwrap(); + + multibar +} diff --git a/src/main.rs b/src/main.rs index b8170f9..6f52bfb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,20 @@ +mod bars; mod paths; mod somebuild_config; + use clap::Parser; -use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; -use indicatif_log_bridge::LogWrapper; +use indicatif::ProgressBar; use log::{error, info}; use std::cmp::min; use std::path::Path; use std::process::exit; use tokio::fs::{self, File}; -use tokio::io::{self, AsyncReadExt, AsyncWriteExt}; +use tokio::io::AsyncReadExt; use tokio::process::Command; use tokio_stream::StreamExt; use tokio_util::io::StreamReader; +use crate::bars::{create_multibar, ProgressStyle}; use crate::paths::normalize_path; use crate::somebuild_config::Config; @@ -39,21 +41,8 @@ struct Args { #[tokio::main] async fn main() { - let logger = - env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")) - .format_timestamp(None) - .format_target(false) - .build(); - - let multibar: MultiProgress = MultiProgress::new(); - LogWrapper::new(multibar.clone(), logger) - .try_init() - .unwrap(); - let sty = ProgressStyle::with_template( - "{spinner:.green} [{elapsed}] {wide_bar:.cyan/blue} {bytes}/{total_bytes} {bytes_per_sec} {msg} ({eta})", - ) - .unwrap() - .progress_chars("#>-"); + let multibar = create_multibar(); + multibar.println("Running Package Build").unwrap(); let args = Args::parse(); @@ -109,7 +98,7 @@ async fn main() { ); let bar = multibar.add(ProgressBar::new(1)); - bar.set_style(sty.clone()); + bar.set_style(ProgressStyle::Download.value()); bar.set_message(format!( "Starting {}-{}", config.general.name, config.source.version @@ -142,20 +131,11 @@ async fn main() { let decoder = async_compression::tokio::bufread::ZstdDecoder::new(StreamReader::new(stream)); - let mut archive = tokio_tar::Archive::new(decoder); - - archive + tokio_tar::Archive::new(decoder) .unpack(&output) .await .expect("Cannot unpack archive"); - bar.finish_with_message(format!( - "Finished downloading {}-{}", - config.general.name, config.source.version - )); - - multibar.remove(&bar); - let hash = hasher.finalize(); if hash.to_string() != config.source.hash { @@ -166,40 +146,62 @@ async fn main() { hash.to_string() ); } + bar.finish_with_message(format!( + "Finished downloading {}-{}", + config.general.name, config.source.version + )); + + let bar_build = multibar.add(ProgressBar::new(3)); + bar_build.set_style(ProgressStyle::Build.value()); - info!("Running Configure!"); - let cmd_output = Command::new("./configure") + bar_build.set_message(format!( + "Setup {}-{}", + config.general.name, config.source.version + )); + Command::new("./configure") .current_dir(&output.join("xz-5.4.6")) .args([ "--prefix=/usr", - "--docdir=/usr/share/doc/xz-5.4.6", + format!( + "--docdir=/usr/share/doc/{}-{}", + config.general.name, config.source.version + ) + .as_str(), "--disable-static", ]) .output() .await .expect("failed to run configure"); - io::stdout().write_all(&cmd_output.stdout).await.unwrap(); - io::stderr().write_all(&cmd_output.stderr).await.unwrap(); + bar_build.inc(1); - info!("Running make!"); - let cmd_output = Command::new("make") + bar_build.set_message(format!( + "Building {}-{}", + config.general.name, config.source.version + )); + Command::new("make") .current_dir(&output.join("xz-5.4.6")) .output() .await .expect("failed to run make"); - io::stdout().write_all(&cmd_output.stdout).await.unwrap(); - io::stderr().write_all(&cmd_output.stderr).await.unwrap(); + bar_build.inc(1); - info!("Running make install!"); - let cmd_output = Command::new("make") + bar_build.set_message(format!( + "Packaging {}-{}", + config.general.name, config.source.version + )); + Command::new("make") .current_dir(&output.join("xz-5.4.6")) .args([ - "DESTDIR=".to_owned() + output.to_str().unwrap(), - "install".to_string(), + format!("DESTDIR={}", output.to_str().unwrap()).as_str(), + "install", ]) .output() .await .expect("failed to run make"); - io::stdout().write_all(&cmd_output.stdout).await.unwrap(); - io::stderr().write_all(&cmd_output.stderr).await.unwrap(); + bar_build.inc(1); + + bar_build.finish_with_message(format!( + "Finished building {}-{}", + config.general.name, config.source.version + )); } diff --git a/test_packages/xz/SOMEBUILD.toml b/test_packages/xz/SOMEBUILD.toml index 5ec472a..dd0a26b 100644 --- a/test_packages/xz/SOMEBUILD.toml +++ b/test_packages/xz/SOMEBUILD.toml @@ -29,3 +29,6 @@ setup = """ build = """ %make """ +install = """ + %make_install +"""