Skip to content

Commit

Permalink
feat: introduce project loading for workspaces
Browse files Browse the repository at this point in the history
It's because now server-side CLI deprecated the folder option
  • Loading branch information
benoitf committed Jan 20, 2022
1 parent 431b66d commit 4efae76
Show file tree
Hide file tree
Showing 12 changed files with 3,480 additions and 0 deletions.
1 change: 1 addition & 0 deletions code/build/gulpfile.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const compilations = [
'authentication-proxy/tsconfig.json',
'che-api/tsconfig.json',
'che-port/tsconfig.json',
'che-project/tsconfig.json',
'configuration-editing/build/tsconfig.json',
'configuration-editing/tsconfig.json',
'css-language-features/client/tsconfig.json',
Expand Down
1 change: 1 addition & 0 deletions code/build/npm/dirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports.dirs = [
'extensions',
'extensions/che-api',
'extensions/che-port',
'extensions/che-project',
'extensions/configuration-editing',
'extensions/css-language-features',
'extensions/css-language-features/server',
Expand Down
8 changes: 8 additions & 0 deletions code/extensions/che-project/.vscodeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
build/**
src/**
tests/**
coverage/**
out/**
tsconfig.json
extension.webpack.config.js
yarn.lock
7 changes: 7 additions & 0 deletions code/extensions/che-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Eclipse Che Port for Visual Studio Code

**Notice:** This extension is bundled with Visual Studio Code. It can be disabled but not uninstalled.

## Features

This extension loads the correct project folder
45 changes: 45 additions & 0 deletions code/extensions/che-project/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-disable header/header */
/*
* Copyright (c) 2022 Red Hat, Inc.
* All rights reserved. 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/
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/

/**
* Mock of vscode module
* @author Florent Benoit
*/
const vscode: any = {};
export class EventEmitter {
constructor() { }
fire() { }
}

export enum TreeItemCollapsibleState {
None,
Collapsed,
Expanded,
}

vscode.extensions = {};
const extensionsExports = {};

vscode.extensions.setExtensionExport = (name: string, value: any): void => {
(extensionsExports as any)[name] = value;
};

vscode.extensions.getExtension = (name: string): any => {
return {
activate: jest.fn(),
exports: (extensionsExports as any)[name],
};
};


vscode.EventEmitter = EventEmitter;
vscode.TreeItemCollapsibleState = TreeItemCollapsibleState;
module.exports = vscode;
27 changes: 27 additions & 0 deletions code/extensions/che-project/extension.webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**********************************************************************
* Copyright (c) 2022 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
***********************************************************************/

/* eslint-disable header/header */

//@ts-check

'use strict';

const withDefaults = require('../shared.webpack.config');

module.exports = withDefaults({
context: __dirname,
resolve: {
mainFields: ['module', 'main']
},
entry: {
extension: './src/extension.ts',
}
});
Binary file added code/extensions/che-project/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions code/extensions/che-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "project",
"displayName": "%displayName%",
"description": "%description%",
"publisher": "eclipse-che",
"license": "EPL-2.0",
"version": "0.0.1",
"engines": {
"vscode": "^1.63.0"
},
"icon": "images/icon.png",
"categories": [
"Other"
],
"activationEvents": [
"*"
],
"capabilities": {
"virtualWorkspaces": true,
"untrustedWorkspaces": {
"supported": true
}
},
"main": "./out/extension.js",
"scripts": {
"compile": "gulp compile-extension:che-project",
"watch": "gulp watch-extension:che-project",
"vscode:prepublish": "npm run compile",
"test": "jest",
"lint:fix": "eslint --fix --cache=true --no-error-on-unmatched-pattern=true \"{src,tests}/**/*.{ts,tsx}\""
},
"dependencies": {
"fs-extra": "^10.0.0",
"vscode-nls": "^5.0.0"
},
"devDependencies": {
"jest": "27.3.1",
"ts-jest": "27.0.7",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^27.4.0",
"@types/js-yaml": "^4.0.5",
"@types/node": "14.x"
},
"repository": {
"type": "git",
"url": "https://github.com/che-incubator/che-code.git"
},
"extensionDependencies": [
"eclipse-che.api"
],
"jest": {
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.ts"
],
"coverageDirectory": "./coverage",
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"modulePathIgnorePatterns": [
"<rootDir>/dist"
],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
]
}
}
4 changes: 4 additions & 0 deletions code/extensions/che-project/package.nls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"displayName": "Eclipse Che Project",
"description": "Open project automatically at startup if no project is provided"
}
102 changes: 102 additions & 0 deletions code/extensions/che-project/src/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**********************************************************************
* Copyright (c) 2022 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
***********************************************************************/

