feat(openapi): the OpenAPI document, and the paperwork the crate shipped without
`Cargo.toml` has declared `license = "MIT"` since the first release and the published crate carried no license text, so anyone vendoring the source had the claim without the grant. `LICENSE` is that text. `DCO` is the certificate the README has been requiring a sign-off against without shipping, and `CONTRIBUTING.md` says how — including that GitHub is a mirror of `main` and tags, so a pull request there is carried upstream by a maintainer rather than merged, which would put the two forges out of sync. `CHANGELOG.md` reconstructs 26.7.0 and 26.8.0-beta.0 from their tags. The CLI ships on its own version line, so the header says what that does and does not mean about the core version a scaffold is pinned to, and each entry says when that pin moved. `matcha openapi` prints core's structural OpenAPI 3.1 for the route table — `--title`, `--api-version`, `--out`. It reuses the entry resolution and the generated-script runner that `graph` and `explain` already had, so it is the same `app.ready()` that resolves the graph without booting providers. Structural is the honest word: paths, methods and parameters come from the decorators, and nothing invents a response schema. The loose end from the capability table is closed. `cli.rs` still carried its own copies of the `create` kinds and the `add` capabilities, so adding either meant editing two files that could disagree — a value the parser accepts and the dispatcher rejects. Both now read the list from the module that owns it, and the three `create` piece kinds became rows in a table rather than near-identical match arms, which also retires a `too_many_arguments` allow. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
12 files changed
+542
-47
No files matched your search
+18
-2
@@ -1,5 +1,8 @@
|
||||
use clap::builder::PossibleValuesParser;
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use crate::{cmd_add, cmd_create};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "matcha", version, about = "green-tea CLI")]
|
||||
pub struct Cli {
|
||||
@@ -21,7 +24,7 @@ pub enum Command {
|
||||
Run,
|
||||
/// Generate a piece and auto-wire it into the module
|
||||
Create {
|
||||
#[arg(value_parser = ["module", "controller", "step", "provider"])]
|
||||
#[arg(value_parser = PossibleValuesParser::new(cmd_create::kinds()))]
|
||||
kind: String,
|
||||
name: String,
|
||||
#[arg(long)]
|
||||
@@ -40,6 +43,19 @@ pub enum Command {
|
||||
#[arg(long)]
|
||||
out: Option<String>,
|
||||
},
|
||||
/// Print the structural OpenAPI 3.1 document for the route table
|
||||
Openapi {
|
||||
/// API title (default: core's own)
|
||||
#[arg(long)]
|
||||
title: Option<String>,
|
||||
/// API version (default: core's own)
|
||||
#[arg(long)]
|
||||
api_version: Option<String>,
|
||||
#[arg(long)]
|
||||
entry: Option<String>,
|
||||
#[arg(long)]
|
||||
out: Option<String>,
|
||||
},
|
||||
/// Explain one route's chain, in execution order
|
||||
Explain {
|
||||
/// The route pattern as declared, e.g. /users/:id
|
||||
@@ -51,7 +67,7 @@ pub enum Command {
|
||||
},
|
||||
/// Add a capability to a controller
|
||||
Add {
|
||||
#[arg(value_parser = ["sse", "stream", "buffer", "ws", "upload"])]
|
||||
#[arg(value_parser = PossibleValuesParser::new(cmd_add::names()))]
|
||||
capability: String,
|
||||
/// Which controller to edit (default: the only one in src/controllers)
|
||||
#[arg(long)]
|
||||
|
||||
+68
-43
@@ -7,61 +7,86 @@ use std::path::Path;
|
||||
|
||||
use crate::{entry, wire};
|
||||
|
||||
/// A piece that lives inside a module: a file under `src/<subdir>/` plus an
|
||||
/// entry in one of `@Module`'s arrays.
|
||||
///
|
||||
/// `module` is not one of these — it is registered in the `createApp` call
|
||||
/// rather than in a module array — so it stays a case of its own below.
|
||||
struct Piece {
|
||||
/// Name as typed on the command line.
|
||||
kind: &'static str,
|
||||
/// Directory under `src/`, and the `@Module` key, which are the same word
|
||||
/// for all three.
|
||||
subdir: &'static str,
|
||||
/// Filename infix: `users.controller.ts`.
|
||||
ext: &'static str,
|
||||
/// Appended to the given name to form the exported symbol.
|
||||
suffix: &'static str,
|
||||
stub: fn(&str) -> String,
|
||||
}
|
||||
|
||||
const PIECES: &[Piece] = &[
|
||||
Piece {
|
||||
kind: "controller",
|
||||
subdir: "controllers",
|
||||
ext: "controller",
|
||||
suffix: "Controller",
|
||||
stub: controller_stub,
|
||||
},
|
||||
Piece {
|
||||
kind: "step",
|
||||
subdir: "steps",
|
||||
ext: "step",
|
||||
suffix: "Step",
|
||||
stub: step_stub,
|
||||
},
|
||||
Piece {
|
||||
kind: "provider",
|
||||
subdir: "providers",
|
||||
ext: "provider",
|
||||
suffix: "Provider",
|
||||
stub: provider_stub,
|
||||
},
|
||||
];
|
||||
|
||||
/// Every `create` kind, for clap's `value_parser` and for error messages. One
|
||||
/// list, so a new kind cannot be accepted by the parser and rejected by `run`
|
||||
/// (or the reverse).
|
||||
pub fn kinds() -> Vec<&'static str> {
|
||||
std::iter::once("module")
|
||||
.chain(PIECES.iter().map(|p| p.kind))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn run(kind: &str, name: &str, check: bool) -> std::io::Result<()> {
|
||||
match kind {
|
||||
"controller" => create_piece(
|
||||
name,
|
||||
check,
|
||||
"controllers",
|
||||
"controller",
|
||||
"Controller",
|
||||
"controllers",
|
||||
controller_stub(name),
|
||||
),
|
||||
"step" => create_piece(
|
||||
name,
|
||||
check,
|
||||
"steps",
|
||||
"step",
|
||||
"Step",
|
||||
"steps",
|
||||
step_stub(name),
|
||||
),
|
||||
"provider" => create_piece(
|
||||
name,
|
||||
check,
|
||||
"providers",
|
||||
"provider",
|
||||
"Provider",
|
||||
"providers",
|
||||
provider_stub(name),
|
||||
),
|
||||
"module" => create_module(name, check),
|
||||
other => Err(Error::new(
|
||||
if kind == "module" {
|
||||
return create_module(name, check);
|
||||
}
|
||||
match PIECES.iter().find(|p| p.kind == kind) {
|
||||
Some(piece) => create_piece(name, check, piece),
|
||||
None => Err(Error::new(
|
||||
ErrorKind::InvalidInput,
|
||||
format!("unknown kind {other}"),
|
||||
format!("unknown kind {kind} (try: {})", kinds().join(", ")),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a piece file under `src/<subdir>/` and wires it into the `@Module`
|
||||
/// in `src/app.module.ts` (`module_key` array). Emit-only + hint on failure.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn create_piece(
|
||||
name: &str,
|
||||
check: bool,
|
||||
subdir: &str,
|
||||
ext: &str,
|
||||
suffix: &str,
|
||||
module_key: &str,
|
||||
stub: String,
|
||||
) -> std::io::Result<()> {
|
||||
/// in `src/app.module.ts`. Emit-only + hint on failure.
|
||||
fn create_piece(name: &str, check: bool, piece: &Piece) -> std::io::Result<()> {
|
||||
let Piece {
|
||||
subdir,
|
||||
ext,
|
||||
suffix,
|
||||
..
|
||||
} = *piece;
|
||||
let module_key = subdir;
|
||||
let lower = name.to_lowercase();
|
||||
let file = Path::new("src")
|
||||
.join(subdir)
|
||||
.join(format!("{lower}.{ext}.ts"));
|
||||
std::fs::create_dir_all(file.parent().unwrap())?;
|
||||
std::fs::write(&file, stub)?;
|
||||
std::fs::write(&file, (piece.stub)(name))?;
|
||||
println!("✓ {}", file.display());
|
||||
|
||||
let symbol = format!("{name}{suffix}");
|
||||
|
||||
@@ -81,6 +81,42 @@ console.log(lines.join('\n'));"#
|
||||
emit(dir, entry_override, out, &body)
|
||||
}
|
||||
|
||||
/// `matcha openapi` — the structural OpenAPI 3.1 document for the route table.
|
||||
///
|
||||
/// Structural is the honest word for it: green-tea derives paths, methods and
|
||||
/// parameters from the decorators, which is everything the graph knows. It does
|
||||
/// not invent response schemas, so what comes out describes the surface rather
|
||||
/// than the payloads.
|
||||
pub fn openapi(
|
||||
dir: &Path,
|
||||
title: Option<&str>,
|
||||
version: Option<&str>,
|
||||
entry_override: Option<&str>,
|
||||
out: Option<&str>,
|
||||
) -> std::io::Result<()> {
|
||||
// `app.openapi()` takes an optional info object; passing nothing lets core
|
||||
// apply its own defaults rather than having the CLI invent a title.
|
||||
let info = match (title, version) {
|
||||
(None, None) => String::new(),
|
||||
(t, v) => {
|
||||
let mut fields = Vec::new();
|
||||
if let Some(t) = t {
|
||||
fields.push(format!("title: {}", js_string(t)));
|
||||
}
|
||||
if let Some(v) = v {
|
||||
fields.push(format!("version: {}", js_string(v)));
|
||||
}
|
||||
format!("{{ {} }}", fields.join(", "))
|
||||
}
|
||||
};
|
||||
emit(
|
||||
dir,
|
||||
entry_override,
|
||||
out,
|
||||
&format!("console.log(JSON.stringify(app.openapi({info}), null, 2));"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Resolves the entry, generates the script around `body`, runs it under the
|
||||
/// detected runtime, and routes its stdout to a file or to ours.
|
||||
fn emit(
|
||||
|
||||
+17
@@ -56,6 +56,23 @@ fn main() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Command::Openapi {
|
||||
title,
|
||||
api_version,
|
||||
entry,
|
||||
out,
|
||||
} => {
|
||||
if let Err(e) = cmd_graph::openapi(
|
||||
std::path::Path::new("."),
|
||||
title.as_deref(),
|
||||
api_version.as_deref(),
|
||||
entry.as_deref(),
|
||||
out.as_deref(),
|
||||
) {
|
||||
eprintln!("error: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Command::Explain { route, entry, out } => {
|
||||
if let Err(e) = cmd_graph::explain(
|
||||
std::path::Path::new("."),
|
||||
|
||||
Reference in new issue
Block a user