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 5bcfc17 commit 63ee65c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 26 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
on:
push:
branches: ['main']
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
MISE_EXPERIMENTAL: 1

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: jdx/mise-action@v2
- run: mise r ci
10 changes: 8 additions & 2 deletions .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ raw = true
[tasks.test]
run = 'cargo test --all --all-features'

[tasks.insta]
run = 'cargo test --all --all-features'
[tasks.lint]
run = 'cargo clippy --all --all-features -- -D warnings'

[tasks.format-check]
run = 'cargo fmt --all -- --check'

[tasks.ci]
depends = ['test', 'lint', 'format-check']
33 changes: 26 additions & 7 deletions cli/src/cli/generate/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::{Display, Formatter};
use std::fs;
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -162,6 +163,24 @@ enum UsageMdDirective {
Plain(String),
}

impl Display for UsageMdDirective {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
UsageMdDirective::Load { file } => {
write!(f, "<!-- [USAGE] load file=\"{}\" -->", file.display())
}
UsageMdDirective::Title => write!(f, "<!-- [USAGE] title -->"),
UsageMdDirective::UsageOverview => write!(f, "<!-- [USAGE] usage_overview -->"),
UsageMdDirective::GlobalArgs => write!(f, "<!-- [USAGE] global_args -->"),
UsageMdDirective::GlobalFlags => write!(f, "<!-- [USAGE] global_flags -->"),
UsageMdDirective::CommandIndex => write!(f, "<!-- [USAGE] command_index -->"),
UsageMdDirective::Commands => write!(f, "<!-- [USAGE] commands -->"),
UsageMdDirective::EndToken => write!(f, "<!-- [USAGE] -->"),
UsageMdDirective::Plain(line) => write!(f, "{}", line),
}
}
}

struct UsageMdContext {
plain: bool,
spec: Option<Spec>,
Expand Down Expand Up @@ -191,21 +210,21 @@ impl UsageMdDirective {
UsageMdDirective::Load { file } => {
let file = context::prepend_load_root(file);
ctx.spec = Some(fs::read_to_string(&file).into_diagnostic()?.parse()?);
ctx.push(format!("<!-- [USAGE] load file=\"{}\" -->", file.display()));
ctx.push(self.to_string());
}
UsageMdDirective::Title => {
ensure!(ctx.spec.is_some(), "spec must be loaded before title");
ctx.plain = false;
let spec = ctx.spec.as_ref().unwrap();
ctx.push(format!("<!-- [USAGE] title -->"));
ctx.push(self.to_string());
ctx.push(format!("# {name}", name = spec.name));
ctx.push(format!("<!-- [USAGE] -->"));
}
UsageMdDirective::UsageOverview => {
ctx.plain = false;
let spec = ctx.spec.as_ref().unwrap();

ctx.push("<!-- [USAGE] usage_overview -->".to_string());
ctx.push(self.to_string());
ctx.push("```".to_string());
ctx.push(format!("{bin} [flags] [args]", bin = spec.bin));
ctx.push("```".to_string());
Expand All @@ -215,7 +234,7 @@ impl UsageMdDirective {
ctx.plain = false;
let spec = ctx.spec.as_ref().unwrap();

ctx.push("<!-- [USAGE] global_args -->".to_string());
ctx.push(self.to_string());
let args = spec.cmd.args.iter().filter(|a| !a.hide).collect::<Vec<_>>();
if !args.is_empty() {
for arg in args {
Expand All @@ -236,7 +255,7 @@ impl UsageMdDirective {
ctx.plain = false;
let spec = ctx.spec.as_ref().unwrap();

ctx.push("<!-- [USAGE] global_flags -->".to_string());
ctx.push(self.to_string());
let flags = spec
.cmd
.flags
Expand All @@ -261,14 +280,14 @@ impl UsageMdDirective {
UsageMdDirective::CommandIndex => {
ctx.plain = false;
let spec = ctx.spec.as_ref().unwrap();
ctx.push(format!("<!-- [USAGE] command_index -->"));
ctx.push(self.to_string());
print_commands_index(&ctx, &[&spec.cmd])?;
ctx.push(format!("<!-- [USAGE] -->"));
}
UsageMdDirective::Commands => {
ctx.plain = false;
let spec = ctx.spec.as_ref().unwrap();
ctx.push(format!("<!-- [USAGE] commands -->"));
ctx.push(self.to_string());
print_commands(&ctx, &[&spec.cmd])?;
ctx.push(format!("<!-- [USAGE] -->"));
}
Expand Down
1 change: 0 additions & 1 deletion examples/MISE_README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<!-- [USAGE] load file="./mise.usage.kdl" -->
<!-- [USAGE] load file="./mise.usage.kdl" -->
<!-- [USAGE] title -->
# mise
<!-- [USAGE] -->
Expand Down
35 changes: 19 additions & 16 deletions src/parse/spec.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::context::get_load_root;
use crate::error::UsageErr;
use crate::parse::cmd::SchemaCmd;
use kdl::{KdlDocument, KdlEntry, KdlNode};
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};
use std::path::Path;
use std::str::FromStr;
use xx::file;

use kdl::{KdlDocument, KdlEntry, KdlNode};

use xx::{context, file};

use crate::error::UsageErr;
use crate::parse::cmd::SchemaCmd;

#[derive(Debug, Default)]
pub struct Spec {
Expand Down Expand Up @@ -63,7 +65,7 @@ fn get_string_prop(node: &KdlNode, name: &str) -> Option<String> {

impl FromStr for Spec {
type Err = miette::Error;
fn from_str(input: &str) -> miette::Result<Spec> {
fn from_str(input: &str) -> Result<Spec, Self::Err> {
let kdl: KdlDocument = input
.parse()
.map_err(|err: kdl::KdlError| UsageErr::KdlError(err))?;
Expand All @@ -81,15 +83,16 @@ impl FromStr for Spec {
schema.cmd.subcommands.insert(node.name.to_string(), node);
}
"include" => {
let file = match get_string_prop(node, "file").map(PathBuf::from) {
Some(file) if file.is_relative() => get_load_root().join(file),
Some(file) => file.to_path_buf(),
None => Err(UsageErr::InvalidInput(
node.to_string(),
*node.span(),
input.to_string(),
))?,
};
let file = get_string_prop(node, "file")
.map(context::prepend_load_root)
.ok_or_else(|| {
UsageErr::InvalidInput(
node.to_string(),
*node.span(),
input.to_string(),
)
})?;
ensure!(file.exists(), "File not found: {}", file.display());
info!("include: {}", file.display());
let (spec, _) = split_script(&file)?;
schema.merge(spec.parse()?);
Expand Down

0 comments on commit 63ee65c

Please sign in to comment.