greentic-extension-sdk-cli 1.2.2

gtdx: CLI for Greentic Designer Extensions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Template rendering and file writing.

use std::{collections::HashMap, fs, path::Path};

use include_dir::{Dir, include_dir};

static TEMPLATES_COMMON: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/common");
static TEMPLATES_DESIGN: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/design");
static TEMPLATES_BUNDLE: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/bundle");
static TEMPLATES_DEPLOY: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/deploy");
static TEMPLATES_PROVIDER: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/provider");
static TEMPLATES_WASM_COMPONENT: Dir<'_> =
    include_dir!("$CARGO_MANIFEST_DIR/templates/wasm-component");
static TEMPLATES_LLM: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/llm");
static TEMPLATES_MCP: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/mcp");
static TEMPLATES_OPENAPI_CONNECTOR: Dir<'_> =
    include_dir!("$CARGO_MANIFEST_DIR/templates/openapi-connector");

#[derive(Debug, Clone)]
pub struct TemplateEntry {
    pub src_bytes: &'static [u8],
    /// Destination relative path inside the project (with `.tmpl` stripped and
    /// `gitignore` renamed to `.gitignore`).
    pub dst_rel: String,
}

fn collect(dir: &'static Dir<'static>) -> Vec<TemplateEntry> {
    let mut out = Vec::new();
    collect_rec(dir, &mut out);
    out
}

fn collect_rec(dir: &'static Dir<'static>, out: &mut Vec<TemplateEntry>) {
    for entry in dir.entries() {
        match entry {
            include_dir::DirEntry::File(f) => {
                let rel = f.path().to_string_lossy().to_string();
                let dst = translate_dst(&rel);
                out.push(TemplateEntry {
                    src_bytes: f.contents(),
                    dst_rel: dst,
                });
            }
            include_dir::DirEntry::Dir(d) => collect_rec(d, out),
        }
    }
}

fn translate_dst(rel: &str) -> String {
    let mut dst = rel.trim_end_matches(".tmpl").to_string();
    if dst == "gitignore" {
        dst = ".gitignore".to_string();
    }
    // The template tree can't hold a literal `.claude/` directory — a dotfile
    // dir under the SDK's own `templates/` would be acted on by local tooling
    // (same reason `.gitignore` ships as `gitignore`). Author it under
    // `claude/` and re-dot it to `.claude/` at write time.
    if let Some(rest) = dst.strip_prefix("claude/") {
        dst = format!(".claude/{rest}");
    }
    dst
}

pub fn load_templates_common() -> Vec<TemplateEntry> {
    collect(&TEMPLATES_COMMON)
}

/// Layer `over` on top of `base`, keyed by destination path: an entry in
/// `over` replaces the `base` entry that would land at the same path, and new
/// paths are appended. Order within `base` is preserved so the generated file
/// list stays stable.
fn overlay(base: Vec<TemplateEntry>, over: Vec<TemplateEntry>) -> Vec<TemplateEntry> {
    let mut out = base;
    for entry in over {
        if let Some(slot) = out.iter_mut().find(|e| e.dst_rel == entry.dst_rel) {
            *slot = entry;
        } else {
            out.push(entry);
        }
    }
    out
}

pub fn load_templates_kind(kind: &str) -> Vec<TemplateEntry> {
    match kind {
        "design" => collect(&TEMPLATES_DESIGN),
        "bundle" => collect(&TEMPLATES_BUNDLE),
        "deploy" => collect(&TEMPLATES_DEPLOY),
        "provider" => collect(&TEMPLATES_PROVIDER),
        // `wasm-component` is a `design` extension whose describe additionally
        // declares the OCI component that executes its palette node, so it
        // reuses the design crate wholesale and overrides only the files that
        // genuinely differ. It used to carry its own two-crate workspace
        // (`extension/` + `runtime/`), which duplicated the crate, drifted
        // against the contract, and put the vendored WIT deps outside the
        // crate's target path so nothing it generated ever built.
        "wasm-component" => overlay(
            collect(&TEMPLATES_DESIGN),
            collect(&TEMPLATES_WASM_COMPONENT),
        ),
        "llm" => collect(&TEMPLATES_LLM),
        "mcp" => collect(&TEMPLATES_MCP),
        "openapi-connector" => collect(&TEMPLATES_OPENAPI_CONNECTOR),
        _ => Vec::new(),
    }
}

pub struct Context {
    values: HashMap<&'static str, String>,
}

impl Context {
    pub fn new() -> Self {
        Self {
            values: HashMap::new(),
        }
    }

