Skip to content

Commit

Permalink
FUSETOOLS2-2113 - Allows selection of folder when creating projects
Browse files Browse the repository at this point in the history
- Removes "enablement" for the project creation commands because we will
  be always choosing a folder work with.
- Project creation commands do not depend on the workspace root anymore
  and can be used without one.
- Refactor existing tests to use the additional folder selection dialog.
- Opens a new VSCode instance in the newly created project's folder.

Signed-off-by: Marcelo Henrique Diniz de Araujo <[email protected]>
  • Loading branch information
hdamarcelo committed Oct 5, 2024
1 parent 89822f0 commit 6b37f60
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 42 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change Log

## 1.6.0
- Provide folder selection when using `Create Camel Quarkus/SpringBoot Project` command

## 1.5.0

Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,12 @@
{
"command": "camel.jbang.project.quarkus.new",
"title": "Create a Camel Quarkus project",
"category": "Camel",
"enablement": "workspaceFolderCount != 0"
"category": "Camel"
},
{
"command": "camel.jbang.project.springboot.new",
"title": "Create a Camel on SpringBoot project",
"category": "Camel",
"enablement": "workspaceFolderCount != 0"
"category": "Camel"
},
{
"command": "camel.jbang.routes.yaml.fromopenapi",
Expand Down
71 changes: 42 additions & 29 deletions src/commands/NewCamelProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,35 @@
*/
'use strict';

import { Uri, WorkspaceFolder, window, workspace } from "vscode";
import { CamelExportJBangTask } from "../tasks/CamelExportJBangTask";
import * as path from "path";
import { TaskScope, Uri, commands, window, workspace } from "vscode";
import { CamelExportJBangTask } from "../tasks/CamelExportJBangTask";

export abstract class NewCamelProjectCommand {

async create() {
const input = await this.askForGAV();
if (input) {
let workspaceFolder: WorkspaceFolder | undefined;
if (workspace.workspaceFolders) {
// default to root workspace folder
workspaceFolder = workspace.workspaceFolders[0];

// Uses the user selected folder otherwise default to the root workspace folder
const outputFolder = await this.showDialogToPickFolder();
if (!outputFolder) {
await window.showErrorMessage('Operation canceled or invalid folder selection');
return;
}

const runtime = this.getRuntime();
if(workspaceFolder){
await new CamelExportJBangTask(workspaceFolder, input, runtime).execute();
}
await new CamelExportJBangTask(TaskScope.Workspace, input, runtime, outputFolder.fsPath).execute();

// if not exist, init .vscode with tasks.json and launch.json config files
await this.initFolder('.vscode');
await workspace.fs.createDirectory(Uri.file(path.join(outputFolder.fsPath, '.vscode')));
for (const filename of ['tasks', 'launch']) {
await this.copyFile(`../../../resources/${runtime}/${filename}.json`, `.vscode/${filename}.json`);
await this.copyFile(`../../../resources/${runtime}/${filename}.json`, path.join(outputFolder.fsPath, `.vscode/${filename}.json`));
}

// open the newly created project in a new vscode instance
await commands.executeCommand('vscode.openFolder', outputFolder, true);

}
}

Expand Down Expand Up @@ -81,35 +87,43 @@ export abstract class NewCamelProjectCommand {
}
for (const groupidSubPart of groupIdSplit) {
const regExpSearch = /^[a-z]\w*$/.exec(groupidSubPart);
if(regExpSearch === null || regExpSearch.length === 0) {
if (regExpSearch === null || regExpSearch.length === 0) {
return `Invalid subpart of group Id: ${groupidSubPart}} . It must follow groupId:artifactId:version pattern with group Id subpart separated by dot needs to follow this specific pattern: [a-zA-Z]\\w*`;
}
}

const artifactId = gavs[1];
const regExpSearchArtifactId = /^[a-zA-Z]\w*$/.exec(artifactId);
if(regExpSearchArtifactId === null || regExpSearchArtifactId.length === 0) {
if (regExpSearchArtifactId === null || regExpSearchArtifactId.length === 0) {
return `Invalid artifact Id: ${artifactId}} . It must follow groupId:artifactId:version pattern with artifactId specific pattern: [a-zA-Z]\\w*`;
}

const version = gavs[2];
const regExpSearch = /^\d[\w-.]*$/.exec(version);
if(regExpSearch === null || regExpSearch.length === 0) {
if (regExpSearch === null || regExpSearch.length === 0) {
return `Invalid version: ${version} . It must follow groupId:artifactId:version pattern with version specific pattern: \\d[\\w-.]*`;
}
return undefined;
}

/**
* Create a new folder inside the root of vscode workspace
* Open a dialog to select a folder to create the project in.
*
* @param folder Name of the folder
* @returns Uri of the selected folder or undefined if canceled by the user.
*/
private async initFolder(folder: string): Promise<void> {
if (workspace.workspaceFolders){
const wsPath = workspace.workspaceFolders[0].uri.fsPath;
await workspace.fs.createDirectory(Uri.file(path.join(wsPath, folder)));
private async showDialogToPickFolder(): Promise<Uri | undefined> {
const selectedFolders = await window.showOpenDialog(
{
canSelectMany: false,
canSelectFolders: true,
canSelectFiles: false,
openLabel: 'Select',
title: 'Select a folder to create the project in. ESC to cancel the project creation'
});
if (selectedFolders !== undefined) {
return selectedFolders[0];
}
return undefined;
}

/**
Expand All @@ -119,15 +133,14 @@ export abstract class NewCamelProjectCommand {
* @param destPath Path of destination
*/
private async copyFile(sourcePath: string, destPath: string): Promise<void> {
if (workspace.workspaceFolders){
const wsPath = workspace.workspaceFolders[0].uri.fsPath;
const sourcePathUri = Uri.file(path.resolve(__dirname, sourcePath));
const destPathUri = Uri.file(path.join(wsPath, destPath));
try {
await workspace.fs.copy(sourcePathUri, destPathUri, { overwrite: false });
} catch (error) {
// Do nothing in case there already exists tasks.json and launch.json files
}

const sourcePathUri = Uri.file(path.resolve(__dirname, sourcePath));
const destPathUri = Uri.file(path.resolve(__dirname, destPath));
try {
await workspace.fs.copy(sourcePathUri, destPathUri, { overwrite: false });
} catch (error) {
// Do nothing in case there already exists tasks.json and launch.json files
}

}
}
10 changes: 8 additions & 2 deletions src/requirements/CamelJBang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ export class CamelJBang {
return new ShellExecution('jbang', [`'-Dcamel.jbang.version=${this.camelVersion}'`, 'camel@apache/camel', 'bind', '--source', source, '--sink', sink, `'${file}'`]);
}

public createProject(gav: string, runtime: string): ShellExecution {
return new ShellExecution('jbang', [`'-Dcamel.jbang.version=${this.camelVersion}'`, 'camel@apache/camel', 'export', `--runtime=${runtime}`, `--gav=${gav}`]);
public createProject(gav: string, runtime: string, outputPath: string): ShellExecution {
return new ShellExecution('jbang',
[`'-Dcamel.jbang.version=${this.camelVersion}'`,
'camel@apache/camel',
'export',
`--runtime=${runtime}`,
`--gav=${gav}`,
`'--directory=${outputPath}'`]);
}

public generateRest(routefile: string, openApiFile: string): ShellExecution {
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/CamelExportJBangTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { CamelJBang } from "../requirements/CamelJBang";

export class CamelExportJBangTask extends CamelJBangTask {

constructor(scope: WorkspaceFolder | TaskScope.Workspace, gav: string, runtime: string) {
constructor(scope: WorkspaceFolder | TaskScope.Workspace, gav: string, runtime: string, outputPath: string) {
super(scope,
'Create a Camel project',
new CamelJBang().createProject(gav, runtime));
new CamelJBang().createProject(gav, runtime, outputPath));
}
}
13 changes: 8 additions & 5 deletions src/ui-test/tests/commands.projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
} from '../utils/testUtils';
import * as pjson from '../../../package.json';

describe('Create a Camel Project using command', function () {
describe.only('Create a Camel Project using command', function () {
this.timeout(400000);

let driver: WebDriver;
Expand Down Expand Up @@ -71,12 +71,15 @@ describe('Create a Camel Project using command', function () {
it(`Create project`, async function () {
await new Workbench().executeCommand(command);

await driver.wait(async function () {
input = await InputBox.create();
return input;
}, 30000);
input = await InputBox.create(30000);
await input.setText('com.demo:test:1.0-SNAPSHOT');
await input.confirm();
await driver.sleep(1000);

input = await InputBox.create(30000);
while (await input.isDisplayed()) {
await input.confirm();
}

await waitUntilFileAvailable(driver, 'pom.xml', SPECIFIC_WORKSPACE_NAME, 60000);
});
Expand Down

0 comments on commit 6b37f60

Please sign in to comment.