MeiliSearch/meilisearch/build.rs

91 lines
3.1 KiB
Rust
Raw Normal View History

use vergen::{vergen, Config, SemverKind};
2020-12-12 13:32:06 +01:00
fn main() {
// Note: any code that needs VERGEN_ environment variables should take care to define them manually in the Dockerfile and pass them
// in the corresponding GitHub workflow (publish_docker.yml).
// This is due to the Dockerfile building the binary outside of the git directory.
let mut config = Config::default();
// allow using non-annotated tags
*config.git_mut().semver_kind_mut() = SemverKind::Lightweight;
if let Err(e) = vergen(config) {
2021-08-30 17:41:24 +02:00
println!("cargo:warning=vergen: {}", e);
}
2021-04-20 13:28:12 +02:00
2021-04-22 12:39:23 +02:00
#[cfg(feature = "mini-dashboard")]
mini_dashboard::setup_mini_dashboard().expect("Could not load the mini-dashboard assets");
2020-12-12 13:32:06 +01:00
}
2021-04-20 15:20:09 +02:00
2021-04-22 12:39:23 +02:00
#[cfg(feature = "mini-dashboard")]
mod mini_dashboard {
use std::env;
use std::fs::{create_dir_all, File, OpenOptions};
use std::io::{Cursor, Read, Write};
use std::path::PathBuf;
use anyhow::Context;
use cargo_toml::Manifest;
use reqwest::blocking::get;
use sha1::{Digest, Sha1};
use static_files::resource_dir;
2021-04-22 12:39:23 +02:00
pub fn setup_mini_dashboard() -> anyhow::Result<()> {
let cargo_manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let cargo_toml = cargo_manifest_dir.join("Cargo.toml");
2021-04-27 09:32:17 +02:00
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
2021-04-22 12:39:23 +02:00
2021-04-27 09:32:17 +02:00
let sha1_path = out_dir.join(".mini-dashboard.sha1");
let dashboard_dir = out_dir.join("mini-dashboard");
2021-04-20 15:20:09 +02:00
2021-04-22 12:39:23 +02:00
let manifest = Manifest::from_path(cargo_toml).unwrap();
2021-04-20 15:20:09 +02:00
2021-04-22 12:39:23 +02:00
let meta = &manifest
.package
.as_ref()
.context("package not specified in Cargo.toml")?
.metadata
.as_ref()
.context("no metadata specified in Cargo.toml")?["mini-dashboard"];
2021-04-20 15:20:09 +02:00
2021-04-22 12:39:23 +02:00
// Check if there already is a dashboard built, and if it is up to date.
if sha1_path.exists() && dashboard_dir.exists() {
let mut sha1_file = File::open(&sha1_path)?;
let mut sha1 = String::new();
sha1_file.read_to_string(&mut sha1)?;
if sha1 == meta["sha1"].as_str().unwrap() {
// Nothing to do.
2021-05-31 16:03:39 +02:00
return Ok(());
2021-04-22 12:39:23 +02:00
}
}
2021-04-20 15:20:09 +02:00
2021-04-22 12:39:23 +02:00
let url = meta["assets-url"].as_str().unwrap();
2021-04-20 15:20:09 +02:00
2021-04-22 12:39:23 +02:00
let dashboard_assets_bytes = get(url)?.bytes()?;
2021-04-20 15:20:09 +02:00
2021-04-22 12:39:23 +02:00
let mut hasher = Sha1::new();
hasher.update(&dashboard_assets_bytes);
let sha1 = hex::encode(hasher.finalize());
2021-04-20 15:20:09 +02:00
2021-05-31 16:03:39 +02:00
assert_eq!(
meta["sha1"].as_str().unwrap(),
sha1,
"Downloaded mini-dashboard shasum differs from the one specified in the Cargo.toml"
);
2021-04-22 12:39:23 +02:00
create_dir_all(&dashboard_dir)?;
let cursor = Cursor::new(&dashboard_assets_bytes);
let mut zip = zip::read::ZipArchive::new(cursor)?;
zip.extract(&dashboard_dir)?;
resource_dir(&dashboard_dir).build()?;
// Write the sha1 for the dashboard back to file.
2022-10-20 18:00:07 +02:00
let mut file =
OpenOptions::new().write(true).create(true).truncate(true).open(sha1_path)?;
2021-04-22 12:39:23 +02:00
file.write_all(sha1.as_bytes())?;
file.flush()?;
Ok(())
}
2021-04-20 15:20:09 +02:00
}