Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracing, asset caching and fix binary package assets #12

Merged
merged 7 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions cli-support/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub fn process_file(file: &FileAsset, output_folder: &Path) -> anyhow::Result<()

impl Process for FileOptions {
fn process(&self, input_location: &FileLocation, output_folder: &Path) -> anyhow::Result<()> {
if output_folder.join(input_location.unique_name()).exists() {
return Ok(());
}
match self {
Self::Other { .. } => {
let mut output_location = output_folder.to_path_buf();
Expand Down
43 changes: 32 additions & 11 deletions cli-support/src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
pub use railwind::warning::Warning as TailwindWarning;
use rustc_hash::FxHashSet;
use std::path::{Path, PathBuf};
use std::{
fmt::Write,
path::{Path, PathBuf},
};

use cargo_lock::{
dependency::{self, graph::NodeIndex},
Lockfile,
};
use manganis_common::{
cache::asset_cache_dir, cache::push_package_cache_dir, AssetManifest, AssetType, PackageAssets,
cache::{asset_cache_dir, package_identifier, push_package_identifier},
AssetManifest, AssetType, PackageAssets,
};
use petgraph::visit::EdgeRef;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
Expand Down Expand Up @@ -76,8 +80,11 @@ impl AssetManifestExt for AssetManifest {
}
}
self.packages().par_iter().try_for_each(|package| {
tracing::trace!("Copying static assets for package {}", package.package());
package.assets().par_iter().try_for_each(|asset| {
if let AssetType::File(file_asset) = asset {
tracing::info!("Optimizing and bundling {}", file_asset);
tracing::trace!("Copying asset from {:?} to {:?}", file_asset, location);
match process_file(file_asset, &location) {
Ok(_) => {}
Err(err) => {
Expand Down Expand Up @@ -127,35 +134,49 @@ fn collect_dependencies(
) {
// First find any assets that do have assets. The vast majority of packages will not have any so we can rule them out quickly with a hashset before touching the filesystem
let mut packages = FxHashSet::default();
if let Ok(read_dir) = cache_dir.read_dir() {
for path in read_dir.flatten() {
if path.file_type().unwrap().is_dir() {
let file_name = path.file_name();
let package_name = file_name.to_string_lossy();
if let Some((package_name, _)) = package_name.rsplit_once('-') {
match cache_dir.read_dir() {
Ok(read_dir) => {
for path in read_dir.flatten() {
if path.file_type().unwrap().is_dir() {
let file_name = path.file_name();
let package_name = file_name.to_string_lossy();
packages.insert(package_name.to_string());
}
}
}
Err(err) => {
tracing::error!("Failed to read asset cache directory: {}", err);
}
}
tracing::trace!(
"Found packages with assets: {:?}",
packages.iter().cloned().collect::<Vec<_>>().join(", ")
);

let mut packages_to_visit = vec![root_package_id];
let mut dependency_path = PathBuf::new();
while let Some(package_id) = packages_to_visit.pop() {
let package = tree.graph().node_weight(package_id).unwrap();
// First make sure this package has assets
if !packages.contains(package.name.as_str()) {
let identifier = package_identifier(
package.name.as_str(),
bin.filter(|_| package_id == root_package_id),
&package.version,
);
if !packages.contains(&identifier) {
continue;
}

// Add the assets for this dependency
dependency_path.clear();
dependency_path.push(cache_dir);
push_package_cache_dir(
let os_string = dependency_path.as_mut_os_string();
os_string.write_char(std::path::MAIN_SEPARATOR).unwrap();
push_package_identifier(
package.name.as_str(),
bin.filter(|_| package_id == root_package_id),
&package.version,
&mut dependency_path,
os_string,
);
tracing::trace!("Looking for assets in {}", dependency_path.display());
dependency_path.push("assets.toml");
Expand Down
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ infer = "0.11.0"
# Remote assets
url = { version = "2.4.0", features = ["serde"] }
reqwest = { version = "0.11.18", features = ["blocking"] }
tracing = "0.1.40"

[features]
html = []
32 changes: 32 additions & 0 deletions common/src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
fmt::Display,
hash::{Hash, Hasher},
path::{Path, PathBuf},
str::FromStr,
Expand Down Expand Up @@ -31,6 +32,20 @@ pub enum FileSource {
Remote(Url),
}

impl Display for FileSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let as_string = match self {
Self::Local(path) => path.display().to_string(),
Self::Remote(url) => url.as_str().to_string(),
};
if as_string.len() > 25 {
write!(f, "{}...", &as_string[..25])
} else {
write!(f, "{}", as_string)
}
}
}

impl FileSource {
/// Returns the last segment of the file source used to generate a unique name
pub fn last_segment(&self) -> &str {
Expand Down Expand Up @@ -268,6 +283,23 @@ pub struct FileAsset {
url_encoded: bool,
}

impl Display for FileAsset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let url_encoded = if self.url_encoded {
" [url encoded]"
} else {
""
};
write!(
f,
"{} [{}]{}",
self.location.source(),
self.options,
url_encoded
)
}
}

impl FileAsset {
/// Creates a new file asset
pub fn new(source: FileSource) -> Self {
Expand Down
47 changes: 27 additions & 20 deletions common/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,31 @@ pub(crate) fn current_package_identifier() -> String {
package_identifier(
&std::env::var("CARGO_PKG_NAME").unwrap(),
std::env::var("CARGO_BIN_NAME").ok().as_deref(),
&current_package_version(),
current_package_version(),
)
}

/// The identifier for a package used to cache assets
pub fn package_identifier(package: &str, bin: Option<&str>, version: &str) -> String {
let mut string = package.to_string();
if let Some(bin) = bin {
string.push('-');
string.push_str(bin);
}
string.push('-');
string.push_str(version);
string
pub fn package_identifier(package: &str, bin: Option<&str>, version: impl Display) -> String {
let mut id = String::new();
push_package_identifier(package, bin, version, &mut id);
id
}

/// Like `package_identifier`, but appends the identifier to the given path
pub fn push_package_cache_dir(
/// Like `package_identifier`, but appends the identifier to the given writer
pub fn push_package_identifier(
package: &str,
bin: Option<&str>,
version: impl Display,
dir: &mut PathBuf,
to: &mut impl Write,
) {
let as_string = dir.as_mut_os_string();
as_string.write_char(std::path::MAIN_SEPARATOR).unwrap();
as_string.write_str(package).unwrap();
to.write_str(package).unwrap();
if let Some(bin) = bin {
as_string.write_char('-').unwrap();
as_string.write_str(bin).unwrap();
to.write_char('-').unwrap();
to.write_str(bin).unwrap();
}
as_string.write_char('-').unwrap();
as_string.write_fmt(format_args!("{}", version)).unwrap();
to.write_char('-').unwrap();
to.write_fmt(format_args!("{}", version)).unwrap();
}

pub(crate) fn current_package_version() -> String {
Expand All @@ -69,3 +62,17 @@ pub(crate) fn current_package_cache_dir() -> PathBuf {
dir.push(current_package_identifier());
dir
}

/// The location where logs are stored while expanding macros
pub fn macro_log_directory() -> PathBuf {
let mut dir = asset_cache_dir();
dir.push("logs");
dir
}

/// The current log file for the macro expansion
pub fn macro_log_file() -> PathBuf {
let mut dir = macro_log_directory();
dir.push(current_package_identifier());
dir
}
Loading
Loading