-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds Create Camel project to the welcome screen #1994
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License", destination); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
import * as path from "path"; | ||
import { TaskScope, Uri, commands, window, workspace } from "vscode"; | ||
import { CamelExportJBangTask } from "../tasks/CamelExportJBangTask"; | ||
import { arePathsEqual, getCurrentWorkingDirectory } from "../requirements/utils"; | ||
|
||
export abstract class AbstractNewCamelProjectCommand { | ||
|
||
async create() { | ||
const runtime = await this.getRuntime(); | ||
const input = await this.askForGAV(); | ||
if (runtime && input) { | ||
|
||
// Uses the user selected folder otherwise default to the root workspace folder | ||
const outputFolder = await this.showDialogToPickFolder(); | ||
if (!outputFolder) { | ||
await window.showErrorMessage('Camel project creation canceled or invalid folder selection'); | ||
return; | ||
} | ||
|
||
// If the chosen ouputh folder is diferent from the first folder from the current workspace warns the user about | ||
// potentially deleting files in the selected folder. | ||
// Executing the command from the same folder does not delete files. | ||
const cwd = getCurrentWorkingDirectory(); | ||
if (cwd && !arePathsEqual(cwd, outputFolder.fsPath)){ | ||
const userChoice = await this.confirmDestructiveActionInSelectedFolder(outputFolder.fsPath); | ||
|
||
if (userChoice === undefined){ | ||
await window.showInformationMessage('Camel project creation canceled'); | ||
return; | ||
} | ||
} | ||
|
||
await new CamelExportJBangTask(TaskScope.Workspace, input, runtime, outputFolder.fsPath).execute(); | ||
|
||
// if not exist, init .vscode with tasks.json and launch.json config files | ||
await workspace.fs.createDirectory(Uri.file(path.join(outputFolder.fsPath, '.vscode'))); | ||
for (const filename of ['tasks', 'launch']) { | ||
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); | ||
|
||
} | ||
} | ||
|
||
abstract getRuntime(): Promise<string|undefined>; | ||
|
||
async askForGAV() { | ||
return await window.showInputBox({ | ||
prompt: 'Please provide repository coordinate', | ||
placeHolder: 'com.acme:myproject:1.0-SNAPSHOT', | ||
value: 'com.acme:myproject:1.0-SNAPSHOT', | ||
validateInput: (gav) => { | ||
return this.validateGAV(gav); | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* Maven GAV validation | ||
* - no empty name | ||
* - Have 2 double-dots (similar check than Camel JBang) | ||
* - following mostly recommendations from Maven Central for name rules | ||
* | ||
* @param name | ||
* @returns string | undefined | ||
*/ | ||
public validateGAV(name: string): string | undefined { | ||
if (!name) { | ||
return 'Please provide a GAV for the new project following groupId:artifactId:version pattern.'; | ||
} | ||
if (name.includes(' ')) { | ||
return 'The GAV cannot contain a space. It must constituted from groupId, artifactId and version following groupId:artifactId:version pattern.'; | ||
} | ||
const gavs = name.split(':'); | ||
if (gavs.length !== 3) { | ||
return 'The GAV needs to have double-dot `:` separator and constituted from groupId, artifactId and version'; | ||
} | ||
const groupIdSplit = gavs[0].split('.'); | ||
if (groupIdSplit[0].length === 0) { | ||
return 'The group id cannot start with a .'; | ||
} | ||
for (const groupidSubPart of groupIdSplit) { | ||
const regExpSearch = /^[a-z]\w*$/.exec(groupidSubPart); | ||
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) { | ||
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) { | ||
return `Invalid version: ${version} . It must follow groupId:artifactId:version pattern with version specific pattern: \\d[\\w-.]*`; | ||
} | ||
return undefined; | ||
} | ||
|
||
/** | ||
* Open a dialog to select a folder to create the project in. | ||
* | ||
* @returns Uri of the selected folder or undefined if canceled by the user. | ||
*/ | ||
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; | ||
} | ||
|
||
/** | ||
* Handles copy of the resources from the extension to the vscode workspace | ||
* | ||
* @param sourcePath Path of source | ||
* @param destPath Path of destination | ||
*/ | ||
private async copyFile(sourcePath: string, destPath: string): Promise<void> { | ||
|
||
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 | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Shows a modal asking for user confirmation of a potential desctructive action in the selected folder. | ||
* VSCode automatically provides a 'Cancel' option which return `undefined`. The continue option will return the string `Continue`. | ||
* | ||
* @param outputPath path to be shown in the warning message. | ||
* | ||
* @returns string | undefined | ||
*/ | ||
private async confirmDestructiveActionInSelectedFolder(outputPath: string) { | ||
const message = `Files in the folder: ${outputPath} WILL BE DELETED before project creation, continue?`; | ||
const continueOption = 'Continue'; | ||
|
||
return await window.showWarningMessage(message, { modal: true }, continueOption); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to decalre it as async to have a better typing and ensure that await is called.
For instance, in camel.project.command.test.ts it is not. It is causing the title of the test to be incorrect
Should validate Create a Camel Project command GAV validation of [object Promise]
, see https://github.com/camel-tooling/camel-lsp-client-vscode/actions/runs/12173733325/job/33963279530?pr=1994#step:13:123