From 596f25b54d765b13282556a8d68b0f685ae94a39 Mon Sep 17 00:00:00 2001 From: "Sergey A." Date: Wed, 13 Mar 2024 01:13:14 +0300 Subject: [PATCH] refactor(packaging): simplify building docs Fixes #101 Why? - no build.rs that is invoked on _any_ `cargo build/run` - no extra dev dependency - no intermediate `docs/*.out` files - 3 lines of bash instead - maximum gzip compression (-9/--best) - docs will be built only when needed, i.e. on `make docs` or `make install` To quickly check: ```sh make TARGET_DIR=/tmp/wayshot MAN{1,7}_DIR=/tmp/wayshot install ``` --- .gitignore | 1 - Cargo.lock | 1 - Makefile | 8 +++++- wayshot/Cargo.toml | 3 -- wayshot/build.rs | 70 ---------------------------------------------- wayshot/docs | 1 - 6 files changed, 7 insertions(+), 77 deletions(-) delete mode 100644 wayshot/build.rs delete mode 120000 wayshot/docs diff --git a/.gitignore b/.gitignore index 5a4f6354..00c54e64 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ target *.gz -*.out .direnv diff --git a/Cargo.lock b/Cargo.lock index 2d5bff5d..b1d0678c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -756,7 +756,6 @@ version = "1.3.2-dev" dependencies = [ "clap", "dialoguer", - "flate2", "image", "libwayshot", "tracing", diff --git a/Makefile b/Makefile index 0290ceae..86bb9e84 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,13 @@ build: run: @cargo run +docs: install: build + @for scd in ./docs/*.scd; do \ + scdoc < "$$scd" | gzip --best > "$${scd%.scd}.gz" & \ + done + +install: build docs @mkdir -p $(TARGET_DIR) @cp $(SOURCE_DIR)/$(BINARY) $(TARGET_DIR) @chmod +x $(TARGET_DIR)/$(BINARY) @@ -37,4 +43,4 @@ setup: @rustup install stable @rustup default stable -.PHONY: check clean setup all install build +.PHONY: check clean setup all install build docs diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index 8d4aa528..aa38c42c 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -10,9 +10,6 @@ keywords.workspace = true license.workspace = true repository.workspace = true -[build-dependencies] -flate2 = "1.0.27" - [dependencies] tracing.workspace = true diff --git a/wayshot/build.rs b/wayshot/build.rs deleted file mode 100644 index 14152acb..00000000 --- a/wayshot/build.rs +++ /dev/null @@ -1,70 +0,0 @@ -extern crate flate2; -use flate2::{write::GzEncoder, Compression}; -use std::{ - fs::{read_dir, File, OpenOptions}, - io::{copy, BufReader, ErrorKind}, - path::Path, - process::{exit, Command, Stdio}, -}; - -fn main() { - if let Err(e) = Command::new("scdoc") - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - { - if let ErrorKind::NotFound = e.kind() { - exit(0); - } - } - - // 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() != search[1..] { - continue; - } - - let file = file_name.replace(search, replace); - files.push((file_name.to_string(), file)); - } - } - files -} diff --git a/wayshot/docs b/wayshot/docs deleted file mode 120000 index a9594bfe..00000000 --- a/wayshot/docs +++ /dev/null @@ -1 +0,0 @@ -../docs \ No newline at end of file