Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 13, 2024
1 parent 03a2634 commit 8bfed7a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 28 deletions.
42 changes: 30 additions & 12 deletions cli/src/cli/generate/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,19 @@ impl Markdown {
let raw = fs::read_to_string(inject).into_diagnostic()?;
println!("{}", raw);
let root = inject.parent().unwrap().to_path_buf();
let cwd = env::current_dir().into_diagnostic()?;
env::set_current_dir(&root).into_diagnostic()?;
let out = raw
.lines()
.map(|line| line.parse())
.collect::<Result<Vec<UsageMdDirective>>>()?
.into_iter()
.try_fold(UsageMdContext::new(root), |ctx, d| d.run(ctx))?
.output
.out
.join("\n");

env::set_current_dir(cwd).into_diagnostic()?;
println!("{}", out);
//fs::write(inject, out).into_diagnostic()?;
fs::write(inject, out).into_diagnostic()?;
Ok(())
}

Expand Down Expand Up @@ -147,21 +148,24 @@ impl Markdown {
enum UsageMdDirective {
Load { file: PathBuf },
CommandIndex,
EndToken,
Plain(String),
}

struct UsageMdContext {
root: PathBuf,
_root: PathBuf,
plain: bool,
spec: Option<Spec>,
output: Vec<String>,
out: Vec<String>,
}

impl UsageMdContext {
fn new(root: PathBuf) -> Self {
fn new(_root: PathBuf) -> Self {
Self {
root,
_root,
plain: true,
spec: None,
output: vec![],
out: vec![],
}
}
}
Expand All @@ -175,12 +179,16 @@ impl UsageMdDirective {
// false => file.clone(),
// };
ctx.spec = Some(fs::read_to_string(file).into_diagnostic()?.parse()?);
ctx.out
.push(format!("<!-- [USAGE] load file=\"{}\" -->", file.display()));

Ok(ctx)
}
UsageMdDirective::CommandIndex => {
ctx.plain = false;
let spec = ctx.spec.as_ref().unwrap();
let mut out = vec![];
out.push(format!("<!-- [USAGE] command_index -->"));
out.push(format!("# {name}", name = spec.name));
out.push(format!("## Usage"));
out.push(format!("```"));
Expand Down Expand Up @@ -244,11 +252,18 @@ impl UsageMdDirective {
}
}

ctx.output.push(out.join("\n"));
out.push(format!("<!-- [USAGE] -->"));
ctx.out.push(out.join("\n"));
Ok(ctx)
}
UsageMdDirective::EndToken => {
ctx.plain = true;
Ok(ctx)
}
UsageMdDirective::Plain(line) => {
ctx.output.push(line.clone());
if ctx.plain {
ctx.out.push(line.clone());
}
Ok(ctx)
}
}
Expand All @@ -271,14 +286,17 @@ struct MarkdownError {
impl FromStr for UsageMdDirective {
type Err = miette::Error;
fn from_str(line: &str) -> Result<Self, Self::Err> {
let directive = if let Some(x) = regex!(r#"<!-- \[USAGE\] (.+) -->"#).captures(line) {
if line == "<!-- [USAGE] -->" {
return Ok(UsageMdDirective::EndToken);
}
let directive = if let Some(x) = regex!(r#"<!-- \[USAGE\] (.*) -->"#).captures(line) {
let doc: KdlDocument = x.get(1).unwrap().as_str().parse()?;
if !doc.nodes().len() == 1 {
bail!("only one node allowed in usage directive");
}
let node = doc.nodes().first().unwrap();
let err = |msg: String, span| MarkdownError {
msg: msg,
msg,
src: doc.to_string(),
err_span: span,
};
Expand Down
51 changes: 51 additions & 0 deletions examples/MISE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,54 @@

<!-- [USAGE] load file="./mise.usage.kdl" -->
<!-- [USAGE] command_index -->
# mise
## Usage
```
[flags] [args]
```
## Flags
- `-C,--cd <DIR>`: Change directory before running command
- `-q,--quiet`: Suppress non-error messages
- `-v,--verbose`: Show extra output (use -vv for even more)
- `-y,--yes`: Answer yes to all confirmation prompts
## Commands
- [`activate`](./activate)
- [`alias`](./alias)
- [`bin-paths`](./bin-paths)
- [`cache`](./cache)
- [`completion`](./completion)
- [`config`](./config)
- [`current`](./current)
- [`deactivate`](./deactivate)
- [`direnv`](./direnv)
- [`doctor`](./doctor)
- [`env`](./env)
- [`exec`](./exec)
- [`implode`](./implode)
- [`install`](./install)
- [`latest`](./latest)
- [`link`](./link)
- [`ls`](./ls)
- [`ls-remote`](./ls-remote)
- [`outdated`](./outdated)
- [`plugins`](./plugins)
- [`prune`](./prune)
- [`reshim`](./reshim)
- [`run`](./run)
- [`self-update`](./self-update)
- [`set`](./set)
- [`settings`](./settings)
- [`shell`](./shell)
- [`sync`](./sync)
- [`task`](./task)
- [`trust`](./trust)
- [`uninstall`](./uninstall)
- [`unset`](./unset)
- [`upgrade`](./upgrade)
- [`usage`](./usage)
- [`use`](./use)
- [`version`](./version)
- [`watch`](./watch)
- [`where`](./where)
- [`which`](./which)
<!-- [USAGE] -->
36 changes: 20 additions & 16 deletions src/parse/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ impl Spec {
}
Ok((schema, body))
}

fn merge(&mut self, other: Spec) {
if !other.name.is_empty() {
self.name = other.name;
}
if !other.bin.is_empty() {
self.bin = other.bin;
}
for flag in other.cmd.flags {
self.cmd.flags.push(flag);
}
for arg in other.cmd.args {
self.cmd.args.push(arg);
}
for (name, cmd) in other.cmd.subcommands {
self.cmd.subcommands.insert(name, cmd);
}
}
}

fn split_script(file: &Path) -> Result<(String, String), UsageErr> {
Expand Down Expand Up @@ -58,23 +76,9 @@ impl FromStr for Spec {
"include" => {
let file = node.get("file").unwrap().value().as_string().unwrap();
info!("include: {}", file);
let (spec, body) = split_script(Path::new(file))?;
let (spec, _) = split_script(Path::new(file))?;
let include = Self::from_str(&spec)?;
if !include.name.is_empty() {
schema.name = include.name;
}
if !include.bin.is_empty() {
schema.bin = include.bin;
}
for flag in include.cmd.flags {
schema.cmd.flags.push(flag);
}
for arg in include.cmd.args {
schema.cmd.args.push(arg);
}
for (name, cmd) in include.cmd.subcommands {
schema.cmd.subcommands.insert(name, cmd);
}
schema.merge(include);
}
_ => Err(UsageErr::InvalidInput(
node.to_string(),
Expand Down

0 comments on commit 8bfed7a

Please sign in to comment.