- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathvalidate-sketch.ts
198 lines (187 loc) · 5.93 KB
/
validate-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
import{Dialog}from'@theia/core/lib/browser/dialogs';
import{nls}from'@theia/core/lib/common/nls';
import{Deferred,waitForEvent}from'@theia/core/lib/common/promise-util';
import{injectable}from'@theia/core/shared/inversify';
import{WorkspaceCommands}from'@theia/workspace/lib/browser/workspace-commands';
import{CurrentSketch}from'../sketches-service-client-impl';
import{CloudSketchContribution}from'./cloud-contribution';
import{Sketch,URI}from'./contribution';
import{SaveAsSketch}from'./save-as-sketch';
@injectable()
exportclassValidateSketchextendsCloudSketchContribution{
overrideonReady(): void{
this.validate();
}
privateasyncvalidate(): Promise<void>{
constresult=awaitthis.promptFixActions();
if(!result){
constyes=awaitthis.prompt(
nls.localize('arduino/validateSketch/abortFixTitle','Invalid sketch'),
nls.localize(
'arduino/validateSketch/abortFixMessage',
"The sketch is still invalid. Do you want to fix the remaining problems? By clicking '{0}', a new sketch will open.",
Dialog.NO
),
[Dialog.NO,Dialog.YES]
);
if(yes){
returnthis.validate();
}
constsketch=awaitthis.sketchesService.createNewSketch();
this.workspaceService.open(newURI(sketch.uri),{
preserveWindow: true,
});
}
}
/**
* Returns with an array of actions the user has to perform to fix the invalid sketch.
*/
privatevalidateSketch(
sketch: Sketch,
dataDirUri: URI|undefined
): FixAction[]{
// sketch code file validation errors first as they do not require window reload
constactions=Sketch.uris(sketch)
.filter((uri)=>uri!==sketch.mainFileUri)
.map((uri)=>newURI(uri))
.filter((uri)=>Sketch.Extensions.CODE_FILES.includes(uri.path.ext))
.map((uri)=>({
uri,
error: this.doValidate(sketch,dataDirUri,uri.path.name),
}))
.filter(({ error })=>Boolean(error))
.map((object)=><{uri: URI;error: string}>object)
.map(({ uri, error })=>({
execute: async()=>{
constunknown=
(awaitthis.promptRenameSketchFile(uri,error))&&
(awaitthis.commandService.executeCommand(
WorkspaceCommands.FILE_RENAME.id,
uri
));
return!!unknown;
},
}));
// sketch folder + main sketch file last as it requires a `Save as...` and the window reload
constsketchFolderName=newURI(sketch.uri).path.base;
constsketchFolderNameError=this.doValidate(
sketch,
dataDirUri,
sketchFolderName
);
if(sketchFolderNameError){
actions.push({
execute: async()=>{
constunknown=
(awaitthis.promptRenameSketch(sketch,sketchFolderNameError))&&
(awaitthis.commandService.executeCommand(
SaveAsSketch.Commands.SAVE_AS_SKETCH.id,
<SaveAsSketch.Options>{
markAsRecentlyOpened: true,
openAfterMove: true,
wipeOriginal: true,
}
));
return!!unknown;
},
});
}
returnactions;
}
privatedoValidate(
sketch: Sketch,
dataDirUri: URI|undefined,
toValidate: string
): string|undefined{
constcloudUri=this.createFeatures.isCloud(sketch,dataDirUri);
returncloudUri
? Sketch.validateCloudSketchFolderName(toValidate)
: Sketch.validateSketchFolderName(toValidate);
}
privateasynccurrentSketch(): Promise<Sketch>{
constsketch=this.sketchServiceClient.tryGetCurrentSketch();
if(CurrentSketch.isValid(sketch)){
returnsketch;
}
constdeferred=newDeferred<Sketch>();
constdisposable=this.sketchServiceClient.onCurrentSketchDidChange(
(sketch)=>{
if(CurrentSketch.isValid(sketch)){
disposable.dispose();
deferred.resolve(sketch);
}
}
);
returndeferred.promise;
}
privateasyncpromptFixActions(): Promise<boolean>{
constmaybeDataDirUri=this.configService.tryGetDataDirUri();
const[sketch,dataDirUri]=awaitPromise.all([
this.currentSketch(),
maybeDataDirUri??
waitForEvent(this.configService.onDidChangeDataDirUri,5_000),
]);
constfixActions=this.validateSketch(sketch,dataDirUri);
for(constfixActionoffixActions){
constresult=awaitfixAction.execute();
if(!result){
returnfalse;
}
}
returntrue;
}
privateasyncpromptRenameSketch(
sketch: Sketch,
error: string
): Promise<boolean>{
returnthis.prompt(
nls.localize(
'arduino/validateSketch/renameSketchFolderTitle',
'Invalid sketch name'
),
nls.localize(
'arduino/validateSketch/renameSketchFolderMessage',
"The sketch '{0}' cannot be used. {1} To get rid of this message, rename the sketch. Do you want to rename the sketch now?",
sketch.name,
error
)
);
}
privateasyncpromptRenameSketchFile(
uri: URI,
error: string
): Promise<boolean>{
returnthis.prompt(
nls.localize(
'arduino/validateSketch/renameSketchFileTitle',
'Invalid sketch filename'
),
nls.localize(
'arduino/validateSketch/renameSketchFileMessage',
"The sketch file '{0}' cannot be used. {1} Do you want to rename the sketch file now?",
uri.path.base,
error
)
);
}
privateasyncprompt(
title: string,
message: string,
buttons: string[]=[Dialog.CANCEL,Dialog.OK]
): Promise<boolean>{
const{ response }=awaitthis.dialogService.showMessageBox({
title,
message,
type: 'warning',
buttons,
});
// cancel
if(response===0){
returnfalse;
}
returntrue;
}
}
interfaceFixAction{
execute(): Promise<boolean>;
}