From 81af7c1c2accd36ec9a7dafab5c1d2396326784d Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 11 Jul 2026 13:24:29 -0500 Subject: [PATCH] feat: matcha create controller with tree-sitter auto-wire + emit-only fallback Signed-off-by: Diego Resendez --- src/cmd_create.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 8 ++++++- src/wire.rs | 2 -- tests/create.rs | 23 +++++++++++++++++++ 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/cmd_create.rs create mode 100644 tests/create.rs diff --git a/src/cmd_create.rs b/src/cmd_create.rs new file mode 100644 index 0000000..5bece6a --- /dev/null +++ b/src/cmd_create.rs @@ -0,0 +1,58 @@ +//! `matcha create ` — generate a stub file for `` and +//! auto-wire it into `src/app.module.ts` via the pure text-editing helpers +//! in `wire`. +//! +//! `module` and `step` are intentionally stubbed as "not implemented yet" +//! for v1 — they will reuse the same `wire` primitives as `controller` in a +//! fast follow. + +use std::io::{Error, ErrorKind}; +use std::path::Path; + +use crate::wire; + +fn controller_stub(name: &str) -> String { + format!( +"import {{ Route, Get }} from '@green-tea/core';\n\n\ +@Route('/{lower}')\nexport class {name}Controller {{\n \ +@Get('/')\n list() {{\n return [];\n }}\n}}\n", + lower = name.to_lowercase(), name = name) +} + +pub fn run(kind: &str, name: &str, _check: bool) -> std::io::Result<()> { + match kind { + "controller" => create_controller(name), + "module" | "step" => Err(Error::new(ErrorKind::Unsupported, + format!("create {kind} not implemented yet"))), + other => Err(Error::new(ErrorKind::InvalidInput, format!("unknown kind {other}"))), + } +} + +fn create_controller(name: &str) -> std::io::Result<()> { + let file = Path::new("src/controllers") + .join(format!("{}.controller.ts", name.to_lowercase())); + std::fs::create_dir_all(file.parent().unwrap())?; + std::fs::write(&file, controller_stub(name))?; + println!("✓ {}", file.display()); + + let symbol = format!("{name}Controller"); + let from = format!("./controllers/{}.controller", name.to_lowercase()); + let module_path = Path::new("src/app.module.ts"); + if !module_path.exists() { + println!("→ add {symbol} to your @Module manually (no src/app.module.ts found)"); + return Ok(()); + } + let src = std::fs::read_to_string(module_path)?; + let imported = wire::add_import(&src, &symbol, &from); + match wire::add_to_module_array(&imported, "controllers", &symbol) { + Some(wired) => { + std::fs::write(module_path, wired)?; + println!("✓ wired {symbol} into AppModule"); + } + None => { + // agreed fallback: emit-only, do NOT corrupt the module + println!("→ could not auto-wire; add {symbol} to controllers[] in src/app.module.ts"); + } + } + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 3bae357..808e1e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod cli; +mod cmd_create; mod cmd_new; mod cmd_run; mod runtime; @@ -23,7 +24,12 @@ fn main() { std::process::exit(1); } } - Command::Create { .. } => eprintln!("create: not yet implemented"), + Command::Create { kind, name, check } => { + if let Err(e) = cmd_create::run(&kind, &name, check) { + eprintln!("error: {e}"); + std::process::exit(1); + } + } Command::Add { .. } => eprintln!("add: not yet implemented"), } } diff --git a/src/wire.rs b/src/wire.rs index de65ec0..068b47a 100644 --- a/src/wire.rs +++ b/src/wire.rs @@ -52,7 +52,6 @@ fn has_error(node: Node) -> bool { /// Inserts `import { } from '';` after the last top-level /// import statement. No-op (returns `src` unchanged) if the exact import /// line is already present, making repeated calls idempotent. -#[allow(dead_code)] pub fn add_import(src: &str, symbol: &str, from: &str) -> String { let line = format!("import {{ {symbol} }} from '{from}';"); if src.contains(&line) { @@ -84,7 +83,6 @@ pub fn add_import(src: &str, symbol: &str, from: &str) -> String { /// original text untouched — when the edited text fails to re-parse clean /// (an `ERROR`/missing node appears), which is also what happens when `key` /// doesn't exist in the module object at all (the splice can't be located). -#[allow(dead_code)] pub fn add_to_module_array(src: &str, key: &str, symbol: &str) -> Option { let tree = parser().parse(src, None).unwrap(); let (open, close, existing) = find_array(&tree.root_node(), src, key)?; diff --git a/tests/create.rs b/tests/create.rs new file mode 100644 index 0000000..fb747bf --- /dev/null +++ b/tests/create.rs @@ -0,0 +1,23 @@ +use assert_cmd::Command; +use tempfile::tempdir; + +fn seed_module(dir: &std::path::Path) { + std::fs::create_dir_all(dir.join("src/controllers")).unwrap(); + std::fs::write(dir.join("src/app.module.ts"), +"import { Module } from '@green-tea/core';\n\n\ +@Module({ controllers: [] })\nexport class AppModule {}\n").unwrap(); +} + +#[test] +fn create_controller_writes_and_wires() { + let d = tempdir().unwrap(); + seed_module(d.path()); + Command::cargo_bin("matcha").unwrap() + .current_dir(d.path()) + .args(["create", "controller", "Users"]).assert().success(); + + assert!(d.path().join("src/controllers/users.controller.ts").exists()); + let module = std::fs::read_to_string(d.path().join("src/app.module.ts")).unwrap(); + assert!(module.contains("UsersController")); + assert!(module.contains("import { UsersController }")); +}