From 3b8fdb6687f7abec255e74cd5628c05715acc955 Mon Sep 17 00:00:00 2001 From: Shinyzenith Date: Wed, 7 Sep 2022 19:21:35 +0530 Subject: [PATCH] [build.rs] Properly gzip the scdoc output Signed-off-by: Shinyzenith --- build.rs | 59 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/build.rs b/build.rs index eddddec0..63121a72 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,9 @@ +extern crate flate2; + +use flate2::{write::GzEncoder, Compression}; use std::{ fs::{read_dir, File, OpenOptions}, - io::ErrorKind, + io::{copy, BufReader, ErrorKind}, path::Path, process::{exit, Command, Stdio}, }; @@ -17,23 +20,8 @@ fn main() { } } - let mut man_pages: Vec<(String, String)> = Vec::new(); - for path in read_dir("./docs").unwrap() { - let path = path.unwrap(); - if path.file_type().unwrap().is_dir() { - continue; - } - - if let Some(file_name) = path.path().to_str() { - if path.path().extension().unwrap().to_str().unwrap() == "gz" { - continue; - } - - let man_page_name = file_name.replace(".scd", ".gz"); - man_pages.push((file_name.to_string(), man_page_name)); - } - } - + // We just append "out" so it's easy to find all the scdoc output later in line 38. + let man_pages: Vec<(String, String)> = read_and_replace_by_ext("./docs", ".scd", ".out"); for man_page in man_pages { let output = OpenOptions::new() .write(true) @@ -45,4 +33,39 @@ fn main() { .stdout(output) .spawn(); } + + // Gzipping the man pages + let scdoc_output_files: Vec<(String, String)> = + read_and_replace_by_ext("./docs", ".out", ".gz"); + for scdoc_output in scdoc_output_files { + let mut input = BufReader::new(File::open(scdoc_output.0).unwrap()); + let output = OpenOptions::new() + .write(true) + .create(true) + .open(Path::new(&scdoc_output.1)) + .unwrap(); + let mut encoder = GzEncoder::new(output, Compression::default()); + copy(&mut input, &mut encoder).unwrap(); + encoder.finish().unwrap(); + } +} + +fn read_and_replace_by_ext(path: &str, search: &str, replace: &str) -> Vec<(String, String)> { + let mut files: Vec<(String, String)> = Vec::new(); + for path in read_dir(path).unwrap() { + let path = path.unwrap(); + if path.file_type().unwrap().is_dir() { + continue; + } + + if let Some(file_name) = path.path().to_str() { + if *path.path().extension().unwrap().to_str().unwrap() != search[1..] { + continue; + } + + let file = file_name.replace(search, replace); + files.push((file_name.to_string(), file)); + } + } + files }