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

commit e167f8de3cd2f59a7b6b495880e7cb721fe7f9e2
Author: Marat Gubaidullin <marat.gubaidul...@gmail.com>
AuthorDate: Tue Dec 13 11:31:01 2022 -0500

    Refactor run functions
---
 karavan-vscode/package.json              | 16 ------
 karavan-vscode/src/exec.ts               | 13 +++++
 karavan-vscode/src/jbang.ts              | 86 +++-----------------------------
 karavan-vscode/src/{exec.ts => maven.ts} | 21 ++++----
 karavan-vscode/src/utils.ts              | 29 +++++++++++
 5 files changed, 59 insertions(+), 106 deletions(-)

diff --git a/karavan-vscode/package.json b/karavan-vscode/package.json
index 290821f..72e3d35 100644
--- a/karavan-vscode/package.json
+++ b/karavan-vscode/package.json
@@ -45,7 +45,6 @@
     "onCommand:karavan.deploy",
     "onCommand:karavan.open",
     "onCommand:karavan.open-file",
-    "onCommand:karavan.jbang-run-file",
     "onCommand:karavan.jbang-run-project",
     "onCommand:karavan.jbang-export",
     "onCommand:karavan.generate-rest",
@@ -323,11 +322,6 @@
         "command": "karavan.open-file",
         "title": "Karavan: Open editor"
       },
-      {
-        "command": "karavan.jbang-run-file",
-        "title": "Karavan: Run File",
-        "icon": "$(run)"
-      },
       {
         "command": "karavan.jbang-run-project",
         "title": "Karavan: Run",
@@ -396,11 +390,6 @@
           "when": "resourceFilename =~ /.camel.yaml$/",
           "group": "karavan@4"
         },
-        {
-          "command": "karavan.jbang-run-file",
-          "when": "resourceFilename =~ /.camel.yaml$/",
-          "group": "karavan@5"
-        },
         {
           "command": "karavan.jbang-run-project",
           "when": "explorerResourceIsFolder || explorerResourceIsRoot",
@@ -477,11 +466,6 @@
           "when": "view == integrations && viewItem == 'integration'",
           "group": "navigation_1_open@2"
         },
-        {
-          "command": "karavan.jbang-run-file",
-          "when": "view == integrations && viewItem == 'integration'",
-          "group": "navigation_2_run@1"
-        },
         {
           "command": "karavan.generate-rest",
           "when": "view == openapi && viewItem == 'openapi'",
diff --git a/karavan-vscode/src/exec.ts b/karavan-vscode/src/exec.ts
index 7034981..afc7b29 100644
--- a/karavan-vscode/src/exec.ts
+++ b/karavan-vscode/src/exec.ts
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 import * as shell from 'shelljs';
+import { window, Terminal, ThemeIcon } from "vscode";
 
 export interface Result {
     result: boolean
@@ -27,4 +28,16 @@ export function execCommand(cmd: string, execPath?: string): 
Promise<Result> {
         if (execPath) shell.cd(execPath);
         shell.exec(cmd, (code, stdout, stderr) => resolve({ result: code === 
0, value: stdout, error: stderr }));
     });
+}
+
+const TERMINALS: Map<string, Terminal> = new Map<string, Terminal>();
+
+
+export function execTerminalCommand(terminalId: string, command: string, env?: 
{ [key: string]: string | null | undefined }) {
+    const existTerminal = TERMINALS.get(terminalId);
+    if (existTerminal) existTerminal.dispose();
+    const terminal = window.createTerminal({ name: terminalId, env: env, 
iconPath: new ThemeIcon("layers") });
+    TERMINALS.set(terminalId, terminal);
+    terminal.show();
+    terminal.sendText(command);
 }
\ No newline at end of file
diff --git a/karavan-vscode/src/jbang.ts b/karavan-vscode/src/jbang.ts
index d90bbab..12db2ef 100644
--- a/karavan-vscode/src/jbang.ts
+++ b/karavan-vscode/src/jbang.ts
@@ -14,15 +14,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-import { workspace, window, Terminal, ThemeIcon } from "vscode";
+import { workspace, window, ThemeIcon } from "vscode";
 import * as path from "path";
 import * as shell from 'shelljs';
-import { CamelDefinitionYaml } from "core/api/CamelDefinitionYaml";
 import * as utils from "./utils";
 import * as exec from "./exec";
 
