Skip to content

Commit

Permalink
[docs] Properly gzip the man pages!
Browse files Browse the repository at this point in the history
Signed-off-by: Shinyzenith <[email protected]>
  • Loading branch information
Shinyzenith committed Oct 6, 2022
1 parent 36281fe commit fdf5e10
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
*.zip
*.gz
*.out
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -30,19 +30,19 @@ 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
@cargo 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

Expand All @@ -51,7 +51,7 @@ test:

clean:
@cargo clean
@rm ./docs/*.gz
@$(RM) -f ./docs/*.gz

setup:
@rustup install stable
Expand Down
3 changes: 3 additions & 0 deletions swhkd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ authors = [
"EdenQwQ <[email protected]>\n"
]

[build-dependencies]
flate2 = "1.0.24"

[dependencies]
clap = "3.1.6"
env_logger = "0.9.0"
Expand Down
49 changes: 34 additions & 15 deletions swhkd/build.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand All @@ -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
}

0 comments on commit fdf5e10

Please sign in to comment.