From 7ed2d37e52531dbe3cc49e07d86d7be455875e74 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Mon, 23 Jan 2023 20:41:02 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Quita=20c=C3=B3digo=20incluido?= =?UTF-8?q?=20ahora=20en=20[pagetop-build]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/Cargo.toml | 2 +- pagetop/build.rs | 21 +++-------- pagetop/src/util.rs | 85 --------------------------------------------- 3 files changed, 5 insertions(+), 103 deletions(-) diff --git a/pagetop/Cargo.toml b/pagetop/Cargo.toml index fc11be06..ea87b851 100644 --- a/pagetop/Cargo.toml +++ b/pagetop/Cargo.toml @@ -70,7 +70,7 @@ postgres = ["database", "sea-orm/sqlx-postgres"] sqlite = ["database", "sea-orm/sqlx-sqlite"] [build-dependencies] -static-files = "0.2.3" +pagetop-build = { path = "../pagetop-build", version = "0.0" } [dev-dependencies] tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] } diff --git a/pagetop/build.rs b/pagetop/build.rs index 92f494cf..b20b2739 100644 --- a/pagetop/build.rs +++ b/pagetop/build.rs @@ -1,19 +1,6 @@ -use std::path::Path; - fn main() -> std::io::Result<()> { - bundle_resources("./static/theme", "theme")?; - bundle_resources("./static/aliner", "aliner")?; - bundle_resources("./static/bootsier", "bootsier")?; - bundle_resources("./static/bulmix", "bulmix") -} - -/// This function is a simplified version of `pagetop::util::bundle_resources()`. -pub fn bundle_resources(from_dir: &str, with_name: &str) -> std::io::Result<()> { - 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)), - ); - bundle.with_module_name(format!("resources_{}", with_name)); - bundle.with_generated_fn(format!("bundle_{}", with_name)); - bundle.build() + pagetop_build::bundle_resources("./static/theme", "theme", None)?; + pagetop_build::bundle_resources("./static/aliner", "aliner", None)?; + pagetop_build::bundle_resources("./static/bootsier", "bootsier", None)?; + pagetop_build::bundle_resources("./static/bulmix", "bulmix", None) } diff --git a/pagetop/src/util.rs b/pagetop/src/util.rs index f7b78a18..1fe37b80 100644 --- a/pagetop/src/util.rs +++ b/pagetop/src/util.rs @@ -1,92 +1,7 @@ pub use static_files::Resource as StaticResource; -use std::path::Path; - pub type HashMapResources = std::collections::HashMap<&'static str, StaticResource>; -/// 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 -/// [dependencies] -/// pagetop = { ... } -/// static-files = "0.2.3" -/// -/// [build-dependencies] -/// pagetop = { ... } -/// ``` -/// -/// Add `build.rs` with call to bundle resources (*guides* will be the magic word in this example): -/// -/// ```rust#ignore -/// use pagetop::util::bundle_resources; -/// -/// fn main() -> std::io::Result<()> { -/// bundle_resources("./static", "guides", None) -/// } -/// ``` -/// -/// Optionally, you can pass a function to filter those files into the `./static` folder which -/// should be included in the resources file: -/// -/// ```rust#ignore -/// use pagetop::util::bundle_resources; -/// -/// fn main() -> std::io::Result<()> { -/// bundle_resources("./static", "guides", Some(except_css_dir)) -/// } -/// -/// 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 source code and a module called -/// `resources_guides` will be added. Then simply reference the `bundle_guides` function to embed -/// the generated HashMap resources collection: -/// -/// ```rust#ignore -/// use pagetop::prelude::*; -/// -/// include!(concat!(env!("OUT_DIR"), "/guides.rs")); -/// static RESOURCES: LazyStatic = LazyStatic::new(bundle_guides); -/// ``` -/// -/// You can build more than one resources file to compile with your project. -pub fn bundle_resources( - from_dir: &str, - with_name: &str, - filtering: Option bool>, -) -> std::io::Result<()> { - 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)), - ); - bundle.with_module_name(format!("resources_{}", with_name)); - bundle.with_generated_fn(format!("bundle_{}", with_name)); - if let Some(filter_files) = filtering { - bundle.with_filter(filter_files); - } - bundle.build() -} - pub type Handle = u64; // https://stackoverflow.com/a/71464396