From 35d05ddb1a886ebc79f57e5cdd759ca1b4a7591a Mon Sep 17 00:00:00 2001 From: Marlon Baeten Date: Mon, 25 Nov 2024 10:28:59 +0100 Subject: [PATCH] Fix OUT_DIR problem with macro --- README.md | 8 ++++---- memory-serve-core/src/code.rs | 30 +++++++++++++++++++----------- memory-serve-macros/src/lib.rs | 2 +- memory-serve/build.rs | 2 +- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b2d1f5c..b14b180 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ memory-serve is designed to work with [axum](https://github.com/tokio-rs/axum) There are two mechanisms to include assets at compile time. -1. Specify the path using a enviroment variable `ASSET_PATH` and call: `MemoryServe::from_env()` (best-practice) +1. Specify the path using a enviroment variable `ASSET_DIR` and call: `MemoryServe::from_env()` (best-practice) 2. Call the `load_assets!` macro, and pass this to the constructor: `MemoryServe::new(load_assets!("/foo/bar"))` The environment variable is handled by a build script and instructs cargo to re-evaluate when an asset in the directory changes. @@ -51,8 +51,8 @@ calling [`Router::into_make_service()`](https://docs.rs/axum/latest/axum/routing ### Named directories -Multiple directories can be included using different environment variables, all prefixed by `ASSET_PATH_`. -For example: if you specify `ASSET_PATH_FOO` and `ASSET_PATH_BAR` the memory serve instances can be loaded +Multiple directories can be included using different environment variables, all prefixed by `ASSET_DIR_`. +For example: if you specify `ASSET_DIR_FOO` and `ASSET_DIR_BAR` the memory serve instances can be loaded using `MemoryServe::from_env_name("FOO")` and `MemoryServe::from_env_name("BAR")` respectively. ### Features @@ -61,7 +61,7 @@ Use the `force-embed` feature flag to always include assets in the binary - also ### Environment variables -Use `MEMORY_SERVE_ROOT` to specify a root directory for relative paths provided to the `load_assets!` macro (or th `ASSET_PATH` variable). +Use `MEMORY_SERVE_ROOT` to specify a root directory for relative paths provided to the `load_assets!` macro (or th `ASSET_DIR` variable). Uee `MEMORY_SERVE_QUIET=1` to not print log messages at compile time. diff --git a/memory-serve-core/src/code.rs b/memory-serve-core/src/code.rs index d6b24e7..dfba623 100644 --- a/memory-serve-core/src/code.rs +++ b/memory-serve-core/src/code.rs @@ -1,12 +1,15 @@ -use std::path::{Path, PathBuf}; +use std::path::Path; use crate::{asset::Asset, list::list_assets}; /// Generate code with metadata and contents for the assets -pub fn assets_to_code(asset_dir: &str, path: &Path, embed: bool, log: fn(&str)) -> String { - let out_dir: String = std::env::var("OUT_DIR").expect("OUT_DIR environment variable not set."); - let out_dir = PathBuf::from(&out_dir); - +pub fn assets_to_code( + asset_dir: &str, + path: &Path, + out_dir: Option<&Path>, + embed: bool, + log: fn(&str), +) -> String { log(&format!("Loading static assets from {asset_dir}")); if embed { @@ -29,20 +32,25 @@ pub fn assets_to_code(asset_dir: &str, path: &Path, embed: bool, log: fn(&str)) compressed_bytes, } = asset; + let mut is_compressed = compressed_bytes.is_some(); + let bytes = if !embed { "None".to_string() } else if let Some(compressed_bytes) = &compressed_bytes { - let file_name = path.file_name().expect("Unable to get file name."); - let file_path = Path::new(&out_dir).join(file_name); - std::fs::write(&file_path, compressed_bytes).expect("Unable to write file to out dir."); + if let Some(out_dir) = out_dir { + let file_path = Path::new(&out_dir).join(&etag); + std::fs::write(&file_path, compressed_bytes) + .expect("Unable to write file to out dir."); - format!("Some(include_bytes!(r\"{}\"))", file_path.to_string_lossy()) + format!("Some(include_bytes!(r\"{}\"))", file_path.to_string_lossy()) + } else { + is_compressed = false; + format!("Some(include_bytes!(r\"{}\"))", path.to_string_lossy()) + } } else { format!("Some(include_bytes!(r\"{}\"))", path.to_string_lossy()) }; - let is_compressed = compressed_bytes.is_some(); - code.push_str(&format!( " memory_serve::Asset {{ diff --git a/memory-serve-macros/src/lib.rs b/memory-serve-macros/src/lib.rs index 5b4f291..acd2f66 100644 --- a/memory-serve-macros/src/lib.rs +++ b/memory-serve-macros/src/lib.rs @@ -34,7 +34,7 @@ pub fn load_assets(input: TokenStream) -> TokenStream { let embed = !cfg!(debug_assertions) || cfg!(feature = "force-embed"); - let assets = assets_to_code(asset_dir, &path, embed, log); + let assets = assets_to_code(asset_dir, &path, None, embed, log); assets.parse().expect("Could not parse assets to code") } diff --git a/memory-serve/build.rs b/memory-serve/build.rs index a888fa1..23b015b 100644 --- a/memory-serve/build.rs +++ b/memory-serve/build.rs @@ -62,7 +62,7 @@ fn main() { let name = key.trim_start_matches(format!("{ENV_NAME}_").as_str()); let path = resolve_asset_dir(&out_dir, &key, &asset_dir); - let assets = assets_to_code(&asset_dir, &path, embed, log); + let assets = assets_to_code(&asset_dir, &path, Some(out_dir.as_path()), embed, log); println!("cargo::rerun-if-changed={asset_dir}");