1
- import { inject , injectable } from 'inversify' ;
1
+ import { inject , injectable , postConstruct } from 'inversify' ;
2
2
import { Emitter } from '@theia/core/lib/common/event' ;
3
- import { CoreService } from '../../common/protocol' ;
3
+ import { BoardUserField , CoreService } from '../../common/protocol' ;
4
4
import { ArduinoMenus } from '../menu/arduino-menus' ;
5
5
import { ArduinoToolbar } from '../toolbar/arduino-toolbar' ;
6
6
import { BoardsDataStore } from '../boards/boards-data-store' ;
@@ -14,6 +14,7 @@ import {
14
14
KeybindingRegistry ,
15
15
TabBarToolbarRegistry ,
16
16
} from './contribution' ;
17
+ import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog' ;
17
18
import { nls } from '@theia/core/lib/browser/nls' ;
18
19
19
20
@injectable ( )
@@ -30,16 +31,81 @@ export class UploadSketch extends SketchContribution {
30
31
@inject ( BoardsServiceProvider )
31
32
protected readonly boardsServiceClientImpl : BoardsServiceProvider ;
32
33
34
+ @inject ( UserFieldsDialog )
35
+ protected readonly userFieldsDialog : UserFieldsDialog ;
36
+
37
+ protected cachedUserFields : Map < string , BoardUserField [ ] > = new Map ( ) ;
38
+
33
39
protected readonly onDidChangeEmitter = new Emitter < Readonly < void > > ( ) ;
34
40
readonly onDidChange = this . onDidChangeEmitter . event ;
35
41
36
42
protected uploadInProgress = false ;
43
+ protected boardRequiresUserFields = false ;
44
+
45
+ @postConstruct ( )
46
+ protected init ( ) : void {
47
+ this . boardsServiceClientImpl . onBoardsConfigChanged ( async ( ) => {
48
+ const userFields = await this . boardsServiceClientImpl . selectedBoardUserFields ( ) ;
49
+ this . boardRequiresUserFields = userFields . length > 0 ;
50
+ } )
51
+ }
52
+
53
+ private selectedFqbnAddress ( ) : string {
54
+ const { boardsConfig } = this . boardsServiceClientImpl ;
55
+ const fqbn = boardsConfig . selectedBoard ?. fqbn ;
56
+ if ( ! fqbn ) {
57
+ return "" ;
58
+ }
59
+ const address = boardsConfig . selectedBoard ?. port ?. address
60
+ if ( ! address ) {
61
+ return "" ;
62
+ }
63
+ return fqbn + "|" + address ;
64
+ }
37
65
38
66
registerCommands ( registry : CommandRegistry ) : void {
39
67
registry . registerCommand ( UploadSketch . Commands . UPLOAD_SKETCH , {
40
- execute : ( ) => this . uploadSketch ( ) ,
68
+ execute : async ( ) => {
69
+ const key = this . selectedFqbnAddress ( ) ;
70
+ if ( ! key ) {
71
+ return ;
72
+ }
73
+ if ( this . boardRequiresUserFields && ! this . cachedUserFields . has ( key ) ) {
74
+ // Deep clone the array of board fields to avoid editing the cached ones
75
+ this . userFieldsDialog . value = ( await this . boardsServiceClientImpl . selectedBoardUserFields ( ) ) . map ( f => ( { ...f } ) ) ;
76
+ const result = await this . userFieldsDialog . open ( ) ;
77
+ if ( ! result ) {
78
+ return ;
79
+ }
80
+ this . cachedUserFields . set ( key , result ) ;
81
+ }
82
+ this . uploadSketch ( ) ;
83
+ } ,
41
84
isEnabled : ( ) => ! this . uploadInProgress ,
42
85
} ) ;
86
+ registry . registerCommand (
87
+ UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION ,
88
+ {
89
+ execute : async ( ) => {
90
+ const key = this . selectedFqbnAddress ( ) ;
91
+ if ( ! key ) {
92
+ return ;
93
+ }
94
+
95
+ const cached = this . cachedUserFields . get ( key ) ;
96
+ // Deep clone the array of board fields to avoid editing the cached ones
97
+ this . userFieldsDialog . value = ( cached ?? await this . boardsServiceClientImpl . selectedBoardUserFields ( ) ) . map ( f => ( { ...f } ) ) ;
98
+
99
+ const result = await this . userFieldsDialog . open ( )
100
+ if ( ! result ) {
101
+ return ;
102
+ }
103
+ this . cachedUserFields . set ( key , result ) ;
104
+ this . uploadSketch ( ) ;
105
+ } ,
106
+ isEnabled : ( ) => ! this . uploadInProgress && this . boardRequiresUserFields ,
107
+ }
108
+ ) ;
43
109
registry . registerCommand (
44
110
UploadSketch . Commands . UPLOAD_SKETCH_USING_PROGRAMMER ,
45
111
{
@@ -63,13 +129,18 @@ export class UploadSketch extends SketchContribution {
63
129
label : nls . localize ( 'arduino/sketch/upload' , 'Upload' ) ,
64
130
order : '1' ,
65
131
} ) ;
132
+ registry . registerMenuAction ( ArduinoMenus . SKETCH__MAIN_GROUP , {
133
+ commandId : UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION . id ,
134
+ label : UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION . label ,
135
+ order : '2' ,
136
+ } ) ;
66
137
registry . registerMenuAction ( ArduinoMenus . SKETCH__MAIN_GROUP , {
67
138
commandId : UploadSketch . Commands . UPLOAD_SKETCH_USING_PROGRAMMER . id ,
68
139
label : nls . localize (
69
140
'arduino/sketch/uploadUsingProgrammer' ,
70
141
'Upload Using Programmer'
71
142
) ,
72
- order : '2 ' ,
143
+ order : '3 ' ,
73
144
} ) ;
74
145
}
75
146
@@ -135,6 +206,11 @@ export class UploadSketch extends SketchContribution {
135
206
const optimizeForDebug = this . editorMode . compileForDebug ;
136
207
const { selectedPort } = boardsConfig ;
137
208
const port = selectedPort ;
209
+ const userFields = this . cachedUserFields . get ( this . selectedFqbnAddress ( ) ) ;
210
+ if ( ! userFields ) {
211
+ this . messageService . error ( nls . localize ( 'arduino/sketch/userFieldsNotFoundError' , "Can't find user fields for connected board" ) ) ;
212
+ return ;
213
+ }
138
214
139
215
if ( usingProgrammer ) {
140
216
const programmer = selectedProgrammer ;
@@ -147,6 +223,7 @@ export class UploadSketch extends SketchContribution {
147
223
verbose,
148
224
verify,
149
225
sourceOverride,
226
+ userFields,
150
227
} ;
151
228
} else {
152
229
options = {
@@ -157,6 +234,7 @@ export class UploadSketch extends SketchContribution {
157
234
verbose,
158
235
verify,
159
236
sourceOverride,
237
+ userFields,
160
238
} ;
161
239
}
162
240
this . outputChannelManager . getChannel ( 'Arduino' ) . clear ( ) ;
@@ -207,6 +285,11 @@ export namespace UploadSketch {
207
285
export const UPLOAD_SKETCH : Command = {
208
286
id : 'arduino-upload-sketch' ,
209
287
} ;
288
+ export const UPLOAD_WITH_CONFIGURATION : Command = {
289
+ id : 'arduino-upload-with-configuration-sketch' ,
290
+ label : nls . localize ( 'arduino/sketch/configureAndUpload' , 'Configure And Upload' ) ,
291
+ category : 'Arduino' ,
292
+ }
210
293
export const UPLOAD_SKETCH_USING_PROGRAMMER : Command = {
211
294
id : 'arduino-upload-sketch-using-programmer' ,
212
295
} ;
0 commit comments