Skip to content

Commit

Permalink
Generate embed code instead of using interpolation (#828)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
helaan authored Dec 29, 2023
1 parent c5fca6d commit 3e5a635
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion utoipa-swagger-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
18 changes: 16 additions & 2 deletions utoipa-swagger-ui/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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<const N: usize>(
Expand Down Expand Up @@ -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::<PathBuf>();
fs::write(&path, &contents).unwrap();
}
4 changes: 1 addition & 3 deletions utoipa-swagger-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3e5a635

Please sign in to comment.