-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fca3064
commit 75a0f65
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[alias] | ||
xtask = "run --package xtask --" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "xtask" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1.0.86" | ||
clap = { version = "3.2.25", features = ["derive", "wrap_help"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::fs; | ||
use std::process::Command; | ||
use std::process::Stdio; | ||
|
||
use anyhow::Context; | ||
use clap::Parser; | ||
|
||
/// Regenerate help for absh. | ||
#[derive(clap::Parser, Debug)] | ||
struct GenReadme { | ||
/// Do not regenerate help, just check that it can be generated. | ||
#[clap(long)] | ||
check: bool, | ||
} | ||
|
||
struct ReadmeMd { | ||
orignial: String, | ||
before: String, | ||
after: String, | ||
} | ||
|
||
impl GenReadme { | ||
fn parse_readme_md() -> anyhow::Result<ReadmeMd> { | ||
eprintln!("Reading README.md"); | ||
let readme_md = fs::read_to_string("README.md")?; | ||
let lines = readme_md.lines().collect::<Vec<_>>(); | ||
let start = lines | ||
.iter() | ||
.position(|&line| line == "<!-- absh-help:start -->") | ||
.context("<!-- absh-help:start --> not found in README.md")?; | ||
let end = lines | ||
.iter() | ||
.position(|&line| line == "<!-- absh-help:end -->") | ||
.context("<!-- absh-help:end --> not found in README.md")?; | ||
let before = lines[..start + 1] | ||
.iter() | ||
.map(|&line| format!("{}\n", line)) | ||
.collect::<String>(); | ||
let after = lines[end..] | ||
.iter() | ||
.map(|&line| format!("{}\n", line)) | ||
.collect::<String>(); | ||
Ok(ReadmeMd { | ||
orignial: readme_md, | ||
before, | ||
after, | ||
}) | ||
} | ||
|
||
fn normalize_help_output(help: &str) -> String { | ||
help.lines() | ||
.map(|line| { | ||
let line = line.trim_end(); | ||
format!("{}\n", line) | ||
}) | ||
.collect() | ||
} | ||
|
||
fn run(&self) -> anyhow::Result<()> { | ||
eprintln!("Running absh --help"); | ||
let out = Command::new("cargo") | ||
.arg("run") | ||
.arg("--bin") | ||
.arg("absh") | ||
.arg("--") | ||
.arg("--help") | ||
.stdin(Stdio::null()) | ||
.stdout(Stdio::piped()) | ||
.output()?; | ||
|
||
let ReadmeMd { | ||
orignial, | ||
before, | ||
after, | ||
} = Self::parse_readme_md()?; | ||
let help = String::from_utf8(out.stdout)?; | ||
let help = Self::normalize_help_output(&help); | ||
|
||
let readme_md = format!("{before}```\n{help}```\n{after}"); | ||
if self.check { | ||
anyhow::ensure!(orignial == readme_md, "README.md is not up to date"); | ||
eprintln!("README.md is up to date"); | ||
} else { | ||
eprintln!("Writing README.md"); | ||
fs::write("README.md", readme_md)?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// absh build helper. | ||
#[derive(clap::Parser, Debug)] | ||
enum AbshXTaskOpts { | ||
GenHelp(GenReadme), | ||
} | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let opts: AbshXTaskOpts = AbshXTaskOpts::parse(); | ||
match opts { | ||
AbshXTaskOpts::GenHelp(gen_help) => gen_help.run(), | ||
} | ||
} |