|
| 1 | +// ***************************************************************************** |
| 2 | +// Copyright (C) 2024 Typefox and others. |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0. |
| 7 | +// |
| 8 | +// This Source Code may also be made available under the following Secondary |
| 9 | +// Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | +// with the GNU Classpath Exception which is available at |
| 12 | +// https://www.gnu.org/software/classpath/license.html. |
| 13 | +// |
| 14 | +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 |
| 15 | +// ***************************************************************************** |
| 16 | + |
| 17 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 18 | +import { AbstractRemoteRegistryContribution, RemoteRegistry } from '@theia/remote/lib/electron-browser/remote-registry-contribution'; |
| 19 | +import { LastContainerInfo, RemoteContainerConnectionProvider } from '../electron-common/remote-container-connection-provider'; |
| 20 | +import { RemotePreferences } from '@theia/remote/lib/electron-browser/remote-preferences'; |
| 21 | +import { WorkspaceStorageService } from '@theia/workspace/lib/browser/workspace-storage-service'; |
| 22 | +import { Command, QuickInputService } from '@theia/core'; |
| 23 | +import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service'; |
| 24 | +import { ContainerOutputProvider } from './container-output-provider'; |
| 25 | + |
| 26 | +export namespace RemoteContainerCommands { |
| 27 | + export const REOPEN_IN_CONTAINER = Command.toLocalizedCommand({ |
| 28 | + id: 'dev-container:reopen-in-container', |
| 29 | + label: 'Reopen in Container', |
| 30 | + category: 'Dev Container' |
| 31 | + }, 'theia/dev-container/connect'); |
| 32 | +} |
| 33 | + |
| 34 | +const LAST_USED_CONTAINER = 'lastUsedContainer'; |
| 35 | +@injectable() |
| 36 | +export class ContainerConnectionContribution extends AbstractRemoteRegistryContribution { |
| 37 | + |
| 38 | + @inject(RemoteContainerConnectionProvider) |
| 39 | + protected readonly connectionProvider: RemoteContainerConnectionProvider; |
| 40 | + |
| 41 | + @inject(RemotePreferences) |
| 42 | + protected readonly remotePreferences: RemotePreferences; |
| 43 | + |
| 44 | + @inject(WorkspaceStorageService) |
| 45 | + protected readonly workspaceStorageService: WorkspaceStorageService; |
| 46 | + |
| 47 | + @inject(WorkspaceService) |
| 48 | + protected readonly workspaceService: WorkspaceService; |
| 49 | + |
| 50 | + @inject(QuickInputService) |
| 51 | + protected readonly quickInputService: QuickInputService; |
| 52 | + |
| 53 | + @inject(ContainerOutputProvider) |
| 54 | + protected readonly containerOutputProvider: ContainerOutputProvider; |
| 55 | + |
| 56 | + registerRemoteCommands(registry: RemoteRegistry): void { |
| 57 | + registry.registerCommand(RemoteContainerCommands.REOPEN_IN_CONTAINER, { |
| 58 | + execute: () => this.openInContainer() |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + async openInContainer(): Promise<void> { |
| 63 | + const devcontainerFile = await this.getOrSelectDevcontainerFile(); |
| 64 | + if (!devcontainerFile) { |
| 65 | + return; |
| 66 | + } |
| 67 | + const lastContainerInfoKey = `${LAST_USED_CONTAINER}:${devcontainerFile}`; |
| 68 | + const lastContainerInfo = await this.workspaceStorageService.getData<LastContainerInfo | undefined>(lastContainerInfoKey); |
| 69 | + |
| 70 | + this.containerOutputProvider.openChannel(); |
| 71 | + |
| 72 | + const connectionResult = await this.connectionProvider.connectToContainer({ |
| 73 | + nodeDownloadTemplate: this.remotePreferences['remote.nodeDownloadTemplate'], |
| 74 | + lastContainerInfo, |
| 75 | + devcontainerFile |
| 76 | + }); |
| 77 | + |
| 78 | + this.workspaceStorageService.setData<LastContainerInfo>(lastContainerInfoKey, { |
| 79 | + id: connectionResult.containerId, |
| 80 | + lastUsed: Date.now() |
| 81 | + }); |
| 82 | + |
| 83 | + this.openRemote(connectionResult.port, false, connectionResult.workspacePath); |
| 84 | + } |
| 85 | + |
| 86 | + async getOrSelectDevcontainerFile(): Promise<string | undefined> { |
| 87 | + const devcontainerFiles = await this.connectionProvider.getDevContainerFiles(); |
| 88 | + |
| 89 | + if (devcontainerFiles.length === 1) { |
| 90 | + return devcontainerFiles[0].path; |
| 91 | + } |
| 92 | + |
| 93 | + return (await this.quickInputService.pick(devcontainerFiles.map(file => ({ |
| 94 | + type: 'item', |
| 95 | + label: file.name, |
| 96 | + description: file.path, |
| 97 | + file: file.path, |
| 98 | + })), { |
| 99 | + title: 'Select a devcontainer.json file' |
| 100 | + }))?.file; |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments