-
-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathnew-cloud-sketch.ts
267 lines (255 loc) · 8.93 KB
/
new-cloud-sketch.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
import { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { CompositeTreeNode } from '@theia/core/lib/browser/tree';
import { codicon } from '@theia/core/lib/browser/widgets/widget';
import {
Disposable,
DisposableCollection,
} from '@theia/core/lib/common/disposable';
import { Emitter } from '@theia/core/lib/common/event';
import { nls } from '@theia/core/lib/common/nls';
import { inject, injectable } from '@theia/core/shared/inversify';
import { CreateApi } from '../create/create-api';
import { CreateUri } from '../create/create-uri';
import { Create } from '../create/typings';
import { WorkspaceInputDialog } from '../theia/workspace/workspace-input-dialog';
import { CloudSketchbookTree } from '../widgets/cloud-sketchbook/cloud-sketchbook-tree';
import { CloudSketchbookTreeModel } from '../widgets/cloud-sketchbook/cloud-sketchbook-tree-model';
import { CloudSketchbookTreeWidget } from '../widgets/cloud-sketchbook/cloud-sketchbook-tree-widget';
import { SketchbookCommands } from '../widgets/sketchbook/sketchbook-commands';
import { SketchbookWidget } from '../widgets/sketchbook/sketchbook-widget';
import { SketchbookWidgetContribution } from '../widgets/sketchbook/sketchbook-widget-contribution';
import { Command, CommandRegistry, Contribution, URI } from './contribution';
@injectable()
export class NewCloudSketch extends Contribution {
@inject(CreateApi)
private readonly createApi: CreateApi;
@inject(SketchbookWidgetContribution)
private readonly sketchbookWidgetContribution: SketchbookWidgetContribution;
private toDisposeOnNewTreeModel: Disposable | undefined;
private treeModel: CloudSketchbookTreeModel | undefined;
private readonly onDidChangeEmitter = new Emitter<void>();
private readonly toDisposeOnStop = new DisposableCollection(
this.onDidChangeEmitter
);
override onReady(): void {
const handleCurrentTreeDidChange = (widget: SketchbookWidget) => {
this.toDisposeOnStop.push(
widget.onCurrentTreeDidChange(() => this.onDidChangeEmitter.fire())
);
const treeWidget = widget.getTreeWidget();
if (treeWidget instanceof CloudSketchbookTreeWidget) {
this.onDidChangeEmitter.fire();
}
};
const widget = this.sketchbookWidgetContribution.tryGetWidget();
if (widget) {
handleCurrentTreeDidChange(widget);
} else {
this.sketchbookWidgetContribution.widget.then(handleCurrentTreeDidChange);
}
}
onStop(): void {
this.toDisposeOnStop.dispose();
if (this.toDisposeOnNewTreeModel) {
this.toDisposeOnNewTreeModel.dispose();
}
}
override registerCommands(registry: CommandRegistry): void {
registry.registerCommand(NewCloudSketch.Commands.CREATE_SKETCH, {
execute: () => this.createNewSketch(),
isEnabled: () => !!this.treeModel,
});
registry.registerCommand(NewCloudSketch.Commands.CREATE_SKETCH_TOOLBAR, {
execute: () =>
this.commandService.executeCommand(
NewCloudSketch.Commands.CREATE_SKETCH.id
),
isVisible: (arg: unknown) => {
if (arg instanceof SketchbookWidget) {
const treeWidget = arg.getTreeWidget();
if (treeWidget instanceof CloudSketchbookTreeWidget) {
const model = treeWidget.model;
if (model instanceof CloudSketchbookTreeModel) {
if (this.treeModel !== model) {
this.treeModel = model;
if (this.toDisposeOnNewTreeModel) {
this.toDisposeOnNewTreeModel.dispose();
this.toDisposeOnNewTreeModel = this.treeModel.onChanged(() =>
this.onDidChangeEmitter.fire()
);
}
}
}
}
return (
!!this.treeModel && treeWidget instanceof CloudSketchbookTreeWidget
);
}
return false;
},
});
}
override registerToolbarItems(registry: TabBarToolbarRegistry): void {
registry.registerItem({
id: NewCloudSketch.Commands.CREATE_SKETCH_TOOLBAR.id,
command: NewCloudSketch.Commands.CREATE_SKETCH_TOOLBAR.id,
tooltip: NewCloudSketch.Commands.CREATE_SKETCH_TOOLBAR.label,
onDidChange: this.onDidChangeEmitter.event,
});
}
private async createNewSketch(
initialValue?: string | undefined
): Promise<URI | undefined> {
if (!this.treeModel) {
return undefined;
}
const newSketchName = await this.newSketchName(initialValue);
if (!newSketchName) {
return undefined;
}
let result: Create.Sketch | undefined | 'conflict';
try {
result = await this.createApi.createSketch(newSketchName);
} catch (err) {
if (isConflict(err)) {
result = 'conflict';
} else {
throw err;
}
} finally {
if (result) {
await this.treeModel.updateRoot();
await this.treeModel.refresh();
}
}
if (result === 'conflict') {
return this.createNewSketch(newSketchName);
}
if (result) {
const newSketch = result;
const treeModel = this.treeModel;
const yes = nls.localize('vscode/extensionsUtils/yes', 'Yes');
this.messageService
.info(
nls.localize(
'arduino/newCloudSketch/openNewSketch',
'Do you want to pull the new remote sketch {0} and open it in a new window?',
newSketchName
),
yes
)
.then(async (answer) => {
if (answer === yes) {
const node = treeModel.getNode(
CreateUri.toUri(newSketch).path.toString()
);
if (!node) {
return;
}
if (CloudSketchbookTree.CloudSketchDirNode.is(node)) {
try {
await treeModel.sketchbookTree().pull({ node });
} catch (err) {
if (isNotFound(err)) {
await treeModel.updateRoot();
await treeModel.refresh();
this.messageService.error(
nls.localize(
'arduino/newCloudSketch/notFound',
`Could not pull the remote sketch {0}. It does not exist.`,
newSketchName
)
);
return;
}
throw err;
}
return this.commandService.executeCommand(
SketchbookCommands.OPEN_NEW_WINDOW.id,
{ node }
);
}
}
});
}
return undefined;
}
private async newSketchName(
initialValue?: string | undefined
): Promise<string | undefined> {
const rootNode = this.rootNode();
if (!rootNode) {
return undefined;
}
const existingNames = rootNode.children
.filter(CloudSketchbookTree.CloudSketchDirNode.is)
.map(({ fileStat }) => fileStat.name);
return new WorkspaceInputDialog(
{
title: nls.localize(
'arduino/newCloudSketch/newSketchTitle',
'Name of a new remote sketch'
),
parentUri: CreateUri.root,
initialValue,
validate: (input) => {
if (existingNames.includes(input)) {
return nls.localize(
'arduino/newCloudSketch/sketchAlreadyExists',
"Remote sketch '{0}' already exists.",
input
);
}
// This is how https://create.arduino.cc/editor/ works when renaming a sketch.
if (/^[0-9a-zA-Z_]{1,36}$/.test(input)) {
return '';
}
return nls.localize(
'arduino/newCloudSketch/invalidSketchName',
'The name must consist of basic letters, numbers, or underscores. The maximum length is 36 characters.'
);
},
},
this.labelProvider
).open();
}
private rootNode(): CompositeTreeNode | undefined {
return this.treeModel && CompositeTreeNode.is(this.treeModel.root)
? this.treeModel.root
: undefined;
}
}
export namespace NewCloudSketch {
export namespace Commands {
export const CREATE_SKETCH = Command.toLocalizedCommand(
{
id: 'arduino-cloud-sketchbook--create-sketch',
label: 'New Remote Sketch...',
category: 'Arduino',
},
'arduino/newCloudSketch/createSketch'
) as Command & { label: string };
export const CREATE_SKETCH_TOOLBAR: Command & { label: string } = {
...CREATE_SKETCH,
id: `${CREATE_SKETCH.id}-toolbar`,
iconClass: codicon('new-folder'),
};
}
}
function isConflict(err: unknown): boolean {
return isErrorWithStatusOf(err, 409);
}
function isNotFound(err: unknown): boolean {
return isErrorWithStatusOf(err, 404);
}
function isErrorWithStatusOf(
err: unknown,
status: number
): err is Error & { status: number } {
if (err instanceof Error) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const object = err as any;
return 'status' in object && object.status === status;
}
return false;
}