@theia/dev-container 1.46.0-next.241

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/README.md +44 -0
  2. package/lib/dev-container-server/dev-container-server.d.ts +2 -0
  3. package/lib/dev-container-server/dev-container-server.d.ts.map +1 -0
  4. package/lib/dev-container-server/dev-container-server.js +48 -0
  5. package/lib/dev-container-server/dev-container-server.js.map +1 -0
  6. package/lib/electron-browser/container-connection-contribution.d.ts +28 -0
  7. package/lib/electron-browser/container-connection-contribution.d.ts.map +1 -0
  8. package/lib/electron-browser/container-connection-contribution.js +147 -0
  9. package/lib/electron-browser/container-connection-contribution.js.map +1 -0
  10. package/lib/electron-browser/container-info-contribution.d.ts +11 -0
  11. package/lib/electron-browser/container-info-contribution.d.ts.map +1 -0
  12. package/lib/electron-browser/container-info-contribution.js +51 -0
  13. package/lib/electron-browser/container-info-contribution.js.map +1 -0
  14. package/lib/electron-browser/container-output-provider.d.ts +8 -0
  15. package/lib/electron-browser/container-output-provider.d.ts.map +1 -0
  16. package/lib/electron-browser/container-output-provider.js +41 -0
  17. package/lib/electron-browser/container-output-provider.js.map +1 -0
  18. package/lib/electron-browser/dev-container-frontend-module.d.ts +4 -0
  19. package/lib/electron-browser/dev-container-frontend-module.d.ts.map +1 -0
  20. package/lib/electron-browser/dev-container-frontend-module.js +39 -0
  21. package/lib/electron-browser/dev-container-frontend-module.js.map +1 -0
  22. package/lib/electron-common/container-output-provider.d.ts +4 -0
  23. package/lib/electron-common/container-output-provider.d.ts.map +1 -0
  24. package/lib/electron-common/container-output-provider.js +18 -0
  25. package/lib/electron-common/container-output-provider.js.map +1 -0
  26. package/lib/electron-common/dev-container-workspaces.d.ts +3 -0
  27. package/lib/electron-common/dev-container-workspaces.d.ts.map +1 -0
  28. package/lib/electron-common/dev-container-workspaces.js +21 -0
  29. package/lib/electron-common/dev-container-workspaces.js.map +1 -0
  30. package/lib/electron-common/remote-container-connection-provider.d.ts +30 -0
  31. package/lib/electron-common/remote-container-connection-provider.d.ts.map +1 -0
  32. package/lib/electron-common/remote-container-connection-provider.js +21 -0
  33. package/lib/electron-common/remote-container-connection-provider.js.map +1 -0
  34. package/lib/electron-node/dev-container-backend-module.d.ts +5 -0
  35. package/lib/electron-node/dev-container-backend-module.d.ts.map +1 -0
  36. package/lib/electron-node/dev-container-backend-module.js +58 -0
  37. package/lib/electron-node/dev-container-backend-module.js.map +1 -0
  38. package/lib/electron-node/dev-container-file-service.d.ts +10 -0
  39. package/lib/electron-node/dev-container-file-service.d.ts.map +1 -0
  40. package/lib/electron-node/dev-container-file-service.js +69 -0
  41. package/lib/electron-node/dev-container-file-service.js.map +1 -0
  42. package/lib/electron-node/dev-container-workspace-handler.d.ts +7 -0
  43. package/lib/electron-node/dev-container-workspace-handler.d.ts.map +1 -0
  44. package/lib/electron-node/dev-container-workspace-handler.js +21 -0
  45. package/lib/electron-node/dev-container-workspace-handler.js.map +1 -0
  46. package/lib/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.d.ts +17 -0
  47. package/lib/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.d.ts.map +1 -0
  48. package/lib/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.js +69 -0
  49. package/lib/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.js.map +1 -0
  50. package/lib/electron-node/devcontainer-contributions/main-container-creation-contributions.d.ts +38 -0
  51. package/lib/electron-node/devcontainer-contributions/main-container-creation-contributions.d.ts.map +1 -0
  52. package/lib/electron-node/devcontainer-contributions/main-container-creation-contributions.js +192 -0
  53. package/lib/electron-node/devcontainer-contributions/main-container-creation-contributions.js.map +1 -0
  54. package/lib/electron-node/devcontainer-contributions/profile-file-modification-contribution.d.ts +11 -0
  55. package/lib/electron-node/devcontainer-contributions/profile-file-modification-contribution.d.ts.map +1 -0
  56. package/lib/electron-node/devcontainer-contributions/profile-file-modification-contribution.js +37 -0
  57. package/lib/electron-node/devcontainer-contributions/profile-file-modification-contribution.js.map +1 -0
  58. package/lib/electron-node/devcontainer-file.d.ts +350 -0
  59. package/lib/electron-node/devcontainer-file.d.ts.map +1 -0
  60. package/lib/electron-node/devcontainer-file.js +18 -0
  61. package/lib/electron-node/devcontainer-file.js.map +1 -0
  62. package/lib/electron-node/docker-container-service.d.ts +29 -0
  63. package/lib/electron-node/docker-container-service.d.ts.map +1 -0
  64. package/lib/electron-node/docker-container-service.js +106 -0
  65. package/lib/electron-node/docker-container-service.js.map +1 -0
  66. package/lib/electron-node/remote-container-connection-provider.d.ts +79 -0
  67. package/lib/electron-node/remote-container-connection-provider.d.ts.map +1 -0
  68. package/lib/electron-node/remote-container-connection-provider.js +260 -0
  69. package/lib/electron-node/remote-container-connection-provider.js.map +1 -0
  70. package/lib/package.spec.d.ts +1 -0
  71. package/lib/package.spec.d.ts.map +1 -0
  72. package/lib/package.spec.js +26 -0
  73. package/lib/package.spec.js.map +1 -0
  74. package/package.json +55 -0
  75. package/src/dev-container-server/dev-container-server.ts +53 -0
  76. package/src/electron-browser/container-connection-contribution.ts +151 -0
  77. package/src/electron-browser/container-info-contribution.ts +47 -0
  78. package/src/electron-browser/container-output-provider.ts +36 -0
  79. package/src/electron-browser/dev-container-frontend-module.ts +40 -0
  80. package/src/electron-common/container-output-provider.ts +19 -0
  81. package/src/electron-common/dev-container-workspaces.ts +18 -0
  82. package/src/electron-common/remote-container-connection-provider.ts +52 -0
  83. package/src/electron-node/dev-container-backend-module.ts +63 -0
  84. package/src/electron-node/dev-container-file-service.ts +67 -0
  85. package/src/electron-node/dev-container-workspace-handler.ts +33 -0
  86. package/src/electron-node/devcontainer-contributions/cli-enhancing-creation-contributions.ts +68 -0
  87. package/src/electron-node/devcontainer-contributions/main-container-creation-contributions.ts +196 -0
  88. package/src/electron-node/devcontainer-contributions/profile-file-modification-contribution.ts +35 -0
  89. package/src/electron-node/devcontainer-file.ts +411 -0
  90. package/src/electron-node/docker-container-service.ts +131 -0
  91. package/src/electron-node/remote-container-connection-provider.ts +315 -0
  92. package/src/package.spec.ts +28 -0
