@@ -93,13 +93,23 @@ export class ArduinoApp {
93
93
}
94
94
}
95
95
96
- public async upload ( ) {
96
+ /**
97
+ * Upload code to selected board
98
+ * @param {bool } [compile=true] - Indicates whether to compile the code when using the CLI to upload
99
+ * @param {bool } [useProgrammer=false] - Indicate whether a specific programmer should be used
100
+ */
101
+ public async upload ( compile : boolean = true , useProgrammer : boolean = false ) {
97
102
const dc = DeviceContext . getInstance ( ) ;
98
103
const boardDescriptor = this . getBoardBuildString ( ) ;
99
104
if ( ! boardDescriptor ) {
100
105
return ;
101
106
}
102
107
108
+ const selectProgrammer = useProgrammer ? this . getProgrammerString ( ) : null ;
109
+ if ( useProgrammer && ! selectProgrammer ) {
110
+ return ;
111
+ }
112
+
103
113
if ( ! ArduinoWorkspace . rootPath ) {
104
114
vscode . window . showWarningMessage ( "Cannot find the sketch file." ) ;
105
115
return ;
@@ -140,94 +150,47 @@ export class ArduinoApp {
140
150
}
141
151
}
142
152
153
+ if ( ! compile && ! this . useArduinoCli ( ) ) {
154
+ arduinoChannel . error ( "This command is only availble when using the Arduino CLI" ) ;
155
+ return ;
156
+ }
157
+
143
158
const appPath = path . join ( ArduinoWorkspace . rootPath , dc . sketch ) ;
144
- const args = [ "--upload" , "--board" , boardDescriptor ] ;
159
+ // TODO: add the --clean argument to the cli args when v 0.14 is released (this will clean up the build folder after uploading)
160
+ const args = ( ! compile && this . useArduinoCli ( ) ) ? [ "upload" , "-b" , boardDescriptor ] :
161
+ this . useArduinoCli ( ) ? [ "compile" , "--upload" , "-b" , boardDescriptor ] :
162
+ [ "--upload" , "--board" , boardDescriptor ] ;
163
+
164
+ if ( useProgrammer ) {
165
+ if ( this . useArduinoCli ( ) ) {
166
+ args . push ( "--programmer" , selectProgrammer )
167
+ } else {
168
+ args . push ( "--useprogrammer" , "--pref" , "programmer=" + selectProgrammer )
169
+ }
170
+ }
171
+
145
172
if ( dc . port ) {
146
173
args . push ( "--port" , dc . port ) ;
147
174
}
148
175
args . push ( appPath ) ;
149
176
if ( VscodeSettings . getInstance ( ) . logLevel === "verbose" ) {
150
177
args . push ( "--verbose" ) ;
151
178
}
152
- if ( dc . output ) {
179
+ if ( dc . output && compile ) {
153
180
const outputPath = path . resolve ( ArduinoWorkspace . rootPath , dc . output ) ;
154
181
const dirPath = path . dirname ( outputPath ) ;
155
182
if ( ! util . directoryExistsSync ( dirPath ) ) {
156
183
Logger . notifyUserError ( "InvalidOutPutPath" , new Error ( constants . messages . INVALID_OUTPUT_PATH + outputPath ) ) ;
157
184
return ;
158
185
}
159
186
160
- args . push ( "--pref" , `build.path=${ outputPath } ` ) ;
161
- arduinoChannel . info ( `Please see the build logs in Output path: ${ outputPath } ` ) ;
162
- } else {
163
- const msg = "Output path is not specified. Unable to reuse previously compiled files. Upload could be slow. See README." ;
164
- arduinoChannel . warning ( msg ) ;
165
- }
166
- await util . spawn ( this . _settings . commandPath , arduinoChannel . channel , args ) . then ( async ( ) => {
167
- UsbDetector . getInstance ( ) . resumeListening ( ) ;
168
- if ( needRestore ) {
169
- await serialMonitor . openSerialMonitor ( ) ;
170
- }
171
- arduinoChannel . end ( `Uploaded the sketch: ${ dc . sketch } ${ os . EOL } ` ) ;
172
- } , ( reason ) => {
173
- arduinoChannel . error ( `Exit with code=${ reason . code } ${ os . EOL } ` ) ;
174
- } ) ;
175
- }
176
-
177
- public async uploadUsingProgrammer ( ) {
178
- const dc = DeviceContext . getInstance ( ) ;
179
- const boardDescriptor = this . getBoardBuildString ( ) ;
180
- if ( ! boardDescriptor ) {
181
- return ;
182
- }
183
-
184
- const selectProgrammer = this . getProgrammerString ( ) ;
185
- if ( ! selectProgrammer ) {
186
- return ;
187
- }
187
+ if ( this . useArduinoCli ( ) ) {
188
+ args . push ( "--build-path" , outputPath ) ;
188
189
189
- if ( ! ArduinoWorkspace . rootPath ) {
190
- vscode . window . showWarningMessage ( "Cannot find the sketch file." ) ;
191
- return ;
192
- }
193
-
194
- if ( ! dc . sketch || ! util . fileExistsSync ( path . join ( ArduinoWorkspace . rootPath , dc . sketch ) ) ) {
195
- await this . getMainSketch ( dc ) ;
196
- }
197
- if ( ! dc . port ) {
198
- const choice = await vscode . window . showInformationMessage (
199
- "Serial port is not specified. Do you want to select a serial port for uploading?" ,
200
- "Yes" , "No" ) ;
201
- if ( choice === "Yes" ) {
202
- vscode . commands . executeCommand ( "arduino.selectSerialPort" ) ;
203
- }
204
- return ;
205
- }
206
-
207
- arduinoChannel . show ( ) ;
208
- arduinoChannel . start ( `Upload sketch - ${ dc . sketch } ` ) ;
209
-
210
- const serialMonitor = SerialMonitor . getInstance ( ) ;
211
-
212
- const needRestore = await serialMonitor . closeSerialMonitor ( dc . port ) ;
213
- UsbDetector . getInstance ( ) . pauseListening ( ) ;
214
- await vscode . workspace . saveAll ( false ) ;
215
-
216
- const appPath = path . join ( ArduinoWorkspace . rootPath , dc . sketch ) ;
217
- const args = [ "--upload" , "--board" , boardDescriptor , "--port" , dc . port , "--useprogrammer" ,
218
- "--pref" , "programmer=" + selectProgrammer , appPath ] ;
219
- if ( VscodeSettings . getInstance ( ) . logLevel === "verbose" ) {
220
- args . push ( "--verbose" ) ;
221
- }
222
- if ( dc . output ) {
223
- const outputPath = path . resolve ( ArduinoWorkspace . rootPath , dc . output ) ;
224
- const dirPath = path . dirname ( outputPath ) ;
225
- if ( ! util . directoryExistsSync ( dirPath ) ) {
226
- Logger . notifyUserError ( "InvalidOutPutPath" , new Error ( constants . messages . INVALID_OUTPUT_PATH + outputPath ) ) ;
227
- return ;
190
+ } else {
191
+ args . push ( "--pref" , `build.path=${ outputPath } ` ) ;
228
192
}
229
193
230
- args . push ( "--pref" , `build.path=${ outputPath } ` ) ;
231
194
arduinoChannel . info ( `Please see the build logs in Output path: ${ outputPath } ` ) ;
232
195
} else {
233
196
const msg = "Output path is not specified. Unable to reuse previously compiled files. Upload could be slow. See README." ;
@@ -277,7 +240,7 @@ export class ArduinoApp {
277
240
}
278
241
279
242
const appPath = path . join ( ArduinoWorkspace . rootPath , dc . sketch ) ;
280
- const args = [ "--verify" , "--board" , boardDescriptor , appPath ] ;
243
+ const args = this . useArduinoCli ( ) ? [ "compile" , "-b" , boardDescriptor , appPath ] : [ "--verify" , "--board" , boardDescriptor , appPath ] ;
281
244
if ( VscodeSettings . getInstance ( ) . logLevel === "verbose" ) {
282
245
args . push ( "--verbose" ) ;
283
246
}
@@ -289,7 +252,13 @@ export class ArduinoApp {
289
252
return ;
290
253
}
291
254
292
- args . push ( "--pref" , `build.path=${ outputPath } ` ) ;
255
+ if ( this . useArduinoCli ( ) ) {
256
+ args . push ( "--build-path" , outputPath ) ;
257
+
258
+ } else {
259
+ args . push ( "--pref" , `build.path=${ outputPath } ` ) ;
260
+ }
261
+
293
262
arduinoChannel . info ( `Please see the build logs in Output path: ${ outputPath } ` ) ;
294
263
} else {
295
264
const msg = "Output path is not specified. Unable to reuse previously compiled files. Verify could be slow. See README." ;
@@ -495,33 +464,43 @@ export class ArduinoApp {
495
464
}
496
465
}
497
466
498
- /**
499
- * Install arduino board package based on package name and platform hardware architecture.
500
- */
467
+ /**
468
+ * Installs arduino board package.
469
+ * (If using the aduino CLI this installs the corrosponding core.)
470
+ * @param {string } packageName - board vendor
471
+ * @param {string } arch - board architecture
472
+ * @param {string } version - version of board package or core to download
473
+ * @param {boolean } [showOutput=true] - show raw output from command
474
+ */
501
475
public async installBoard ( packageName : string , arch : string = "" , version : string = "" , showOutput : boolean = true ) {
502
476
arduinoChannel . show ( ) ;
503
477
const updatingIndex = packageName === "dummy" && ! arch && ! version ;
504
478
if ( updatingIndex ) {
505
479
arduinoChannel . start ( `Update package index files...` ) ;
506
480
} else {
507
481
try {
508
- const packagePath = path . join ( this . _settings . packagePath , "packages" , packageName ) ;
482
+ const packagePath = path . join ( this . _settings . packagePath , "packages" , packageName , arch ) ;
509
483
if ( util . directoryExistsSync ( packagePath ) ) {
510
484
util . rmdirRecursivelySync ( packagePath ) ;
511
485
}
512
486
arduinoChannel . start ( `Install package - ${ packageName } ...` ) ;
513
487
} catch ( error ) {
514
488
arduinoChannel . start ( `Install package - ${ packageName } failed under directory : ${ error . path } ${ os . EOL }
515
- Please make sure the folder is not occupied by other procedures .` ) ;
489
+ Please make sure the folder is not occupied by other procedures .`) ;
516
490
arduinoChannel . error ( `Error message - ${ error . message } ${ os . EOL } ` ) ;
517
491
arduinoChannel . error ( `Exit with code=${ error . code } ${ os . EOL } ` ) ;
518
492
return ;
519
493
}
520
494
}
495
+ arduinoChannel . info ( `${ packageName } ${ arch && ":" + arch } ${ version && ":" + version } ` ) ;
521
496
try {
522
- await util . spawn ( this . _settings . commandPath ,
523
- showOutput ? arduinoChannel . channel : null ,
524
- [ "--install-boards" , `${ packageName } ${ arch && ":" + arch } ${ version && ":" + version } ` ] ) ;
497
+ this . useArduinoCli ( ) ?
498
+ await util . spawn ( this . _settings . commandPath ,
499
+ showOutput ? arduinoChannel . channel : null ,
500
+ [ "core" , "install" , `${ packageName } ${ arch && ":" + arch } ${ version && "@" + version } ` ] ) :
501
+ await util . spawn ( this . _settings . commandPath ,
502
+ showOutput ? arduinoChannel . channel : null ,
503
+ [ "--install-boards" , `${ packageName } ${ arch && ":" + arch } ${ version && ":" + version } ` ] ) ;
525
504
526
505
if ( updatingIndex ) {
527
506
arduinoChannel . end ( "Updated package index files." ) ;
@@ -548,6 +527,13 @@ Please make sure the folder is not occupied by other procedures .`);
548
527
arduinoChannel . end ( `Uninstalled board package - ${ boardName } ${ os . EOL } ` ) ;
549
528
}
550
529
530
+ /**
531
+ * Downloads or updates a library
532
+ * @param {string } libName - name of the library to download
533
+ * @param {string } version - version of library to download
534
+ * @param {boolean } [showOutput=true] - show raw output from command
535
+ */
536
+
551
537
public async installLibrary ( libName : string , version : string = "" , showOutput : boolean = true ) {
552
538
arduinoChannel . show ( ) ;
553
539
const updatingIndex = ( libName === "dummy" && ! version ) ;
@@ -557,6 +543,10 @@ Please make sure the folder is not occupied by other procedures .`);
557
543
arduinoChannel . start ( `Install library - ${ libName } ` ) ;
558
544
}
559
545
try {
546
+ this . useArduinoCli ( ) ?
547
+ await util . spawn ( this . _settings . commandPath ,
548
+ showOutput ? arduinoChannel . channel : null ,
549
+ [ "lib" , "install" , `${ libName } ${ version && "@" + version } ` ] ) :
560
550
await util . spawn ( this . _settings . commandPath ,
561
551
showOutput ? arduinoChannel . channel : null ,
562
552
[ "--install-library" , `${ libName } ${ version && ":" + version } ` ] ) ;
@@ -769,6 +759,15 @@ Please make sure the folder is not occupied by other procedures .`);
769
759
this . _programmerManager = value ;
770
760
}
771
761
762
+ /**
763
+ * Checks if the arduino cli is being used
764
+ * @returns {bool } - true if arduino cli is being use
765
+ */
766
+ private useArduinoCli ( ) {
767
+ return this . _settings . useArduinoCli ;
768
+ // return VscodeSettings.getInstance().useArduinoCli;
769
+ }
770
+
772
771
private getProgrammerString ( ) : string {
773
772
const selectProgrammer = this . programmerManager . currentProgrammer ;
774
773
if ( ! selectProgrammer ) {
0 commit comments