22
22
23
23
package processing .app ;
24
24
25
+ import cc .arduino .Compiler ;
25
26
import cc .arduino .Constants ;
26
27
import cc .arduino .UpdatableBoardsLibsFakeURLsHandler ;
28
+ import cc .arduino .UploaderUtils ;
27
29
import cc .arduino .contributions .*;
28
30
import cc .arduino .contributions .libraries .*;
29
31
import cc .arduino .contributions .libraries .ui .LibraryManagerUI ;
@@ -84,7 +86,6 @@ public class Base {
84
86
private static boolean commandLine ;
85
87
public static volatile Base INSTANCE ;
86
88
87
- public static SplashScreenHelper splashScreenHelper = new SplashScreenHelper (SplashScreen .getSplashScreen ());
88
89
public static Map <String , Object > FIND_DIALOG_STATE = new HashMap <>();
89
90
private final ContributionInstaller contributionInstaller ;
90
91
private final LibraryInstaller libraryInstaller ;
@@ -128,59 +129,13 @@ static public void main(String args[]) throws Exception {
128
129
}
129
130
130
131
try {
131
- guardedMain (args );
132
+ INSTANCE = new Base (args );
132
133
} catch (Throwable e ) {
133
134
e .printStackTrace (System .err );
134
135
System .exit (255 );
135
136
}
136
137
}
137
138
138
- static public void guardedMain (String args []) throws Exception {
139
- Thread deleteFilesOnShutdownThread = new Thread (DeleteFilesOnShutdown .INSTANCE );
140
- deleteFilesOnShutdownThread .setName ("DeleteFilesOnShutdown" );
141
- Runtime .getRuntime ().addShutdownHook (deleteFilesOnShutdownThread );
142
-
143
- BaseNoGui .initLogger ();
144
-
145
- initLogger ();
146
-
147
- BaseNoGui .initPlatform ();
148
-
149
- BaseNoGui .getPlatform ().init ();
150
-
151
- BaseNoGui .initPortableFolder ();
152
-
153
- BaseNoGui .initParameters (args );
154
-
155
- splashScreenHelper .splashText (tr ("Loading configuration..." ));
156
-
157
- BaseNoGui .initVersion ();
158
-
159
- // Use native popups so they don't look so crappy on osx
160
- JPopupMenu .setDefaultLightWeightPopupEnabled (false );
161
-
162
- // Don't put anything above this line that might make GUI,
163
- // because the platform has to be inited properly first.
164
-
165
- // setup the theme coloring fun
166
- Theme .init ();
167
- System .setProperty ("swing.aatext" , PreferencesData .get ("editor.antialias" , "true" ));
168
-
169
- // Set the look and feel before opening the window
170
- try {
171
- BaseNoGui .getPlatform ().setLookAndFeel ();
172
- } catch (Exception e ) {
173
- // ignore
174
- }
175
-
176
- // Create a location for untitled sketches
177
- untitledFolder = FileUtils .createTempFolder ("untitled" + new Random ().nextInt (Integer .MAX_VALUE ), ".tmp" );
178
- DeleteFilesOnShutdown .add (untitledFolder );
179
-
180
- INSTANCE = new Base (args );
181
- }
182
-
183
-
184
139
static public void initLogger () {
185
140
Handler consoleHandler = new ConsoleLogger ();
186
141
consoleHandler .setLevel (Level .ALL );
@@ -208,12 +163,6 @@ static public void initLogger() {
208
163
209
164
}
210
165
211
-
212
- static protected void setCommandLine () {
213
- commandLine = true ;
214
- }
215
-
216
-
217
166
static protected boolean isCommandLine () {
218
167
return commandLine ;
219
168
}
@@ -227,10 +176,60 @@ static public File absoluteFile(String path) {
227
176
}
228
177
229
178
public Base (String [] args ) throws Exception {
230
- BaseNoGui .notifier = new GUIUserNotifier (this );
179
+ Thread deleteFilesOnShutdownThread = new Thread (DeleteFilesOnShutdown .INSTANCE );
180
+ deleteFilesOnShutdownThread .setName ("DeleteFilesOnShutdown" );
181
+ Runtime .getRuntime ().addShutdownHook (deleteFilesOnShutdownThread );
182
+
183
+ BaseNoGui .initLogger ();
184
+
185
+ initLogger ();
186
+
187
+ BaseNoGui .initPlatform ();
188
+
189
+ BaseNoGui .getPlatform ().init ();
190
+
191
+ BaseNoGui .initPortableFolder ();
192
+
193
+ // Look for a possible "--preferences-file" parameter and load preferences
194
+ BaseNoGui .initParameters (args );
231
195
232
196
CommandlineParser parser = new CommandlineParser (args );
233
197
parser .parseArgumentsPhase1 ();
198
+ commandLine = !parser .isGuiMode ();
199
+
200
+ SplashScreenHelper splash ;
201
+ if (parser .isGuiMode ()) {
202
+ // Setup all notification widgets
203
+ splash = new SplashScreenHelper (SplashScreen .getSplashScreen ());
204
+ BaseNoGui .notifier = new GUIUserNotifier (this );
205
+
206
+ // Setup the theme coloring fun
207
+ Theme .init ();
208
+ System .setProperty ("swing.aatext" , PreferencesData .get ("editor.antialias" , "true" ));
209
+
210
+ // Set the look and feel before opening the window
211
+ try {
212
+ BaseNoGui .getPlatform ().setLookAndFeel ();
213
+ } catch (Exception e ) {
214
+ // ignore
215
+ }
216
+
217
+ // Use native popups so they don't look so crappy on osx
218
+ JPopupMenu .setDefaultLightWeightPopupEnabled (false );
219
+ } else {
220
+ splash = new SplashScreenHelper (null );
221
+ }
222
+
223
+ splash .splashText (tr ("Loading configuration..." ));
224
+
225
+ BaseNoGui .initVersion ();
226
+
227
+ // Don't put anything above this line that might make GUI,
228
+ // because the platform has to be inited properly first.
229
+
230
+ // Create a location for untitled sketches
231
+ untitledFolder = FileUtils .createTempFolder ("untitled" + new Random ().nextInt (Integer .MAX_VALUE ), ".tmp" );
232
+ DeleteFilesOnShutdown .add (untitledFolder );
234
233
235
234
BaseNoGui .checkInstallationFolder ();
236
235
@@ -246,11 +245,15 @@ public Base(String[] args) throws Exception {
246
245
}
247
246
}
248
247
249
- splashScreenHelper .splashText (tr ("Initializing packages..." ));
248
+ splash .splashText (tr ("Initializing packages..." ));
250
249
BaseNoGui .initPackages ();
251
- splashScreenHelper .splashText (tr ("Preparing boards..." ));
252
- rebuildBoardsMenu ();
253
- rebuildProgrammerMenu ();
250
+
251
+ splash .splashText (tr ("Preparing boards..." ));
252
+
253
+ if (!isCommandLine ()) {
254
+ rebuildBoardsMenu ();
255
+ rebuildProgrammerMenu ();
256
+ }
254
257
255
258
// Setup board-dependent variables.
256
259
onBoardOrPortChange ();
@@ -263,35 +266,6 @@ public Base(String[] args) throws Exception {
263
266
264
267
parser .parseArgumentsPhase2 ();
265
268
266
- for (String path : parser .getFilenames ()) {
267
- // Correctly resolve relative paths
268
- File file = absoluteFile (path );
269
-
270
- // Fix a problem with systems that use a non-ASCII languages. Paths are
271
- // being passed in with 8.3 syntax, which makes the sketch loader code
272
- // unhappy, since the sketch folder naming doesn't match up correctly.
273
- // http://dev.processing.org/bugs/show_bug.cgi?id=1089
274
- if (OSUtils .isWindows ()) {
275
- try {
276
- file = file .getCanonicalFile ();
277
- } catch (IOException e ) {
278
- e .printStackTrace ();
279
- }
280
- }
281
-
282
- boolean showEditor = parser .isGuiMode ();
283
- if (!parser .isForceSavePrefs ())
284
- PreferencesData .setDoSave (showEditor );
285
- if (handleOpen (file , retrieveSketchLocation (".default" ), showEditor , false ) == null ) {
286
- String mess = I18n .format (tr ("Failed to open sketch: \" {0}\" " ), path );
287
- // Open failure is fatal in upload/verify mode
288
- if (parser .isVerifyOrUploadMode ())
289
- showError (null , mess , 2 );
290
- else
291
- showWarning (null , mess , null );
292
- }
293
- }
294
-
295
269
// Save the preferences. For GUI mode, this happens in the quit
296
270
// handler, but for other modes we should also make sure to save
297
271
// them.
@@ -377,35 +351,90 @@ public Base(String[] args) throws Exception {
377
351
System .exit (0 );
378
352
379
353
} else if (parser .isVerifyOrUploadMode ()) {
380
- splashScreenHelper .close ();
381
354
// Set verbosity for command line build
382
- PreferencesData .set ("build.verbose" , "" + parser .isDoVerboseBuild ());
383
- PreferencesData .set ("upload.verbose" , "" + parser .isDoVerboseUpload ());
384
- PreferencesData .set ("runtime.preserve.temp.files" , Boolean .toString (parser .isPreserveTempFiles ()));
355
+ PreferencesData .setBoolean ("build.verbose" , parser .isDoVerboseBuild ());
356
+ PreferencesData .setBoolean ("upload.verbose" , parser .isDoVerboseUpload ());
385
357
386
- // Make sure these verbosity preferences are only for the
387
- // current session
358
+ // Set preserve-temp flag
359
+ PreferencesData .setBoolean ("runtime.preserve.temp.files" , parser .isPreserveTempFiles ());
360
+
361
+ // Make sure these verbosity preferences are only for the current session
388
362
PreferencesData .setDoSave (false );
389
363
390
- Editor editor = editors .get (0 );
364
+ Sketch sketch = null ;
365
+ String outputFile = null ;
391
366
392
- if (parser .isUploadMode ()) {
393
- splashScreenHelper .splashText (tr ("Verifying and uploading..." ));
394
- editor .exportHandler .run ();
395
- } else {
396
- splashScreenHelper .splashText (tr ("Verifying..." ));
397
- editor .runHandler .run ();
398
- }
367
+ try {
368
+ // Build
369
+ splash .splashText (tr ("Verifying..." ));
370
+
371
+ File sketchFile = new File (parser .getFilenames ().get (0 ));
372
+ sketch = new Sketch (sketchFile );
399
373
400
- // Error during build or upload
401
- if (editor .status .isErr ()) {
374
+ outputFile = new Compiler (sketch ).build (progress -> {}, false );
375
+ } catch (Exception e ) {
376
+ // Error during build
402
377
System .exit (1 );
403
378
}
404
379
380
+ if (parser .isUploadMode ()) {
381
+ // Upload
382
+ splash .splashText (tr ("Uploading..." ));
383
+
384
+ try {
385
+ List <String > warnings = new ArrayList <>();
386
+ UploaderUtils uploader = new UploaderUtils ();
387
+ boolean res = uploader .upload (sketch , null , outputFile ,
388
+ parser .isDoUseProgrammer (),
389
+ parser .isNoUploadPort (), warnings );
390
+ for (String warning : warnings ) {
391
+ System .out .println (tr ("Warning" ) + ": " + warning );
392
+ }
393
+ if (!res ) {
394
+ throw new Exception ();
395
+ }
396
+ } catch (Exception e ) {
397
+ // Error during upload
398
+ System .out .flush ();
399
+ System .err .flush ();
400
+ System .err
401
+ .println (tr ("An error occurred while uploading the sketch" ));
402
+ System .exit (1 );
403
+ }
404
+ }
405
+
405
406
// No errors exit gracefully
406
407
System .exit (0 );
407
408
} else if (parser .isGuiMode ()) {
408
- splashScreenHelper .splashText (tr ("Starting..." ));
409
+ splash .splashText (tr ("Starting..." ));
410
+
411
+ for (String path : parser .getFilenames ()) {
412
+ // Correctly resolve relative paths
413
+ File file = absoluteFile (path );
414
+
415
+ // Fix a problem with systems that use a non-ASCII languages. Paths are
416
+ // being passed in with 8.3 syntax, which makes the sketch loader code
417
+ // unhappy, since the sketch folder naming doesn't match up correctly.
418
+ // http://dev.processing.org/bugs/show_bug.cgi?id=1089
419
+ if (OSUtils .isWindows ()) {
420
+ try {
421
+ file = file .getCanonicalFile ();
422
+ } catch (IOException e ) {
423
+ e .printStackTrace ();
424
+ }
425
+ }
426
+
427
+ if (!parser .isForceSavePrefs ())
428
+ PreferencesData .setDoSave (true );
429
+ if (handleOpen (file , retrieveSketchLocation (".default" ), false ) == null ) {
430
+ String mess = I18n .format (tr ("Failed to open sketch: \" {0}\" " ), path );
431
+ // Open failure is fatal in upload/verify mode
432
+ if (parser .isVerifyOrUploadMode ())
433
+ showError (null , mess , 2 );
434
+ else
435
+ showWarning (null , mess , null );
436
+ }
437
+ }
409
438
410
439
installKeyboardInputMap ();
411
440
@@ -472,7 +501,7 @@ protected boolean restoreSketches() throws Exception {
472
501
}
473
502
int [] location = retrieveSketchLocation ("" + i );
474
503
// If file did not exist, null will be returned for the Editor
475
- if (handleOpen (new File (path ), location , nextEditorLocation (), true , false , false ) != null ) {
504
+ if (handleOpen (new File (path ), location , nextEditorLocation (), false , false ) != null ) {
476
505
opened ++;
477
506
}
478
507
}
@@ -764,14 +793,14 @@ public Editor handleOpen(File file) throws Exception {
764
793
}
765
794
766
795
public Editor handleOpen (File file , boolean untitled ) throws Exception {
767
- return handleOpen (file , nextEditorLocation (), true , untitled );
796
+ return handleOpen (file , nextEditorLocation (), untitled );
768
797
}
769
798
770
- protected Editor handleOpen (File file , int [] location , boolean showEditor , boolean untitled ) throws Exception {
771
- return handleOpen (file , location , location , showEditor , true , untitled );
799
+ protected Editor handleOpen (File file , int [] location , boolean untitled ) throws Exception {
800
+ return handleOpen (file , location , location , true , untitled );
772
801
}
773
802
774
- protected Editor handleOpen (File file , int [] storedLocation , int [] defaultLocation , boolean showEditor , boolean storeOpenedSketches , boolean untitled ) throws Exception {
803
+ protected Editor handleOpen (File file , int [] storedLocation , int [] defaultLocation , boolean storeOpenedSketches , boolean untitled ) throws Exception {
775
804
if (!file .exists ()) return null ;
776
805
777
806
// Cycle through open windows to make sure that it's not already open.
@@ -804,9 +833,7 @@ protected Editor handleOpen(File file, int[] storedLocation, int[] defaultLocati
804
833
805
834
// now that we're ready, show the window
806
835
// (don't do earlier, cuz we might move it based on a window being closed)
807
- if (showEditor ) {
808
- SwingUtilities .invokeLater (() -> editor .setVisible (true ));
809
- }
836
+ SwingUtilities .invokeLater (() -> editor .setVisible (true ));
810
837
811
838
return editor ;
812
839
}
@@ -1810,10 +1837,9 @@ public List<JMenu> getBoardsCustomMenus() {
1810
1837
}
1811
1838
1812
1839
public File getDefaultSketchbookFolderOrPromptForIt () {
1813
-
1814
1840
File sketchbookFolder = BaseNoGui .getDefaultSketchbookFolder ();
1815
1841
1816
- if (sketchbookFolder == null ) {
1842
+ if (sketchbookFolder == null && ! isCommandLine () ) {
1817
1843
sketchbookFolder = promptSketchbookLocation ();
1818
1844
}
1819
1845
0 commit comments