@@ -0,0 +1,411 @@
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
+ /**
18
+ * Defines a dev container
19
+ * type generated from https://containers.dev/implementors/json_schema/ and modified
20
+ */
21
+ export type DevContainerConfiguration = (((DockerfileContainer | ImageContainer) & (NonComposeContainerBase)) | ComposeContainer) & DevContainerCommon & { location?: string };
22
+
23
+ export type DockerfileContainer = {
24
+ /**
25
+ * Docker build-related options.
26
+ */
27
+ build: {
28
+ /**
29
+ * The location of the Dockerfile that defines the contents of the container. The path is relative to the folder containing the `devcontainer.json` file.
30
+ */
31
+ dockerfile: string
32
+ /**
33
+ * The location of the context folder for building the Docker image. The path is relative to the folder containing the `devcontainer.json` file.
34
+ */
35
+ context?: string
36
+ } & BuildOptions
37
+ [k: string]: unknown
38
+ } | {
39
+ /**
40
+ * The location of the Dockerfile that defines the contents of the container. The path is relative to the folder containing the `devcontainer.json` file.
41
+ */
42
+ dockerFile: string
43
+ /**
44
+ * The location of the context folder for building the Docker image. The path is relative to the folder containing the `devcontainer.json` file.
45
+ */
46
+ context?: string
47
+
48
+ /**
49
+ * Docker build-related options.
50
+ */
51
+ build?: {
52
+ /**
53
+ * Target stage in a multi-stage build.
54
+ */
55
+ target?: string
56
+ /**
57
+ * Build arguments.
58
+ */
59
+ args?: {
60
+ [k: string]: string
61
+ }
62
+ /**
63
+ * The image to consider as a cache. Use an array to specify multiple images.
64
+ */
65
+ cacheFrom?: string | string[]
66
+ [k: string]: unknown
67
+ }
68
+ [k: string]: unknown
69
+ };
70
+
71
+ export interface BuildOptions {
72
+ /**
73
+ * Target stage in a multi-stage build.
74
+ */
75
+ target?: string
76
+ /**
77
+ * Build arguments.
78
+ */
79
+ args?: {
80
+ [k: string]: string
81
+ }
82
+ /**
83
+ * The image to consider as a cache. Use an array to specify multiple images.
84
+ */
85
+ cacheFrom?: string | string[]
86
+ [k: string]: unknown
87
+ }
88
+ export interface ImageContainer {
89
+ /**
90
+ * The docker image that will be used to create the container.
91
+ */
92
+ image: string
93
+ [k: string]: unknown
94
+ }
95
+
96
+ export interface NonComposeContainerBase {
97
+ /**
98
+ * Application ports that are exposed by the container. This can be a single port or an array of ports. Each port can be a number or a string. A number is mapped to
99
+ * the same port on the host. A string is passed to Docker unchanged and can be used to map ports differently, e.g. '8000:8010'.
100
+ */
101
+ appPort?: number | string | (number | string)[]
102
+ /**
103
+ * Container environment variables.
104
+ */
105
+ containerEnv?: {
106
+ [k: string]: string
107
+ }
108
+ /**
109
+ * The user the container will be started with. The default is the user on the Docker image.
110
+ */
111
+ containerUser?: string
112
+ /**
113
+ * Mount points to set up when creating the container. See Docker's documentation for the --mount option for the supported syntax.
114
+ */
115
+ mounts?: (string | MountConfig)[]
116
+ /**
117
+ * The arguments required when starting in the container.
118
+ */
119
+ runArgs?: string[]
120
+ /**
121
+ * Action to take when the user disconnects from the container in their editor. The default is to stop the container.
122
+ */
123
+ shutdownAction?: 'none' | 'stopContainer'
124
+ /**
125
+ * Whether to overwrite the command specified in the image. The default is true.
126
+ */
127
+ overrideCommand?: boolean
128
+ /**
129
+ * The path of the workspace folder inside the container.
130
+ */
131
+ workspaceFolder?: string
132
+ /**
133
+ * The --mount parameter for docker run. The default is to mount the project folder at /workspaces/$project.
134
+ */
135
+ workspaceMount?: string
136
+ [k: string]: unknown
137
+ }
138
+
139
+ export interface ComposeContainer {
140
+ /**
141
+ * The name of the docker-compose file(s) used to start the services.
142
+ */
143
+ dockerComposeFile: string | string[]
144
+ /**
145
+ * The service you want to work on. This is considered the primary container for your dev environment which your editor will connect to.
146
+ */
147
+ service: string
148
+ /**
149
+ * An array of services that should be started and stopped.
150
+ */
151
+ runServices?: string[]
152
+ /**
153
+ * The path of the workspace folder inside the container. This is typically the target path of a volume mount in the docker-compose.yml.
154
+ */
155
+ workspaceFolder: string
156
+ /**
157
+ * Action to take when the user disconnects from the primary container in their editor. The default is to stop all of the compose containers.
158
+ */
159
+ shutdownAction?: 'none' | 'stopCompose'
160
+ /**
161
+ * Whether to overwrite the command specified in the image. The default is false.
162
+ */
163
+ overrideCommand?: boolean
164
+ [k: string]: unknown
165
+ }
166
+
167
+ export interface DevContainerCommon {
168
+ /**
169
+ * A name for the dev container which can be displayed to the user.
170
+ */
171
+ name?: string
172
+ /**
173
+ * Features to add to the dev container.
174
+ */
175
+ features?: {
176
+ [k: string]: unknown
177
+ }
178
+ /**
179
+ * Array consisting of the Feature id (without the semantic version) of Features in the order the user wants them to be installed.
180
+ */
181
+ overrideFeatureInstallOrder?: string[]
182
+ /**
183
+ * Ports that are forwarded from the container to the local machine. Can be an integer port number, or a string of the format 'host:port_number'.
184
+ */
185
+ forwardPorts?: (number | string)[]
186
+ portsAttributes?: {
187
+ /**
188
+ * A port, range of ports (ex. '40000-55000'), or regular expression (ex. '.+\\/server.js').
189
+ * For a port number or range, the attributes will apply to that port number or range of port numbers.
190
+ * Attributes which use a regular expression will apply to ports whose associated process command line matches the expression.
191
+ *
192
+ * This interface was referenced by `undefined`'s JSON-Schema definition
193
+ * via the `patternProperty` '(^\d+(-\d+)?$)|(.+)'.
194
+ */
195
+ [k: string]: {
196
+ /**
197
+ * Defines the action that occurs when the port is discovered for automatic forwarding
198
+ */
199
+ onAutoForward?:
200
+ | 'notify'
201
+ | 'openBrowser'
202
+ | 'openBrowserOnce'
203
+ | 'openPreview'
204
+ | 'silent'
205
+ | 'ignore'
206
+ /**
207
+ * Automatically prompt for elevation (if needed) when this port is forwarded. Elevate is required if the local port is a privileged port.
208
+ */
209
+ elevateIfNeeded?: boolean
210
+ /**
211
+ * Label that will be shown in the UI for this port.
212
+ */
213
+ label?: string
214
+ requireLocalPort?: boolean
215
+ /**
216
+ * The protocol to use when forwarding this port.
217
+ */
218
+ protocol?: 'http' | 'https'
219
+ [k: string]: unknown
220
+ }
221
+ }
222
+ otherPortsAttributes?: {
223
+ /**
224
+ * Defines the action that occurs when the port is discovered for automatic forwarding
225
+ */
226
+ onAutoForward?:
227
+ | 'notify'
228
+ | 'openBrowser'
229
+ | 'openPreview'
230
+ | 'silent'
231
+ | 'ignore'
232
+ /**
233
+ * Automatically prompt for elevation (if needed) when this port is forwarded. Elevate is required if the local port is a privileged port.
234
+ */
235
+ elevateIfNeeded?: boolean
236
+ /**
237
+ * Label that will be shown in the UI for this port.
238
+ */
239
+ label?: string
240
+ requireLocalPort?: boolean
241
+ /**
242
+ * The protocol to use when forwarding this port.
243
+ */
244
+ protocol?: 'http' | 'https'
245
+ }
246
+ /**
247
+ * Controls whether on Linux the container's user should be updated with the local user's UID and GID. On by default when opening from a local folder.
248
+ */
249
+ updateRemoteUserUID?: boolean
250
+ /**
251
+ * Remote environment variables to set for processes spawned in the container including lifecycle scripts and any remote editor/IDE server process.
252
+ */
253
+ remoteEnv?: {
254
+ [k: string]: string | null
255
+ }
256
+ /**
257
+ * The username to use for spawning processes in the container including lifecycle scripts and any remote editor/IDE server process.
258
+ * The default is the same user as the container.
259
+ */
260
+ remoteUser?: string
261
+
262
+ /**
263
+ * extensions to install in the container at launch. The expeceted format is publisher.name[@version].
264
+ * The default is no extensions being installed.
265
+ */
266
+ extensions?: string[]
267
+
268
+ /**
269
+ * settings to set in the container at launch in the settings.json. The expected format is key=value.
270
+ * The default is no preferences being set.
271
+ */
272
+ settings?: {
273
+ [k: string]: unknown
274
+ }
275
+
276
+ /**
277
+ * A command to run locally before anything else. This command is run before 'onCreateCommand'.
278
+ * If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell.
279
+ */
280
+ initializeCommand?: string | string[]
281
+ /**
282
+ * A command to run when creating the container. This command is run after 'initializeCommand' and before 'updateContentCommand'.
283
+ * If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell.
284
+ */
285
+ onCreateCommand?:
286
+ | string
287
+ | string[]
288
+ | {
289
+ [k: string]: string | string[]
290
+ }
291
+ /**
292
+ * A command to run when creating the container and rerun when the workspace content was updated while creating the container.
293
+ * This command is run after 'onCreateCommand' and before 'postCreateCommand'. If this is a single string, it will be run in a shell.
294
+ * If this is an array of strings, it will be run as a single command without shell.
295
+ */
296
+ updateContentCommand?:
297
+ | string
298
+ | string[]
299
+ | {
300
+ [k: string]: string | string[]
301
+ }
302
+ /**
303
+ * A command to run after creating the container. This command is run after 'updateContentCommand' and before 'postStartCommand'.
304
+ * If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell.
305
+ */
306
+ postCreateCommand?:
307
+ | string
308
+ | string[]
309
+ | {
310
+ [k: string]: string | string[]
311
+ }
312
+ /**
313
+ * A command to run after starting the container. This command is run after 'postCreateCommand' and before 'postAttachCommand'.
314
+ * If this is a single string, it will be run in a shell. If this is an array of strings, it will be run as a single command without shell.
315
+ */
316
+ postStartCommand?:
317
+ | string
318
+ | string[]
319
+ | {
320
+ [k: string]: string | string[]
321
+ }
322
+ /**
323
+ * A command to run when attaching to the container. This command is run after 'postStartCommand'. If this is a single string, it will be run in a shell.
324
+ * If this is an array of strings, it will be run as a single command without shell.
325
+ */
326
+ postAttachCommand?:
327
+ | string
328
+ | string[]
329
+ | {
330
+ [k: string]: string | string[]
331
+ }
332
+ /**
333
+ * The user command to wait for before continuing execution in the background while the UI is starting up. The default is 'updateContentCommand'.
334
+ */
335
+ waitFor?:
336
+ | 'initializeCommand'
337
+ | 'onCreateCommand'
338
+ | 'updateContentCommand'
339
+ | 'postCreateCommand'
340
+ | 'postStartCommand'
341
+ /**
342
+ * User environment probe to run. The default is 'loginInteractiveShell'.
343
+ */
344
+ userEnvProbe?:
345
+ | 'none'
346
+ | 'loginShell'
347
+ | 'loginInteractiveShell'
348
+ | 'interactiveShell'
349
+ /**
350
+ * Host hardware requirements.
351
+ */
352
+ hostRequirements?: {
353
+ /**
354
+ * Number of required CPUs.
355
+ */
356
+ cpus?: number
357
+ /**
358
+ * Amount of required RAM in bytes. Supports units tb, gb, mb and kb.
359
+ */
360
+ memory?: string
361
+ /**
362
+ * Amount of required disk space in bytes. Supports units tb, gb, mb and kb.
363
+ */
364
+ storage?: string
365
+ gpu?:
366
+ | (true | false | 'optional')
367
+ | {
368
+ /**
369
+ * Number of required cores.
370
+ */
371
+ cores?: number
372
+ /**
373
+ * Amount of required RAM in bytes. Supports units tb, gb, mb and kb.
374
+ */
375
+ memory?: string
376
+ }
377
+ [k: string]: unknown
378
+ }
379
+ /**
380
+ * Tool-specific configuration. Each tool should use a JSON object subproperty with a unique name to group its customizations.
381
+ */
382
+ customizations?: {
383
+ [k: string]: unknown,
384
+ vscode?: {
385
+ /**
386
+ * extensions to install in the container at launch. The expeceted format is publisher.name[@version].
387
+ * The default is no extensions being installed.
388
+ */
389
+ extensions?: string[],
390
+
391
+ /**
392
+ * settings to set in the container at launch in the settings.json. The expected format is key=value.
393
+ * The default is no preferences being set.
394
+ */
395
+ settings?: {
396
+ [k: string]: unknown
397
+ }
398
+ [k: string]: unknown
399
+ }
400
+ }
401
+ additionalProperties?: {
402
+ [k: string]: unknown
403
+ }
404
+ [k: string]: unknown
405
+ }
406
+
407
+ export interface MountConfig {
408
+ source: string,
409
+ target: string,
410
+ type: 'volume' | 'bind',
411
+ }
@@ -0,0 +1,131 @@
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 { ContributionProvider, MaybePromise, URI } from '@theia/core';
18
+ import { inject, injectable, named } from '@theia/core/shared/inversify';
19
+ import { WorkspaceServer } from '@theia/workspace/lib/common';
20
+ import * as fs from '@theia/core/shared/fs-extra';
21
+ import * as Docker from 'dockerode';
22
+ import { ContainerConnectionOptions } from '../electron-common/remote-container-connection-provider';
23
+ import { DevContainerConfiguration } from './devcontainer-file';
24
+ import { DevContainerFileService } from './dev-container-file-service';
25
+ import { ContainerOutputProvider } from '../electron-common/container-output-provider';
26
+ import { RemoteDockerContainerConnection } from './remote-container-connection-provider';
27
+
28
+ export const ContainerCreationContribution = Symbol('ContainerCreationContributions');
29
+
30
+ export interface ContainerCreationContribution {
31
+ handleContainerCreation?(createOptions: Docker.ContainerCreateOptions,
32
+ containerConfig: DevContainerConfiguration,
33
+ api: Docker,
34
+ outputProvider?: ContainerOutputProvider): MaybePromise<void>;
35
+
36
+ /**
37
+ * executed after creating and starting the container
38
+ */
39
+ handlePostCreate?(containerConfig: DevContainerConfiguration,
40
+ container: Docker.Container,
41
+ api: Docker,
42
+ outputProvider?: ContainerOutputProvider): MaybePromise<void>;
43
+
44
+ /**
45
+ * executed after a connection has been established with the container and theia has been setup
46
+ */
47
+ handlePostConnect?(containerConfig: DevContainerConfiguration, connection: RemoteDockerContainerConnection,
48
+ outputProvider?: ContainerOutputProvider): MaybePromise<void>;
49
+ }
50
+
51
+ @injectable()
52
+ export class DockerContainerService {
53
+
54
+ @inject(WorkspaceServer)
55
+ protected readonly workspaceServer: WorkspaceServer;
56
+
57
+ @inject(ContributionProvider) @named(ContainerCreationContribution)
58
+ protected readonly containerCreationContributions: ContributionProvider<ContainerCreationContribution>;
59
+
60
+ @inject(DevContainerFileService)
61
+ protected readonly devContainerFileService: DevContainerFileService;
62
+
63
+ async getOrCreateContainer(docker: Docker, options: ContainerConnectionOptions, outputProvider?: ContainerOutputProvider): Promise<Docker.Container> {
64
+ let container;
65
+
66
+ const workspace = new URI(options.workspaceUri ?? await this.workspaceServer.getMostRecentlyUsedWorkspace());
67
+
68
+ if (options.lastContainerInfo && fs.statSync(options.devcontainerFile).mtimeMs < options.lastContainerInfo.lastUsed) {
69
+ try {
70
+ container = docker.getContainer(options.lastContainerInfo.id);
71
+ if ((await container.inspect()).State.Running) {
72
+ await container.restart();
73
+ } else {
74
+ await container.start();
75
+ }
76
+ } catch (e) {
77
+ container = undefined;
78
+ console.warn('DevContainer: could not find last used container');
79
+ }
80
+ }
81
+ if (!container) {
82
+ container = await this.buildContainer(docker, options.devcontainerFile, workspace, outputProvider);
83
+ }
84
+ return container;
85
+ }
86
+
87
+ async postConnect(devcontainerFile: string, connection: RemoteDockerContainerConnection, outputProvider?: ContainerOutputProvider): Promise<void> {
88
+ const devcontainerConfig = await this.devContainerFileService.getConfiguration(devcontainerFile);
89
+
90
+ for (const containerCreateContrib of this.containerCreationContributions.getContributions()) {
91
+ await containerCreateContrib.handlePostConnect?.(devcontainerConfig, connection, outputProvider);
92
+ }
93
+
94
+ }
95
+
96
+ protected async buildContainer(docker: Docker, devcontainerFile: string, workspace: URI, outputProvider?: ContainerOutputProvider): Promise<Docker.Container> {
97
+ const devcontainerConfig = await this.devContainerFileService.getConfiguration(devcontainerFile);
98
+
99
+ if (!devcontainerConfig) {
100
+ // TODO add ability for user to create new config
101
+ throw new Error('No devcontainer.json');
102
+ }
103
+
104
+ const containerCreateOptions: Docker.ContainerCreateOptions = {
105
+ Tty: true,
106
+ ExposedPorts: {},
107
+ HostConfig: {
108
+ PortBindings: {},
109
+ Mounts: [{
110
+ Source: workspace.path.toString(),
111
+ Target: `/workspaces/${workspace.path.name}`,
112
+ Type: 'bind'
113
+ }],
114
+ },
115
+ };
116
+
117
+ for (const containerCreateContrib of this.containerCreationContributions.getContributions()) {
118
+ await containerCreateContrib.handleContainerCreation?.(containerCreateOptions, devcontainerConfig, docker, outputProvider);
119
+ }
120
+
121
+ // TODO add more config
122
+ const container = await docker.createContainer(containerCreateOptions);
123
+ await container.start();
124
+
125
+ for (const containerCreateContrib of this.containerCreationContributions.getContributions()) {
126
+ await containerCreateContrib.handlePostCreate?.(devcontainerConfig, container, docker, outputProvider);
127
+ }
128
+
129
+ return container;
130
+ }
131
+ }