Skip to content

Commit

Permalink
FUSETOOLS2-2433 - provide contextual menu to create Camel file in
Browse files Browse the repository at this point in the history
Explorer

for basic commands (create route with XXX DSL), it will create the file
in the selected folder

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier committed Aug 21, 2024
1 parent f48cc8c commit e92d485
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.4.0

- Provide contextual menu `New Camel file` in Explorer. For `Create Camel Route with XXX DSL`, it creates them in the right-clicked folder.

## 1.3.0

- Update Camel Quarkus Catalog from 3.10.0 to 3.13.0
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@
"commands": [
{
"command": "camel.new.file",
"title": "Camel File",
"title": "New Camel File",
"category": "Camel",
"enablement": "false"
"enablement": "resource != undefined"
},
{
"command": "camel.jbang.routes.yaml",
Expand Down Expand Up @@ -264,6 +264,13 @@
"group": "Camel",
"when": "workspaceFolderCount != 0"
}
],
"explorer/context": [
{
"command": "camel.new.file",
"group": "Camel",
"when": "workspaceFolderCount != 0"
}
]
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/commands/AbstractNewCamelRouteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export abstract class AbstractNewCamelRouteCommand extends AbstractCamelCommand

protected fileNameInputPrompt = 'Please provide a name for the new file (without extension).';

