2023-11-23 18:38:07 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use deno_core::anyhow;
|
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::url::Url;
|
|
|
|
use hyper::body::Bytes;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use tar::Header;
|
|
|
|
|
|
|
|
use crate::util::import_map::ImportMapUnfurler;
|
|
|
|
|
|
|
|
pub fn create_gzipped_tarball(
|
|
|
|
dir: PathBuf,
|
|
|
|
// TODO(bartlomieju): this is too specific, factor it out into a callback that
|
|
|
|
// returns data
|
|
|
|
unfurler: ImportMapUnfurler,
|
2023-12-12 09:45:45 -05:00
|
|
|
) -> Result<(Bytes, Vec<String>), AnyError> {
|
2023-11-23 18:38:07 -05:00
|
|
|
let mut tar = TarGzArchive::new();
|
2023-11-30 13:54:54 -05:00
|
|
|
let dir = dir
|
|
|
|
.canonicalize()
|
|
|
|
.map_err(|_| anyhow::anyhow!("Unable to canonicalize path {:?}", dir))?;
|
2023-12-12 09:45:45 -05:00
|
|
|
let mut diagnostics = vec![];
|
2023-11-23 18:38:07 -05:00
|
|
|
|
2023-11-30 13:54:54 -05:00
|
|
|
for entry in walkdir::WalkDir::new(&dir).follow_links(false) {
|
2023-11-23 18:38:07 -05:00
|
|
|
let entry = entry?;
|
|
|
|
|
|
|
|
if entry.file_type().is_file() {
|
|
|
|
let url = Url::from_file_path(entry.path())
|
2023-11-30 13:54:54 -05:00
|
|
|
.map_err(|_| anyhow::anyhow!("Unable to convert path to url"))?;
|
|
|
|
let relative_path = entry
|
|
|
|
.path()
|
|
|
|
.strip_prefix(&dir)
|
|
|
|
.map_err(|err| anyhow::anyhow!("Unable to strip prefix: {err}"))?;
|
|
|
|
let relative_path = relative_path.to_str().ok_or_else(|| {
|
|
|
|
anyhow::anyhow!("Unable to convert path to string {:?}", relative_path)
|
|
|
|
})?;
|
2023-11-23 18:38:07 -05:00
|
|
|
let data = std::fs::read(entry.path())
|
|
|
|
.with_context(|| format!("Unable to read file {:?}", entry.path()))?;
|
2023-12-12 09:45:45 -05:00
|
|
|
let (content, unfurl_diagnostics) = unfurler
|
2023-11-23 18:38:07 -05:00
|
|
|
.unfurl(&url, data)
|
|
|
|
.with_context(|| format!("Unable to unfurl file {:?}", entry.path()))?;
|
2023-12-12 09:45:45 -05:00
|
|
|
|
|
|
|
diagnostics.extend_from_slice(&unfurl_diagnostics);
|
2023-11-30 13:54:54 -05:00
|
|
|
tar
|
|
|
|
.add_file(relative_path.to_string(), &content)
|
|
|
|
.with_context(|| {
|
|
|
|
format!("Unable to add file to tarball {:?}", entry.path())
|
|
|
|
})?;
|
2023-11-23 18:38:07 -05:00
|
|
|
} else if entry.file_type().is_dir() {
|
|
|
|
// skip
|
|
|
|
} else {
|
2023-12-12 09:45:45 -05:00
|
|
|
diagnostics
|
|
|
|
.push(format!("Unsupported file type at path {:?}", entry.path()));
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = tar.finish().context("Unable to finish tarball")?;
|
2023-12-12 09:45:45 -05:00
|
|
|
Ok((Bytes::from(v), diagnostics))
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TarGzArchive {
|
|
|
|
builder: tar::Builder<Vec<u8>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TarGzArchive {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
builder: tar::Builder::new(Vec::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_file(
|
|
|
|
&mut self,
|
|
|
|
path: String,
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let mut header = Header::new_gnu();
|
|
|
|
header.set_size(data.len() as u64);
|
|
|
|
self.builder.append_data(&mut header, &path, data)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(mut self) -> Result<Vec<u8>, AnyError> {
|
|
|
|
self.builder.finish()?;
|
|
|
|
let bytes = self.builder.into_inner()?;
|
|
|
|
let mut gz_bytes = Vec::new();
|
|
|
|
let mut encoder = flate2::write::GzEncoder::new(
|
|
|
|
&mut gz_bytes,
|
|
|
|
flate2::Compression::default(),
|
|
|
|
);
|
|
|
|
encoder.write_all(&bytes)?;
|
|
|
|
encoder.finish()?;
|
|
|
|
Ok(gz_bytes)
|
|
|
|
}
|
|
|
|
}
|