Skip to content

Commit

Permalink
Make decompression more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
LDprg committed May 14, 2024
1 parent 9b24baa commit dcc60b3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 31 deletions.
31 changes: 31 additions & 0 deletions src/decompress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::path::Path;
use std::process::exit;

use log::error;
use tokio::pin;
use tokio_stream::Stream;
use tokio_util::{bytes::Buf, io::StreamReader};

use crate::fatal;

pub async fn decompress<'a>(
stream: impl Stream<Item = Result<impl Buf, impl Into<std::io::Error>>>,
output: &Path,
file_name: &'a str,
) -> &'a str {
if file_name.ends_with(".tar.zst") {
let decoder =
async_compression::tokio::bufread::ZstdDecoder::new(StreamReader::new(stream));

pin!(decoder);

tokio_tar::Archive::new(decoder)
.unpack(&output)
.await
.expect("Cannot unpack archive");

&file_name[..".tar.zst".len()]
} else {
fatal!("Extension of \"{}\" not supported!", file_name);
}
}
66 changes: 35 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod bars;
mod decompress;
mod paths;
mod somebuild_config;

Expand All @@ -12,10 +13,10 @@ use std::process::exit;
use tokio::fs::{self, File};
use tokio::io::AsyncReadExt;
use tokio_stream::StreamExt;
use tokio_util::io::StreamReader;
use url::Url;

use crate::bars::{create_multibar, ProgressStyle};
use crate::decompress::decompress;
use crate::paths::normalize_path;
use crate::somebuild_config::Config;

Expand Down Expand Up @@ -132,12 +133,10 @@ async fn main() {
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))
});

let decoder = async_compression::tokio::bufread::ZstdDecoder::new(StreamReader::new(stream));
let url = Url::parse(&config.source.url).unwrap();
let file_name = url.path_segments().unwrap().last().unwrap();

tokio_tar::Archive::new(decoder)
.unpack(&output)
.await
.expect("Cannot unpack archive");
let file_name = decompress(stream, &output, file_name).await;

let hash = hasher.finalize();

Expand All @@ -164,29 +163,24 @@ async fn main() {

bar_build.tick();

let url = Url::parse(&config.source.url).unwrap();
let file_name = url.path_segments().unwrap().last().unwrap();

let mut file_name: Vec<&str> = file_name.split('.').collect();

file_name.pop();
file_name.pop();

let file_name = file_name.join(".");

info!("Extraction Folder: {}", file_name);

let configure_cmd = config.build.setup.replace(
"%configure",
format!(
"./configure --prefix=/usr --docdir=/usr/share/doc/{}-{}",
config.general.name, config.source.version
let configure_cmd = config
.build
.setup
.replace(
"%configure",
format!(
"./configure --prefix=/usr --docdir=/usr/share/doc/{}-{}",
config.general.name, config.source.version
)
.trim(),
)
.trim(),
);
.trim()
.to_string();

let mut options = ScriptOptions::new();
options.working_directory = Some(output.join(&file_name));
options.working_directory = Some(output.join(file_name));

let (code, out, error) = run_script::run_script!(configure_cmd, options).unwrap();
if code != 0 {
Expand All @@ -202,10 +196,15 @@ async fn main() {
config.general.name, config.source.version
));

let make_cmd = config.build.build.replace("%make", "make");
let make_cmd = config
.build
.build
.replace("%make", "make")
.trim()
.to_string();

let mut options = ScriptOptions::new();
options.working_directory = Some(output.join(&file_name));
options.working_directory = Some(output.join(file_name));

let (code, out, error) = run_script::run_script!(make_cmd, options).unwrap();
if code != 0 {
Expand All @@ -221,13 +220,18 @@ async fn main() {
config.general.name, config.source.version
));

let make_install_cmd = config.build.install.replace(
"%make_install",
format!("make DESTDIR={} install", output.to_str().unwrap()).trim(),
);
let make_install_cmd = config
.build
.install
.replace(
"%make_install",
format!("make DESTDIR={} install", output.to_str().unwrap()).trim(),
)
.trim()
.to_string();

let mut options = ScriptOptions::new();
options.working_directory = Some(output.join(&file_name));
options.working_directory = Some(output.join(file_name));

let (code, out, error) = run_script::run_script!(make_install_cmd, options).unwrap();
if code != 0 {
Expand Down

0 comments on commit dcc60b3

Please sign in to comment.