summaryrefslogtreecommitdiff
path: root/cli/build.rs
diff options
context:
space:
mode:
authorfigsoda <figsoda@pm.me>2023-04-06 14:14:01 -0400
committerGitHub <noreply@github.com>2023-04-06 20:14:01 +0200
commit1c324765e92c23826dff4f37d3b01761a3ac6ef4 (patch)
tree1c84390a6ff58545374b7cbfa3c74f82af306558 /cli/build.rs
parent75461675c4e57675174d98060124c9e13beaf7e4 (diff)
Add shell completions and man pages (#582)
Diffstat (limited to 'cli/build.rs')
-rw-r--r--cli/build.rs52
1 files changed, 46 insertions, 6 deletions
diff --git a/cli/build.rs b/cli/build.rs
index 014960f4..75e8492a 100644
--- a/cli/build.rs
+++ b/cli/build.rs
@@ -1,9 +1,15 @@
+use std::env;
+use std::fs::{create_dir_all, File};
+use std::path::Path;
use std::process::Command;
-fn main() {
- println!("cargo:rerun-if-env-changed=TYPST_VERSION");
- if option_env!("TYPST_VERSION").is_some() {
- return;
+use clap::{CommandFactory, ValueEnum};
+use clap_complete::{generate_to, Shell};
+use clap_mangen::Man;
+
+pub fn typst_version() -> String {
+ if let Some(version) = option_env!("TYPST_VERSION") {
+ return version.to_owned();
}
let pkg = env!("CARGO_PKG_VERSION");
@@ -13,7 +19,41 @@ fn main() {
.ok()
.filter(|output| output.status.success())
.and_then(|output| String::from_utf8(output.stdout.get(..8)?.into()).ok())
- .unwrap_or_else(|| "(unknown hash)".into());
+ .unwrap_or_else(|| "unknown hash".into());
+
+ format!("{pkg} ({hash})")
+}
+
+mod args {
+ include!("src/args.rs");
+}
+
+fn main() {
+ println!("cargo:rerun-if-env-changed=TYPST_VERSION");
+ println!("cargo:rerun-if-env-changed=GEN_ARTIFACTS");
+
+ if option_env!("TYPST_VERSION").is_none() {
+ println!("cargo:rustc-env=TYPST_VERSION={}", typst_version());
+ }
- println!("cargo:rustc-env=TYPST_VERSION={pkg} ({hash})");
+ if let Some(dir) = env::var_os("GEN_ARTIFACTS") {
+ let out = &Path::new(&dir);
+ create_dir_all(out).unwrap();
+ let cmd = &mut args::CliArguments::command();
+
+ Man::new(cmd.clone())
+ .render(&mut File::create(out.join("typst.1")).unwrap())
+ .unwrap();
+
+ for subcmd in cmd.get_subcommands() {
+ let name = format!("typst-{}", subcmd.get_name());
+ Man::new(subcmd.clone().name(&name))
+ .render(&mut File::create(out.join(format!("{name}.1"))).unwrap())
+ .unwrap();
+ }
+
+ for shell in Shell::value_variants() {
+ generate_to(*shell, cmd, "typst", out).unwrap();
+ }
+ }
}