forked from projen/projen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitpod.ts
416 lines (370 loc) · 9.99 KB
/
gitpod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import { Component } from './component';
import { IDevEnvironment, DevEnvironmentOptions, DevEnvironmentDockerImage } from './dev-env';
import { Project } from './project';
import { Task } from './tasks';
import { YamlFile } from './yaml';
/**
* Relative path of Gitpod file to generate
*/
const GITPOD_FILE = '.gitpod.yml';
/**
* https://www.gitpod.io/docs/configuration/
* https://www.gitpod.io/docs/config-start-tasks/
* https://www.gitpod.io/docs/prebuilds/#configure-the-github-app
* https://www.gitpod.io/docs/vscode-extensions/
*/
/**
* Configure how the terminal should be opened relative to the previous task.
*/
export enum GitpodOpenMode {
/**
* Opens in the same tab group right after the previous tab
*/
TAB_AFTER = 'tab-after',
/**
* Opens in the same tab group left before the previous tab
*/
TAB_BEFORE = 'tab-before',
/**
* Splits and adds the terminal to the right
*/
SPLIT_RIGHT = 'split-right',
/**
* Splits and adds the terminal to the left
*/
SPLIT_LEFT = 'split-left',
/**
* Splits and adds the terminal to the top
*/
SPLIT_TOP = 'split-top',
/**
* Splits and adds the terminal to the bottom
*/
SPLIT_BOTTOM = 'split-bottom'
}
/**
* Configure where in the IDE the terminal should be opened.
*/
export enum GitpodOpenIn {
/**
* the bottom panel (default)
*/
BOTTOM = 'bottom',
/**
* the left panel
*/
LEFT = 'left',
/**
* the right panel
*/
RIGHT = 'right',
/**
* the main editor area
*/
MAIN = 'main'
}
/**
* Configure options for a task to be run when opening a Gitpod
* workspace (e.g. running tests, or starting a dev server).
*
* Start Mode | Execution
* Fresh Workspace | before && init && command
* Restart Workspace | before && command
* Snapshot | before && command
* Prebuild | before && init && prebuild
*/
export interface GitpodTask {
/**
* Required. The shell command to run
*/
readonly command: string;
/**
* A name for this task.
* @default - task names are omitted when blank
*/
readonly name?: string;
/**
* You can configure how the terminal should be opened relative to the previous task.
* @default GitpodOpenMode.TAB_AFTER
*/
readonly openMode?: GitpodOpenMode;
/**
* You can configure where in the IDE the terminal should be opened
* @default GitpodOpenIn.BOTTOM
*/
readonly openIn?: GitpodOpenIn;
/**
* In case you need to run something even before init, that is a requirement for both init and command,
* you can use the before property.
* @default
*/
readonly before?: string;
/**
* The init property can be used to specify shell commands that should only be executed after a workspace
* was freshly cloned and needs to be initialized somehow. Such tasks are usually builds or downloading
* dependencies. Anything you only want to do once but not when you restart a workspace or start a snapshot.
* @default
*/
readonly init?: string;
/**
* The optional prebuild command will be executed during prebuilds. It is meant to run additional long running
* processes that could be useful, e.g. running test suites.
* @default
*/
readonly prebuild?: string;
}
/**
* What to do when a service on a port is detected.
*/
export enum GitpodOnOpen {
/**
* Open a new browser tab
*/
OPEN_BROWSER = 'open-browser',
/**
* Open a preview on the right side of the IDE
*/
OPEN_PREVIEW = 'open-preview',
/**
* Show a notification asking the user what to do (default)
*/
NOTIFY = 'notify',
/**
* Do nothing.
*/
IGNORE = 'ignore',
}
/**
* Whether the port visibility should be private or public
*/
export enum GitpodPortVisibility {
/**
* Allows everyone with the port URL to access the port (default)
*/
PUBLIC = 'public',
/**
* Only allows users with workspace access to access the port
*/
PRIVATE = 'private'
}
/**
* Options for an exposed port on Gitpod
*/
export interface GitpodPort {
/**
* A port that should be exposed (forwarded) from the container.
*
* @example "8080"
*/
readonly port?: string;
/**
* What to do when a service on a port is detected.
*
* @default GitpodOnOpen.NOTIFY
*/
readonly onOpen?: GitpodOnOpen;
/**
* Whether the port visibility should be private or public.
*
* @default GitpodPortVisibility.PUBLIC
*/
readonly visibility?: GitpodPortVisibility;
}
/**
* Configure the Gitpod App for prebuilds.
* Currently only GitHub is supported.
* @see https://www.gitpod.io/docs/prebuilds/
*/
export interface GitpodPrebuilds {
/**
* Enable for the master/default branch
* @default true
*/
readonly master?: boolean;
/**
* Enable for all branches in this repo
* @default false
*/
readonly branches?: boolean;
/**
* Enable for pull requests coming from this repo
* @default true
*/
readonly pullRequests?: boolean;
/**
* Enable for pull requests coming from forks
* @default false
*/
readonly pullRequestsFromForks?: boolean;
/**
* Add a check to pull requests
* @default true
*/
readonly addCheck?: boolean;
/**
* Add a "Review in Gitpod" button as a comment to pull requests
* @default false
*/
readonly addComment?: boolean;
/**
* Add a "Review in Gitpod" button to the pull request's description
* @default false
*/
readonly addBadge?: boolean;
/**
* Add a label once the prebuild is ready to pull requests
* @default false
*/
readonly addLabel?: boolean;
}
/**
* Constructor options for the Gitpod component.
*
* By default, Gitpod uses the 'gitpod/workspace-full' docker image.
* @see https://github.com/gitpod-io/workspace-images/blob/master/full/Dockerfile
*
* By default, all tasks will be run in parallel. To run the tasks in sequence,
* create a new task and specify the other tasks as subtasks.
*/
export interface GitpodOptions extends DevEnvironmentOptions {
/**
* Optional Gitpod's Github App integration for prebuilds
* If this is not set and Gitpod's Github App is installed, then Gitpod will apply
* these defaults: https://www.gitpod.io/docs/prebuilds/#configure-the-github-app
* @default undefined
*/
readonly prebuilds?: GitpodPrebuilds;
}
/**
* The Gitpod component which emits .gitpod.yml
*/
export class Gitpod extends Component implements IDevEnvironment {
private dockerImage: DevEnvironmentDockerImage | undefined;
private prebuilds: GitpodPrebuilds | undefined;
private readonly tasks = new Array<GitpodTask>();
private readonly ports = new Array<GitpodPort>();
private readonly vscodeExtensions = new Array<string>();
/**
* Direct access to the gitpod configuration (escape hatch)
*/
public readonly config: any;
constructor(project: Project, options: GitpodOptions = {}) {
super(project);
this.dockerImage = options?.dockerImage;
if (options?.tasks) {
for (const task of options.tasks) {
this.addTasks(task);
}
}
if (options?.prebuilds) {
this.addPrebuilds(options?.prebuilds);
}
this.config = {
image: () => this.renderDockerImage(),
tasks: this.tasks,
github: () => this.renderPrebuilds(),
ports: this.ports,
vscode: {
extensions: this.vscodeExtensions,
},
};
new YamlFile(this.project, GITPOD_FILE, { obj: this.config, omitEmpty: true, marker: true });
}
/**
* Add a custom Docker image or Dockerfile for the container.
*
* @param image The Docker image
*/
public addDockerImage(image: DevEnvironmentDockerImage) {
if (this.dockerImage) {
throw new Error('dockerImage cannot be redefined.');
}
this.dockerImage = image;
}
/**
* Add tasks to run when gitpod starts.
*
* By default, all tasks will be run in parallel. To run tasks in sequence,
* create a new `Task` and specify the other tasks as subtasks.
*
* @param tasks The new tasks
*/
public addTasks(...tasks: Task[]) {
this.tasks.push(...tasks.map(task => ({
name: task.name,
command: `npx projen ${task.name}`,
})));
}
/**
* Add a prebuilds configuration for the Gitpod App
* @param config The configuration
*/
public addPrebuilds(config: GitpodPrebuilds) {
this.prebuilds = config;
}
/**
* Add a task with more granular options.
*
* By default, all tasks will be run in parallel. To run tasks in sequence,
* create a new `Task` and set the other tasks as subtasks.
*
* @param options The task parameters
*/
public addCustomTask(options: GitpodTask) {
this.tasks.push({
name: options.name,
command: options.command,
openMode: options.openMode,
openIn: options.openIn,
before: options.before,
init: options.init,
prebuild: options.prebuild,
});
}
/**
* Add ports that should be exposed (forwarded) from the container.
*
* @param ports The new ports
*/
public addPorts(...ports: string[]) {
this.ports.push(...ports.map(port => ({ port: port })));
}
/**
* Add a list of VSCode extensions that should be automatically installed
* in the container.
*
* These must be in the format defined in the Open VSX registry.
* @example 'scala-lang.scala@0.3.9:O5XmjwY5Gz+0oDZAmqneJw=='
* @see https://www.gitpod.io/docs/vscode-extensions/
*
* @param extensions The extension IDs
*/
public addVscodeExtensions(...extensions: GitpodCodeExtensionId[]) {
this.vscodeExtensions.push(...extensions);
}
private renderDockerImage() {
if (this.dockerImage?.image) {
return this.dockerImage.image;
} else if (this.dockerImage?.dockerFile) {
return {
file: this.dockerImage.dockerFile,
};
} else {
return undefined;
}
}
private renderPrebuilds() {
if (this.prebuilds) {
return {
prebuilds: this.prebuilds,
};
} else {
return undefined;
}
}
}
/**
* VS Code extensions as defined in the Open VSX registry
* Example: `scala-lang.scala@0.3.9:O5XmjwY5Gz+0oDZAmqneJw==`
*/
type GitpodCodeExtensionId = string;