    pub fn set(&mut self, key: &'static str, value: impl Into<String>) -> &mut Self {
        self.values.insert(key, value.into());
        self
    }

    pub fn render(&self, template: &str) -> anyhow::Result<String> {
        let mut out = template.to_string();
        let mut remaining_passes = 4;
        while remaining_passes > 0 {
            let before = out.clone();
            for (key, value) in &self.values {
                let token = format!("{{{{{key}}}}}");
                out = out.replace(&token, value);
            }
            if out == before {
                break;
            }
            remaining_passes -= 1;
        }
        if let Some(pos) = out.find("{{") {
            let end = out[pos..].find("}}").map_or(out.len(), |e| pos + e + 2);
            anyhow::bail!("unsubstituted placeholder: {}", &out[pos..end]);
        }
        Ok(out)
    }
}

impl Default for Context {
    fn default() -> Self {
        Self::new()
    }
}

pub fn write_file(path: &Path, bytes: &[u8]) -> anyhow::Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(path, bytes)?;
    Ok(())
}

#[allow(dead_code)] // test-only helper; kept for API ergonomics
pub fn render_and_write(ctx: &Context, template: &str, path: &Path) -> anyhow::Result<()> {
    let rendered = ctx.render(template)?;
    write_file(path, rendered.as_bytes())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn render_substitutes_placeholder() {
        let mut ctx = Context::new();
        ctx.set("name", "demo");
        let out = ctx.render("hello {{name}}!").unwrap();
        assert_eq!(out, "hello demo!");
    }

    #[test]
    fn render_multiple_placeholders() {
        let mut ctx = Context::new();
        ctx.set("name", "demo").set("version", "0.1.0");
        let out = ctx.render("{{name}}@{{version}}").unwrap();
        assert_eq!(out, "demo@0.1.0");
    }

    #[test]
    fn render_unsubstituted_placeholder_errors() {
        let ctx = Context::new();
        let err = ctx.render("hello {{missing}}").unwrap_err();
        assert!(err.to_string().contains("{{missing}}"));
    }

    #[test]
    fn render_literal_text_passthrough() {
        let ctx = Context::new();
        let out = ctx.render("plain text no braces").unwrap();
        assert_eq!(out, "plain text no braces");
    }

    #[test]
    fn write_file_creates_parent_dirs() {
        let tmp = tempfile::tempdir().unwrap();
        let dst = tmp.path().join("a/b/c/file.txt");
        write_file(&dst, b"hello").unwrap();
        assert_eq!(std::fs::read(&dst).unwrap(), b"hello");
    }

    #[test]
    fn render_and_write_substitutes_before_writing() {
        let tmp = tempfile::tempdir().unwrap();
        let dst = tmp.path().join("out.txt");
        let mut ctx = Context::new();
        ctx.set("who", "world");
        render_and_write(&ctx, "hello {{who}}", &dst).unwrap();
        assert_eq!(std::fs::read_to_string(&dst).unwrap(), "hello world");
    }

    #[test]
    fn load_common_returns_gitignore_template() {
        let entries = load_templates_common();
        assert!(
            entries
                .iter()
                .any(|e| e.dst_rel == "gitignore.tmpl" || e.dst_rel == ".gitignore")
        );
    }

    /// Every scaffold ships agent-onboarding docs so AI coding tools (Claude
    /// Code, Codex, …) pick up the build/publish workflow and the
    /// placeholder-vs-generated distinctions without re-deriving them. AGENTS.md
    /// is the universal source of truth; CLAUDE.md is a thin pointer to it.
    /// Both live in `common/`, so they apply to every kind.
    #[test]
    fn load_common_returns_agent_onboarding_docs() {
        let entries = load_templates_common();
        for expected in ["AGENTS.md", "CLAUDE.md"] {
            assert!(
                entries.iter().any(|e| e.dst_rel == expected),
                "common templates missing {expected}: {:?}",
                entries.iter().map(|e| &e.dst_rel).collect::<Vec<_>>(),
            );
        }
        // CLAUDE.md must point readers at AGENTS.md so the two never drift.
        let claude = entries
            .iter()
            .find(|e| e.dst_rel == "CLAUDE.md")
            .expect("CLAUDE.md present");
        let body = std::str::from_utf8(claude.src_bytes).expect("utf8");
        assert!(
            body.contains("AGENTS.md"),
            "CLAUDE.md must reference AGENTS.md:\n{body}",
        );
    }

    /// Every scaffold ships Claude Code project config so AI coding tools run the
    /// build/check commands without per-command permission prompts (`settings.json`)
    /// and get a one-shot pre-publish gate (`/check`). Both are authored under
    /// `claude/` and re-dotted to `.claude/` by [`translate_dst`].
    #[test]
    fn load_common_returns_dotclaude_config() {
        let entries = load_templates_common();
        for expected in [".claude/settings.json", ".claude/commands/check.md"] {
            assert!(
                entries.iter().any(|e| e.dst_rel == expected),
                "common templates missing {expected}: {:?}",
                entries.iter().map(|e| &e.dst_rel).collect::<Vec<_>>(),
            );
        }
        // settings.json must parse as JSON and pre-approve the core build commands.
        let settings = entries
            .iter()
            .find(|e| e.dst_rel == ".claude/settings.json")
            .expect(".claude/settings.json present");
        let body = std::str::from_utf8(settings.src_bytes).expect("utf8");
        let parsed: serde_json::Value = serde_json::from_str(body).expect("settings.json parses");
        let allow = parsed
            .get("permissions")
            .and_then(|p| p.get("allow"))
            .and_then(|a| a.as_array())
            .expect("permissions.allow is an array");
        assert!(
            allow.iter().any(|v| v.as_str() == Some("Bash(gtdx:*)")),
            "settings.json must pre-approve gtdx commands:\n{body}",
        );
    }

    #[test]
    fn load_kind_design_returns_cargo_toml() {
        let entries = load_templates_kind("design");
        assert!(entries.iter().any(|e| e.dst_rel == "Cargo.toml"));
        assert!(entries.iter().any(|e| e.dst_rel == "describe.json"));
        assert!(entries.iter().any(|e| e.dst_rel == "src/lib.rs"));
    }

    /// Audit P1 (E.3.a): every kind must scaffold a rust-toolchain.toml
    /// pinned to the same workspace toolchain. Without this the user's
    /// `cargo build` picks up whatever's in their PATH and silently
    /// produces wasm with the wrong feature set.
    #[test]
    fn every_kind_template_ships_rust_toolchain_pinned_to_1_95_0() {
        for kind in [
            "design",
            "bundle",
            "deploy",
            "provider",
            "wasm-component",
            "llm",
            "mcp",
        ] {
            let entries = load_templates_kind(kind);
            let toolchain = entries
                .iter()
                .find(|e| e.dst_rel == "rust-toolchain.toml")
                .unwrap_or_else(|| panic!("kind {kind} missing rust-toolchain.toml template"));
            let content = std::str::from_utf8(toolchain.src_bytes).expect("utf8");
            assert!(
                content.contains("channel = \"1.95.0\""),
                "kind {kind} toolchain template does not pin 1.95.0:\n{content}",
            );
        }
    }

    /// Audit P1 (E.3.b): extension kinds that use cargo-component's generated
    /// bindings must scaffold `wit-bindgen-rt = "0.41"` (the current pinned
    /// version for those kinds). 0.35 emits older intrinsics that newer
    /// cargo-component rejects.
    ///
    /// The `mcp` kind is exempt: it uses inline `wit_bindgen::generate!` via
    /// `wit-bindgen` (macros feature) so the crate compiles on the host as
    /// well as wasm32. It must not use `wit-bindgen-rt` directly.
    #[test]
    fn every_kind_template_ships_wit_bindgen_rt_0_41() {
        for kind in ["design", "bundle", "deploy", "provider", "llm"] {
            let entries = load_templates_kind(kind);
            let cargo_toml = entries
                .iter()
                .find(|e| e.dst_rel == "Cargo.toml")
                .unwrap_or_else(|| panic!("kind {kind} missing Cargo.toml template"));
            let content = std::str::from_utf8(cargo_toml.src_bytes).expect("utf8");
            assert!(
                content.contains("wit-bindgen-rt = { version = \"0.41\""),
                "kind {kind} Cargo.toml does not pin wit-bindgen-rt 0.41:\n{content}",
            );
        }
    }

    /// The `mcp` kind uses inline `wit_bindgen::generate!` (host-compatible
    /// bindings) and must depend on `wit-bindgen` with the `macros` feature,
    /// not the older `wit-bindgen-rt` shim.
    #[test]
    fn mcp_kind_template_ships_wit_bindgen_macros() {
        let entries = load_templates_kind("mcp");
        let cargo_toml = entries
            .iter()
            .find(|e| e.dst_rel == "Cargo.toml")
            .unwrap_or_else(|| panic!("mcp kind missing Cargo.toml template"));
        let content = std::str::from_utf8(cargo_toml.src_bytes).expect("utf8");
        assert!(
            content.contains("wit-bindgen") && content.contains("macros"),
            "mcp Cargo.toml must depend on wit-bindgen with macros feature:\n{content}",
        );
        assert!(
            !content.contains("wit-bindgen-rt"),
            "mcp Cargo.toml must NOT use wit-bindgen-rt (use wit-bindgen + macros instead):\n{content}",
        );
    }

    /// The `wasm-component` scaffold is a standalone cargo-component project
    /// with no parent workspace, so its Cargo.toml must use concrete
    /// `[package]` fields, not `edition.workspace = true` / `*.workspace`
    /// inherits — which would make `cargo build` fail on a fresh scaffold
    /// (audit cycle-2 N12).
    #[test]
    fn wasm_component_cargo_toml_has_no_workspace_inherits() {
        let entries = load_templates_kind("wasm-component");
        let cargo_toml = entries
            .iter()
            .find(|e| e.dst_rel.ends_with("Cargo.toml"))
            .expect("wasm-component must ship a Cargo.toml template");
        let content = std::str::from_utf8(cargo_toml.src_bytes).expect("utf8");
        assert!(
            !content.contains(".workspace = true") && !content.contains(".workspace=true"),
            "wasm-component Cargo.toml must not inherit from a (non-existent) workspace:\n{content}",
        );
        assert!(
            content.contains("edition = \"2024\""),
            "wasm-component Cargo.toml must pin edition concretely:\n{content}",
        );
    }

    /// Every kind's `describe.json` template must emit v2 shape — after
    /// the v1->v2 ecosystem migration (PR-series ending in greentic-biz/
    /// greentic-designer-extensions#58), scaffolded extensions that still
    /// emit `apiVersion: greentic.ai/v1` won't round-trip through the
    /// 1.2.x runtime without manual editing. This guard catches any
    /// future template that regresses to v1 shape.
    #[test]
    fn every_kind_describe_template_is_v2() {
        for kind in [
            "design",
            "bundle",
            "deploy",
            "provider",
            "wasm-component",
            "llm",
            "mcp",
        ] {
            let entries = load_templates_kind(kind);
            let describe = entries
                .iter()
                .find(|e| e.dst_rel == "describe.json")
                .unwrap_or_else(|| panic!("kind {kind} missing describe.json template"));
            let content = std::str::from_utf8(describe.src_bytes).expect("utf8");
            assert!(
                content.contains("\"apiVersion\": \"greentic.ai/v2\""),
                "kind {kind} describe.json template not v2:\n{content}",
            );
            assert!(
                content.contains("\"compat\":"),
                "kind {kind} describe.json missing `compat` block:\n{content}",
            );
            assert!(
                content.contains("\"components\":"),
                "kind {kind} describe.json missing `runtime.components` map:\n{content}",
            );
            assert!(
                !content.contains("\"component\": \"extension.wasm\""),
                "kind {kind} describe.json still has v1 singular `runtime.component`:\n{content}",
            );
        }
    }

    /// E.4.b: the `llm` template tree resolves through `load_templates_kind`
    /// and ships the canonical 5-file design-extension skeleton.
    #[test]
    fn load_kind_llm_returns_full_template_set() {
        let entries = load_templates_kind("llm");
        let names: Vec<&str> = entries.iter().map(|e| e.dst_rel.as_str()).collect();
        assert!(
            names.contains(&"Cargo.toml"),
            "missing Cargo.toml: {names:?}"
        );
        assert!(
            names.contains(&"describe.json"),
            "missing describe.json: {names:?}"
        );
        assert!(
            names.contains(&"src/lib.rs"),
            "missing src/lib.rs: {names:?}"
        );
        assert!(
            names.contains(&"wit/world.wit"),
            "missing wit/world.wit: {names:?}"
        );
        assert!(
            names.contains(&"rust-toolchain.toml"),
            "missing rust-toolchain.toml: {names:?}"
        );
    }

    /// The `mcp` template tree resolves through `load_templates_kind` and ships
    /// the single-file `wasix:mcp/router` skeleton plus the bundled `wasix-mcp`
    /// WIT dep so `cargo component build` resolves the exported router
    /// interface.
    #[test]
    fn load_kind_mcp_returns_full_template_set() {
        let entries = load_templates_kind("mcp");
        let names: Vec<&str> = entries.iter().map(|e| e.dst_rel.as_str()).collect();
        for expected in [
            "Cargo.toml",
            "describe.json",
            "src/lib.rs",
            "wit/world.wit",
            "wit/deps/wasix-mcp/package.wit",
            "rust-toolchain.toml",
        ] {
            assert!(names.contains(&expected), "missing {expected}: {names:?}");
        }
    }
}