2222
2323package processing .app ;
2424
25+ import cc .arduino .Compiler ;
2526import cc .arduino .Constants ;
2627import cc .arduino .UpdatableBoardsLibsFakeURLsHandler ;
28+ import cc .arduino .UploaderUtils ;
2729import cc .arduino .contributions .*;
2830import cc .arduino .contributions .libraries .*;
2931import cc .arduino .contributions .libraries .ui .LibraryManagerUI ;
@@ -84,7 +86,6 @@ public class Base {
8486 private static boolean commandLine ;
8587 public static volatile Base INSTANCE ;
8688
87- public static SplashScreenHelper splashScreenHelper = new SplashScreenHelper (SplashScreen .getSplashScreen ());
8889 public static Map <String , Object > FIND_DIALOG_STATE = new HashMap <>();
8990 private final ContributionInstaller contributionInstaller ;
9091 private final LibraryInstaller libraryInstaller ;
@@ -128,59 +129,13 @@ static public void main(String args[]) throws Exception {
128129 }
129130
130131 try {
131- guardedMain (args );
132+ INSTANCE = new Base (args );
132133 } catch (Throwable e ) {
133134 e .printStackTrace (System .err );
134135 System .exit (255 );
135136 }
136137 }
137138
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-
184139 static public void initLogger () {
185140 Handler consoleHandler = new ConsoleLogger ();
186141 consoleHandler .setLevel (Level .ALL );
@@ -208,12 +163,6 @@ static public void initLogger() {
208163
209164 }
210165
211-
212- static protected void setCommandLine () {
213- commandLine = true ;
214- }
215-
216-
217166 static protected boolean isCommandLine () {
218167 return commandLine ;
219168 }
@@ -227,10 +176,60 @@ static public File absoluteFile(String path) {
227176 }
228177
229178 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 );
231195
232196 CommandlineParser parser = new CommandlineParser (args );
233197 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 );
234233
235234 BaseNoGui .checkInstallationFolder ();
236235
@@ -246,11 +245,15 @@ public Base(String[] args) throws Exception {
246245 }
247246 }
248247
249- splashScreenHelper .splashText (tr ("Initializing packages..." ));
248+ splash .splashText (tr ("Initializing packages..." ));
250249 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+ }
254257
255258 // Setup board-dependent variables.
256259 onBoardOrPortChange ();
@@ -263,35 +266,6 @@ public Base(String[] args) throws Exception {
263266
264267 parser .parseArgumentsPhase2 ();
265268
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-
295269 // Save the preferences. For GUI mode, this happens in the quit
296270 // handler, but for other modes we should also make sure to save
297271 // them.
@@ -377,35 +351,90 @@ public Base(String[] args) throws Exception {
377351 System .exit (0 );
378352
379353 } else if (parser .isVerifyOrUploadMode ()) {
380- splashScreenHelper .close ();
381354 // 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 ());
385357
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
388362 PreferencesData .setDoSave (false );
389363
390- Editor editor = editors .get (0 );
364+ Sketch sketch = null ;
365+ String outputFile = null ;
391366
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 );
399373
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
402377 System .exit (1 );
403378 }
404379
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+
405406 // No errors exit gracefully
406407 System .exit (0 );
407408 } 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+ }
409438
410439 installKeyboardInputMap ();
411440
@@ -472,7 +501,7 @@ protected boolean restoreSketches() throws Exception {
472501 }
473502 int [] location = retrieveSketchLocation ("" + i );
474503 // 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 ) {
476505 opened ++;
477506 }
478507 }
@@ -764,14 +793,14 @@ public Editor handleOpen(File file) throws Exception {
764793 }
765794
766795 public Editor handleOpen (File file , boolean untitled ) throws Exception {
767- return handleOpen (file , nextEditorLocation (), true , untitled );
796+ return handleOpen (file , nextEditorLocation (), untitled );
768797 }
769798
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 );
772801 }
773802
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 {
775804 if (!file .exists ()) return null ;
776805
777806 // 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
804833
805834 // now that we're ready, show the window
806835 // (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 ));
810837
811838 return editor ;
812839 }
@@ -1810,10 +1837,9 @@ public List<JMenu> getBoardsCustomMenus() {
18101837 }
18111838
18121839 public File getDefaultSketchbookFolderOrPromptForIt () {
1813-
18141840 File sketchbookFolder = BaseNoGui .getDefaultSketchbookFolder ();
18151841
1816- if (sketchbookFolder == null ) {
1842+ if (sketchbookFolder == null && ! isCommandLine () ) {
18171843 sketchbookFolder = promptSketchbookLocation ();
18181844 }
18191845
0 commit comments