|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (c) 2021 Red Hat, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + ***********************************************************************/ |
| 10 | + |
| 11 | +import * as jsYaml from 'js-yaml'; |
| 12 | + |
| 13 | +import { FileChangeType, FileChangesEvent } from '@theia/filesystem/lib/common/files'; |
| 14 | +import { FrontendApplication, FrontendApplicationContribution } from '@theia/core/lib/browser'; |
| 15 | +import { inject, injectable } from 'inversify'; |
| 16 | + |
| 17 | +import { ChePluginManager } from '@eclipse-che/theia-plugin-ext/lib/browser/plugin/che-plugin-manager'; |
| 18 | +import { DevfileService } from '@eclipse-che/theia-remote-api/lib/common/devfile-service'; |
| 19 | +import { FileService } from '@theia/filesystem/lib/browser/file-service'; |
| 20 | +import { MessageService } from '@theia/core/lib/common/message-service'; |
| 21 | +import URI from '@theia/core/lib/common/uri'; |
| 22 | +import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service'; |
| 23 | + |
| 24 | +import debounce = require('lodash.debounce'); |
| 25 | + |
| 26 | +/** |
| 27 | + * Abstract watcher allows to track the changes in the project-specific configuration files. |
| 28 | + * A concrete implementation can handle the changes in a specific way. |
| 29 | + */ |
| 30 | +@injectable() |
| 31 | +export abstract class AbstractFileWatcher implements FrontendApplicationContribution { |
| 32 | + @inject(FileService) |
| 33 | + protected readonly fileService: FileService; |
| 34 | + |
| 35 | + @inject(WorkspaceService) |
| 36 | + protected readonly workspaceService: WorkspaceService; |
| 37 | + |
| 38 | + /** File name to watch, e.g. '.vscode/extensions.json'. */ |
| 39 | + protected abstract fileName: string; |
| 40 | + |
| 41 | + /** |
| 42 | + * Called when the frontend application is started. |
| 43 | + */ |
| 44 | + async onStart(app: FrontendApplication): Promise<void> { |
| 45 | + this.trackFilesInRoots(); |
| 46 | + this.workspaceService.onWorkspaceChanged(() => this.trackFilesInRoots()); |
| 47 | + } |
| 48 | + |
| 49 | + private async trackFilesInRoots(): Promise<void> { |
| 50 | + (await this.workspaceService.roots).forEach(root => { |
| 51 | + const fileURI = root.resource.resolve(this.fileName); |
| 52 | + this.fileService.watch(fileURI); |
| 53 | + const onFileChange = async (event: FileChangesEvent) => { |
| 54 | + if (event.contains(fileURI, FileChangeType.ADDED)) { |
| 55 | + this.handleChange(fileURI, FileChangeType.ADDED); |
| 56 | + } else if (event.contains(fileURI, FileChangeType.UPDATED)) { |
| 57 | + this.handleChange(fileURI, FileChangeType.UPDATED); |
| 58 | + } |
| 59 | + }; |
| 60 | + this.fileService.onDidFilesChange(debounce(onFileChange, 1000)); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Allows an implementor to handle a file change. |
| 66 | + * |
| 67 | + * @param fileURI an URI of the modified file |
| 68 | + * @param changeType file change type |
| 69 | + */ |
| 70 | + protected abstract handleChange(fileURI: URI, changeType: FileChangeType): void; |
| 71 | +} |
| 72 | + |
| 73 | +@injectable() |
| 74 | +export class DevfileWatcher extends AbstractFileWatcher { |
| 75 | + protected fileName = 'devfile.yaml'; |
| 76 | + |
| 77 | + @inject(DevfileService) |
| 78 | + protected readonly devfileService: DevfileService; |
| 79 | + |
| 80 | + @inject(ChePluginManager) |
| 81 | + protected readonly chePluginManager: ChePluginManager; |
| 82 | + |
| 83 | + @inject(MessageService) |
| 84 | + protected readonly messageService: MessageService; |
| 85 | + |
| 86 | + protected async handleChange(fileURI: URI, changeType: FileChangeType): Promise<void> { |
| 87 | + const message = |
| 88 | + changeType === FileChangeType.ADDED |
| 89 | + ? `A Devfile is found in ${fileURI}. Do you want to update your Workspace?` |
| 90 | + : 'Do you want to update your Workspace with the changed Devfile?'; |
| 91 | + const answer = await this.messageService.info(message, 'Yes', 'No'); |
| 92 | + if (answer === 'Yes') { |
| 93 | + this.updateWorkspaceWithDevfile(fileURI); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Updates the workspace with the given Devfile. |
| 99 | + * |
| 100 | + * @param devfileURI URI of the Devfile to update the Workspace with |
| 101 | + */ |
| 102 | + protected async updateWorkspaceWithDevfile(devfileURI: URI): Promise<void> { |
| 103 | + const content = await this.fileService.readFile(devfileURI); |
| 104 | + const devfile = jsYaml.load(content.value.toString()); |
| 105 | + await this.devfileService.updateDevfile(devfile); |
| 106 | + await this.chePluginManager.restartWorkspace(); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +@injectable() |
| 111 | +export class ExtensionsJsonWatcher extends AbstractFileWatcher { |
| 112 | + protected fileName = '.vscode/extensions.json'; |
| 113 | + |
| 114 | + @inject(ChePluginManager) |
| 115 | + protected readonly chePluginManager: ChePluginManager; |
| 116 | + |
| 117 | + @inject(MessageService) |
| 118 | + protected readonly messageService: MessageService; |
| 119 | + |
| 120 | + protected async handleChange(fileURI: URI, changeType: FileChangeType): Promise<void> { |
| 121 | + const message = |
| 122 | + changeType === FileChangeType.ADDED |
| 123 | + ? `An extensions list is found in ${fileURI}. Do you want to update your Workspace with these extensions?` |
| 124 | + : 'Do you want to update your Workspace with the changed "extensions.json"?'; |
| 125 | + const answer = await this.messageService.info(message, 'Yes', 'No'); |
| 126 | + if (answer === 'Yes') { |
| 127 | + await this.chePluginManager.restartWorkspace(); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +@injectable() |
| 133 | +export class PluginsYamlWatcher extends AbstractFileWatcher { |
| 134 | + protected fileName = '.che/che-theia-plugins.yaml'; |
| 135 | + |
| 136 | + @inject(ChePluginManager) |
| 137 | + protected readonly chePluginManager: ChePluginManager; |
| 138 | + |
| 139 | + @inject(MessageService) |
| 140 | + protected readonly messageService: MessageService; |
| 141 | + |
| 142 | + protected async handleChange(fileURI: URI, changeType: FileChangeType): Promise<void> { |
| 143 | + const message = |
| 144 | + changeType === FileChangeType.ADDED |
| 145 | + ? `A plug-ins list is found in ${fileURI}. Do you want to update your Workspace with these plug-ins?` |
| 146 | + : 'Do you want to update your Workspace with the changed "che-theia-plugins.yaml"?'; |
| 147 | + const answer = await this.messageService.info(message, 'Yes', 'No'); |
| 148 | + if (answer === 'Yes') { |
| 149 | + await this.chePluginManager.restartWorkspace(); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +@injectable() |
| 155 | +export class TasksJsonWatcher extends AbstractFileWatcher { |
| 156 | + protected fileName = '.vscode/tasks.json'; |
| 157 | + |
| 158 | + @inject(MessageService) |
| 159 | + protected readonly messageService: MessageService; |
| 160 | + |
| 161 | + protected async handleChange(fileURI: URI, changeType: FileChangeType): Promise<void> { |
| 162 | + const answer = await this.messageService.info( |
| 163 | + 'Do you want to update your Workspace with the "tasks.json" changes?', |
| 164 | + 'Yes', |
| 165 | + 'No' |
| 166 | + ); |
| 167 | + if (answer === 'Yes') { |
| 168 | + // TODO: set the tasks to the project's attributes |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments