From 3e5a63511735cedafa6e031ca770fa9cacf814c4 Mon Sep 17 00:00:00 2001 From: Henk van der Laan Date: Fri, 29 Dec 2023 19:39:12 +0100 Subject: [PATCH] Generate embed code instead of using interpolation (#828) Because the output path of the embedded files changes per machine, the interpolate-folder-path feature of rust-embed was used to set the correct embed path. This feature however creates a rather large dependency tree (shell_expand -> dirs -> dirs-sys -> option-ext) By generating a small file with the correct folder already filled in, then including it in lib.rs we can remove this feature and reduce the amount of dependencies that are used. Implementation was inspired by how the "built" crate recommends people to use it. --- utoipa-swagger-ui/Cargo.toml | 2 +- utoipa-swagger-ui/build.rs | 18 ++++++++++++++++-- utoipa-swagger-ui/src/lib.rs | 4 +--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/utoipa-swagger-ui/Cargo.toml b/utoipa-swagger-ui/Cargo.toml index fb451c96..83a5f8ac 100644 --- a/utoipa-swagger-ui/Cargo.toml +++ b/utoipa-swagger-ui/Cargo.toml @@ -15,7 +15,7 @@ debug = [] debug-embed = ["rust-embed/debug-embed"] [dependencies] -rust-embed = { version = "8", features = ["interpolate-folder-path"] } +rust-embed = { version = "8" } mime_guess = { version = "2.0" } actix-web = { version = "4", optional = true, default-features = false } rocket = { version = "0.5.0-rc.3", features = ["json"], optional = true } diff --git a/utoipa-swagger-ui/build.rs b/utoipa-swagger-ui/build.rs index 49e3dcaa..eb65cd81 100644 --- a/utoipa-swagger-ui/build.rs +++ b/utoipa-swagger-ui/build.rs @@ -13,10 +13,8 @@ const SWAGGER_UI_DIST_ZIP: &str = "swagger-ui-5.3.1"; fn main() { println!("cargo:rerun-if-changed=res/{SWAGGER_UI_DIST_ZIP}.zip"); - println!("cargo:rustc-env=UTOIPA_SWAGGER_UI_VERSION={SWAGGER_UI_DIST_ZIP}"); let target_dir = env::var("OUT_DIR").unwrap(); - println!("cargo:rustc-env=UTOIPA_SWAGGER_DIR={}", &target_dir); let swagger_ui_zip = File::open( ["res", &format!("{SWAGGER_UI_DIST_ZIP}.zip")] @@ -29,6 +27,8 @@ fn main() { extract_within_path(&mut zip, [SWAGGER_UI_DIST_ZIP, "dist"], &target_dir).unwrap(); replace_default_url_with_config(&target_dir); + + write_embed_code(&target_dir, &SWAGGER_UI_DIST_ZIP); } fn extract_within_path( @@ -96,3 +96,17 @@ fn replace_default_url_with_config(target_dir: &str) { fs::write(&path, replaced_swagger_initializer.as_ref()).unwrap(); } + +fn write_embed_code(target_dir: &str, swagger_version: &str) { + let contents = format!( + r#" +// This file is auto-generated during compilation, do not modify +#[derive(RustEmbed)] +#[folder = "{}/{}/dist/"] +struct SwaggerUiDist; +"#, + target_dir, swagger_version + ); + let path = [target_dir, "embed.rs"].iter().collect::(); + fs::write(&path, &contents).unwrap(); +} diff --git a/utoipa-swagger-ui/src/lib.rs b/utoipa-swagger-ui/src/lib.rs index 1ad06339..211cf01e 100644 --- a/utoipa-swagger-ui/src/lib.rs +++ b/utoipa-swagger-ui/src/lib.rs @@ -120,9 +120,7 @@ use serde::Serialize; #[cfg(any(feature = "actix-web", feature = "rocket", feature = "axum"))] use utoipa::openapi::OpenApi; -#[derive(RustEmbed)] -#[folder = "$UTOIPA_SWAGGER_DIR/$UTOIPA_SWAGGER_UI_VERSION/dist/"] -struct SwaggerUiDist; +include!(concat!(env!("OUT_DIR"), "/embed.rs")); /// Entry point for serving Swagger UI and api docs in application. It provides /// builder style chainable configuration methods for configuring api doc urls.