@@ -16,30 +16,29 @@ import {
1616import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog' ;
1717import { DisposableCollection , nls } from '@theia/core/lib/common' ;
1818import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl' ;
19+ import { VerifySketch } from './verify-sketch' ;
1920
2021@injectable ( )
2122export class UploadSketch extends CoreServiceContribution {
2223 @inject ( MenuModelRegistry )
23- protected readonly menuRegistry : MenuModelRegistry ;
24+ private readonly menuRegistry : MenuModelRegistry ;
2425
2526 @inject ( BoardsDataStore )
26- protected readonly boardsDataStore : BoardsDataStore ;
27+ private readonly boardsDataStore : BoardsDataStore ;
2728
2829 @inject ( BoardsServiceProvider )
29- protected readonly boardsServiceClientImpl : BoardsServiceProvider ;
30+ private readonly boardsServiceClientImpl : BoardsServiceProvider ;
3031
3132 @inject ( UserFieldsDialog )
32- protected readonly userFieldsDialog : UserFieldsDialog ;
33+ private readonly userFieldsDialog : UserFieldsDialog ;
3334
34- protected cachedUserFields : Map < string , BoardUserField [ ] > = new Map ( ) ;
35+ private boardRequiresUserFields = false ;
36+ private readonly cachedUserFields : Map < string , BoardUserField [ ] > = new Map ( ) ;
37+ private readonly menuActionsDisposables = new DisposableCollection ( ) ;
3538
36- protected readonly onDidChangeEmitter = new Emitter < Readonly < void > > ( ) ;
37- readonly onDidChange = this . onDidChangeEmitter . event ;
38-
39- protected uploadInProgress = false ;
40- protected boardRequiresUserFields = false ;
41-
42- protected readonly menuActionsDisposables = new DisposableCollection ( ) ;
39+ private readonly onDidChangeEmitter = new Emitter < void > ( ) ;
40+ private readonly onDidChange = this . onDidChangeEmitter . event ;
41+ private uploadInProgress = false ;
4342
4443 protected override init ( ) : void {
4544 super . init ( ) ;
@@ -130,7 +129,6 @@ export class UploadSketch extends CoreServiceContribution {
130129
131130 override registerMenus ( registry : MenuModelRegistry ) : void {
132131 this . menuActionsDisposables . dispose ( ) ;
133-
134132 this . menuActionsDisposables . push (
135133 registry . registerMenuAction ( ArduinoMenus . SKETCH__MAIN_GROUP , {
136134 commandId : UploadSketch . Commands . UPLOAD_SKETCH . id ,
@@ -153,7 +151,7 @@ export class UploadSketch extends CoreServiceContribution {
153151 new PlaceholderMenuNode (
154152 ArduinoMenus . SKETCH__MAIN_GROUP ,
155153 // commandId: UploadSketch.Commands.UPLOAD_WITH_CONFIGURATION.id,
156- UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION . label ! ,
154+ UploadSketch . Commands . UPLOAD_WITH_CONFIGURATION . label ,
157155 { order : '2' }
158156 )
159157 )
@@ -193,13 +191,31 @@ export class UploadSketch extends CoreServiceContribution {
193191 }
194192
195193 async uploadSketch ( usingProgrammer = false ) : Promise < void > {
196- // even with buttons disabled, better to double check if an upload is already in progress
197194 if ( this . uploadInProgress ) {
198195 return ;
199196 }
200197
201- const sketch = await this . sketchServiceClient . currentSketch ( ) ;
202- if ( ! CurrentSketch . isValid ( sketch ) ) {
198+ const ref = await this . commandService . executeCommand < VerifySketch . Ref > (
199+ VerifySketch . Commands . VERIFY_SKETCH . id ,
200+ false
201+ ) ;
202+
203+ if ( ! ref ) {
204+ return ;
205+ }
206+
207+ const options = await this . options ( usingProgrammer , ref ) ;
208+ if ( ! options ) {
209+ return ;
210+ }
211+
212+ if ( options . userFields . length === 0 && this . boardRequiresUserFields ) {
213+ this . messageService . error (
214+ nls . localize (
215+ 'arduino/sketch/userFieldsNotFoundError' ,
216+ "Can't find user fields for connected board"
217+ )
218+ ) ;
203219 return ;
204220 }
205221
@@ -209,81 +225,14 @@ export class UploadSketch extends CoreServiceContribution {
209225 this . uploadInProgress = true ;
210226 this . coreErrorHandler . reset ( ) ;
211227 this . onDidChangeEmitter . fire ( ) ;
212- const { boardsConfig } = this . boardsServiceClientImpl ;
213- const [
214- fqbn ,
215- { selectedProgrammer } ,
216- verify ,
217- uploadVerbose ,
218- sourceOverride ,
219- optimizeForDebug ,
220- compileVerbose ,
221- ] = await Promise . all ( [
222- this . boardsDataStore . appendConfigToFqbn (
223- boardsConfig . selectedBoard ?. fqbn
224- ) ,
225- this . boardsDataStore . getData ( boardsConfig . selectedBoard ?. fqbn ) ,
226- this . preferences . get ( 'arduino.upload.verify' ) ,
227- this . preferences . get ( 'arduino.upload.verbose' ) ,
228- this . sourceOverride ( ) ,
229- this . commandService . executeCommand < boolean > (
230- 'arduino-is-optimize-for-debug'
231- ) ,
232- this . preferences . get ( 'arduino.compile.verbose' ) ,
233- ] ) ;
234-
235- const verbose = { compile : compileVerbose , upload : uploadVerbose } ;
236- const board = {
237- ...boardsConfig . selectedBoard ,
238- name : boardsConfig . selectedBoard ?. name || '' ,
239- fqbn,
240- } ;
241- let options : CoreService . Upload . Options | undefined = undefined ;
242- const { selectedPort } = boardsConfig ;
243- const port = selectedPort ;
244- const userFields =
245- this . cachedUserFields . get ( this . selectedFqbnAddress ( ) ) ?? [ ] ;
246- if ( userFields . length === 0 && this . boardRequiresUserFields ) {
247- this . messageService . error (
248- nls . localize (
249- 'arduino/sketch/userFieldsNotFoundError' ,
250- "Can't find user fields for connected board"
251- )
252- ) ;
253- return ;
254- }
228+ await this . doWithProgress ( {
229+ progressText : nls . localize ( 'arduino/sketch/uploading' , 'Uploading...' ) ,
230+ task : ( progressId , coreService ) => {
231+ return coreService . upload ( { ...options , progressId } ) ;
232+ } ,
233+ keepOutput : true ,
234+ } ) ;
255235
256- if ( usingProgrammer ) {
257- const programmer = selectedProgrammer ;
258- options = {
259- sketch,
260- board,
261- optimizeForDebug : Boolean ( optimizeForDebug ) ,
262- programmer,
263- port,
264- verbose,
265- verify,
266- sourceOverride,
267- userFields,
268- } ;
269- } else {
270- options = {
271- sketch,
272- board,
273- optimizeForDebug : Boolean ( optimizeForDebug ) ,
274- port,
275- verbose,
276- verify,
277- sourceOverride,
278- userFields,
279- } ;
280- }
281- this . outputChannelManager . getChannel ( 'Arduino' ) . clear ( ) ;
282- if ( usingProgrammer ) {
283- await this . coreService . uploadUsingProgrammer ( options ) ;
284- } else {
285- await this . coreService . upload ( options ) ;
286- }
287236 this . messageService . info (
288237 nls . localize ( 'arduino/sketch/doneUploading' , 'Done uploading.' ) ,
289238 { timeout : 3000 }
@@ -295,14 +244,47 @@ export class UploadSketch extends CoreServiceContribution {
295244 this . onDidChangeEmitter . fire ( ) ;
296245 }
297246 }
247+
248+ private async options (
249+ usingProgrammer : boolean ,
250+ verifyRef : VerifySketch . Ref
251+ ) : Promise < CoreService . Options . Upload | undefined > {
252+ const sketch = await this . sketchServiceClient . currentSketch ( ) ;
253+ if ( ! CurrentSketch . isValid ( sketch ) ) {
254+ return undefined ;
255+ }
256+ const userFields = this . userFields ( ) ;
257+ const { boardsConfig } = this . boardsServiceClientImpl ;
258+ const [ fqbn , { selectedProgrammer : programmer } , verify , verbose ] =
259+ await Promise . all ( [
260+ verifyRef . fqbn . decorated ,
261+ this . boardsDataStore . getData ( verifyRef . fqbn . original ) ,
262+ this . preferences . get ( 'arduino.upload.verify' ) ,
263+ this . preferences . get ( 'arduino.upload.verbose' ) ,
264+ ] ) ;
265+ const port = boardsConfig . selectedPort ;
266+ return {
267+ sketch,
268+ fqbn,
269+ ...( usingProgrammer && { programmer } ) ,
270+ port,
271+ verbose,
272+ verify,
273+ userFields,
274+ } ;
275+ }
276+
277+ private userFields ( ) {
278+ return this . cachedUserFields . get ( this . selectedFqbnAddress ( ) ) ?? [ ] ;
279+ }
298280}
299281
300282export namespace UploadSketch {
301283 export namespace Commands {
302284 export const UPLOAD_SKETCH : Command = {
303285 id : 'arduino-upload-sketch' ,
304286 } ;
305- export const UPLOAD_WITH_CONFIGURATION : Command = {
287+ export const UPLOAD_WITH_CONFIGURATION : Command & { label : string } = {
306288 id : 'arduino-upload-with-configuration-sketch' ,
307289 label : nls . localize (
308290 'arduino/sketch/configureAndUpload' ,
0 commit comments