This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Refactor Welcome plugin #1155
Draft
vitaliy-guliy
wants to merge
1
commit into
main
Choose a base branch
from
refactor-welcome-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Refactor Welcome plugin #1155
Changes from all commits
Commits
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
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
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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2021 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 * as theia from '@theia/plugin'; | ||
|
||
export namespace Commands { | ||
export const SHOW_WELCOME: theia.CommandDescription = { | ||
id: 'welcome:show_welcome', | ||
label: 'Welcome: Show Welcome...', | ||
}; | ||
|
||
export const ENABLE_WELCOME: theia.CommandDescription = { | ||
id: 'welcome:enable', | ||
}; | ||
|
||
export const DISABLE_WELCOME: theia.CommandDescription = { | ||
id: 'welcome:disable', | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2021 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 * as che from '@eclipse-che/plugin'; | ||
|
||
export const WELCOME_ENABLED = 'extensions.welcome'; | ||
|
||
export async function isWelcomeEnabled(): Promise<boolean> { | ||
const workspace = await che.workspace.getCurrentWorkspace(); | ||
// always has a devfile now | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const devfile = workspace.devfile!; | ||
const attributes = devfile.attributes || {}; | ||
const welcome = attributes[WELCOME_ENABLED] || 'true'; | ||
return welcome !== 'false'; | ||
} | ||
|
||
export async function enableWelcome(enable: boolean): Promise<void> { | ||
const workspace = await che.workspace.getCurrentWorkspace(); | ||
// always has a devfile now | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const devfile = workspace.devfile!; | ||
devfile.attributes = devfile.attributes || {}; | ||
devfile.attributes[WELCOME_ENABLED] = '' + enable; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
await che.workspace.update(workspace.id!, workspace); | ||
} | ||
|
||
export async function isMultiroot(): Promise<boolean> { | ||
const devfile = await che.devfile.get(); | ||
return devfile.metadata?.attributes?.multiRoot !== 'off'; | ||
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. I see |
||
} |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2021 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 * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
import * as theia from '@theia/plugin'; | ||
|
||
import { isMultiroot } from './devfile'; | ||
|
||
export class Readme { | ||
processed: string[] = []; | ||
|
||
constructor(protected context: theia.PluginContext) {} | ||
|
||
async seekAndOpen(): Promise<void> { | ||
if (await isMultiroot()) { | ||
this.context.subscriptions.push( | ||
theia.workspace.onDidChangeWorkspaceFolders( | ||
event => this.handleReadmeFiles(event.added), | ||
undefined, | ||
this.context.subscriptions | ||
) | ||
); | ||
} else { | ||
const workspacePlugin = theia.plugins.getPlugin('@eclipse-che.workspace-plugin'); | ||
if (workspacePlugin && workspacePlugin.exports) { | ||
this.context.subscriptions.push( | ||
workspacePlugin.exports.onDidCloneSources( | ||
() => this.handleReadmeFiles(), | ||
undefined, | ||
this.context.subscriptions | ||
) | ||
); | ||
} else { | ||
this.handleReadmeFiles(); | ||
} | ||
} | ||
} | ||
|
||
async handleReadmeFiles(roots?: theia.WorkspaceFolder[]): Promise<void> { | ||
roots = roots ? roots : theia.workspace.workspaceFolders; | ||
if (!roots || roots.length < 1) { | ||
return; | ||
} | ||
|
||
for (const root of roots) { | ||
this.openReadme(root.uri.fsPath); | ||
} | ||
} | ||
|
||
async openReadme(projectPath: string): Promise<void> { | ||
if (this.processed.some(value => value === projectPath)) { | ||
return; | ||
} | ||
|
||
this.processed.push(projectPath); | ||
|
||
const readmePath = path.join(projectPath, 'README.md'); | ||
if (await fs.pathExists(projectPath)) { | ||
const openPath = theia.Uri.parse(`file://${readmePath}?open-handler=code-editor-preview`); | ||
try { | ||
const doc = await theia.workspace.openTextDocument(openPath); | ||
if (doc) { | ||
await theia.window.showTextDocument(doc); | ||
} | ||
} catch (err) { | ||
// Ignore the error. | ||
// `theia.window.showTextDocument` throws an error with message `Failed to show text document` | ||
// But works as expected. | ||
// It's because `?open-handler=code-editor-preview` was added at the end of the URI | ||
// to open preview instead of the editor | ||
} | ||
} | ||
} | ||
} |
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.
Does It is a breaking change for the plugin which provided by other developer