Skip to content

Commit 244fdb7

Browse files
committed
ai-plugin - clarifying cgen flow
1 parent 39bc9c1 commit 244fdb7

2 files changed

Lines changed: 47 additions & 28 deletions

File tree

ai-plugin/references/cgen-config.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Note the **different namespace** for `<cgen>` (`http://cayenne.apache.org/schema
4949

5050
| Field | Default | Meaning |
5151
|---|---|---|
52-
| `<destDir>` || Output directory, **relative to the project XML directory** (the directory containing `cayenne-*.xml`). Typical values: `.`, `../java`, `../../main/java`. Absolute paths also work. |
52+
| `<destDir>` || Output directory, **relative to the directory containing `*.map.xml`**. Typical values: `.`, `../java`, `../../main/java`. |
5353
| `<mode>` | `entity` | What to generate. `entity` = per-entity classes. `all` = per-entity classes + a single DataMap class with named-query helpers. |
5454

5555
### Pairs vs single class
@@ -107,13 +107,45 @@ The MCP tool:
107107
1. Loads the project at `projectPath`.
108108
2. Finds the named DataMap (`dataMap` argument).
109109
3. Reads its embedded `<cgen>` block.
110-
4. Resolves `destDir` relative to the project file.
110+
4. Resolves `destDir` relative to the `*.map.xml` file.
111111
5. Runs the generator. Returns a JSON object listing files `written`, files `skipped` (already up-to-date), and any `errors`.
112112

113113
If the DataMap has no `<cgen>` block, the tool returns an error and the user must add one — typically via the `cayenne-modeling` skill or the Modeler GUI.
114114

115-
## Recommended starting config
115+
## Determining `destDir`
116+
117+
`destDir` is relative to the directory that contains `*.map.xml`.
118+
119+
**Before writing the default config, inspect the project tree** to find the Java source root and express it as a relative path from the map file's directory. Do not blindly paste `../java`.
120+
121+
### Standard Maven layout
122+
123+
Map files are frequently placed in a package subdirectory under `resources/` rather than directly in `resources/` itself. Count the directory levels between `resources/` and the map file and add that many extra `../` segments.
124+
125+
| Map file location | `destDir` |
126+
|---|---|
127+
| `src/main/resources/mydb.map.xml` | `../java` |
128+
| `src/main/resources/com/mydb.map.xml` | `../../java` |
129+
| `src/main/resources/com/example/mydb.map.xml` | `../../../java` |
130+
| `src/main/resources/com/example/app/mydb.map.xml` | `../../../../java` |
131+
| `src/test/resources/mydb.map.xml` | `../java` |
132+
| `src/test/resources/com/example/app/mydb.map.xml` | `../../../../java` |
133+
134+
The rule: one `../` to escape `resources/`, then one `../` per package segment, then `java`. Always verify by resolving the resulting path mentally before writing it.
116135

136+
### Non-standard layouts
137+
138+
If the project doesn't follow Maven conventions, locate the actual Java sources:
139+
140+
1. Look for existing `.java` files or a `src/` directory near the map file.
141+
2. Compute the relative path from the map file's directory to that sources root.
142+
3. If no Java sources exist yet, ask the user where generated classes should go.
143+
144+
### Distinguishing main vs test maps
145+
146+
If the map file lives under a test resource directory (`src/test/resources/`, `test/`, or a path containing `test`), the generated classes belong in the test source tree. Point `destDir` there rather than at the main sources — test entity classes mixed into `src/main/java/` are a common mistake.
147+
148+
## Recommended starting config
117149

118150
```xml
119151
<cgen xmlns="http://cayenne.apache.org/schema/12/cgen">
@@ -125,10 +157,12 @@ If the DataMap has no `<cgen>` block, the tool returns an error and the user mus
125157
</cgen>
126158
```
127159

128-
Assuming the project XML lives at `src/main/resources/cayenne-mydb.xml`, this writes to `src/main/java/<package>/`.
160+
The `../java` value above assumes a standard Maven layout (`src/main/resources/` or `src/test/resources/`). Adjust if the project uses a non-standard layout — see "Determining `destDir`" above.
129161

130162
## Anti-patterns
131163

132164
- Setting `<makePairs>false</makePairs>` to "simplify" — you lose the ability to add user code without it being overwritten on the next cgen run.
133165
- Editing `_<Entity>.java` files (the superclasses). They are regenerated. Customize `<Entity>.java` (the subclass) instead.
134166
- `<destDir>` as an absolute path checked into source — breaks reproducibility for other developers. Use a relative path.
167+
- Pasting `../java` blindly without checking the actual project layout. Inspect the tree first; see "Determining `destDir`" above.
168+
- Pointing a test DataMap's `destDir` at `src/main/java/` — test entity classes belong in the test source tree.

ai-plugin/skills/cayenne-cgen/SKILL.md

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ description: "Use this skill whenever the user wants to (re)generate Cayenne ent
2323
-->
2424
# cayenne-cgen
2525

26-
Run Cayenne's class generator on a DataMap via the `mcp__cayenne__cgen_run` MCP tool. Reads the embedded `<cgen>` block in the DataMap to determine destination, mode, templates, etc.
26+
Run Cayenne's class generator on a DataMap via the `mcp__cayenne__cgen_run` MCP tool. The tool reads the embedded `<cgen>` block in the DataMap to determine destination, mode, templates, etc.; if no block is present the tool reports an error and the skill adds a default one then retries.
2727

2828
## Required reading
2929

@@ -42,26 +42,7 @@ Locate the descriptor via `project-layout.md`. If multiple descriptors exist, as
4242

4343
If the user named the DataMap directly (e.g. "regenerate classes for the customers DataMap"), use that. If they said "regenerate everything" and there are multiple DataMaps, run `cgen_run` once per DataMap in sequence (the tool generates per-DataMap).
4444

45-
## Step 2 — Verify `<cgen>` block exists
46-
47-
Before calling the tool, check the target DataMap for a `<cgen xmlns="http://cayenne.apache.org/schema/12/cgen">` block. If missing:
48-
49-
1. Don't call `cgen_run` blindly — it will return an error.
50-
2. Either:
51-
- **Add a minimal block** by delegating to the `cayenne-modeling` skill. Use the starter config from `cgen-config.md`:
52-
```xml
53-
<cgen xmlns="http://cayenne.apache.org/schema/12/cgen">
54-
<destDir>../java</destDir>
55-
<mode>entity</mode>
56-
<makePairs>true</makePairs>
57-
<usePkgPath>true</usePkgPath>
58-
<createPKProperties>true</createPKProperties>
59-
</cgen>
60-
```
61-
Confirm `destDir` with the user — it's relative to the project XML directory and decides where generated `.java` files land.
62-
- Or, if the user prefers the GUI, suggest the `cayenne-modeler` skill and tell them to configure the DataMap's "Class Generation" tab.
63-
64-
## Step 3 — Call `cgen_run`
45+
## Step 2 — Call `cgen_run`
6546

6647
```
6748
mcp__cayenne__cgen_run({
@@ -72,7 +53,11 @@ mcp__cayenne__cgen_run({
7253

7354
If the tool is not available (MCP server not registered), surface `cayenne-mcp-server/README.md` and stop. **Do not** suggest `mvn cayenne:cgen` or the Gradle cgen task.
7455

75-
## Step 4 — Surface the result
56+
If the tool errors because no `<cgen>` block is present in the DataMap, add a minimal one using the starting config approach from `cgen-config.md`
57+
58+
If the `destDir` can't be determined reliably (non-standard project layout, empty project, etc.), conform it with the user
59+
60+
## Step 3 — Surface the result
7661

7762
The tool returns structured JSON. Report:
7863

@@ -82,14 +67,14 @@ The tool returns structured JSON. Report:
8267

8368
If `writtenCount` is 0 and `skippedCount` covers everything, say so — it means everything is already up-to-date and no work was needed.
8469

85-
## Step 5 — Next steps
70+
## Step 4 — Next steps
8671

8772
- If new `_<Entity>.java` superclass files were generated, gently remind the user not to edit those — they will be overwritten next run. User code goes in the matching `<Entity>.java` subclass.
8873
- If the user just ran reverse engineering, this skill is the natural follow-up. Cross-link back to `cayenne-modeling` if they need to tweak names/types before regenerating.
8974

9075
## Anti-patterns
9176

92-
- **Do not** call `cgen_run` without verifying the `<cgen>` block exists — the tool returns an error and confuses the user.
77+
- **Do not** pre-check the DataMap for a `<cgen>` block before calling `cgen_run`. Call the tool first; add the default config only if the tool reports it is missing.
9378
- **Do not** suggest Maven (`mvn cayenne:cgen`) or Gradle (`cayenneCgen`) goals when MCP is unavailable. Those build plugins are out of scope. Point at MCP setup instead.
9479
- **Do not** edit `_<Entity>.java` files. Generated superclasses. Edit `<Entity>.java` subclasses.
9580
- **Do not** confuse `projectPath` and `dataMap` arguments. `projectPath` is a file system path to `cayenne-*.xml`. `dataMap` is a logical name (e.g. `mydb`), not a path to `mydb.map.xml`.

0 commit comments

Comments
 (0)