Skip to content

Commit 1d39a04

Browse files
build: Gzip ardupilot parameters
1 parent fe14c25 commit 1d39a04

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

build.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
2-
env,
3-
path::Path,
2+
env, fs,
3+
path::{Path, PathBuf},
44
process::{Command, exit},
55
};
66

@@ -73,6 +73,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
7373
let p = Path::new(&out_dir).join(format!("src/webpage/{}", path));
7474
println!("cargo:rerun-if-changed={}", p.display());
7575
}
76+
let params_src =
77+
Path::new(&out_dir).join("src/lib/drivers/rest/autopilot/parameters/ardupilot_parameters");
78+
println!("cargo:rerun-if-changed={}", params_src.display());
7679

7780
vergen_gix::Emitter::default()
7881
.add_instructions(&BuildBuilder::all_build()?)?
@@ -82,6 +85,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8285
)?
8386
.emit()?;
8487

88+
gzip_ardupilot_parameters(&out_dir)?;
89+
8590
let dist_dir = Path::new(&out_dir).join("src/webpage/dist");
8691
if std::env::var("SKIP_FRONTEND").is_ok() {
8792
fs::create_dir_all(&dist_dir)?;
@@ -167,5 +172,36 @@ fn collect_files(dir: &Path) -> Result<Vec<PathBuf>, Box<dyn std::error::Error>>
167172
Ok(files)
168173
}
169174

175+
/// Produce gzipped parameter JSONs in ardupilot_parameters_gz
176+
fn gzip_ardupilot_parameters(out_dir: &str) -> Result<(), Box<dyn std::error::Error>> {
177+
let src =
178+
Path::new(out_dir).join("src/lib/drivers/rest/autopilot/parameters/ardupilot_parameters");
179+
let dst = Path::new(out_dir).join("ardupilot_parameters_gz");
180+
if !src.is_dir() {
181+
fs::create_dir_all(&dst)?;
182+
return Ok(());
183+
}
184+
fs::create_dir_all(&dst)?;
185+
for entry in fs::read_dir(&src)? {
186+
let entry = entry?;
187+
let path = entry.path();
188+
if !path.is_dir() {
189+
continue;
190+
}
191+
let name = path.file_name().unwrap();
192+
let json_path = path.join("apm.pdef.json");
193+
if !json_path.is_file() {
194+
continue;
195+
}
196+
let out_sub = dst.join(name);
197+
fs::create_dir_all(&out_sub)?;
198+
let gz_path = out_sub.join("apm.pdef.json.gz");
199+
let output = Command::new("gzip").arg("-c").arg(&json_path).output()?;
200+
if !output.status.success() {
201+
eprintln!("cargo:warning=gzip failed for {json_path:?}");
202+
continue;
203+
}
204+
fs::write(&gz_path, output.stdout)?;
205+
}
170206
Ok(())
171207
}

0 commit comments

Comments
 (0)