/* eslint-disable header/header */

import * as vscode from 'vscode';

export async function activate(_context: vscode.ExtensionContext): Promise<void> {
const output = vscode.window.createOutputChannel('Che Project');
// check if there are projects already defined
// if true, exit
const projects = vscode.workspace.workspaceFolders;
if (projects) {
output.appendLine('Found existing projects in the workspace, skipping the sequence');
return;
}

// open the project if found one
const workspacePath = await getCheWorkspacePath(output);
if (workspacePath) {
output.appendLine(`Opening the folder... ${workspacePath.toString()}`);
await vscode.commands.executeCommand('vscode.openFolder', workspacePath, { forceReuseWindow: true });
} else {
output.appendLine('no workspace found');
}

}

/**
* Check if PROJECTS_ROOT is defined.
* If there is no folders inside, return PROJECTS_ROOT directory
* If there is a file named eclipse-che.code-workspace inside PROJECTS_ROOT, return it
* if there is only one folder: if there is one code-workspace file inside, return it else return this unique folder
* if there are more than one folder, create a eclipse-che.code-workspace file and return that folder
*/
export async function getCheWorkspacePath(output: vscode.OutputChannel): Promise<vscode.Uri | undefined> {
if (process.env.PROJECTS_ROOT) {
const projectsRoot = process.env.PROJECTS_ROOT;
const projectsRootUri = vscode.Uri.file(projectsRoot);

// list everything from that directory
const children = await vscode.workspace.fs.readDirectory(projectsRootUri);

// do we have a eclipse-che.code-workspace file there ?
const cheCodeWorkspace = children.filter(child => child[1] === vscode.FileType.Directory).find(child => child[0] === 'eclipse-che.code-workspace');
if (cheCodeWorkspace) {
output.appendLine('Found a eclipse-che.code-workspace file in the projects root, using it');
return vscode.Uri.joinPath(projectsRootUri, cheCodeWorkspace[0]);
}

// no, so grab all folders inside PROJECTS_ROOT and add them to a file
const folders = children.filter(child => child[1] === vscode.FileType.Directory);

// no folder in PROJECTS_ROOT, open the default folder
if (folders.length === 0) {
output.appendLine(`No folder found in the projects root, opening the default projects root folder ${projectsRoot}`);
return projectsRootUri;
} else if (folders.length === 1) {
// check if we have a workspace in that folder.
// if yes, use it
const folderPath = vscode.Uri.joinPath(projectsRootUri, folders[0][0]);
const projectsFiles = await vscode.workspace.fs.readDirectory(folderPath);
const anyCodeWorkspaces = projectsFiles.filter(child => child[1] === vscode.FileType.File).filter(file => file[0].endsWith('.code-workspace'));
if (anyCodeWorkspaces.length === 1) {
output.appendLine('Found a single folder with a single code-workspace file in it, using it');
const anyCodeWorkspace = anyCodeWorkspaces[0][0];
output.appendLine(`anyCodeWorkspace = ${anyCodeWorkspace.toString()}`);
// use that file
const workspacePath = vscode.Uri.joinPath(folderPath, anyCodeWorkspace);
output.appendLine(`workspacePath = ${workspacePath.toString()}`);
return workspacePath;
}
output.appendLine('Found a single folder with no code-workspace file in it, using folder as it is');
const workspacePath = vscode.Uri.joinPath(projectsRootUri, folders[0][0]);
output.appendLine(`folder is ${folders[0][0]}, workspacePath = ${workspacePath.toString()}`);
return workspacePath;
} else {
const eclipseCheCodeWorkspace = {
folders: folders.map(folder => { return { path: folder[0] } })
};
output.appendLine('Found multiple folders, creating a eclipse-che.code-workspace file in the projects root');
const eclipseCheCodeWorkspacePath = vscode.Uri.joinPath(projectsRootUri, 'eclipse-che.code-workspace');
await vscode.workspace.fs.writeFile(eclipseCheCodeWorkspacePath, Buffer.from(JSON.stringify(eclipseCheCodeWorkspace, undefined, 2), 'utf-8'));
return eclipseCheCodeWorkspacePath;
}
}
// return empty if not found
return undefined;

}


export function deactivate(): void {

}
17 changes: 17 additions & 0 deletions code/extensions/che-project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"outDir": "./out",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types": [
"node",
"jest",
"reflect-metadata"
]
},
"include": [
"src/**/*",
"../../src/vscode-dts/vscode.d.ts",
]
}
Loading

0 comments on commit 4efae76

Please sign in to comment.