-const TERMINALS: Map<string, Terminal> = new Map<string, Terminal>();
-
 export async function camelJbangGenerate(rootPath: string, openApiFullPath: 
string, fullPath: string, add: boolean, crd?: boolean, generateRoutes?: 
boolean) {
     let command = prepareCommand("generate rest -i " + openApiFullPath);
     if (generateRoutes === true) command = command + " --routes";
@@ -34,11 +31,11 @@ export async function camelJbangGenerate(rootPath: string, 
openApiFullPath: stri
             if (add) {
                 utils.readFile(fullPath).then(readData => {
                     const camelYaml = Buffer.from(readData).toString('utf8');
-                    yaml = createYaml(filename, stdout, camelYaml, undefined);
+                    yaml = utils.createYaml(filename, stdout, camelYaml, 
undefined);
                     utils.write(fullPath, yaml);
                 });
             } else {
-                yaml = createYaml(filename, stdout, undefined, crd);
+                yaml = utils.createYaml(filename, stdout, undefined, crd);
                 utils.write(fullPath, yaml);
             }
         } else {
@@ -47,31 +44,6 @@ export async function camelJbangGenerate(rootPath: string, 
openApiFullPath: stri
     });
 }
 
-export function createYaml(filename: string, restYaml: string, camelYaml?: 
string, crd?: boolean): 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;
-    }
-}
-
-export function camelJbangPackage(rootPath: string, profile: string, callback: 
(code: number) => any) {
-    executeJbangCommand(rootPath, prepareCommand("package uber-jar"), (code, 
stdout, stderr) => callback(code));
-}
-
-
-export function cacheClear(rootPath: string, callback: (code: number) => any) {
-    executeJbangCommand(rootPath, "jbang cache clear", (code, stdout, stderr) 
=> callback(code));
-}
-
 function prepareCommand(command: string): string {
     const version = workspace.getConfiguration().get("camel.version");
     return "jbang -Dcamel.jbang.version=" + version + " camel@apache/camel " + 
command;
@@ -86,12 +58,7 @@ export function camelJbangRun(filename?: string) {
         + (kameletsPath && kameletsPath.trim().length > 0 ? " 
--local-kamelet-dir=" + kameletsPath : "");
     const command = prepareCommand(cmd) + (dev === true ? " --dev" : "");
     const terminalId = "run_" + filename;
-    const existTerminal = TERMINALS.get(terminalId);
-    if (existTerminal) existTerminal.dispose();
-    const terminal = window.createTerminal('Camel run: ' + filename ? filename 
: "project");
-    TERMINALS.set(terminalId, terminal);
-    terminal.show();
-    terminal.sendText(command);
+    exec.execTerminalCommand(terminalId, command);
 }
 
 export async function camelJbangExport(fullPath: string, run?: boolean) {
@@ -101,13 +68,7 @@ export async function camelJbangExport(fullPath: string, 
run?: boolean) {
         const mvn = runtime === 'quarkus' ? "quarkus:dev" : "spring-boot:run";
         command = command.concat(" && mvn clean ").concat(mvn).concat(" -f 
").concat(fullPath);
     }
-    const terminalId = "export";
-    const existTerminal = TERMINALS.get(terminalId);
-    if (existTerminal) existTerminal.dispose();
-    const terminal = window.createTerminal('export');
-    TERMINALS.set(terminalId, terminal);
-    terminal.show();
-    terminal.sendText(command);
+    exec.execTerminalCommand("export", command);
 }
 
 export function createExportCommand(fullPath: string) {
@@ -136,34 +97,12 @@ export function camelDeploy(directory: string) {
         }
         const deployCommand: string = 
workspace.getConfiguration().get("Karavan.".concat(runtime.replaceAll("-", 
"")).concat(utils.capitalize(target)).concat("Deploy")) || '';
         const command = createExportCommand(directory).concat(" && 
").concat(deployCommand).concat(" -f ").concat(exportFolder);
-        camelRunDeploy(command, env);
+        exec.execTerminalCommand("deploy", command, env);
     }).catch((reason: any) => {
         window.showErrorMessage("Error: \n" + reason.message);
     });
 }
 
-export function camelRunDeploy(command: string, env?: { [key: string]: string 
| null | undefined }) {
-    const terminalId = "deploy";
-    const existTerminal = TERMINALS.get(terminalId);
-    if (existTerminal) existTerminal.dispose();
-    const terminal = window.createTerminal({ name: terminalId, env: env, 
iconPath: new ThemeIcon("layers") });
-    TERMINALS.set(terminalId, terminal);
-    terminal.show();
-    terminal.sendText(command);
-}
-
-export function createPackageAndPushImageCommand(directory: string) {
-    return "mvn clean package -f " + directory
-        +  " -Dquarkus.kubernetes.deploy=false"
-        +  " -Dquarkus.container-image.build=true 
-Dquarkus.container-image.push=true"
-}
-
-export function createPackageAndDeployCommand(directory: string) {
-    return "mvn clean package -f " + directory
-        +  " -Dquarkus.kubernetes.deploy=true 
-Dquarkus.container-image.registry=image-registry.openshift-image-registry.svc:5000"
-        +  " -Dquarkus.container-image.build=false 
-Dquarkus.container-image.push=false"
-}
-
 function executeJbangCommand(rootPath: string, command: string, callback: 
(code: number, stdout: any, stderr: any) => any) {
     console.log("excute command", command);
     const jbang = shell.which('jbang');
@@ -179,15 +118,4 @@ function executeJbangCommand(rootPath: string, command: 
string, callback: (code:
     }    
 }
 
-function setMinikubeEnvVariables(env: string): Map<string, string> {
-    const map = new Map<string, string>();
-    const linesAll = env.split(/\r?\n/);
-    const vars = linesAll.filter(l => l !== undefined && 
l.startsWith("export")).map(line => line.replace("export", ""));
-    vars.forEach(line => {
-        const parts = line.split("=");
-        const key = parts[0].trim();
-        const value = parts[1].replaceAll('"', '').trim();
-        map.set(key, value);
-    })
-    return map;
-}
+
diff --git a/karavan-vscode/src/exec.ts b/karavan-vscode/src/maven.ts
similarity index 57%
copy from karavan-vscode/src/exec.ts
copy to karavan-vscode/src/maven.ts
index 7034981..ebb801e 100644
--- a/karavan-vscode/src/exec.ts
+++ b/karavan-vscode/src/maven.ts
@@ -14,17 +14,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-import * as shell from 'shelljs';
 
-export interface Result {
-    result: boolean
-    value: any
-    error: string
+export function createPackageAndPushImageCommand(directory: string) {
+    return "mvn clean package -f " + directory
+        +  " -Dquarkus.kubernetes.deploy=false"
+        +  " -Dquarkus.container-image.build=true 
-Dquarkus.container-image.push=true"
+}
+
+export function createPackageAndDeployCommand(directory: string) {
+    return "mvn clean package -f " + directory
+        +  " -Dquarkus.kubernetes.deploy=true 
-Dquarkus.container-image.registry=image-registry.openshift-image-registry.svc:5000"
+        +  " -Dquarkus.container-image.build=false 
-Dquarkus.container-image.push=false"
 }
 
-export function execCommand(cmd: string, execPath?: string): Promise<Result> {
-    return new Promise<Result>((resolve) => {
-        if (execPath) shell.cd(execPath);
-        shell.exec(cmd, (code, stdout, stderr) => resolve({ result: code === 
0, value: stdout, error: stderr }));
-    });
-}
\ No newline at end of file
diff --git a/karavan-vscode/src/utils.ts b/karavan-vscode/src/utils.ts
index b02254d..bec776a 100644
--- a/karavan-vscode/src/utils.ts
+++ b/karavan-vscode/src/utils.ts
@@ -312,4 +312,33 @@ export async function createApplicationproperties(runtime: 
string, gav: string,
         }).join('\n');
         write(path.join(uriFolder.path, "application.properties"), text);
     }
+}
+
+export function createYaml(filename: string, restYaml: string, camelYaml?: 
string, crd?: boolean): 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/);
+    const vars = linesAll.filter(l => l !== undefined && 
l.startsWith("export")).map(line => line.replace("export", ""));
+    vars.forEach(line => {
+        const parts = line.split("=");
+        const key = parts[0].trim();
+        const value = parts[1].replaceAll('"', '').trim();
+        map.set(key, value);
+    })
+    return map;
 }
\ No newline at end of file

Reply via email to