Move configuration out of README and into permanent docs (#3150)

This commit is contained in:
Charlie Marsh 2023-02-22 19:25:53 -05:00 committed by GitHub
parent 2d4fae45d9
commit b9bfb81e36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 641 additions and 540 deletions

View file

@ -1,11 +1,14 @@
//! Generate CLI help.
#![allow(clippy::print_stdout, clippy::print_stderr)]
use std::str;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::{fs, str};
use anyhow::Result;
use crate::utils::replace_readme_section;
use crate::ROOT_DIR;
const COMMAND_HELP_BEGIN_PRAGMA: &str = "<!-- Begin auto-generated command help. -->\n";
const COMMAND_HELP_END_PRAGMA: &str = "<!-- End auto-generated command help. -->";
@ -15,7 +18,7 @@ const SUBCOMMAND_HELP_END_PRAGMA: &str = "<!-- End auto-generated subcommand hel
#[derive(clap::Args)]
pub struct Args {
/// Write the generated help to stdout (rather than to `README.md`).
/// Write the generated help to stdout (rather than to `docs/configuration.md`).
#[arg(long)]
pub(crate) dry_run: bool,
}
@ -24,6 +27,32 @@ fn trim_lines(s: &str) -> String {
s.lines().map(str::trim_end).collect::<Vec<_>>().join("\n")
}
fn replace_docs_section(content: &str, begin_pragma: &str, end_pragma: &str) -> Result<()> {
// Read the existing file.
let file = PathBuf::from(ROOT_DIR).join("docs/configuration.md");
let existing = fs::read_to_string(&file)?;
// Extract the prefix.
let index = existing
.find(begin_pragma)
.expect("Unable to find begin pragma");
let prefix = &existing[..index + begin_pragma.len()];
// Extract the suffix.
let index = existing
.find(end_pragma)
.expect("Unable to find end pragma");
let suffix = &existing[index..];
// Write the prefix, new contents, and suffix.
let mut f = OpenOptions::new().write(true).truncate(true).open(&file)?;
writeln!(f, "{prefix}")?;
write!(f, "{content}")?;
write!(f, "{suffix}")?;
Ok(())
}
pub fn main(args: &Args) -> Result<()> {
// Generate `ruff help`.
let command_help = trim_lines(ruff_cli::command_help().trim());
@ -35,12 +64,12 @@ pub fn main(args: &Args) -> Result<()> {
print!("{command_help}");
print!("{subcommand_help}");
} else {
replace_readme_section(
replace_docs_section(
&format!("```text\n{command_help}\n```\n\n"),
COMMAND_HELP_BEGIN_PRAGMA,
COMMAND_HELP_END_PRAGMA,
)?;
replace_readme_section(
replace_docs_section(
&format!("```text\n{subcommand_help}\n```\n\n"),
SUBCOMMAND_HELP_BEGIN_PRAGMA,
SUBCOMMAND_HELP_END_PRAGMA,

View file

@ -15,7 +15,6 @@ mod print_ast;
mod print_cst;
mod print_tokens;
mod round_trip;
mod utils;
const ROOT_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../");

View file

@ -1,34 +0,0 @@
use std::fs;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use anyhow::Result;
use crate::ROOT_DIR;
pub fn replace_readme_section(content: &str, begin_pragma: &str, end_pragma: &str) -> Result<()> {
// Read the existing file.
let file = PathBuf::from(ROOT_DIR).join("README.md");
let existing = fs::read_to_string(&file)?;
// Extract the prefix.
let index = existing
.find(begin_pragma)
.expect("Unable to find begin pragma");
let prefix = &existing[..index + begin_pragma.len()];
// Extract the suffix.
let index = existing
.find(end_pragma)
.expect("Unable to find end pragma");
let suffix = &existing[index..];
// Write the prefix, new contents, and suffix.
let mut f = OpenOptions::new().write(true).truncate(true).open(&file)?;
writeln!(f, "{prefix}")?;
write!(f, "{content}")?;
write!(f, "{suffix}")?;
Ok(())
}