Skip to content

Commit bf5b0c2

Browse files
authored
[red-knot] Minor refactor of red_knot_vendored/build.rs (#15372)
## Summary See #15370 (comment): - Rename `zip_dir` to `write_zipped_typeshed_to` to clarify it's not a generic function (anymore) - Hard-code `TYPESHED_SOURCE_DIR` instead of using a `directory_path` argument
1 parent 097aa04 commit bf5b0c2

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

crates/red_knot_vendored/build.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ const TYPESHED_SOURCE_DIR: &str = "vendor/typeshed";
1818
const KNOT_EXTENSIONS_STUBS: &str = "knot_extensions/knot_extensions.pyi";
1919
const TYPESHED_ZIP_LOCATION: &str = "/zipped_typeshed.zip";
2020

21-
/// Recursively zip the contents of an entire directory.
21+
/// Recursively zip the contents of the entire typeshed directory and patch typeshed
22+
/// on the fly to include the `knot_extensions` module.
2223
///
2324
/// This routine is adapted from a recipe at
2425
/// <https://github.com/zip-rs/zip-old/blob/5d0f198124946b7be4e5969719a7f29f363118cd/examples/write_dir.rs>
25-
fn zip_dir(directory_path: &str, writer: File) -> ZipResult<File> {
26+
fn write_zipped_typeshed_to(writer: File) -> ZipResult<File> {
2627
let mut zip = ZipWriter::new(writer);
2728

2829
// Use deflated compression for WASM builds because compiling `zstd-sys` requires clang
@@ -44,11 +45,11 @@ fn zip_dir(directory_path: &str, writer: File) -> ZipResult<File> {
4445
.compression_method(method)
4546
.unix_permissions(0o644);
4647

47-
for entry in walkdir::WalkDir::new(directory_path) {
48+
for entry in walkdir::WalkDir::new(TYPESHED_SOURCE_DIR) {
4849
let dir_entry = entry.unwrap();
4950
let absolute_path = dir_entry.path();
5051
let normalized_relative_path = absolute_path
51-
.strip_prefix(Path::new(directory_path))
52+
.strip_prefix(Path::new(TYPESHED_SOURCE_DIR))
5253
.unwrap()
5354
.to_slash()
5455
.expect("Unexpected non-utf8 typeshed path!");
@@ -97,6 +98,6 @@ fn main() {
9798
// which can't be done at compile time.)
9899
let zipped_typeshed_location = format!("{out_dir}{TYPESHED_ZIP_LOCATION}");
99100

100-
let zipped_typeshed = File::create(zipped_typeshed_location).unwrap();
101-
zip_dir(TYPESHED_SOURCE_DIR, zipped_typeshed).unwrap();
101+
let zipped_typeshed_file = File::create(zipped_typeshed_location).unwrap();
102+
write_zipped_typeshed_to(zipped_typeshed_file).unwrap();
102103
}

0 commit comments

Comments
 (0)