Skip to content

Commit

Permalink
write code to generate the FCP comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Jul 10, 2024
1 parent 42b4981 commit 425586a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 39 deletions.
31 changes: 31 additions & 0 deletions mdbook-goals/src/fcp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{collections::BTreeSet, path::Path};

use crate::{goal, team::TeamName};

pub fn generate_comment(path: &Path) -> anyhow::Result<()> {
let goal_documents = goal::goals_in_dir(path)?;
let teams_with_asks: BTreeSet<&TeamName> = goal_documents
.iter()
.flat_map(|g| &g.team_asks)
.flat_map(|ask| &ask.teams)
.copied()
.collect();

for team_name in teams_with_asks {
let team_data = team_name.data();

println!("\n## {}\n", team_data.name);

let (leads, members): (Vec<_>, Vec<_>) = team_data.members.iter().partition(|m| m.is_lead);

for lead in leads {
println!("* [ ] @{} (required, lead)", lead.github);
}

for member in members {
println!("* [ ] @{} (optional)", member.github);
}
}

Ok(())
}
25 changes: 18 additions & 7 deletions mdbook-goals/src/goal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{collections::BTreeSet, path::PathBuf};
use regex::Regex;

use crate::team::{self, TeamName};
use crate::util::commas;
use crate::util::{commas, markdown_files};
use crate::{
markwaydown::{self, Section, Table},
util::{self, ARROW},
Expand Down Expand Up @@ -42,22 +42,33 @@ pub struct Metadata {
#[derive(Debug)]
pub struct TeamAsk {
/// Path to the markdown file containing this ask (appropriate for a link)
link_path: Arc<PathBuf>,
pub link_path: Arc<PathBuf>,

/// Title of the subgoal (or goal, if there are no subgoals)
subgoal: String,
pub subgoal: String,

/// What the team is being asked for (e.g., RFC decision)
heading: String,
pub heading: String,

/// Name(s) of the teams being asked to do the thing
teams: Vec<&'static TeamName>,
pub teams: Vec<&'static TeamName>,

/// Owners of the subgoal or goal
owners: String,
pub owners: String,

/// Any notes
notes: String,
pub notes: String,
}

/// Load all the goals from a given directory
pub fn goals_in_dir(directory_path: &Path) -> anyhow::Result<Vec<GoalDocument>> {
let mut goal_documents = vec![];
for (path, link_path) in markdown_files(&directory_path)? {
if let Some(goal_document) = GoalDocument::load(&path, &link_path)? {
goal_documents.push(goal_document);
}
}
Ok(goal_documents)
}

impl GoalDocument {
Expand Down
9 changes: 8 additions & 1 deletion mdbook-goals/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
use mdbook_preprocessor::GoalPreprocessor;
use semver::{Version, VersionReq};
use std::io;
use std::{io, path::PathBuf};
use structopt::StructOpt;

mod fcp;
mod goal;
mod markwaydown;
mod mdbook_preprocessor;
Expand All @@ -21,6 +22,8 @@ struct Opt {
#[allow(dead_code)]
enum Command {
Supports { renderer: String },

FCP { path: PathBuf },
}

fn main() -> anyhow::Result<()> {
Expand All @@ -31,6 +34,10 @@ fn main() -> anyhow::Result<()> {
handle_supports(&GoalPreprocessor, renderer)?;
}

Some(Command::FCP { path }) => {
fcp::generate_comment(&path)?;
}

None => {
handle_preprocessing(&GoalPreprocessor)?;
}
Expand Down
43 changes: 13 additions & 30 deletions mdbook-goals/src/mdbook_preprocessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use mdbook::book::{Book, Chapter};
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
use mdbook::BookItem;
use regex::{Captures, Regex};
use walkdir::WalkDir;

use crate::goal::{self, format_team_asks, GoalDocument, Status, TeamAsk};
use crate::team;
Expand Down Expand Up @@ -247,40 +246,22 @@ impl<'c> GoalPreprocessorWithContext<'c> {
if let Some(goals) = self.goal_document_map.get(chapter_path) {
return Ok(goals.clone());
}
let mut goal_documents = vec![];
for (path, link_path) in self.markdown_files(&chapter_path)? {
if let Some(goal_document) = GoalDocument::load(&path, &link_path)? {
goal_documents.push(goal_document);
}
}

let goal_documents = goal::goals_in_dir(
self.ctx
.config
.book
.src
.join(chapter_path)
.parent()
.unwrap(),
)?;
let goals = Arc::new(goal_documents);
self.goal_document_map
.insert(chapter_path.to_path_buf(), goals.clone());
Ok(goals)
}

fn markdown_files(&mut self, chapter_path: &Path) -> anyhow::Result<Vec<(PathBuf, PathBuf)>> {
let chapter_path = self.ctx.config.book.src.join(chapter_path);
let parent_path = chapter_path.parent().unwrap();

let mut files = vec![];
for entry in WalkDir::new(parent_path) {
let entry = entry?;

if entry.file_type().is_file() && entry.path().extension() == Some("md".as_ref()) {
files.push((
entry.path().to_path_buf(),
entry
.path()
.strip_prefix(parent_path)
.unwrap()
.to_path_buf(),
));
}
}
Ok(files)
}

fn link_users(&mut self, chapter: &mut Chapter) -> anyhow::Result<()> {
let usernames: BTreeSet<String> = self
.username
Expand Down Expand Up @@ -372,7 +353,9 @@ impl<'c> GoalPreprocessorWithContext<'c> {
fn link_teams(&self, chapter: &mut Chapter) -> anyhow::Result<()> {
chapter.content.push_str("\n\n");
for team in team::get_team_names()? {
chapter.content.push_str(&format!("{team}: {}\n", team.url()));
chapter
.content
.push_str(&format!("{team}: {}\n", team.url()));
}
Ok(())
}
Expand Down
28 changes: 27 additions & 1 deletion mdbook-goals/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{
fmt::{Display, Write},
path::PathBuf,
path::{Path, PathBuf},
};

use disk_persist::DiskPersist;
use walkdir::WalkDir;

pub const ARROW: &str = "↳";

Expand Down Expand Up @@ -96,3 +97,28 @@ pub fn commas(iter: impl IntoIterator<Item: Display>) -> String {
}
output
}

/// Returns all markdown files in `directory_path` as `(absolute, relative)` pairs,
/// where `relative` is relative to `directory_path`.
pub fn markdown_files(directory_path: &Path) -> anyhow::Result<Vec<(PathBuf, PathBuf)>> {
if !directory_path.is_dir() {
anyhow::bail!("`{}` is not a directory", directory_path.display());
}

let mut files = vec![];
for entry in WalkDir::new(directory_path) {
let entry = entry?;

if entry.file_type().is_file() && entry.path().extension() == Some("md".as_ref()) {
files.push((
entry.path().to_path_buf(),
entry
.path()
.strip_prefix(directory_path)
.unwrap()
.to_path_buf(),
));
}
}
Ok(files)
}

0 comments on commit 425586a

Please sign in to comment.