Skip to content

Commit 49980ff

Browse files
authored
fix(cli): bundle report templates when installed from crates.io (#877)
The build script zipped ../templates, which is outside the crate root and therefore missing from the published package; cargo install builds then embedded an empty zip, so ~/.fontspector was never populated and --html/--ghmarkdown failed with TemplateNotFound. Move the templates into the crate so they are packaged, write the zip to a growable buffer instead of a fixed 64KB array (which also embedded trailing zero padding), and fail the build if no templates are found. Fixes #722, fixes #119
1 parent 0f612af commit 49980ff

12 files changed

Lines changed: 14 additions & 14 deletions

File tree

fontspector-cli/build.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,37 @@ use zip::{write::SimpleFileOptions, ZipWriter};
1717
fn main() -> Result<(), Box<dyn std::error::Error>> {
1818
ShadowBuilder::builder().build()?;
1919

20-
let walkdir = WalkDir::new("../templates");
20+
let walkdir = WalkDir::new("templates");
2121
let it = walkdir.into_iter();
22-
let mut buf = [0; 65536];
2322
let options = SimpleFileOptions::default();
24-
let mut zip = ZipWriter::new(std::io::Cursor::new(&mut buf[..]));
23+
let mut zip = ZipWriter::new(std::io::Cursor::new(Vec::new()));
2524
let mut buffer = Vec::new();
25+
let mut files_added = 0;
2626
for entry in it.flatten() {
2727
let path = entry.path();
28-
#[allow(clippy::unwrap_used)] // We put .. in there ourselves.
29-
let name = path.strip_prefix("..").unwrap();
30-
let path_as_string = name
28+
let path_as_string = path
3129
.to_str()
3230
.map(str::to_owned)
33-
.unwrap_or_else(|| panic!("{name:?} Is a Non UTF-8 Path"));
31+
.unwrap_or_else(|| panic!("{path:?} Is a Non UTF-8 Path"));
3432
if path.is_file() {
35-
println!("adding file {path:?} as {name:?} ...");
33+
println!("adding file {path:?} ...");
3634
zip.start_file(path_as_string, options)?;
3735
let mut f = File::open(path)?;
3836

3937
f.read_to_end(&mut buffer)?;
4038
zip.write_all(&buffer)?;
4139
buffer.clear();
42-
println!("cargo:rerun-if-changed={path:?}");
43-
} else if !name.as_os_str().is_empty() {
44-
// Only if not root! Avoids path spec / warning
45-
// and mapname conversion failed error on unzip
46-
println!("adding dir {path_as_string:?} as {name:?} ...");
40+
files_added += 1;
41+
println!("cargo:rerun-if-changed={}", path.display());
42+
} else {
43+
println!("adding dir {path_as_string:?} ...");
4744
zip.add_directory(path_as_string, options)?;
4845
}
4946
}
50-
zip.finish()?;
47+
if files_added == 0 {
48+
return Err("No report templates found in the templates directory".into());
49+
}
50+
let buf = zip.finish()?.into_inner();
5151
#[allow(clippy::unwrap_used)] // We're a build script, we expect OUT_DIR to be set.
5252
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("templates.rs");
5353
let mut file = BufWriter::new(File::create(path)?);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)