Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Correct resolving of current project path #588

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 28 additions & 35 deletions plugins/task-plugin/src/variable/project-path-variable-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,73 +14,66 @@ 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<void> {
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<string> {
if (!this.projectsRoot) {
return this.onError('Projects root is not set');
}
let value = '';

const selections = await theia.commands.executeCommand<Uri[]>(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 {
return {
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);
}
}