diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..35049cb --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +xtask = "run --package xtask --" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 443a11d..6e8890e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -57,7 +57,6 @@ jobs: if: ${{ matrix.use-cross == true }} shell: bash run: | - mkdir .cargo cat > .cargo/config.toml <\d+)\.(?P\d+)' '$dollars dollars and $cents cents'\fR +.br +123 dollars and 45 cents +.TP +Find & replace in file +\fB$ sd 'window.fetch' 'fetch' http.js\fR +.TP +Find & replace from STDIN an emit to STDOUT +\fB$ sd 'window.fetch' 'fetch' < http.js\fR + diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 0000000..89071ad --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "xtask" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +clap = "4.3.1" +clap_complete = "4.3.1" +man = "0.3.0" diff --git a/build.rs b/xtask/src/gen.rs similarity index 79% rename from build.rs rename to xtask/src/gen.rs index d8bfd9a..b85f70f 100644 --- a/build.rs +++ b/xtask/src/gen.rs @@ -1,25 +1,30 @@ -include!("src/cli.rs"); +include!("../../src/cli.rs"); -fn main() { - use std::{env::var, fs}; +use std::{fs, path::Path}; - use clap::{CommandFactory, ValueEnum}; - use clap_complete::{generate_to, Shell}; +use clap::{CommandFactory, ValueEnum}; +use clap_complete::{generate_to, Shell}; +use man::prelude::*; - let out_dir = var("SHELL_COMPLETIONS_DIR").or(var("OUT_DIR")).unwrap(); +pub fn gen() { + let gen_dir = Path::new("gen"); + gen_shell(gen_dir); + gen_man(gen_dir); +} - fs::create_dir_all(&out_dir).unwrap(); +fn gen_shell(base_dir: &Path) { + let completions_dir = base_dir.join("completions"); + fs::create_dir_all(&completions_dir).unwrap(); let mut cmd = Options::command(); for &shell in Shell::value_variants() { - generate_to(shell, &mut cmd, "sd", &out_dir).unwrap(); + generate_to(shell, &mut cmd, "sd", &completions_dir).unwrap(); } - - create_man_page(); } -fn create_man_page() { - use man::prelude::*; +fn gen_man(base_dir: &Path) { + let man_path = base_dir.join("sd.1"); + let page = Manual::new("sd") .flag( Flag::new() @@ -84,8 +89,5 @@ w - match full words only ) .render(); - let mut man_path = - std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()); - man_path.push("sd.1"); - std::fs::write(man_path, page).expect("Error writing man page"); + std::fs::write(man_path, page).unwrap(); } diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 0000000..081c7c2 --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,41 @@ +use std::{ + env, + path::{Path, PathBuf}, +}; + +use clap::{Parser, Subcommand}; + +mod gen; + +#[derive(Parser)] +struct Cli { + #[command(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +enum Commands { + /// Generate static assets + Gen, +} + +fn main() { + let Cli { command } = Cli::parse(); + + env::set_current_dir(project_root()).unwrap(); + + match command { + Commands::Gen => gen::gen(), + } +} + +fn project_root() -> PathBuf { + Path::new( + &env::var("CARGO_MANIFEST_DIR") + .unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), + ) + .ancestors() + .nth(1) + .unwrap() + .to_path_buf() +}