Skip to content
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

feat: Add 'Restart From Local Devfile' action #185

Merged
Merged
Show file tree
Hide file tree
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
26 changes: 19 additions & 7 deletions code/extensions/che-remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"lint:fix": "eslint --fix --cache=true --no-error-on-unmatched-pattern=true \"{src,tests}/**/*.{ts,tsx}\""
},
"dependencies": {
"vscode-nls": "^5.0.0"
"vscode-nls": "^5.0.0",
"axios": "0.21.2",
"@eclipse-che/che-devworkspace-generator": "0.0.1-96cdbb4"
},
"devDependencies": {
"jest": "27.3.1",
Expand All @@ -51,11 +53,16 @@
"title": "%stopWorkspaceCommand%",
"enablement": "che-remote.workspace-enabled"
},
{
{
"command": "che-remote.command.restartWorkspace",
"title": "%restartWorkspaceCommand%",
"enablement": "che-remote.workspace-enabled"
},
{
"command": "che-remote.command.restartFromLocalDevfile",
"title": "%restartWorkspaceFromLocalDevfileCommand%",
"enablement": "che-remote.workspace-enabled"
},
{
"command": "che-remote.command.openDocumentation",
"title": "%openDocumentationCommand%"
Expand All @@ -73,11 +80,16 @@
"group": "remote_40_che_navigation@10",
"when": "che-remote.workspace-enabled"
},
{
"command": "che-remote.command.restartWorkspace",
"group": "remote_40_che_navigation@15",
"when": "che-remote.workspace-enabled"
},
{
"command": "che-remote.command.restartWorkspace",
"group": "remote_40_che_navigation@15",
"when": "che-remote.workspace-enabled"
},
{
"command": "che-remote.command.restartFromLocalDevfile",
"group": "remote_40_che_navigation@16",
"when": "che-remote.workspace-enabled"
},
{
"command": "che-remote.command.openDashboard",
"group": "remote_40_che_navigation@20",
Expand Down
3 changes: 2 additions & 1 deletion code/extensions/che-remote/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"openDocumentationCommand": "Eclipse Che: Open Documentation",
"openDashboardCommand": "Eclipse Che: Open Dashboard",
"stopWorkspaceCommand": "Eclipse Che: Stop Workspace",
"restartWorkspaceCommand": "Eclipse Che: Restart Workspace"
"restartWorkspaceCommand": "Eclipse Che: Restart Workspace",
"restartWorkspaceFromLocalDevfileCommand": "Eclipse Che: Restart Workspace From Local Devfile"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, the preposition would not be capitalized:

"Eclipse Che: Restart Workspace from Local Devfile"

}
58 changes: 57 additions & 1 deletion code/extensions/che-remote/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@
/* eslint-disable header/header */

// local import to find the correct type
import { Main as DevWorkspaceGenerator } from '@eclipse-che/che-devworkspace-generator/lib/main';
import * as axios from 'axios';
import * as fs from 'fs-extra';
import * as vscode from 'vscode';

const DEVFILE_NAME = 'devfile.yaml';
const DOT_DEVFILE_NAME = '.devfile.yaml';
const DEFAULT_EDITOR_ENTRY = 'che-incubator/che-code/insiders';

export async function activate(context: vscode.ExtensionContext): Promise<void> {

// open documentation
Expand Down Expand Up @@ -46,11 +53,60 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
await workspaceService.stop();
})
);
}

context.subscriptions.push(
vscode.commands.registerCommand('che-remote.command.restartFromLocalDevfile', async () => {
try {
await updateDevfile(cheApi);
await vscode.commands.executeCommand('che-remote.command.restartWorkspace');
} catch (error) {
console.error(`Something went wrong for the 'Restart From Local Devfile' action: ${error}`);
vscode.window.showErrorMessage(`Can not restart the workspace from the local devfile: ${error}`);
}
})
);
}
}


export function deactivate(): void {

}

async function updateDevfile(cheApi: any): Promise<void> {
const devfileService: {
get(): Promise<any>;
getRaw(): Promise<string>;
updateDevfile(devfile: any): Promise<void>;
} = cheApi.getDevfileService();
const devWorkspaceGenerator = new DevWorkspaceGenerator();

const projectPath = process.env.PROJECT_SOURCE
let devfilePath: string | undefined = `${projectPath}/${DEVFILE_NAME}`;

let devfileExists = await fs.pathExists(devfilePath);
if (!devfileExists) {
devfilePath = `${projectPath}/${DOT_DEVFILE_NAME}`;
devfileExists = await fs.pathExists(devfilePath);
}

if (!devfileExists) {
devfilePath = await vscode.window.showInputBox({ title: 'Path to the devfile', value: `${projectPath}/` });
devfileExists = devfilePath ? await fs.pathExists(devfilePath) : false;
}

if (!devfileExists) {
throw new Error(`The devfile was not found by path: ${devfilePath}`);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->

The devfile was not found at:

}

const currentDevfile = await devfileService.get()
const projects = currentDevfile.projects || [];

const newContent = await devWorkspaceGenerator.generateDevfileContext({ devfilePath, editorEntry: DEFAULT_EDITOR_ENTRY, projects: [] }, axios.default);
if (newContent) {
newContent.devWorkspace.spec!.template!.projects = projects;
await devfileService.updateDevfile(newContent.devWorkspace.spec?.template);
} else {
throw new Error('An error occurred while performing generation a new devfile context');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->

An error occurred while generating new devfile context

}
}
Loading