-
Notifications
You must be signed in to change notification settings - Fork 35
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
RomanNikitenko
merged 2 commits into
che-incubator:main
from
RomanNikitenko:restartFromLocalDevfile
Mar 8, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -> An error occurred while generating new devfile context |
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"