Skip to content

Commit

Permalink
[build.rs] Properly gzip the scdoc output
Browse files Browse the repository at this point in the history
Signed-off-by: Shinyzenith <[email protected]>
  • Loading branch information
Shinyzenith committed Sep 7, 2022
1 parent 2a4dbab commit 3b8fdb6
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand All @@ -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)
Expand All @@ -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
}

0 comments on commit 3b8fdb6

Please sign in to comment.