diff --git a/plugins/task-plugin/src/variable/project-path-variable-resolver.ts b/plugins/task-plugin/src/variable/project-path-variable-resolver.ts index af3768b9f..023126fba 100644 --- a/plugins/task-plugin/src/variable/project-path-variable-resolver.ts +++ b/plugins/task-plugin/src/variable/project-path-variable-resolver.ts @@ -14,57 +14,58 @@ import * as che from '@eclipse-che/plugin'; import * as theia from '@theia/plugin'; import * as startPoint from '../task-plugin-backend'; +const fs = require('fs'); + const VARIABLE_NAME = 'current.project.path'; const SELECTED_CONTEXT_COMMAND = 'theia.plugin.workspace.selectedContext'; const PROJECTS_ROOT_VARIABLE = 'CHE_PROJECTS_ROOT'; -const ERROR_MESSAGE_TEMPLATE = 'Can not resolve \'current.project.path\' variable.'; +const SELECT_PROJECT_MESSAGE = 'Please select a project before executing a command to make it possible to resolve the current project path.'; /** * Contributes the variable for getting path for current project as a relative path to the first directory under the root workspace. */ @injectable() export class ProjectPathVariableResolver { - private projectsRoot: string | undefined; - private isResolved: boolean = false; + private projectsRoot: string; async registerVariables(): Promise { - this.projectsRoot = await theia.env.getEnvVariable(PROJECTS_ROOT_VARIABLE); + const projectsRoot = await theia.env.getEnvVariable(PROJECTS_ROOT_VARIABLE); + if (projectsRoot === undefined) { + return Promise.reject('Projects root is not provided'); + } + + this.projectsRoot = projectsRoot; const variableSubscription = await che.variables.registerVariable(this.createVariable()); startPoint.getSubscriptions().push(variableSubscription); } async resolve(): Promise { - if (!this.projectsRoot) { - return this.onError('Projects root is not set'); - } + let value = ''; const selections = await theia.commands.executeCommand(SELECTED_CONTEXT_COMMAND); - if (!selections || selections.length < 1) { - return this.onError('Please select a project.'); - } - const selection = selections[0]; - const selectionPath = selection.path; - const workspaceFolder = theia.workspace.getWorkspaceFolder(theia.Uri.file(selectionPath)); - if (!workspaceFolder) { - return this.onError('Selection doesn\'t match any workspace folder.'); - } + if (selections !== undefined && selections.length === 1) { + // retrieve project path from selection + const relPath = selections[0].path.substring(this.projectsRoot.length).split('/'); + const project = relPath.shift() || relPath.shift(); - const workspaceFolderPath = workspaceFolder.uri.path; - if (workspaceFolderPath === this.projectsRoot) { - const splittedSelectionUri = selectionPath.substring(workspaceFolderPath.length).split('/'); - const project = splittedSelectionUri.shift() || splittedSelectionUri.shift(); + if (project !== undefined) { + value = `${this.projectsRoot}/${project}`; + } + } else { + // get project folder from workspace folders, first folder in workspaceFolders is .theia + const folders = fs.readdirSync(this.projectsRoot, {withFileTypes: false}); - this.isResolved = true; - return `${this.projectsRoot}/${project}`; + if (folders !== undefined && folders.length === 2) { + value = `${this.projectsRoot}/${folders[1]}`; + } } - if (workspaceFolderPath.startsWith(this.projectsRoot)) { - this.isResolved = true; - return workspaceFolderPath; + if (value.length === 0) { + theia.window.showWarningMessage(SELECT_PROJECT_MESSAGE); } - return this.onError('The selection isn\'t under the current workspace root folder.'); + return value; } private createVariable(): che.Variable { @@ -72,15 +73,7 @@ export class ProjectPathVariableResolver { name: VARIABLE_NAME, description: 'The path of the project root folder', resolve: async () => this.resolve(), - isResolved: this.isResolved + isResolved: false }; } - - private onError(error?: string) { - this.isResolved = false; - - const errorMessage = error ? `${ERROR_MESSAGE_TEMPLATE} ${error}` : ERROR_MESSAGE_TEMPLATE; - theia.window.showErrorMessage(errorMessage); - return Promise.reject(errorMessage); - } }