From 693969113dcec95d7181200f321fda2398d1c343 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 | 11 +++++-- wayshot/Cargo.toml | 5 --- wayshot/build.rs | 81 ---------------------------------------------- wayshot/docs | 1 - 6 files changed, 9 insertions(+), 91 deletions(-) delete mode 100644 wayshot/build.rs delete mode 120000 wayshot/docs diff --git a/.gitignore b/.gitignore index 2622eacf..f5966131 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ target *.gz -*.out .direnv *.jpg *.jpeg diff --git a/Cargo.lock b/Cargo.lock index 9244fa4d..37e0e0b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -792,7 +792,6 @@ dependencies = [ "clap", "dialoguer", "eyre", - "flate2", "image", "libwayshot", "tracing", diff --git a/Makefile b/Makefile index 0290ceae..7e8944dd 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,14 @@ build: run: @cargo run -install: build +docs: + @echo -n 'Generating docs with scdoc and gzip ... ' + @for file in ./docs/*.scd ; do \ + scdoc < "$$file" | gzip --best > "$${file%.scd}.gz" ; \ + done + @echo 'done!' + +install: build docs @mkdir -p $(TARGET_DIR) @cp $(SOURCE_DIR)/$(BINARY) $(TARGET_DIR) @chmod +x $(TARGET_DIR)/$(BINARY) @@ -37,4 +44,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 4594d0b4..f3b93a62 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -10,11 +10,6 @@ keywords.workspace = true license.workspace = true repository.workspace = true -[build-dependencies] -flate2 = "1.0.27" -eyre = "0.6.8" - - [dependencies] tracing.workspace = true diff --git a/wayshot/build.rs b/wayshot/build.rs deleted file mode 100644 index 37104611..00000000 --- a/wayshot/build.rs +++ /dev/null @@ -1,81 +0,0 @@ -extern crate flate2; -use eyre::{ContextCompat, Result}; -use flate2::{write::GzEncoder, Compression}; -use std::{ - fs::{read_dir, File, OpenOptions}, - io::{copy, BufReader, ErrorKind}, - path::Path, - process::{Command, Stdio}, -}; - -fn main() -> Result<()> { - if let Err(e) = Command::new("scdoc") - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - { - if let ErrorKind::NotFound = e.kind() { - return Ok(()); - } - } - - // 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))?; - _ = Command::new("scdoc") - .stdin(Stdio::from(File::open(man_page.0)?)) - .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)?); - let output = OpenOptions::new() - .write(true) - .create(true) - .open(Path::new(&scdoc_output.1))?; - let mut encoder = GzEncoder::new(output, Compression::default()); - copy(&mut input, &mut encoder)?; - encoder.finish()?; - } - - Ok(()) -} - -fn read_and_replace_by_ext( - path: &str, - search: &str, - replace: &str, -) -> Result> { - let mut files: Vec<(String, String)> = Vec::new(); - for path in read_dir(path)? { - let path = path?; - if path.file_type()?.is_dir() { - continue; - } - - if let Some(file_name) = path.path().to_str() { - if *path - .path() - .extension() - .wrap_err_with(|| format!("no extension found for {}", path.path().display()))? - .to_string_lossy() - != search[1..] - { - continue; - } - - let file = file_name.replace(search, replace); - files.push((file_name.to_string(), file)); - } - } - Ok(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