Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance utoipa-swagger-ui build #936

Merged
merged 1 commit into from
May 21, 2024
Merged

Conversation

juhaku
Copy link
Owner

@juhaku juhaku commented May 21, 2024

Use curl to download Swagger UI by default and allow using reqwest optionally with reqwest feature flag. Reqwest will be automatically enabled on Windows targets.

Change the re-build parameters as follows.

  • For file protocol the re-build should happen when the upstream file changes
  • For http protocol the re-build should happen when the environment variable holding the downloadd URL will change.

@juhaku
Copy link
Owner Author

juhaku commented May 21, 2024

Follow up for #923

@nightkr @oscar6echo
I am making further enhancements for the build.

@juhaku juhaku force-pushed the chore-swagger-ui-build-enhancements branch 2 times, most recently from 5e72315 to 62e5264 Compare May 21, 2024 17:26
Use `curl` to download Swagger UI by default and allow using `reqwest`
optionally with `reqwest` feature flag. Reqwest will be automatically
enabled on Windows targets.

Change the re-build parameters as follows.
* For file protocol the re-build should happen when the upstream file
  changes
* For http protocol the re-build should happen when the environment
  variable holding the downloadd URL will change.
@juhaku juhaku force-pushed the chore-swagger-ui-build-enhancements branch from 62e5264 to c54e1ab Compare May 21, 2024 17:27
@juhaku juhaku merged commit ffcd202 into master May 21, 2024
14 checks passed
@juhaku juhaku deleted the chore-swagger-ui-build-enhancements branch May 21, 2024 18:09
@oscar6echo
Copy link
Contributor

oscar6echo commented May 21, 2024

Thx for refining it further 👍

Comment on lines +18 to +19
reqwest = ["dep:reqwest"]
url = ["dep:url"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see anything feature-flagged on url, so it looks like it's just always required?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are some places like here.

But in principle at least, url is necessary to parse SWAGGER_UI_DOWNLOAD_URL in all cases except in the case of vendored swagger_ui.zip so maybe the vendored flag is enough ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, would be enough at this point, and would work without explicit url feature since the optional dependencies becomes implicit feature flags anyways. But it does not matter.

Comment on lines +43 to +47
// with file protocol utoipa swagger ui should compile when file changes
println!("cargo:rerun-if-changed={:?}", file_path);

println!("start copy to : {:?}", zip_path);
fs::copy(file_path, zip_path.clone()).unwrap();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point this really just seems equivalent to zip_path = file_path;.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, while this is little bit older commit but in practice yes, the copy seems to be superfluous since it is anyway extracted to the OUTPUT dir. But perhaps we can live with that 😆


let url =
env::var("SWAGGER_UI_DOWNLOAD_URL").unwrap_or(SWAGGER_UI_DOWNLOAD_URL_DEFAULT.to_string());
env::var(SWAGGER_UI_DOWNLOAD_URL).unwrap_or(SWAGGER_UI_DOWNLOAD_URL_DEFAULT.to_string());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url is parsed into url::Url on every branch taken (explicitly for file://, explicitly for curl, and implicitly via IntoUrl for reqwest).

Doing it up here instead would also give access to Url::schema instead of approximating it with str::starts_with.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True that. Maybe something that can be considered in future if it starts to bother someone 😆 Not going to edit it for now though

let zip_filename = url.split('/').last().unwrap().to_string();
let zip_path = [&target_dir, &zip_filename].iter().collect::<PathBuf>();

if !zip_path.exists() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the cache is still desired for HTTP(S) downloads.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes: I understand that this is covered by println!("cargo:rerun-if-env-changed={SWAGGER_UI_DOWNLOAD_URL}"); here, right ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of, rerun-if-env-changed tells cargo about more reasons to rebuild, while the old cache was trying to avoid redownloading when it was rebuilt. (On the other hand, I'm not sure how useful that was anyway given that I'm not sure how Cargo decides on when to recreate out folders.)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear: I'm fine with the changed behaviour, just wanted to double check that it was intended.

Copy link
Owner Author

@juhaku juhaku May 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I removed it just because it appears to be not consistent on creating the folders thus even if there was the cache form previous builds, cargo might still create new set of folders when building thus invalidating the cache in a mean time as well. Perhaps just better of to let it download it when ever cargo should rebuild it. After all that is not going to be build unless one runs cargo clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Released
Development

Successfully merging this pull request may close these issues.

3 participants