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

Commit

Permalink
feat(multiple-workspace-root): Multi-root workspaces based on /projec…
Browse files Browse the repository at this point in the history
…ts (or what has been configured as ROOT folder)

Checks if there is a che.theia-workspace file.
  if there is one, returns that file as being a workspace root
  if none, generate the file by getting all clonedPath/path of the devfile projects

enhancements:
 1. make it optional for now (like if there is a special attribute in devfile, use that mode)
 2. track new folders in /projects and add them to the che.theia-workspace file automatically (and if we delete folders)
 3. allow to customize/provide different projects to be added in workspace root. Could be useful for a maven project to see only sub-folders/modules

Change-Id: Idfb9370dc503bcfb5504ba37446f7f1ab171b515
Signed-off-by: Florent Benoit <fbenoit@redhat.com>
  • Loading branch information
benoitf authored and RomanNikitenko committed Oct 8, 2020
1 parent 8d39edf commit 23712a2
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 4 deletions.
4 changes: 3 additions & 1 deletion extensions/eclipse-che-theia-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"src"
],
"dependencies": {
"@theia/core": "next",
"@eclipse-che/api": "latest",
"@theia/workspace": "next",
"@eclipse-che/theia-remote-api": "^0.0.1",
Expand All @@ -29,7 +30,8 @@
"license": "EPL-2.0",
"theiaExtensions": [
{
"frontend": "lib/browser/che-workspace-module"
"frontend": "lib/browser/che-workspace-module",
"backend": "lib/node/workspace-backend-module"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*********************************************************************
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import { injectable, inject } from 'inversify';
import { DefaultWorkspaceServer } from '@theia/workspace/lib/node/default-workspace-server';
import { CheApiService } from '@eclipse-che/theia-plugin-ext/lib/common/che-protocol';
import { FileUri } from '@theia/core/lib/node';
import * as fs from 'fs-extra';
import * as path from 'path';

interface TheiaWorkspace {
folders: TheiaWorkspacePath[]
};

interface TheiaWorkspacePath {
path: string
};

@injectable()
export class CheWorkspaceServer extends DefaultWorkspaceServer {

@inject(CheApiService)
private cheApiService: CheApiService;

// override any workspace that could have been defined through CLI and use entries from the devfile
// if not possible, use default method
protected async getRoot(): Promise<string | undefined> {

let projectsRoot: string;
if (process.env.CHE_PROJECTS_ROOT) {
projectsRoot = process.env.CHE_PROJECTS_ROOT;
} else {
projectsRoot = '/projects';
}

// first, check if we have a che.theia-workspace file
const cheTheiaWorkspaceFile = path.resolve(projectsRoot, 'che.theia-workspace');
const exists = await fs.pathExists(cheTheiaWorkspaceFile);
if (exists) {
return FileUri.create(cheTheiaWorkspaceFile).toString();
}

// no, then create the file

const workspace = await this.cheApiService.currentWorkspace();
const devfile = workspace.devfile;
if (devfile) {
const projects = devfile.projects;
if (projects) {
// create a struc for each project
const theiaWorkspace: TheiaWorkspace = { folders: [] };
for (const project of projects) {
const projectPath = project.clonePath ? path.join(projectsRoot, project.clonePath) : path.join(projectsRoot, project.name!);
// check parent folder exists
const parentDir = path.resolve(projectPath, '..');
await fs.ensureDir(parentDir);
theiaWorkspace.folders.push({ path: FileUri.create(projectPath).toString() });
}

// now, need to write the content of this file
await fs.writeFile(cheTheiaWorkspaceFile, JSON.stringify(theiaWorkspace), { encoding: 'utf8' });

// return this content
return FileUri.create(cheTheiaWorkspaceFile).toString();
}
}

return super.getRoot();
}

}
11 changes: 11 additions & 0 deletions extensions/eclipse-che-theia-workspace/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*********************************************************************
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

export * from './workspace-backend-module';
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*********************************************************************
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import { ContainerModule } from 'inversify';
import { CheWorkspaceServer } from './che-workspace-server';
import { WorkspaceServer } from '@theia/workspace/lib/common';

export default new ContainerModule((bind, unbind, isBound, rebind) => {
bind(CheWorkspaceServer).toSelf().inSingletonScope();
rebind(WorkspaceServer).toService(CheWorkspaceServer);
});
5 changes: 2 additions & 3 deletions plugins/workspace-plugin/src/workspace-projects-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ abstract class WorkspaceProjectsManager {
abstract deleteProject(workspace: cheApi.workspace.Workspace, projectFolderURI: string): void;

async run(workspace?: cheApi.workspace.Workspace): Promise<void> {
if (!theia.workspace.name) {
/* if (!theia.workspace.name) {
// no workspace opened, so nothing to clone / watch
return;
}

} */
if (!workspace) {
workspace = await che.workspace.getCurrentWorkspace();
}
Expand Down

0 comments on commit 23712a2

Please sign in to comment.