|
28 | 28 | import org.apache.cayenne.gen.CgenConfiguration; |
29 | 29 | import org.apache.cayenne.gen.MetadataUtils; |
30 | 30 | import org.apache.cayenne.gen.ToolsUtilsFactory; |
| 31 | +import org.apache.cayenne.gen.internal.Utils; |
31 | 32 | import org.apache.cayenne.map.DataMap; |
32 | 33 | import org.apache.cayenne.mcp.project.McpProjectLoaderModule; |
33 | 34 | import org.apache.cayenne.mcp.log.McpLoggingHandler; |
@@ -157,16 +158,15 @@ public CgenRunResult run(String projectPath, String dataMapName) { |
157 | 158 | new CgenValidation(true, false, null, null, null)); |
158 | 159 | } |
159 | 160 |
|
160 | | - // Step 4 — cgen configuration present? |
| 161 | + // Step 4 — cgen configuration: use the <cgen> block stored in the DataMap, or, if there is none, |
| 162 | + // synthesize a default. This mirrors CayenneModeler and the Maven/Gradle plugins, all of which |
| 163 | + // fall back to a default config rather than failing — a missing <cgen> block is not an error. |
161 | 164 | DataChannelMetaData metaData = injector.getInstance(DataChannelMetaData.class); |
162 | 165 | CgenConfigList configList = metaData.get(dataMap, CgenConfigList.class); |
163 | | - if (configList == null || configList.getAll().isEmpty()) { |
164 | | - return validationFailed(CgenErrorCode.cgen_config_missing, |
165 | | - "DataMap '" + dataMapName + "' has no <cgen> configuration. " |
166 | | - + "Configure code generation for this DataMap in CayenneModeler before invoking the tool.", |
167 | | - new CgenValidation(true, true, false, null, null)); |
168 | | - } |
169 | | - CgenConfiguration cgenConfig = configList.getAll().getFirst(); |
| 166 | + boolean usedDefaultConfig = configList == null || configList.getAll().isEmpty(); |
| 167 | + CgenConfiguration cgenConfig = usedDefaultConfig |
| 168 | + ? defaultConfig(dataMap) |
| 169 | + : configList.getAll().getFirst(); |
170 | 170 |
|
171 | 171 | // Set the DataMap file's mtime as the timestamp so fileNeedUpdate() can detect DataMap changes correctly. |
172 | 172 | if (dataMap.getConfigurationSource() != null) { |
@@ -246,6 +246,26 @@ public CgenRunResult run(String projectPath, String dataMapName) { |
246 | 246 | ); |
247 | 247 | } |
248 | 248 |
|
| 249 | + /** |
| 250 | + * Builds a default cgen configuration for a DataMap that has no embedded {@code <cgen>} block. |
| 251 | + * Mirrors {@code CgenPanel.createDefaultCgenConfiguration} in CayenneModeler: generate every |
| 252 | + * entity and embeddable, with the destination derived from the standard Maven source layout |
| 253 | + * ({@code src/main/resources} → {@code src/main/java}, likewise for {@code test}). For projects |
| 254 | + * that don't follow the Maven layout the destination falls back to the DataMap's own directory. |
| 255 | + */ |
| 256 | + private static CgenConfiguration defaultConfig(DataMap dataMap) { |
| 257 | + Path mapDir = Utils.getRootPathForDataMap(dataMap); |
| 258 | + Path outputPath = Utils.getMavenSrcPathForPath(mapDir).map(Path::of).orElse(mapDir); |
| 259 | + |
| 260 | + CgenConfiguration config = new CgenConfiguration(); |
| 261 | + config.setDataMap(dataMap); |
| 262 | + dataMap.getObjEntities().forEach(config::loadEntity); |
| 263 | + dataMap.getEmbeddables().forEach(config::loadEmbeddable); |
| 264 | + config.setRootPath(mapDir); |
| 265 | + config.updateOutputPath(outputPath); |
| 266 | + return config; |
| 267 | + } |
| 268 | + |
249 | 269 | private static CgenRunResult validationFailed(CgenErrorCode code, String message, CgenValidation validation) { |
250 | 270 | return new CgenRunResult( |
251 | 271 | "validation_failed", |
|
0 commit comments