Skip to main content

lux_cli/
unpack.rs

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