Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Add unpacking.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Aug 20, 2023
1 parent 8d805ea commit 45d6555
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 21 deletions.
68 changes: 66 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "MIT"
jpm_common = { path = "../common" }
miette = { workspace = true }
reqwest = { workspace = true }
starbase_archive = { workspace = true, features = ["tar-gz", "tar-xz"] }
starbase_styles = { workspace = true }
starbase_utils = { workspace = true }
thiserror = { workspace = true }
Expand Down
19 changes: 14 additions & 5 deletions crates/store/src/storage_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use jpm_common::{EsTarget, PackageName, Version};
use std::path::PathBuf;

pub trait StorageItem {
fn get_archive_ext(&self) -> String;
fn get_archive_ext(&self) -> &str;
fn get_label(&self) -> &str;
fn to_file_path(&self) -> PathBuf;
fn to_file_prefix(&self) -> String;
}
Expand All @@ -14,8 +15,12 @@ pub struct PackageItem<'app> {
}

impl<'app> StorageItem for PackageItem<'app> {
fn get_archive_ext(&self) -> String {
"tar.xz".into()
fn get_archive_ext(&self) -> &str {
"tar.xz"
}

fn get_label(&self) -> &str {
self.package.as_str()
}

fn to_file_path(&self) -> PathBuf {
Expand All @@ -42,8 +47,12 @@ pub struct TypeScriptItem<'app> {
}

impl<'app> StorageItem for TypeScriptItem<'app> {
fn get_archive_ext(&self) -> String {
"tar.gz".into() // What npm uses
fn get_archive_ext(&self) -> &str {
"tar.gz" // What npm uses
}

fn get_label(&self) -> &str {
"typescript"
}

fn to_file_path(&self) -> PathBuf {
Expand Down
62 changes: 48 additions & 14 deletions crates/store/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::storage_item::StorageItem;
use crate::store_error::StoreError;
use starbase_archive::Archiver;
use starbase_utils::fs::{self, FsError};
use std::io;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use tracing::debug;

pub struct Store {
Expand All @@ -17,22 +18,22 @@ impl Store {
url: &str,
item: impl StorageItem,
) -> miette::Result<PathBuf> {
let filename = format!("{}.{}", item.to_file_prefix(), item.get_archive_ext());
let archive_file = self.cache_dir.join(format!(
"{}.{}",
item.to_file_prefix(),
item.get_archive_ext()
));

self.download_archive_with_name(url, &filename).await
}

pub async fn download_archive_with_name(
&self,
url: &str,
filename: &str,
) -> miette::Result<PathBuf> {
let archive_file = self.cache_dir.join(filename);

debug!(url = ?url, cache_file = ?archive_file, "Downloading package archive");
debug!(
item = item.get_label(),
archive_url = ?url,
cache_file = ?archive_file,
"Downloading package archive",
);

if archive_file.exists() {
debug!(
item = item.get_label(),
cache_file = ?archive_file,
"Package archive already exists in local cache, skipping download"
);
Expand Down Expand Up @@ -74,8 +75,41 @@ impl Store {
error,
})?;

debug!(cache_file = ?archive_file, "Downloaded package archive");
debug!(
item = item.get_label(),
cache_file = ?archive_file,
"Downloaded package archive",
);

Ok(archive_file)
}

pub async fn unpack_archive(
&self,
archive_file: &Path,
item: impl StorageItem,
) -> miette::Result<PathBuf> {
let output_dir = self.packages_dir.join(item.to_file_path());

debug!(
item = item.get_label(),
archive_file = ?archive_file,
output_dir = ?output_dir,
"Unpacking package archive",
);

if output_dir.exists() {
return Ok(output_dir);
}

Archiver::new(&output_dir, archive_file).unpack_from_ext()?;

debug!(
item = item.get_label(),
output_dir = ?output_dir,
"Unpacked package archive",
);

Ok(output_dir)
}
}

0 comments on commit 45d6555

Please sign in to comment.