41
41
import processing .app .debug .RunnerException ;
42
42
import processing .app .debug .TargetPlatform ;
43
43
import processing .app .helpers .PreferencesMap ;
44
+ import processing .app .helpers .PreferencesMapException ;
44
45
import processing .app .helpers .StringReplacer ;
45
46
46
47
import java .io .File ;
@@ -105,17 +106,11 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String
105
106
else
106
107
prefs .put ("upload.verify" , prefs .get ("upload.params.noverify" , "" ));
107
108
108
- boolean uploadResult ;
109
109
try {
110
- String pattern = prefs .getOrExcept ("upload.pattern" );
111
- String [] cmd = StringReplacer .formatAndSplit (pattern , prefs );
112
- uploadResult = executeUploadCommand (cmd );
113
- } catch (Exception e ) {
114
- throw new RunnerException (e );
110
+ return runCommand ("upload.pattern" , prefs );
115
111
} finally {
116
112
BaseNoGui .getDiscoveryManager ().getSerialDiscoverer ().pausePolling (false );
117
113
}
118
- return uploadResult ;
119
114
}
120
115
121
116
// need to do a little dance for Leonardo and derivatives:
@@ -127,7 +122,7 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String
127
122
boolean doTouch = prefs .getBoolean ("upload.use_1200bps_touch" );
128
123
boolean waitForUploadPort = prefs .getBoolean ("upload.wait_for_upload_port" );
129
124
130
- String userSelectedUploadPort = prefs .getOrExcept ("serial.port" );
125
+ String userSelectedUploadPort = prefs .get ("serial.port" , " " );
131
126
String actualUploadPort = null ;
132
127
133
128
if (doTouch ) {
@@ -177,7 +172,7 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String
177
172
Thread .sleep (100 );
178
173
}
179
174
180
- BoardPort boardPort = BaseNoGui .getDiscoveryManager ().find (PreferencesData .get ("serial.port" ));
175
+ BoardPort boardPort = BaseNoGui .getDiscoveryManager ().find (PreferencesData .get ("serial.port" , "" ));
181
176
try {
182
177
prefs .put ("serial.port.iserial" , boardPort .getPrefs ().getOrExcept ("iserial" ));
183
178
} catch (Exception e ) {
@@ -199,13 +194,7 @@ public boolean uploadUsingPreferences(File sourcePath, String buildPath, String
199
194
200
195
boolean uploadResult ;
201
196
try {
202
- String pattern = prefs .getOrExcept ("upload.pattern" );
203
- String [] cmd = StringReplacer .formatAndSplit (pattern , prefs );
204
- uploadResult = executeUploadCommand (cmd );
205
- } catch (RunnerException e ) {
206
- throw e ;
207
- } catch (Exception e ) {
208
- throw new RunnerException (e );
197
+ uploadResult = runCommand ("upload.pattern" , prefs );
209
198
} finally {
210
199
BaseNoGui .getDiscoveryManager ().getSerialDiscoverer ().pausePolling (false );
211
200
}
@@ -328,15 +317,7 @@ private boolean uploadUsingProgrammer(String buildPath, String className) throws
328
317
else
329
318
prefs .put ("program.verify" , prefs .get ("program.params.noverify" , "" ));
330
319
331
- try {
332
- String pattern = prefs .getOrExcept ("program.pattern" );
333
- String [] cmd = StringReplacer .formatAndSplit (pattern , prefs );
334
- return executeUploadCommand (cmd );
335
- } catch (RunnerException e ) {
336
- throw e ;
337
- } catch (Exception e ) {
338
- throw new RunnerException (e );
339
- }
320
+ return runCommand ("program.pattern" , prefs );
340
321
}
341
322
342
323
@ Override
@@ -393,13 +374,27 @@ public boolean burnBootloader() throws Exception {
393
374
394
375
new LoadVIDPIDSpecificPreferences ().load (prefs );
395
376
396
- String pattern = prefs .getOrExcept ("erase.pattern" );
397
- String [] cmd = StringReplacer .formatAndSplit (pattern , prefs );
398
- if (!executeUploadCommand (cmd ))
377
+ if (!runCommand ("erase.pattern" , prefs ))
399
378
return false ;
400
379
401
- pattern = prefs .getOrExcept ("bootloader.pattern" );
402
- cmd = StringReplacer .formatAndSplit (pattern , prefs );
403
- return executeUploadCommand (cmd );
380
+ return runCommand ("bootloader.pattern" , prefs );
381
+ }
382
+
383
+ private boolean runCommand (String patternKey , PreferencesMap prefs ) throws Exception , RunnerException {
384
+ try {
385
+ String pattern = prefs .getOrExcept (patternKey );
386
+ StringReplacer .checkIfRequiredKeyIsMissingOrExcept ("serial.port" , pattern , prefs );
387
+ String [] cmd = StringReplacer .formatAndSplit (pattern , prefs );
388
+ return executeUploadCommand (cmd );
389
+ } catch (RunnerException e ) {
390
+ throw e ;
391
+ } catch (PreferencesMapException e ) {
392
+ if (e .getMessage ().equals ("serial.port" )) {
393
+ throw new SerialNotFoundException (e );
394
+ }
395
+ throw e ;
396
+ } catch (Exception e ) {
397
+ throw new RunnerException (e );
398
+ }
404
399
}
405
400
}
0 commit comments