This is an automated email from the ASF dual-hosted git repository.

marat pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-karavan.git


The following commit(s) were added to refs/heads/main by this push:
     new 3df4c3fc Fix #414
3df4c3fc is described below

commit 3df4c3fc470820a1f34de5e24689901e8f0e13b2
Author: Marat Gubaidullin <[email protected]>
AuthorDate: Tue Jan 17 20:13:19 2023 -0500

    Fix #414
---
 karavan-core/src/core/api/CamelDefinitionYaml.ts | 25 ++++++++++++++++++++++++
 karavan-vscode/src/jbang.ts                      | 17 ++++++++--------
 karavan-vscode/src/openapiView.ts                |  9 ++++++---
 karavan-vscode/src/utils.ts                      | 17 +---------------
 4 files changed, 40 insertions(+), 28 deletions(-)

diff --git a/karavan-core/src/core/api/CamelDefinitionYaml.ts 
b/karavan-core/src/core/api/CamelDefinitionYaml.ts
index 3d96f2be..8b9af809 100644
--- a/karavan-core/src/core/api/CamelDefinitionYaml.ts
+++ b/karavan-core/src/core/api/CamelDefinitionYaml.ts
@@ -228,4 +228,29 @@ export class CamelDefinitionYaml {
         }
         return properties;
     }
+
+   // add generated Integration YAML into existing Integration YAML
+    static addYamlToIntegrationYaml = (filename: string, camelYaml: string | 
undefined, restYaml: string, addREST: boolean, addRoutes: boolean): string => {
+        const existing = camelYaml != undefined ? 
CamelDefinitionYaml.yamlToIntegration(filename, camelYaml) : 
Integration.createNew(filename);
+        const generated = CamelDefinitionYaml.yamlToIntegration(filename, 
restYaml);
+
+        const flows: CamelElement [] = existing.spec.flows?.filter(f => 
!['RouteDefinition', 'RestDefinition'].includes(f.dslName)) || [];
+
+        const restE: CamelElement [] = existing.spec.flows?.filter(f => 
f.dslName === 'RestDefinition') || [];
+        const restG: CamelElement []  = generated.spec.flows?.filter(f => 
f.dslName === 'RestDefinition') || [];
+        if (addREST) {
+            flows.push(...restG);
+        } else {
+            flows.push(...restE)
+        }
+        const routeE: CamelElement [] = existing.spec.flows?.filter(f => 
f.dslName === 'RouteDefinition') || [];
+        const routeG: CamelElement []  = generated.spec.flows?.filter(f => 
f.dslName === 'RouteDefinition') || [];
+        if (addRoutes) {
+            flows.push(...routeG);
+        } else {
+            flows.push(...routeE)
+        }
+        existing.spec.flows = flows;
+        return CamelDefinitionYaml.integrationToYaml(existing);
+    }
 }
diff --git a/karavan-vscode/src/jbang.ts b/karavan-vscode/src/jbang.ts
index 34722e31..f7c98af1 100644
--- a/karavan-vscode/src/jbang.ts
+++ b/karavan-vscode/src/jbang.ts
@@ -19,23 +19,22 @@ import * as path from "path";
 import * as shell from 'shelljs';
 import * as utils from "./utils";
 import * as exec from "./exec";
+import { CamelDefinitionYaml } from "core/api/CamelDefinitionYaml";
 
