lux_cli/
unpack.rs

1use std::{fs::File, io::Cursor, path::PathBuf};
2
3use clap::Args;
4use eyre::Result;
5use lux_lib::{
6    config::Config,
7    operations,
8    package::PackageReq,
9    progress::{MultiProgress, Progress},
10};
11
12#[derive(Args)]
13pub struct Unpack {
14    /// A path to a .src.rock file. Usually obtained via `lux download`.
15    path: PathBuf,
16    /// Where to unpack the rock.
17    destination: Option<PathBuf>,
18}
19
20#[derive(Args)]
21pub struct UnpackRemote {
22    pub package_req: PackageReq,
23    /// The directory to unpack to
24    pub path: Option<PathBuf>,
25}
26
27pub async fn unpack(data: Unpack) -> Result<()> {
28    let destination = data.destination.unwrap_or_else(|| {
29        PathBuf::from(data.path.to_string_lossy().trim_end_matches(".src.rock"))
30    });
31    let src_file = File::open(data.path)?;
32    let progress = MultiProgress::new();
33    let bar = Progress::Progress(progress.new_bar());
34
35    let unpack_path = lux_lib::operations::unpack_src_rock(src_file, destination, &bar).await?;
36
37    bar.map(|b| {
38        b.finish_with_message(format!(
39            "
40You may now enter the following directory:
41{}
42and type `lux make` to build.",
43            unpack_path.display()
44        ))
45    });
46
47    Ok(())
48}
49
50pub async fn unpack_remote(data: UnpackRemote, config: Config) -> Result<()> {
51    let package_req = data.package_req;
52    let progress = MultiProgress::new();
53    let bar = Progress::Progress(progress.new_bar());
54    let rock = operations::Download::new(&package_req, &config, &bar)
55        .search_and_download_src_rock()
56        .await?;
57    let cursor = Cursor::new(rock.bytes);
58
59    let destination = data
60        .path
61        .unwrap_or_else(|| PathBuf::from(format!("{}-{}", &rock.name, &rock.version)));
62    let unpack_path = lux_lib::operations::unpack_src_rock(cursor, destination, &bar).await?;
63
64    bar.map(|p| {
65        p.finish_with_message(format!(
66            "
67You may now enter the following directory:
68{}
69and type `lux make` to build.",
70            unpack_path.display()
71        ))
72    });
73
74    Ok(())
75}