♻️ Major code restructuring

This commit is contained in:
Manuel Cillero 2024-02-09 14:05:38 +01:00
parent a96e203bb3
commit fa66d628a0
221 changed files with 228 additions and 315 deletions

View file

@ -0,0 +1,17 @@
[package]
name = "pagetop-build"
version = "0.0.7"
edition = "2021"
authors = [
"Manuel Cillero <manuel@cillero.es>"
]
description = """\
Allows including resources in binaries when compiling applications built with PageTop.\
"""
homepage = "https://pagetop.cillero.es"
repository = "https://github.com/manuelcillero/pagetop"
license = "MIT OR Apache-2.0"
[dependencies]
static-files = "0.2.3"

View file

@ -0,0 +1,27 @@
Permite incluir fácilmente recursos en los archivos binarios al compilar aplicaciones creadas con
**PageTop**.
[PageTop](https://github.com/manuelcillero/pagetop/tree/main/pagetop), es un entorno de desarrollo
basado en algunos de los *crates* más estables y populares del ecosistema Rust para proporcionar
APIs, patrones de desarrollo y buenas prácticas para la creación de soluciones web SSR (*Server-Side
Rendering*).
# 🚧 Advertencia
**PageTop** sólo libera actualmente versiones de desarrollo. La API no es estable y los cambios son
constantes. No puede considerarse preparado hasta que se libere la versión **0.1.0**.
# 📜 Licencia
Este proyecto tiene licencia, de hecho tiene dos, puedes aplicar cualquiera de las siguientes a tu
elección:
* Licencia Apache versión 2.0
([LICENSE-APACHE](https://github.com/manuelcillero/pagetop/blob/main/LICENSE-APACHE) o
[http://www.apache.org/licenses/LICENSE-2.0]).
* Licencia MIT
([LICENSE-MIT](https://github.com/manuelcillero/pagetop/blob/main/LICENSE-MIT) o
[http://opensource.org/licenses/MIT]).

View file

@ -0,0 +1,101 @@
//! This function uses the [static_files](https://docs.rs/static-files/latest/static_files/) library
//! to embed at compile time a bundle of static files in your binary.
//!
//! Just create folder with static resources in your project (for example `static`):
//!
//! ```bash
//! cd project_dir
//! mkdir static
//! echo "Hello, world!" > static/hello
//! ```
//!
//! Add to `Cargo.toml` the required dependencies:
//!
//! ```toml
//! [build-dependencies]
//! pagetop-build = { ... }
//! ```
//!
//! Add `build.rs` with call to bundle resources (*guides* will be the magic word in this example):
//!
//! ```rust#ignore
//! use pagetop_build::StaticFilesBundle;
//!
//! fn main() -> std::io::Result<()> {
//! StaticFilesBundle::from_dir("./static")
//! .with_name("guides")
//! .build()
//! }
//! ```
//!
//! Optionally, you can pass a function to filter those files into the `./static` folder which
//! should be excluded in the resources bundle:
//!
//! ```rust#ignore
//! use pagetop_build::StaticFilesBundle;
//!
//! fn main() -> std::io::Result<()> {
//! StaticFilesBundle::from_dir("./static")
//! .with_name("guides")
//! .with_filter(except_css_dir)
//! .build()
//! }
//!
//! fn except_css_dir(p: &Path) -> bool {
//! if let Some(parent) = p.parent() {
//! !matches!(parent.to_str(), Some("/css"))
//! }
//! true
//! }
//! ```
//!
//! This will create a file called `guides.rs` in the standard directory
//! [OUT_DIR](https://doc.rust-lang.org/cargo/reference/environment-variables.html) where all
//! intermediate and output artifacts are placed during compilation.
//!
//! You don't need to access this file, just include it in your project using the builder name as an
//! identifier:
//!
//! ```rust#ignore
//! use pagetop::prelude::*;
//!
//! static_files!(guides);
//! ```
//!
//! Also you can get the bundle as a static reference to the generated HashMap resources collection:
//!
//! ```rust#ignore
//! use pagetop::prelude::*;
//!
//! static_files!(guides => BUNDLE_GUIDES);
//! ```
//!
//! You can build more than one resources file to compile with your project.
use std::path::Path;
pub struct StaticFilesBundle(static_files::ResourceDir);
impl StaticFilesBundle {
pub fn from_dir(dir: &'static str) -> Self {
StaticFilesBundle(static_files::resource_dir(dir))
}
pub fn with_name(mut self, name: &'static str) -> Self {
self.0.with_generated_filename(
Path::new(std::env::var("OUT_DIR").unwrap().as_str()).join(format!("{}.rs", name)),
);
self.0.with_module_name(format!("bundle_{}", name));
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()
}
}