-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlib.ts
More file actions
662 lines (615 loc) · 16.8 KB
/
lib.ts
File metadata and controls
662 lines (615 loc) · 16.8 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* ======== state ========
*/
import {
Course,
Organization,
RunResult,
StyleValidationResult,
SubmissionFinished,
} from "./langsSchema";
import * as util from "node:util";
import { createIs } from "typia";
import { Uri } from "vscode";
/**
* Contains the state of the webview.
*/
export type State = {
panel: Panel;
};
/*
* ======== panels ========
*/
/**
* Represents a panel that is rendered by the webview.
*
* `id`: used to make sure messages are delivered to the correct panels
*/
export type Panel =
| AppPanel
| WelcomePanel
| LoginPanel
| MyCoursesPanel
| CourseDetailsPanel
| SelectOrganizationPanel
| SelectCoursePanel
| ExerciseTestsPanel
| ExerciseSubmissionPanel
| InitializationErrorHelpPanel;
export type PanelType = Panel["type"];
// used to define messages that should only be sent to a specific instance of a panel
// for example, the course selected by the user on the SelectCoursePanel should only be sent
// to the panel which initiated the course selection
export type TargetPanel<T extends Panel> = Pick<Extract<Panel, { type: T["type"] }>, "id" | "type">;
// used to define messages that should be sent to any instance of a given panel type
// for example, a change in an exercise's status should be sent to all panels that display the status
export type BroadcastPanel<T extends Panel> = Pick<Extract<Panel, { type: T["type"] }>, "type">;
export type AppPanel = {
id: number;
type: "App";
};
export type WelcomePanel = {
id: number;
type: "Welcome";
version?: string;
};
export type LoginPanel = {
id: number;
type: "Login";
};
export type MyCoursesPanel = {
id: number;
type: "MyCourses";
courses?: Array<CourseData>;
tmcDataPath?: string;
tmcDataSize?: string;
courseDeadlines: Record<number, string>;
};
export type CourseDetailsPanel = {
id: number;
type: "CourseDetails";
courseId: number;
course?: CourseData;
offlineMode?: boolean;
exerciseGroups?: Array<ExerciseGroup>;
updateableExercises?: Array<number>;
disabled?: boolean;
exerciseStatuses?: Record<number, ExerciseStatus>;
};
export type SelectOrganizationPanel = {
id: number;
type: "SelectOrganization";
// the result of the selection is sent back to this panel
requestingPanel: TargetPanel<MyCoursesPanel>;
};
export type SelectCoursePanel = {
id: number;
type: "SelectCourse";
organizationSlug: string;
// the result of the selection is sent back to this panel
requestingPanel: TargetPanel<MyCoursesPanel>;
};
export type ExerciseTestsPanel = {
id: number;
type: "ExerciseTests";
course: TestCourse;
exercise: TestExercise;
exerciseUri: Uri;
testRunId: number;
};
export type ExerciseSubmissionPanel = {
id: number;
type: "ExerciseSubmission";
course: TestCourse;
exercise: TestExercise;
};
export type InitializationErrorHelpPanel = {
id: number;
type: "InitializationErrorHelp";
};
/*
* ======== messages to webview ========
*/
/**
* For use with `webview.postMessage` in `TmcPanel`.
* Handled by the Svelte app.
*/
export type ExtensionToWebview =
| {
type: "setPanel";
target: TargetPanel<AppPanel>;
panel: Panel;
}
| {
type: "setWelcomeData";
target: TargetPanel<WelcomePanel>;
version: string;
}
| {
type: "setMyCourses";
target: TargetPanel<MyCoursesPanel>;
courses: Array<CourseData>;
}
| {
type: "setTmcDataPath";
target: BroadcastPanel<MyCoursesPanel>;
tmcDataPath: string;
}
| {
type: "setNextCourseDeadline";
target: TargetPanel<MyCoursesPanel>;
courseId: number;
deadline: string;
}
| {
type: "setTmcDataSize";
target: TargetPanel<MyCoursesPanel>;
tmcDataSize: string;
}
| {
type: "loginError";
target: TargetPanel<LoginPanel>;
error: string;
}
| {
type: "setCourseData";
target: TargetPanel<CourseDetailsPanel>;
courseData: CourseData;
}
| {
type: "setCourseGroups";
target: TargetPanel<CourseDetailsPanel>;
offlineMode: boolean;
exerciseGroups: Array<ExerciseGroup>;
}
| {
type: "setCourseDisabledStatus";
target: BroadcastPanel<MyCoursesPanel | CourseDetailsPanel>;
courseId: number;
disabled: boolean;
}
| {
type: "exerciseStatusChange";
target: BroadcastPanel<CourseDetailsPanel>;
exerciseId: number;
status: ExerciseStatus;
}
| {
type: "setUpdateables";
target: BroadcastPanel<CourseDetailsPanel>;
exerciseIds: Array<number>;
}
| {
type: "setOrganizations";
target: TargetPanel<SelectOrganizationPanel>;
organizations: Array<Organization>;
}
| {
type: "setTmcBackendUrl";
target: TargetPanel<SelectOrganizationPanel | SelectCoursePanel>;
tmcBackendUrl: string;
}
| {
type: "setOrganization";
target: TargetPanel<SelectCoursePanel>;
organization: Organization;
}
| {
type: "setSelectableCourses";
target: TargetPanel<SelectCoursePanel>;
courses: Array<Course>;
}
| {
type: "testResults";
target: TargetPanel<ExerciseTestsPanel>;
testResults: TestResultData;
}
| {
type: "testError";
target: TargetPanel<ExerciseTestsPanel>;
error: BaseError;
}
| {
type: "pasteResult";
target: TargetPanel<ExerciseTestsPanel | ExerciseSubmissionPanel>;
pasteLink: string;
}
| {
type: "pasteError";
target: TargetPanel<ExerciseTestsPanel | ExerciseSubmissionPanel>;
error: string;
}
| {
type: "submissionStatusUrl";
target: TargetPanel<ExerciseSubmissionPanel>;
url: string;
}
| {
type: "submissionStatusUpdate";
target: TargetPanel<ExerciseSubmissionPanel>;
progressPercent: number;
message?: string;
}
| {
type: "submissionResult";
target: TargetPanel<ExerciseSubmissionPanel>;
result: SubmissionFinished;
questions: Array<FeedbackQuestion>;
}
| {
type: "submissionStatusError";
target: TargetPanel<ExerciseSubmissionPanel>;
error: Error;
}
| {
type: "setNewExercises";
target: BroadcastPanel<MyCoursesPanel>;
courseId: number;
exerciseIds: Array<number>;
}
| {
type: "willNotRunTestsForExam";
target: TargetPanel<ExerciseTestsPanel>;
}
| {
type: "initializationErrors";
target: TargetPanel<InitializationErrorHelpPanel>;
cliFolder: string;
initializationErrors: {
tmc: { error: string; stack: string } | null;
userData: { error: string; stack: string } | null;
workspaceManager: { error: string; stack: string } | null;
exerciseDecorationProvider: { error: string; stack: string } | null;
resources: { error: string; stack: string } | null;
};
}
// the last variant exists just to make TypeScript think that every panel type has
// at least two different message types, which makes TS treat them differently than if
// they only had one...
| {
type: never;
target: TargetPanel<never>;
};
// helper type for messages from the extension to a specific panel
export type TargetedExtensionToWebview<T extends PanelType> = Targeted<ExtensionToWebview, T>;
// helper type for messages from the extension to a specific panel type
export type BroadcastExtensionToWebview<T extends PanelType> = Broadcast<ExtensionToWebview, T>;
/*
* ======== from webview ========
*/
/**
* For use with `vscode.postMessage` in the Svelte app.
* Handled by the extension host in `TmcPanel`.
*/
export type WebviewToExtension =
| {
type: "requestCourseDetailsData";
sourcePanel: CourseDetailsPanel;
}
| {
type: "requestExerciseSubmissionData";
sourcePanel: ExerciseSubmissionPanel;
}
| {
type: "requestExerciseTestsData";
sourcePanel: ExerciseTestsPanel;
}
| {
type: "requestLoginData";
sourcePanel: LoginPanel;
}
| {
type: "requestMyCoursesData";
sourcePanel: MyCoursesPanel;
}
| {
type: "requestSelectCourseData";
sourcePanel: SelectCoursePanel;
}
| {
type: "requestSelectOrganizationData";
sourcePanel: SelectOrganizationPanel;
}
| {
type: "requestWelcomeData";
sourcePanel: WelcomePanel;
}
| {
type: "login";
sourcePanel: LoginPanel;
username: string;
password: string;
}
| {
type: "selectOrganization";
sourcePanel: TargetPanel<MyCoursesPanel>;
}
| {
type: "removeCourse";
id: number;
}
| {
type: "openCourseWorkspace";
courseName: string;
}
| {
type: "downloadExercises";
ids: Array<number>;
courseName: string;
organizationSlug: string;
courseId: number;
mode: "download" | "update";
}
| {
type: "clearNewExercises";
courseId: number;
}
| {
type: "changeTmcDataPath";
}
| {
type: "openCourseDetails";
courseId: number;
}
| {
type: "openMyCourses";
}
| {
type: "refreshCourseDetails";
id: number;
useCache: boolean;
}
| {
type: "openExercises";
ids: Array<number>;
courseName: string;
}
| {
type: "closeExercises";
ids: Array<number>;
courseName: string;
}
| {
type: "refreshCourseDetails";
id: number;
useCache: boolean;
}
| {
type: "selectCourse";
sourcePanel: TargetPanel<MyCoursesPanel>;
slug: string;
}
| {
type: "addCourse";
organizationSlug: string;
courseId: number;
requestingPanel: TargetPanel<MyCoursesPanel>;
}
| {
type: "relayToWebview";
// the message type is handled by the webview
message: unknown;
}
| {
type: "closeSidePanel";
}
| {
type: "cancelTests";
testRunId: number;
}
| {
type: "submitExercise";
course: TestCourse;
exercise: TestExercise;
exerciseUri: Uri;
}
| {
type: "pasteExercise";
course: TestCourse;
exercise: TestExercise;
requestingPanel: TargetPanel<ExerciseTestsPanel | ExerciseSubmissionPanel>;
}
| {
type: "openLinkInBrowser";
url: string;
}
| {
type: "requestInitializationErrors";
sourcePanel: InitializationErrorHelpPanel;
};
/*
* ======== additional types ========
*/
export type CourseData = {
id: number;
name: string;
title: string;
description: string;
organization: string;
awardedPoints: number;
availablePoints: number;
exercises: Array<NewExercise>;
newExercises: Array<number>;
disabled: boolean;
materialUrl: string | null;
perhapsExamMode: boolean;
};
export type NewExercise = {
id: number;
};
export type ExerciseGroup = {
name: string;
exercises: Array<Exercise>;
nextDeadlineString: string;
};
export type Exercise = {
id: number;
name: string;
isHard: boolean;
hardDeadlineString: string;
softDeadlineString: string;
passed: boolean;
};
export type ExerciseStatus =
| "closed"
| "downloading"
| "downloadFailed"
| "expired"
| "missing"
| "new"
| "opened";
export type TestExercise = {
id: number;
availablePoints: number;
awardedPoints: number;
/// Equivalent to exercise slug
name: string;
deadline: string | null;
passed: boolean;
softDeadline: string | null;
};
export type TestResultData = {
testResult: RunResult;
id: number;
courseSlug: string;
exerciseName: string;
tmcLogs: {
stdout?: string;
stderr?: string;
};
pasteLink?: string;
disabled?: boolean;
styleValidationResult?: StyleValidationResult | null;
};
export type TestCourse = {
id: number;
name: string;
title: string;
description: string;
organization: string;
availablePoints: number;
awardedPoints: number;
perhapsExamMode: boolean;
newExercises: number[];
notifyAfter: number;
disabled: boolean;
materialUrl: string | null;
};
export type FeedbackQuestion = {
id: number;
kind: string;
lower?: number;
upper?: number;
question: string;
};
/*
* ======== helpers ========
*/
// excludes from the message union all variants where the
// target type doesn't have the panel type
// works...somehow
export type Targeted<M, T extends PanelType> = Exclude<
M,
{ target: { type: Exclude<PanelType, T> } }
>;
export type Broadcast<M, T extends PanelType> = Omit<Targeted<M, T>, "target">;
export function assertUnreachable(x: never): never {
throw new Error(`Unreachable ${JSON.stringify(x, null, 2)}`);
}
/*
* ======== errors ========
*/
export class BaseError extends Error {
public readonly name: string = "Base Error";
public details?: string;
public cause?: NodeJS.ErrnoException | string;
public stack?: string;
// possible fields from ErrnoException
public errno?: number;
public code?: string;
public path?: string;
public syscall?: string;
constructor(err: unknown);
constructor(err: Error, details?: string);
constructor(message?: string, details?: string);
constructor(err: unknown, details?: string) {
let message = "";
let stack = "";
let cause: NodeJS.ErrnoException | string = "";
let errno: number | undefined = undefined;
let code: string | undefined = undefined;
let path: string | undefined = undefined;
let syscall: string | undefined = undefined;
// simple check first...
if (typeof err === "string") {
message = err;
} else if (util.types.isNativeError(err)) {
// deal with regular error stuff first
message = err.message;
if (err.stack) {
stack = err.stack;
}
// also check for special NodeJS error
if (createIs<NodeJS.ErrnoException>()(err)) {
// nodejs error with error code
errno = err.errno;
code = err.code;
path = err.path;
syscall = err.syscall;
}
if (err.cause) {
// same checks for cause
if (
util.types.isNativeError(err.cause) &&
createIs<NodeJS.ErrnoException>()(err.cause)
) {
cause = err.cause;
} else {
cause = err.cause.toString();
}
}
} else {
// it's expected that this function is only called with
// strings or error objects. but since errors are often "unknown"
// in catch statements etc., this function accepts unknown types
// and thus we'll handle them here just in case
message = `Unexpected error ${err} (${typeof err})`;
}
super(message);
this.details = details;
if (stack) {
this.stack = stack;
}
if (cause) {
this.cause = cause;
}
// errno fields
this.errno = errno;
this.code = code;
this.path = path;
this.syscall = syscall;
}
public toString(): string {
let errorMessage = "";
if (this.errno) {
errorMessage += `[${this.errno}] `;
}
if (this.code) {
errorMessage += `(${this.code}) `;
}
if (this.syscall) {
errorMessage += `\`${this.syscall}\` `;
}
if (this.path) {
errorMessage += `@${this.path} `;
}
errorMessage += `${this.name}: ${this.message}.`;
if (this.details) {
errorMessage += ` ${this.details}.`;
}
if (this.cause) {
errorMessage += ` Caused by: ${this.cause}.`;
}
return errorMessage;
}
}