~cdv/rpls

506338f8a4b5354b80c8feb9393428663863908b — Christopher Vittal 6 months ago ca2a360 master
Build script to generate documentation
4 files changed, 101 insertions(+), 0 deletions(-)

M Cargo.lock
M Cargo.toml
A build.rs
A doc/ls.1.scd
M Cargo.lock => Cargo.lock +10 -0
@@ 1,6 1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "rpls"
version = "0.1.2"
dependencies = [
 "glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]

[metadata]
"checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"

M Cargo.toml => Cargo.toml +4 -0
@@ 6,7 6,11 @@ edition = "2018"
description = "A (mostly) POSIX implementation of ls, that also respects LS_COLORS"
license = "GPL-3.0-only"
repository = "https://git.sr.ht/~cdv/rpls"
exclude = ["doc/**/*.scd"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[build-dependencies]
glob = "0.3"

A build.rs => build.rs +64 -0
@@ 0,0 1,64 @@
use std::{
    env, fs, io,
    path::Path,
    process::{self, Command, Stdio},
    thread,
};

fn main() {
    let outdir = match env::var_os("OUT_DIR") {
        Some(outdir) => outdir,
        None => {
            eprintln!(
                "OUT_DIR environment variable not defined. \
                 This a cargo bug, you may file the bug upstream at: \
                 https://github.com/rust-lang/cargo/issues/new"
            );
            process::exit(1);
        }
    };
    let docdir = Path::new(&outdir).join("doc");
    fs::create_dir_all(&docdir).unwrap();

    if let Err(e) = generate_man(&docdir) {
        eprintln!("could not generate manpages: {}", e);
        process::exit(1);
    }
}

fn generate_man<P: AsRef<Path>>(outdir: P) -> io::Result<()> {
    for path in glob::glob("doc/*.scd").unwrap() {
        let path = path.unwrap();
        let outp = outdir
            .as_ref()
            .join(path.file_stem().expect("should have a file name"));
        let mut inp = fs::File::open(&path)?;
        let mut out = fs::File::create(&outp)?;

        let mut scdoc = Command::new("scdoc")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()?;

        let mut scdoc_out = scdoc.stdout.take().unwrap();

        let wtr: thread::JoinHandle<io::Result<_>> =
            thread::spawn(move || io::copy(&mut scdoc_out, &mut out));

        io::copy(&mut inp, scdoc.stdin.as_mut().unwrap())?;

        scdoc.wait().and_then(|e| {
            if e.success() {
                Ok(())
            } else {
                Err(io::Error::new(
                    io::ErrorKind::Other,
                    format!("could not write documentation for file: {:?}", outp),
                ))
            }
        })?;
        wtr.join().unwrap()?;
    }

    Ok(())
}

A doc/ls.1.scd => doc/ls.1.scd +23 -0
@@ 0,0 1,23 @@
ls(1) "rpls"

# NAME

ls - list contents of directories

# SYNOPSIS

*ls* [-ikqrs] [-glno] [-A|-a] [-C|-m|-x|-1] \
[-F|-p] [-H|-L] [-R|-d] [-S|-f|-t] [-c|-u] [_file_...]

# AUTHORS

Originally written and maintained by Chris Vittal
<christopher.vittal@gmail.com>. The most current sources for this tool can be
found at https://git.sr.ht/~cdv/rpls. Bugs and patches may be submitted by
emailing the author.

# LICENSE

This program is licensed under the terms of the GNU General Public License v3.0
only <https://gnu.org/licenses/gpl-3.0.txt>.++
SPDX-License-Identifier: GPL-3.0-only