Skip to content

Commit

Permalink
Move opts to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancheg committed May 27, 2024
1 parent fca3064 commit 75a0f65
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ jobs:
with:
command: test
args: --all --all-features
readme-check:
name: Check README.md
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Check README.md
uses: actions-rs/cargo@v1
with:
command: xtask
args: gen-help --check
macos-stable:
name: macOS stable
runs-on: macos-latest
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.0.0"
authors = ["Stiopa Koltsov <stepan.koltsov@gmail.com>"]
edition = "2018"

[workspace]
members = ["xtask"]

[lib]
doctest = false

Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,33 @@ cargo install --git https://github.com/stepancheg/absh
```

Cargo is a Rust package manager and build system. It can be downloaded [from rustup.rs](https://rustup.rs/).

## absh --help

<!-- absh-help:start -->
```
absh
A/B testing for shell scripts
USAGE:
absh [OPTIONS] -a <A>
OPTIONS:
-a <A> A variant shell script
-A, --a-warmup <AW> A variant warmup shell script
-b <B> B variant shell script
-B, --b-warmup <BW> B variant warmup shell script
-c <C> C variant shell script
-C, --c-warmup <CW> C variant warmup shell script
-d <D> D variant shell script
-D, --d-warmup <DW> D variant warmup shell script
-e <E> E variant shell script
-E, --e-warmup <EW> E variant warmup shell script
-h, --help Print help information
-i Ignore the results of the first iteration
-m, --mem Also measure max resident set size
--max-time <MAX_TIME> Test is considered failed if it takes longer than this many seconds
-n <ITERATIONS> Stop after n successful iterations (run forever if not specified)
-r Randomise test execution order
```
<!-- absh-help:end -->
8 changes: 8 additions & 0 deletions xtask/Cargo.toml
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"] }
102 changes: 102 additions & 0 deletions xtask/src/main.rs
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(),
}
}

0 comments on commit 75a0f65

Please sign in to comment.