Skip to content

Commit

Permalink
Rework progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
LDprg committed May 7, 2024
1 parent 90f78fe commit 717e0cf
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 44 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
36 changes: 36 additions & 0 deletions src/bars.rs
Original file line number Diff line number Diff line change
@@ -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
}
90 changes: 46 additions & 44 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
));
}
3 changes: 3 additions & 0 deletions test_packages/xz/SOMEBUILD.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ setup = """
build = """
%make
"""
install = """
%make_install
"""

0 comments on commit 717e0cf

Please sign in to comment.