Skip to content

Commit

Permalink
Crete Kamelet in VS Code #315
Browse files Browse the repository at this point in the history
  • Loading branch information
mgubaidullin committed Oct 31, 2023
1 parent 38236be commit 4604989
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
16 changes: 16 additions & 0 deletions karavan-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
],
"activationEvents": [
"onCommand:karavan.create-yaml",
"onCommand:karavan.create-kamelet",
"onCommand:karavan.create-application",
"onCommand:karavan.deploy",
"onCommand:karavan.open",
Expand Down Expand Up @@ -470,6 +471,11 @@
"title": "Karavan: Create Integration",
"icon": "$(add)"
},
{
"command": "karavan.create-kamelet",
"title": "Karavan: Create Kamelet",
"icon": "$(add)"
},
{
"command": "karavan.create-application",
"title": "Karavan: Create Application",
Expand Down Expand Up @@ -539,6 +545,11 @@
"when": "explorerResourceIsFolder || explorerResourceIsRoot",
"group": "karavan@2"
},
{
"command": "karavan.create-kamelet",
"when": "explorerResourceIsFolder || explorerResourceIsRoot",
"group": "karavan@2"
},
{
"command": "karavan.topology",
"when": "explorerResourceIsFolder || explorerResourceIsRoot || resourceFilename =~ /.camel.yaml$/",
Expand Down Expand Up @@ -613,6 +624,11 @@
"when": "view == integrations",
"group": "navigation@0"
},
{
"command": "karavan.create-kamelet",
"when": "view == integrations",
"group": "navigation@0"
},
{
"command": "karavan.run-project-jbang",
"when": "view == integrations",
Expand Down
28 changes: 22 additions & 6 deletions karavan-vscode/src/designerView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {Uri, window, commands, WebviewPanel, ExtensionContext, ViewColumn, Webvi
import * as path from "path";
import * as utils from "./utils";
import { CamelDefinitionYaml } from "core/api/CamelDefinitionYaml";
import { Integration } from "core/model/IntegrationDefinition";
import { Integration, KameletTypesArray, KameletTypes, Metadata, MetadataLabels } from "core/model/IntegrationDefinition";
import { getWebviewContent } from "./webviewContent";

const KARAVAN_LOADED = "karavan:loaded";
Expand Down Expand Up @@ -58,11 +58,24 @@ export class DesignerView {
}

createIntegration(type: 'crd' | 'plain' | 'kamelet', rootPath?: string) {
if (type === 'kamelet') {
const kameletTypes = ["sink", "source", "action"];
window.showQuickPick(kameletTypes, { title: "Select Type", canPickMany: false }).then((kameletType) => {
if (kameletType) {
this.inputIntegrationName(type, rootPath, (kameletType as KameletTypes));
}
})
} else {
this.inputIntegrationName(type, rootPath);
}
}

inputIntegrationName(type: 'crd' | 'plain' | 'kamelet', rootPath?: string, kameletType?: KameletTypes) {
window
.showInputBox({
title: type === 'crd' ? "Create Camel Integration CRD" : "Create Camel Integration YAML",
title: type === 'kamelet' ? 'Create Kamelet' : "Create Integration",
ignoreFocusOut: true,
prompt: "Integration name",
prompt: type === 'kamelet' ? 'Kamelet Name' : "Integration name",
validateInput: (text: string): string | undefined => {
if (!text || text.length === 0) {
return 'Name should not be empty';
Expand All @@ -72,11 +85,14 @@ export class DesignerView {
}
}).then(value => {
if (value) {
const name = utils.nameFromTitle(value);
const i = Integration.createNew(name);
const name = utils.nameFromTitle(type, value, kameletType);
const filename = utils.fileNameFromName(type, name, kameletType);
const i:Integration = Integration.createNew(name, type);
if (type === 'kamelet' && i.metadata && kameletType) {
i.metadata.labels = new MetadataLabels({"camel.apache.org/kamelet.type": kameletType});
}
i.type = type;
const yaml = CamelDefinitionYaml.integrationToYaml(i);
const filename = name.toLocaleLowerCase().endsWith('.camel.yaml') ? name : name.split('.')[0] + '.camel.yaml';
const relativePath = (this.rootPath ? rootPath?.replace(this.rootPath, "") : rootPath) + path.sep + filename;
const fullPath = (rootPath ? rootPath : this.rootPath) + path.sep + filename;
utils.save(relativePath, yaml);
Expand Down
10 changes: 9 additions & 1 deletion karavan-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ export function activate(context: ExtensionContext) {
});
context.subscriptions.push(topologyCommand);

// Create new Integration YAML command
// Create new Integration command
const createYaml = commands.registerCommand("karavan.create-yaml", (...args: any[]) => {
designer.createIntegration("plain", args[0]?.fsPath)
});
context.subscriptions.push(createYaml);


// Create new Kamelet command
const createKamelet = commands.registerCommand("karavan.create-kamelet", (...args: any[]) => {
designer.createIntegration("kamelet", args[0]?.fsPath)
});
context.subscriptions.push(createKamelet);


// Open integration in designer command
const open = commands.registerCommand("karavan.open", (...args: any[]) => {
designer.karavanOpen(args[0].fsPath, args[0].tab);
Expand Down
2 changes: 1 addition & 1 deletion karavan-vscode/src/openapiView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export async function inputFileName(rootPath?: string, openApi?: OpenApiItem) {
}
}).then(value => {
if (value && openApi?.fsPath && rootPath) {
const name = utils.nameFromTitle(value);
const name = utils.nameFromTitle('plain', value);
const filename = name.toLocaleLowerCase().endsWith('.camel.yaml') ? name : name.split('.')[0] + '.camel.yaml';
const fullPath = rootPath + path.sep + filename;
selectRouteGeneration(rootPath, openApi.fsPath, fullPath, false);
Expand Down
21 changes: 18 additions & 3 deletions karavan-vscode/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +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";
import { KameletTypes } from "webview/core/model/IntegrationDefinition";

export function getRoot(): string | undefined {
return (workspace.workspaceFolders && (workspace.workspaceFolders.length > 0))
Expand Down Expand Up @@ -152,8 +152,23 @@ export function toCliFilename(filename: string): string {
: filename.replace(/\s/g, "\\ ");
}

export function nameFromTitle(title: string): string {
return title.replace(/[^a-z0-9+]+/gi, "-").toLowerCase();
export function nameFromTitle(type: 'crd' | 'plain' | 'kamelet', title: string, kameletType?: KameletTypes): string {
title = title.replace(/[^a-z0-9+]+/gi, "-").toLowerCase();
if (type === 'kamelet') {
const suffix = '-' + kameletType;
return title.toLocaleLowerCase().endsWith(suffix) ? title : title + suffix;
} else {
return title;
}
}

export function fileNameFromName(type: 'crd' | 'plain' | 'kamelet', name: string, kameletType?: KameletTypes): string {
if (type === 'kamelet') {
const suffix = '.kamelet.yaml'
return name.toLocaleLowerCase().endsWith(suffix) ? name : name.split('.')[0] + suffix;
} else {
return name.toLocaleLowerCase().endsWith('.camel.yaml') ? name : name.split('.')[0] + '.camel.yaml';
}
}

export async function getAllFiles(dirPath, arrayOfFiles: string[]) {
Expand Down

0 comments on commit 4604989

Please sign in to comment.