Skip to content

Commit 8bfed7a

Browse files
committed
wip
1 parent 03a2634 commit 8bfed7a

File tree

3 files changed

+101
-28
lines changed

3 files changed

+101
-28
lines changed

cli/src/cli/generate/markdown.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@ impl Markdown {
4848
let raw = fs::read_to_string(inject).into_diagnostic()?;
4949
println!("{}", raw);
5050
let root = inject.parent().unwrap().to_path_buf();
51+
let cwd = env::current_dir().into_diagnostic()?;
5152
env::set_current_dir(&root).into_diagnostic()?;
5253
let out = raw
5354
.lines()
5455
.map(|line| line.parse())
5556
.collect::<Result<Vec<UsageMdDirective>>>()?
5657
.into_iter()
5758
.try_fold(UsageMdContext::new(root), |ctx, d| d.run(ctx))?
58-
.output
59+
.out
5960
.join("\n");
60-
61+
env::set_current_dir(cwd).into_diagnostic()?;
6162
println!("{}", out);
62-
//fs::write(inject, out).into_diagnostic()?;
63+
fs::write(inject, out).into_diagnostic()?;
6364
Ok(())
6465
}
6566

@@ -147,21 +148,24 @@ impl Markdown {
147148
enum UsageMdDirective {
148149
Load { file: PathBuf },
149150
CommandIndex,
151+
EndToken,
150152
Plain(String),
151153
}
152154

153155
struct UsageMdContext {
154-
root: PathBuf,
156+
_root: PathBuf,
157+
plain: bool,
155158
spec: Option<Spec>,
156-
output: Vec<String>,
159+
out: Vec<String>,
157160
}
158161

159162
impl UsageMdContext {
160-
fn new(root: PathBuf) -> Self {
163+
fn new(_root: PathBuf) -> Self {
161164
Self {
162-
root,
165+
_root,
166+
plain: true,
163167
spec: None,
164-
output: vec![],
168+
out: vec![],
165169
}
166170
}
167171
}
@@ -175,12 +179,16 @@ impl UsageMdDirective {
175179
// false => file.clone(),
176180
// };
177181
ctx.spec = Some(fs::read_to_string(file).into_diagnostic()?.parse()?);
182+
ctx.out
183+
.push(format!("<!-- [USAGE] load file=\"{}\" -->", file.display()));
178184

179185
Ok(ctx)
180186
}
181187
UsageMdDirective::CommandIndex => {
188+
ctx.plain = false;
182189
let spec = ctx.spec.as_ref().unwrap();
183190
let mut out = vec![];
191+
out.push(format!("<!-- [USAGE] command_index -->"));
184192
out.push(format!("# {name}", name = spec.name));
185193
out.push(format!("## Usage"));
186194
out.push(format!("```"));
@@ -244,11 +252,18 @@ impl UsageMdDirective {
244252
}
245253
}
246254

247-
ctx.output.push(out.join("\n"));
255+
out.push(format!("<!-- [USAGE] -->"));
256+
ctx.out.push(out.join("\n"));
257+
Ok(ctx)
258+
}
259+
UsageMdDirective::EndToken => {
260+
ctx.plain = true;
248261
Ok(ctx)
249262
}
250263
UsageMdDirective::Plain(line) => {
251-
ctx.output.push(line.clone());
264+
if ctx.plain {
265+
ctx.out.push(line.clone());
266+
}
252267
Ok(ctx)
253268
}
254269
}
@@ -271,14 +286,17 @@ struct MarkdownError {
271286
impl FromStr for UsageMdDirective {
272287
type Err = miette::Error;
273288
fn from_str(line: &str) -> Result<Self, Self::Err> {
274-
let directive = if let Some(x) = regex!(r#"<!-- \[USAGE\] (.+) -->"#).captures(line) {
289+
if line == "<!-- [USAGE] -->" {
290+
return Ok(UsageMdDirective::EndToken);
291+
}
292+
let directive = if let Some(x) = regex!(r#"<!-- \[USAGE\] (.*) -->"#).captures(line) {
275293
let doc: KdlDocument = x.get(1).unwrap().as_str().parse()?;
276294
if !doc.nodes().len() == 1 {
277295
bail!("only one node allowed in usage directive");
278296
}
279297
let node = doc.nodes().first().unwrap();
280298
let err = |msg: String, span| MarkdownError {
281-
msg: msg,
299+
msg,
282300
src: doc.to_string(),
283301
err_span: span,
284302
};

examples/MISE_README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,54 @@
22

33
<!-- [USAGE] load file="./mise.usage.kdl" -->
44
<!-- [USAGE] command_index -->
5+
# mise
6+
## Usage
7+
```
8+
[flags] [args]
9+
```
10+
## Flags
11+
- `-C,--cd <DIR>`: Change directory before running command
12+
- `-q,--quiet`: Suppress non-error messages
13+
- `-v,--verbose`: Show extra output (use -vv for even more)
14+
- `-y,--yes`: Answer yes to all confirmation prompts
15+
## Commands
16+
- [`activate`](./activate)
17+
- [`alias`](./alias)
18+
- [`bin-paths`](./bin-paths)
19+
- [`cache`](./cache)
20+
- [`completion`](./completion)
21+
- [`config`](./config)
22+
- [`current`](./current)
23+
- [`deactivate`](./deactivate)
24+
- [`direnv`](./direnv)
25+
- [`doctor`](./doctor)
26+
- [`env`](./env)
27+
- [`exec`](./exec)
28+
- [`implode`](./implode)
29+
- [`install`](./install)
30+
- [`latest`](./latest)
31+
- [`link`](./link)
32+
- [`ls`](./ls)
33+
- [`ls-remote`](./ls-remote)
34+
- [`outdated`](./outdated)
35+
- [`plugins`](./plugins)
36+
- [`prune`](./prune)
37+
- [`reshim`](./reshim)
38+
- [`run`](./run)
39+
- [`self-update`](./self-update)
40+
- [`set`](./set)
41+
- [`settings`](./settings)
42+
- [`shell`](./shell)
43+
- [`sync`](./sync)
44+
- [`task`](./task)
45+
- [`trust`](./trust)
46+
- [`uninstall`](./uninstall)
47+
- [`unset`](./unset)
48+
- [`upgrade`](./upgrade)
49+
- [`usage`](./usage)
50+
- [`use`](./use)
51+
- [`version`](./version)
52+
- [`watch`](./watch)
53+
- [`where`](./where)
54+
- [`which`](./which)
55+
<!-- [USAGE] -->

src/parse/spec.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ impl Spec {
2525
}
2626
Ok((schema, body))
2727
}
28+
29+
fn merge(&mut self, other: Spec) {
30+
if !other.name.is_empty() {
31+
self.name = other.name;
32+
}
33+
if !other.bin.is_empty() {
34+
self.bin = other.bin;
35+
}
36+
for flag in other.cmd.flags {
37+
self.cmd.flags.push(flag);
38+
}
39+
for arg in other.cmd.args {
40+
self.cmd.args.push(arg);
41+
}
42+
for (name, cmd) in other.cmd.subcommands {
43+
self.cmd.subcommands.insert(name, cmd);
44+
}
45+
}
2846
}
2947

3048
fn split_script(file: &Path) -> Result<(String, String), UsageErr> {
@@ -58,23 +76,9 @@ impl FromStr for Spec {
5876
"include" => {
5977
let file = node.get("file").unwrap().value().as_string().unwrap();
6078
info!("include: {}", file);
61-
let (spec, body) = split_script(Path::new(file))?;
79+
let (spec, _) = split_script(Path::new(file))?;
6280
let include = Self::from_str(&spec)?;
63-
if !include.name.is_empty() {
64-
schema.name = include.name;
65-
}
66-
if !include.bin.is_empty() {
67-
schema.bin = include.bin;
68-
}
69-
for flag in include.cmd.flags {
70-
schema.cmd.flags.push(flag);
71-
}
72-
for arg in include.cmd.args {
73-
schema.cmd.args.push(arg);
74-
}
75-
for (name, cmd) in include.cmd.subcommands {
76-
schema.cmd.subcommands.insert(name, cmd);
77-
}
81+
schema.merge(include);
7882
}
7983
_ => Err(UsageErr::InvalidInput(
8084
node.to_string(),

0 commit comments

Comments
 (0)