-export async function camelJbangGenerate(rootPath: string, openApiFullPath: 
string, fullPath: string, add: boolean, generateRoutes?: boolean) {
+export async function camelJbangGenerate(rootPath: string, openApiFullPath: 
string, fullPath: string, add: boolean, generateRoutes: boolean, generateRest: 
boolean) {
     let command = prepareCommand("generate rest -i " + openApiFullPath);
     if (generateRoutes === true) command = command + " --routes";
-    executeJbangCommand(rootPath, command, (code, stdout, stderr) => {
+    executeJbangCommand(rootPath, command, async (code, stdout, stderr) => {
         console.log('Exit code:', code);
         if (code === 0) {
             const filename = path.basename(fullPath);
-            let yaml;
             if (add) {
-                utils.readFile(fullPath).then(readData => {
-                    const camelYaml = Buffer.from(readData).toString('utf8');
-                    yaml = utils.createYaml(filename, stdout, camelYaml);
-                    utils.write(fullPath, yaml);
-                });
+                const readData = await utils.readFile(fullPath);
+                const sourceYaml = Buffer.from(readData).toString('utf8');
+                const yaml = 
CamelDefinitionYaml.addYamlToIntegrationYaml(filename, sourceYaml, stdout, 
generateRest, generateRoutes);
+                utils.write(fullPath, yaml);
             } else {
-                yaml = utils.createYaml(filename, stdout, undefined);
+                const yaml = 
CamelDefinitionYaml.addYamlToIntegrationYaml(filename, undefined, stdout, 
generateRest, generateRoutes);
                 utils.write(fullPath, yaml);
             }
         } else {
diff --git a/karavan-vscode/src/openapiView.ts 
b/karavan-vscode/src/openapiView.ts
index 21c94033..89cf70b1 100644
--- a/karavan-vscode/src/openapiView.ts
+++ b/karavan-vscode/src/openapiView.ts
@@ -94,13 +94,16 @@ export class OpenApiItem extends TreeItem {
  * Select routes generation
  */
 export async function selectRouteGeneration(rootPath: string, openApiFullPath: 
string, fullPath: string, add: boolean) {
-       const options = ["Generate REST and Routes", 'Generate REST only'];
+       const options = ["Generate REST and Routes", 'Generate REST only', 
'Generate Routes only'];
        await window.showQuickPick(options, {
                title: "Generate route stubs for REST API",
                placeHolder: 'Select option',
        }).then(option => {
-               const generateRoutes: boolean = option !== undefined && option 
=== options[0];
-               jbang.camelJbangGenerate(rootPath, openApiFullPath, fullPath, 
add, generateRoutes);
+               switch (option) {
+                       case options[0]: jbang.camelJbangGenerate(rootPath, 
openApiFullPath, fullPath, add, true, true); break;
+                       case options[1]: jbang.camelJbangGenerate(rootPath, 
openApiFullPath, fullPath, add, false, true); break;
+                       case options[2]: jbang.camelJbangGenerate(rootPath, 
openApiFullPath, fullPath, add, true, false); break;
+               }
        });
 }
 
diff --git a/karavan-vscode/src/utils.ts b/karavan-vscode/src/utils.ts
index 4dce88e4..551db383 100644
--- a/karavan-vscode/src/utils.ts
+++ b/karavan-vscode/src/utils.ts
@@ -17,6 +17,7 @@
 import * as path from "path";
 import { workspace, Uri, window, ExtensionContext, FileType } from "vscode";
 import { CamelDefinitionYaml } from "core/api/CamelDefinitionYaml";
+import { Integration } from "core/model/IntegrationDefinition";
 
 export function getRoot(): string | undefined {
     return (workspace.workspaceFolders && (workspace.workspaceFolders.length > 
0))
@@ -341,22 +342,6 @@ export async function createApplicationGitignore() {
     }
 }
 
-export function createYaml(filename: string, restYaml: string, camelYaml?: 
string): string {
-    if (camelYaml) {
-        const i = CamelDefinitionYaml.yamlToIntegration(filename, camelYaml);
-        const rest = CamelDefinitionYaml.yamlToIntegration(filename, restYaml);
-        i.spec.flows = i.spec.flows?.filter(f => f.dslName !== 
'RestDefinition');
-        i.spec.flows?.push(...rest.spec.flows || []);
-        return CamelDefinitionYaml.integrationToYaml(i);
-    // } else if (crd === true) {
-        // const i = CamelDefinitionYaml.yamlToIntegration(filename, restYaml);
-        // i.type = 'crd';
-        // return CamelDefinitionYaml.integrationToYaml(i);
-    } else {
-        return restYaml;
-    }
-}
-
 function setMinikubeEnvVariables(env: string): Map<string, string> {
     const map = new Map<string, string>();
     const linesAll = env.split(/\r?\n/);

Reply via email to