Skip to content

Commit

Permalink
Fix OUT_DIR problem with macro
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonbaeten committed Nov 25, 2024
1 parent 0f9ed0c commit 35d05dd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.

Expand Down
30 changes: 19 additions & 11 deletions memory-serve-core/src/code.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {{
Expand Down
2 changes: 1 addition & 1 deletion memory-serve-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion memory-serve/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");

Expand Down

0 comments on commit 35d05dd

Please sign in to comment.