1- import { inject , injectable } from 'inversify' ;
1+ import { inject , injectable , postConstruct } from 'inversify' ;
22import { Emitter } from '@theia/core/lib/common/event' ;
3- import { CoreService } from '../../common/protocol' ;
3+ import { BoardUserField , CoreService } from '../../common/protocol' ;
44import { ArduinoMenus } from '../menu/arduino-menus' ;
55import { ArduinoToolbar } from '../toolbar/arduino-toolbar' ;
66import { BoardsDataStore } from '../boards/boards-data-store' ;
@@ -14,6 +14,7 @@ import {
1414 KeybindingRegistry ,
1515 TabBarToolbarRegistry ,
1616} from './contribution' ;
17+ import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog' ;
1718
1819@injectable ( )
1920export class UploadSketch extends SketchContribution {
@@ -29,16 +30,79 @@ export class UploadSketch extends SketchContribution {
2930 @inject ( BoardsServiceProvider )
3031 protected readonly boardsServiceClientImpl : BoardsServiceProvider ;
3132
33+ @inject ( UserFieldsDialog )
34+ protected readonly userFieldsDialog : UserFieldsDialog ;
35+
36+ protected cachedUserFields : Map < string , BoardUserField [ ] > = new Map ( ) ;
37+
3238 protected readonly onDidChangeEmitter = new Emitter < Readonly < void > > ( ) ;
3339 readonly onDidChange = this . onDidChangeEmitter . event ;
3440
3541 protected uploadInProgress = false ;
42+ protected boardRequiresUserFields = false ;
43+
44+ @postConstruct ( )
45+ protected init ( ) : void {
46+ this . boardsServiceClientImpl . onBoardsConfigChanged ( async ( ) => {
47+ const userFields = await this . boardsServiceClientImpl . selectedBoardUserFields ( ) ;
48+ this . boardRequiresUserFields = userFields . length > 0 ;
49+ } )
50+ }
51+
52+ private selectedFqbnAddress ( ) : string {
53+ const { boardsConfig } = this . boardsServiceClientImpl ;
54+ const fqbn = boardsConfig . selectedBoard ?. fqbn ;
55+ if ( ! fqbn ) {
56+ return "" ;
57+ }
58+ const address = boardsConfig . selectedBoard ?. port ?. address
59+ if ( ! address ) {
60+ return "" ;
61+ }
62+ return fqbn + "|" + address ;
63+ }
3664
3765 registerCommands ( registry : CommandRegistry ) : void {
3866 registry . registerCommand ( UploadSketch . Commands . UPLOAD_SKETCH , {
39- execute : ( ) => this . uploadSketch ( ) ,
67+ execute : async ( ) => {
68+ const key = this . selectedFqbnAddress ( ) ;
69+ if ( ! key ) {
70+ return ;
71+ }
72+ if ( this . boardRequiresUserFields && ! this . cachedUserFields . has ( key ) ) {
73+ this . userFieldsDialog . value = await this . boardsServiceClientImpl . selectedBoardUserFields ( ) ;
74+ const result = await this . userFieldsDialog . open ( ) ;
75+ if ( ! result ) {
76+ return ;
77+ }
78+ this . cachedUserFields . set ( key , result ) ;
79+ }
80+ this . uploadSketch ( ) ;
81+ } ,
4082 isEnabled : ( ) => ! this . uploadInProgress ,
4183 } ) ;
84+ registry . registerCommand (
85+ UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION ,
86+ {
87+ execute : async ( ) => {
88+ const key = this . selectedFqbnAddress ( ) ;
89+ if ( ! key ) {
90+ return ;
91+ }
92+
93+ const cached = this . cachedUserFields . get ( key ) ;
94+ this . userFieldsDialog . value = cached ?? await this . boardsServiceClientImpl . selectedBoardUserFields ( ) ;
95+
96+ const result = await this . userFieldsDialog . open ( )
97+ if ( ! result ) {
98+ return ;
99+ }
100+ this . cachedUserFields . set ( key , result ) ;
101+ this . uploadSketch ( ) ;
102+ } ,
103+ isEnabled : ( ) => ! this . uploadInProgress && this . boardRequiresUserFields ,
104+ }
105+ ) ;
42106 registry . registerCommand (
43107 UploadSketch . Commands . UPLOAD_SKETCH_USING_PROGRAMMER ,
44108 {
@@ -62,10 +126,15 @@ export class UploadSketch extends SketchContribution {
62126 label : 'Upload' ,
63127 order : '1' ,
64128 } ) ;
129+ registry . registerMenuAction ( ArduinoMenus . SKETCH__MAIN_GROUP , {
130+ commandId : UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION . id ,
131+ label : UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION . label ,
132+ order : '2' ,
133+ } ) ;
65134 registry . registerMenuAction ( ArduinoMenus . SKETCH__MAIN_GROUP , {
66135 commandId : UploadSketch . Commands . UPLOAD_SKETCH_USING_PROGRAMMER . id ,
67136 label : 'Upload Using Programmer' ,
68- order : '2 ' ,
137+ order : '3 ' ,
69138 } ) ;
70139 }
71140
@@ -131,6 +200,11 @@ export class UploadSketch extends SketchContribution {
131200 const optimizeForDebug = this . editorMode . compileForDebug ;
132201 const { selectedPort } = boardsConfig ;
133202 const port = selectedPort ;
203+ const userFields = this . cachedUserFields . get ( this . selectedFqbnAddress ( ) ) ;
204+ if ( ! userFields ) {
205+ this . messageService . error ( "Can't find user fields for connected board" ) ;
206+ return ;
207+ }
134208
135209 if ( usingProgrammer ) {
136210 const programmer = selectedProgrammer ;
@@ -143,6 +217,7 @@ export class UploadSketch extends SketchContribution {
143217 verbose,
144218 verify,
145219 sourceOverride,
220+ userFields,
146221 } ;
147222 } else {
148223 options = {
@@ -153,6 +228,7 @@ export class UploadSketch extends SketchContribution {
153228 verbose,
154229 verify,
155230 sourceOverride,
231+ userFields,
156232 } ;
157233 }
158234 this . outputChannelManager . getChannel ( 'Arduino' ) . clear ( ) ;
@@ -196,6 +272,11 @@ export namespace UploadSketch {
196272 export const UPLOAD_SKETCH : Command = {
197273 id : 'arduino-upload-sketch' ,
198274 } ;
275+ export const UPLOAD_WITH_CONFIGURATION : Command = {
276+ id : 'arduino-upload-with-configuration-sketch' ,
277+ label : 'Configure And Upload' ,
278+ category : 'Arduino' ,
279+ }
199280 export const UPLOAD_SKETCH_USING_PROGRAMMER : Command = {
200281 id : 'arduino-upload-sketch-using-programmer' ,
201282 } ;
0 commit comments