protected async showInputBoxForFileName(): Promise<string> {
protected async showInputBoxForFileName(targetFolder? :string): Promise<string> {
const input = await window.showInputBox({
prompt: this.fileNameInputPrompt,
placeHolder: this.camelDSL?.placeHolder ?? '',
validateInput: (fileName) => {
return this.validateCamelFileName(fileName ?? '');
return this.validateCamelFileName(fileName ?? '', targetFolder);
},
});

Expand Down
6 changes: 3 additions & 3 deletions src/commands/NewCamelFileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
'use strict';

import { QuickPickItem, commands, window } from "vscode";
import { QuickPickItem, Uri, commands, window } from "vscode";
import { NewCamelRouteCommand } from "./NewCamelRouteCommand";
import { NewCamelRouteFromOpenAPICommand } from "./NewCamelRouteFromOpenAPICommand";
import { NewCamelKameletCommand } from "./NewCamelKameletCommand";
Expand All @@ -26,12 +26,12 @@ export class NewCamelFileCommand {

public static readonly ID_COMMAND_CAMEL_NEW_FILE = 'camel.new.file';

public async create(): Promise<void> {
public async create(uri: Uri): Promise<void> {
const selection = await this.showQuickPickForCamelFileType();
if (selection) {
const cmd = this.getCamelRouteCommandFromSelection(selection.label);
if(cmd){
await commands.executeCommand(cmd);
await commands.executeCommand(cmd, uri);
}
}
}
Expand Down
21 changes: 16 additions & 5 deletions src/commands/NewCamelRouteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,34 @@
*/
'use strict';

import { commands, Uri } from 'vscode';
import { commands, FileType, Uri, workspace } from 'vscode';
import { CamelInitJBangTask } from '../tasks/CamelInitJBangTask';
import { AbstractNewCamelRouteCommand } from './AbstractNewCamelRouteCommand';
import * as path from 'path';

export class NewCamelRouteCommand extends AbstractNewCamelRouteCommand {

public static ID_COMMAND_CAMEL_ROUTE_JBANG_YAML = 'camel.jbang.routes.yaml';
public static ID_COMMAND_CAMEL_ROUTE_JBANG_JAVA = 'camel.jbang.routes.java';
public static ID_COMMAND_CAMEL_ROUTE_JBANG_XML = 'camel.jbang.routes.xml';

public async create(): Promise<void> {
const input = await this.showInputBoxForFileName();
public async create(targetFolder : Uri): Promise<void> {
const input = await this.showInputBoxForFileName(targetFolder ? targetFolder.fsPath : undefined);
if(input && this.camelDSL && this.workspaceFolder) {
const fileName = this.getFullName(input, this.camelDSL.extension);
const filePath = this.computeFullPath(this.workspaceFolder.uri.fsPath, fileName);
let parentFolder;
if (targetFolder !== undefined) {
if((await workspace.fs.stat(targetFolder)).type === FileType.Directory) {
parentFolder = targetFolder.fsPath;
} else {
parentFolder = path.dirname(targetFolder.fsPath);
}
} else {
parentFolder = this.workspaceFolder.uri.fsPath;
}
const filePath = this.computeFullPath(parentFolder, fileName);

await new CamelInitJBangTask(this.workspaceFolder, fileName).execute();
await new CamelInitJBangTask(this.workspaceFolder, path.relative(this.workspaceFolder.uri.fsPath, filePath)).execute();
await commands.executeCommand('vscode.open', Uri.file(filePath));
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { TelemetryEvent, TelemetryService } from '@redhat-developer/vscode-redhat-telemetry';
import * as path from 'path';
import { ExtensionContext, StatusBarAlignment, StatusBarItem, TextEditor, commands, languages, window, workspace } from 'vscode';
import { ExtensionContext, StatusBarAlignment, StatusBarItem, TextEditor, Uri, commands, languages, window, workspace } from 'vscode';
import { DidChangeConfigurationNotification, LanguageClientOptions } from 'vscode-languageclient';
import { Executable, LanguageClient } from 'vscode-languageclient/node';
import * as telemetry from './Telemetry';
Expand Down Expand Up @@ -126,16 +126,16 @@ export async function activate(context: ExtensionContext) {
});

// Register commands for new Camel Route files - YAML DSL, Java DSL
context.subscriptions.push(commands.registerCommand(NewCamelRouteCommand.ID_COMMAND_CAMEL_ROUTE_JBANG_YAML, async () => {
await new NewCamelRouteCommand('YAML').create();
context.subscriptions.push(commands.registerCommand(NewCamelRouteCommand.ID_COMMAND_CAMEL_ROUTE_JBANG_YAML, async (uri :Uri) => {
await new NewCamelRouteCommand('YAML').create(uri);
await sendCommandTrackingEvent(NewCamelRouteCommand.ID_COMMAND_CAMEL_ROUTE_JBANG_YAML);
}));
context.subscriptions.push(commands.registerCommand(NewCamelRouteCommand.ID_COMMAND_CAMEL_ROUTE_JBANG_JAVA, async () => {
await new NewCamelRouteCommand('JAVA').create();
context.subscriptions.push(commands.registerCommand(NewCamelRouteCommand.ID_COMMAND_CAMEL_ROUTE_JBANG_JAVA, async (uri :Uri) => {
await new NewCamelRouteCommand('JAVA').create(uri);
await sendCommandTrackingEvent(NewCamelRouteCommand.ID_COMMAND_CAMEL_ROUTE_JBANG_JAVA);
}));
context.subscriptions.push(commands.registerCommand(NewCamelRouteCommand.ID_COMMAND_CAMEL_ROUTE_JBANG_XML, async () => {
await new NewCamelRouteCommand('XML').create();
context.subscriptions.push(commands.registerCommand(NewCamelRouteCommand.ID_COMMAND_CAMEL_ROUTE_JBANG_XML, async (uri :Uri) => {
await new NewCamelRouteCommand('XML').create(uri);
await sendCommandTrackingEvent(NewCamelRouteCommand.ID_COMMAND_CAMEL_ROUTE_JBANG_XML);
}));

Expand Down Expand Up @@ -186,8 +186,8 @@ export async function activate(context: ExtensionContext) {
await new TransformCamelRoutesInMultipleFilesCommand('XML').create();
await sendCommandTrackingEvent(TransformCamelRoutesInMultipleFilesCommand.ID_COMMAND_CAMEL_JBANG_TRANSFORM_ROUTES_IN_MULTIPLES_FILES_TO_XML);
}));
context.subscriptions.push(commands.registerCommand(NewCamelFileCommand.ID_COMMAND_CAMEL_NEW_FILE, async () => {
await new NewCamelFileCommand().create();
context.subscriptions.push(commands.registerCommand(NewCamelFileCommand.ID_COMMAND_CAMEL_NEW_FILE, async (uri :Uri) => {
await new NewCamelFileCommand().create(uri);
await sendCommandTrackingEvent(NewCamelFileCommand.ID_COMMAND_CAMEL_NEW_FILE);
}));

Expand Down
2 changes: 1 addition & 1 deletion src/tasks/CamelInitJBangTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { CamelJBang } from "../requirements/CamelJBang";

export class CamelInitJBangTask extends CamelJBangTask {

constructor(scope: WorkspaceFolder | TaskScope.Workspace, file: any) {
constructor(scope: WorkspaceFolder | TaskScope.Workspace, file: string) {
super(scope,
'Init Camel Route file with JBang',
new CamelJBang().init(file));
Expand Down
21 changes: 19 additions & 2 deletions src/test/suite/camel.route.command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
'use strict';

import * as path from 'path';
import * as sinon from 'sinon';
import * as vscode from 'vscode';
import { expect } from 'chai';
Expand Down Expand Up @@ -76,6 +77,17 @@ describe('Should execute Create a Camel Route command', function () {
expect(openedEditor).to.be.true;
});

it('New Camel YAML DSL file can be created - in subfolder', async function () {
const workspaceFolder :vscode.WorkspaceFolder = vscode.workspace.workspaceFolders![0];
await initNewFile(fileNameWithSpace, 'YAML DSL', vscode.Uri.file(path.posix.join(workspaceFolder.uri.fsPath, 'a sub folder')));

createdFile = await waitUntilFileIsCreated(path.posix.join('a sub folder', fullFileNameWithSpace));
expect(createdFile.fsPath).not.to.be.undefined;

const openedEditor = await waitUntilEditorIsOpened(fullFileNameWithSpace);
expect(openedEditor).to.be.true;
});

it('New Camel Java DSL file can be created', async function () {
await initNewFile(javaFileName, 'Java DSL');

Expand Down Expand Up @@ -132,6 +144,11 @@ describe('Should execute Create a Camel Route command', function () {
expect(newCamelRouteCommand.validateCamelFileName('jbangInitRoute')).to.not.be.undefined;
});

it('Validate file already exists in subfolder', function () {
const workspaceFolder :vscode.WorkspaceFolder = vscode.workspace.workspaceFolders![0];
expect(newCamelRouteCommand.validateCamelFileName('jbangInitRouteInSubFolder', path.posix.join(workspaceFolder.uri.fsPath, 'a sub folder'))).to.not.be.undefined;
});

it('Validate special characters', function () {
expect(newCamelRouteCommand.validateCamelFileName('spe<ia|')).to.not.be.undefined;
});
Expand Down Expand Up @@ -172,9 +189,9 @@ describe('Should execute Create a Camel Route command', function () {

});

async function initNewFile(name: string, dsl: string): Promise<void> {
async function initNewFile(name: string, dsl: string, targetFolder?: vscode.Uri): Promise<void> {
showQuickPickStub.resolves({ label: dsl });
showInputBoxStub.resolves(name);
await vscode.commands.executeCommand(NewCamelFileCommand.ID_COMMAND_CAMEL_NEW_FILE);
await vscode.commands.executeCommand(NewCamelFileCommand.ID_COMMAND_CAMEL_NEW_FILE, targetFolder);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# camel-k: language=yaml

# Write your routes here, for example:
- from:
uri: "timer:yaml"
parameters:
period: "1000"
steps:
- setBody:
constant: "Hello Camel from yaml"
- log: "${body}"

0 comments on commit e92d485

Please sign in to comment.