💥 Cmabia creación de recursos estáticos

This commit is contained in:
Manuel Cillero 2023-06-23 12:21:30 +02:00
parent 7fb30f9ee4
commit f7faf4d345

View file

@ -6,7 +6,7 @@
//! ```bash //! ```bash
//! cd project_dir //! cd project_dir
//! mkdir static //! mkdir static
//! echo "Hello, world" > static/hello //! echo "Hello, world!" > static/hello
//! ``` //! ```
//! //!
//! Add to `Cargo.toml` the required dependencies: //! Add to `Cargo.toml` the required dependencies:
@ -19,17 +19,26 @@
//! Add `build.rs` with call to bundle resources (*guides* will be the magic word in this example): //! Add `build.rs` with call to bundle resources (*guides* will be the magic word in this example):
//! //!
//! ```rust#ignore //! ```rust#ignore
//! use pagetop_build::StaticFilesBundle;
//!
//! fn main() -> std::io::Result<()> { //! fn main() -> std::io::Result<()> {
//! pagetop_build::bundle_resources("./static", "guides", None) //! StaticFilesBundle::from_dir("./static")
//! .with_name("guides")
//! .build()
//! } //! }
//! ``` //! ```
//! //!
//! Optionally, you can pass a function to filter those files into the `./static` folder which //! Optionally, you can pass a function to filter those files into the `./static` folder which
//! should be excluded in the resources file: //! should be excluded in the resources bundle:
//! //!
//! ```rust#ignore //! ```rust#ignore
//! use pagetop_build::StaticFilesBundle;
//!
//! fn main() -> std::io::Result<()> { //! fn main() -> std::io::Result<()> {
//! pagetop_build::bundle_resources("./static", "guides", Some(except_css_dir)) //! StaticFilesBundle::from_dir("./static")
//! .with_name("guides")
//! .with_filter(except_css_dir)
//! .build()
//! } //! }
//! //!
//! fn except_css_dir(p: &Path) -> bool { //! fn except_css_dir(p: &Path) -> bool {
@ -44,34 +53,49 @@
//! [OUT_DIR](https://doc.rust-lang.org/cargo/reference/environment-variables.html) where all //! [OUT_DIR](https://doc.rust-lang.org/cargo/reference/environment-variables.html) where all
//! intermediate and output artifacts are placed during compilation. //! intermediate and output artifacts are placed during compilation.
//! //!
//! You don't need to access this file, just include it in your project source code and a module //! You don't need to access this file, just include it in your project using the builder name as an
//! called `resources_guides` will be added. Then simply reference the `bundle_guides` function to //! identifier:
//! embed the generated HashMap resources collection:
//! //!
//! ```rust#ignore //! ```rust#ignore
//! use pagetop::prelude::*; //! use pagetop::prelude::*;
//! //!
//! include!(concat!(env!("OUT_DIR"), "/guides.rs")); //! use_static!(guides);
//! static RESOURCES: LazyStatic<HashMapResources> = LazyStatic::new(bundle_guides); //! ```
//!
//! Also you can get the bundle as a static reference to the generated HashMap resources collection:
//!
//! ```rust#ignore
//! use pagetop::prelude::*;
//!
//! use_static!(guides => BUNDLE_GUIDES);
//! ``` //! ```
//! //!
//! You can build more than one resources file to compile with your project. //! You can build more than one resources file to compile with your project.
use std::path::Path; use std::path::Path;
pub fn bundle_resources( pub struct StaticFilesBundle(static_files::ResourceDir);
from_dir: &str,
with_name: &str, impl StaticFilesBundle {
filtering: Option<fn(p: &Path) -> bool>, pub fn from_dir(dir: &'static str) -> Self {
) -> std::io::Result<()> { StaticFilesBundle(static_files::resource_dir(dir))
let mut bundle = static_files::resource_dir(from_dir); }
bundle.with_generated_filename(
Path::new(std::env::var("OUT_DIR").unwrap().as_str()).join(format!("{}.rs", with_name)), pub fn with_name(mut self, name: &'static str) -> Self {
); self.0.with_generated_filename(
bundle.with_module_name(format!("resources_{}", with_name)); Path::new(std::env::var("OUT_DIR").unwrap().as_str()).join(format!("{}.rs", name)),
bundle.with_generated_fn(format!("bundle_{}", with_name)); );
if let Some(filter_files) = filtering { self.0.with_module_name(format!("bundle_{}", name));
bundle.with_filter(filter_files); self.0.with_generated_fn(name);
self
}
pub fn with_filter(mut self, filter: fn(p: &Path) -> bool) -> Self {
self.0.with_filter(filter);
self
}
pub fn build(self) -> std::io::Result<()> {
self.0.build()
} }
bundle.build()
} }