Skip to content

Commit

Permalink
adding xtask for generating a man page
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Feb 18, 2025
1 parent 8b669b5 commit e7dcedf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
name = "xtask"
version = "0.1.0"
edition = "2021"

[dependencies]
man = "0.3.0"
58 changes: 57 additions & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
// https://github.com/matklad/cargo-xtask
use man::prelude::*;
use std::{
env, fs,
path::{Path, PathBuf},
process::exit,
};

const DESCRIPTION: &str = "\
ad is a text editor and command line stream editor. The text editor interface for
ad is inspired by the likes of vim and kakoune, along with the acme and sam editors
from plan9. ad aims to provide an 'integrating development environment' as opposed
to an 'integrated' one: leveraging the surrounding system for the majority of
functionality outisde of the core text editing actions.
";

type DynResult = Result<(), Box<dyn std::error::Error>>;

macro_rules! err {
Expand All @@ -29,6 +38,7 @@ fn try_main() -> DynResult {

match task.as_deref() {
Some("lint-ts-queries") => lint_ts_queries()?,
Some("gen-man-page") => generate_manpage()?,

_ => list_tasks(),
}
Expand All @@ -39,7 +49,8 @@ fn try_main() -> DynResult {
fn list_tasks() {
eprintln!(
"Available tasks:
* lint-ts-queries ensure that the tree-sitter queries in /data are valid"
* lint-ts-queries ensure that the tree-sitter queries in /data are valid
* gen-man-page re-generate the man page"
);
}

Expand Down Expand Up @@ -100,10 +111,55 @@ fn lint_ts_queries() -> DynResult {
Ok(())
}

fn generate_manpage() -> DynResult {
eprintln!(">> Generating man page");
fs::create_dir_all(dist_dir())?;

let content = Manual::new("ad")
.about("An adaptable text editor")
.author(Author::new("Innes Anderson-Morrison"))
.description(DESCRIPTION)
.option(
Opt::new("script")
.short("-e")
.help("Execute an edit script on file(s)"),
)
.option(
Opt::new("script-file")
.short("-f")
.help("Execute an edit script loaded from a script-file on file(s)"),
)
.arg(Arg::new("[file...]"))
.flag(
Flag::new()
.short("-h")
.long("--help")
.help("Print command line help and exit"),
)
.flag(
Flag::new()
.short("-v")
.long("--version")
.help("Print version information and exit"),
)
.render();

let p = dist_dir().join("ad.1");
eprintln!(" [ ] writing manpage to {}", p.display());
fs::write(p, content)?;
eprintln!(" [ ] done");

Ok(())
}

fn project_root() -> PathBuf {
Path::new(&env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(1)
.unwrap()
.to_path_buf()
}

fn dist_dir() -> PathBuf {
project_root().join("target/dist")
}

0 comments on commit e7dcedf

Please sign in to comment.