Skip to content

Commit

Permalink
Generate man pages
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed May 22, 2024
1 parent 38f2d6c commit 8adc8ab
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ backoff = { version = "0.4.0", default-features = false }
camino = "1.1.4"
# Clap 4.4 is the last version supporting Rust 1.72.
clap = { version = "~4.4", features = ["derive", "wrap_help", "env", "string"] }
clap_mangen = { version = "=0.2.19", optional = true }
clearscreen = "2.0.1"
command-group = { version = "2.1.0", features = ["tokio", "with-tokio"] }
crossterm = { version = "0.27.0", features = ["event-stream"] }
Expand Down
42 changes: 41 additions & 1 deletion nix/packages/ghciwatch.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
stdenv,
libiconv,
darwin,
buildPackages,
haskell,
haskellPackages,
ghc,
Expand All @@ -12,6 +13,7 @@
rustPlatform,
rust-analyzer,
mdbook,
installShellFiles,
# Versions of GHC to include in the environment for integration tests.
# These should be attributes of `haskell.compiler`.
ghcVersions ? null,
Expand Down Expand Up @@ -103,6 +105,9 @@
inherit cargoArtifacts;
};

can-run-ghciwatch = stdenv.hostPlatform.emulatorAvailable buildPackages;
run-ghciwatch = "${stdenv.hostPlatform.emulator buildPackages} $out/bin/ghciwatch";

releaseArgs =
commonArgs
// {
Expand All @@ -119,8 +124,31 @@
};
};

ghciwatch-man = craneLib.buildPackage (releaseArgs
// {
pnameSuffix = "-man";

cargoExtraArgs = "--locked --features clap_mangen";

nativeBuildInputs = (releaseArgs.nativeBuildInputs or []) ++ [installShellFiles];

postInstall =
(releaseArgs.postInstall or "")
+ lib.optionalString can-run-ghciwatch ''
manpages=$(mktemp -d)
${run-ghciwatch} --generate-man-pages "$manpages"
for manpage in "$manpages"/*; do
installManPage "$manpage"
done
rm -rf "$out/bin"
'';
});

ghciwatch-with-clap-markdown = craneLib.buildPackage (releaseArgs
// {
pnameSuffix = "-cli-markdown";

cargoExtraArgs = "--locked --features clap-markdown";
});

Expand Down Expand Up @@ -245,4 +273,16 @@
];
};
in
craneLib.buildPackage releaseArgs
craneLib.buildPackage (releaseArgs
// {
postInstall =
(releaseArgs.postInstall or "")
+ ''
cp -r ${ghciwatch-man}/share $out/share
# For some reason this is needed to strip references:
# stripping references to cargoVendorDir from share/man/man1/ghciwatch.1.gz
# sed: couldn't open temporary file share/man/man1/sedwVs75O: Permission denied
chmod -R +w $out/share
'';
})
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ pub struct Opts {
#[arg(long, hide = true)]
pub generate_markdown_help: bool,

/// Generate `man` pages in the given directory.
#[cfg(feature = "clap_mangen")]
#[arg(long, hide = true)]
pub generate_man_pages: Option<Utf8PathBuf>,

/// Lifecycle hooks and commands to run at various points.
#[command(flatten)]
pub hooks: crate::hooks::HookOpts,
Expand Down
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ async fn main() -> miette::Result<()> {
return Ok(());
}

#[cfg(feature = "clap_mangen")]
if let Some(out_dir) = opts.generate_man_pages {
use clap::CommandFactory;
use miette::IntoDiagnostic;
use miette::WrapErr;

let command = cli::Opts::command();
clap_mangen::generate_to(command, out_dir)
.into_diagnostic()
.wrap_err("Failed to generate man pages")?;
return Ok(());
}

std::env::set_var("IN_GHCIWATCH", "1");

let (ghci_sender, ghci_receiver) = mpsc::channel(32);
Expand Down

0 comments on commit 8adc8ab

Please sign in to comment.