From fdf5e10ae42b58d2b45b10016ad63ebec5e16dd0 Mon Sep 17 00:00:00 2001 From: Shinyzenith Date: Thu, 6 Oct 2022 15:05:16 +0530 Subject: [PATCH] [docs] Properly gzip the man pages! Signed-off-by: Shinyzenith --- .gitignore | 1 + Cargo.lock | 35 ++++++++++++++++++++++++++++++++++ Makefile | 16 ++++++++-------- swhkd/Cargo.toml | 3 +++ swhkd/build.rs | 49 +++++++++++++++++++++++++++++++++--------------- 5 files changed, 81 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 2457b64..6300bad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target *.zip *.gz +*.out diff --git a/Cargo.lock b/Cargo.lock index ea439c5..ecb296c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,7 @@ dependencies = [ "clap", "env_logger", "evdev", + "flate2", "itertools", "log", "nix", @@ -19,6 +20,12 @@ dependencies = [ "tokio-stream", ] +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aho-corasick" version = "0.7.18" @@ -111,6 +118,15 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossbeam-channel" version = "0.5.6" @@ -190,6 +206,16 @@ dependencies = [ "tokio", ] +[[package]] +name = "flate2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "funty" version = "2.0.0" @@ -282,6 +308,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34" +dependencies = [ + "adler", +] + [[package]] name = "mio" version = "0.8.4" diff --git a/Makefile b/Makefile index ead90e8..eeeb02a 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ install: @mkdir -p $(TARGET_DIR) @mkdir -p /etc/$(DAEMON_BINARY) @find ./docs -type f -iname "*.1.gz" -exec cp {} $(MAN1_DIR) \; - @find ./docs -type f -iname "*.7.gz" -exec cp {} $(MAN7_DIR) \; + @find ./docs -type f -iname "*.5.gz" -exec cp {} $(MAN5_DIR) \; @touch /etc/$(DAEMON_BINARY)/$(DAEMON_BINARY)rc @cp ./target/release/$(DAEMON_BINARY) $(TARGET_DIR) @cp ./target/release/$(SERVER_BINARY) $(TARGET_DIR) @@ -30,11 +30,11 @@ install: @chmod +x $(TARGET_DIR)/$(SERVER_BINARY) uninstall: - @rm -f /usr/share/man/**/swhkd.* - @rm -f /usr/share/man/**/swhks.* - @rm $(TARGET_DIR)/$(SERVER_BINARY) - @rm $(TARGET_DIR)/$(DAEMON_BINARY) - @rm $(POLKIT_DIR)/$(POLKIT_POLICY_FILE) + @$(RM) -f /usr/share/man/**/swhkd.* + @$(RM) -f /usr/share/man/**/swhks.* + @$(RM) $(TARGET_DIR)/$(SERVER_BINARY) + @$(RM) $(TARGET_DIR)/$(DAEMON_BINARY) + @$(RM) $(POLKIT_DIR)/$(POLKIT_POLICY_FILE) check: @cargo fmt @@ -42,7 +42,7 @@ check: @cargo clippy release: - @rm -f Cargo.lock + @$(RM) -f Cargo.lock @$(MAKE) -s @zip -r "glibc-x86_64-$(VERSION).zip" ./target/release/swhkd ./target/release/swhks @@ -51,7 +51,7 @@ test: clean: @cargo clean - @rm ./docs/*.gz + @$(RM) -f ./docs/*.gz setup: @rustup install stable diff --git a/swhkd/Cargo.toml b/swhkd/Cargo.toml index f0e9f30..383b65b 100644 --- a/swhkd/Cargo.toml +++ b/swhkd/Cargo.toml @@ -9,6 +9,9 @@ authors = [ "EdenQwQ \n" ] +[build-dependencies] +flate2 = "1.0.24" + [dependencies] clap = "3.1.6" env_logger = "0.9.0" diff --git a/swhkd/build.rs b/swhkd/build.rs index 394a084..0ea10f4 100644 --- a/swhkd/build.rs +++ b/swhkd/build.rs @@ -1,6 +1,8 @@ +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,29 +19,46 @@ fn main() { } } - let mut man_pages: Vec<(String, String)> = Vec::new(); - for path in read_dir("../docs").unwrap() { + // 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).create(true).open(Path::new(&man_page.1)).unwrap(); + _ = Command::new("scdoc") + .stdin(Stdio::from(File::open(man_page.0).unwrap())) + .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() == "gz" { + if *path.path().extension().unwrap().to_str().unwrap() != search[1..] { continue; } - let man_page_name = file_name.replace(".scd", ".gz"); - man_pages.push((file_name.to_string(), man_page_name)); + let file = file_name.replace(search, replace); + files.push((file_name.to_string(), file)); } } - - for man_page in man_pages { - let output = - OpenOptions::new().write(true).create(true).open(Path::new(&man_page.1)).unwrap(); - _ = Command::new("scdoc") - .stdin(Stdio::from(File::open(man_page.0).unwrap())) - .stdout(output) - .spawn(); - } + files }