|
11 | 11 | import java.util.Arrays;
|
12 | 12 | import java.util.Date;
|
13 | 13 | import java.util.HashMap;
|
| 14 | +import java.util.LinkedList; |
14 | 15 | import java.util.List;
|
15 | 16 | import java.util.Map;
|
16 | 17 | import java.util.logging.Level;
|
|
20 | 21 | import org.apache.commons.logging.impl.NoOpLog;
|
21 | 22 |
|
22 | 23 | import cc.arduino.packages.DiscoveryManager;
|
| 24 | +import cc.arduino.packages.Uploader; |
23 | 25 |
|
| 26 | +import processing.app.debug.Compiler; |
24 | 27 | import processing.app.debug.TargetBoard;
|
25 | 28 | import processing.app.debug.TargetPackage;
|
26 | 29 | import processing.app.debug.TargetPlatform;
|
27 | 30 | import processing.app.debug.TargetPlatformException;
|
28 | 31 | import processing.app.helpers.BasicUserNotifier;
|
| 32 | +import processing.app.helpers.CommandlineParser; |
29 | 33 | import processing.app.helpers.OSUtils;
|
30 | 34 | import processing.app.helpers.PreferencesMap;
|
31 | 35 | import processing.app.helpers.UserNotifier;
|
@@ -132,7 +136,7 @@ static public String getAvrBasePath() {
|
132 | 136 |
|
133 | 137 | static public File getBuildFolder() {
|
134 | 138 | if (buildFolder == null) {
|
135 |
| - String buildPath = Preferences.get("build.path"); |
| 139 | + String buildPath = PreferencesData.get("build.path"); |
136 | 140 | if (buildPath != null) {
|
137 | 141 | buildFolder = absoluteFile(buildPath);
|
138 | 142 | if (!buildFolder.exists())
|
@@ -407,6 +411,159 @@ static public String[] headerListFromIncludePath(File path) throws IOException {
|
407 | 411 | return list;
|
408 | 412 | }
|
409 | 413 |
|
| 414 | + static public void init(String[] args) { |
| 415 | + getPlatform().init(); |
| 416 | + |
| 417 | + String sketchbookPath = getSketchbookPath(); |
| 418 | + |
| 419 | + // If no path is set, get the default sketchbook folder for this platform |
| 420 | + if (sketchbookPath == null) { |
| 421 | + if (BaseNoGui.getPortableFolder() != null) |
| 422 | + PreferencesData.set("sketchbook.path", getPortableSketchbookFolder()); |
| 423 | + else |
| 424 | + showError(_("No sketchbook"), _("Sketchbook path not defined"), null); |
| 425 | + } |
| 426 | + |
| 427 | + BaseNoGui.initPackages(); |
| 428 | + |
| 429 | + // Setup board-dependent variables. |
| 430 | + onBoardOrPortChange(); |
| 431 | + |
| 432 | + CommandlineParser parser = CommandlineParser.newCommandlineParser(args); |
| 433 | + |
| 434 | + for (String path: parser.getFilenames()) { |
| 435 | + // Correctly resolve relative paths |
| 436 | + File file = absoluteFile(path); |
| 437 | + |
| 438 | + // Fix a problem with systems that use a non-ASCII languages. Paths are |
| 439 | + // being passed in with 8.3 syntax, which makes the sketch loader code |
| 440 | + // unhappy, since the sketch folder naming doesn't match up correctly. |
| 441 | + // http://dev.processing.org/bugs/show_bug.cgi?id=1089 |
| 442 | + if (OSUtils.isWindows()) { |
| 443 | + try { |
| 444 | + file = file.getCanonicalFile(); |
| 445 | + } catch (IOException e) { |
| 446 | + e.printStackTrace(); |
| 447 | + } |
| 448 | + } |
| 449 | + |
| 450 | + if (!parser.isVerifyOrUploadMode() && !parser.isGetPrefMode()) |
| 451 | + showError(_("Mode not supported"), _("Only --verify, --upload or --get-pref are supported"), null); |
| 452 | + |
| 453 | + if (!parser.isForceSavePrefs()) |
| 454 | + PreferencesData.setDoSave(false); |
| 455 | + if (!file.exists()) { |
| 456 | + String mess = I18n.format(_("Failed to open sketch: \"{0}\""), path); |
| 457 | + // Open failure is fatal in upload/verify mode |
| 458 | + showError(null, mess, 2); |
| 459 | + } |
| 460 | + } |
| 461 | + |
| 462 | + // Save the preferences. For GUI mode, this happens in the quit |
| 463 | + // handler, but for other modes we should also make sure to save |
| 464 | + // them. |
| 465 | + PreferencesData.save(); |
| 466 | + |
| 467 | + if (parser.isVerifyOrUploadMode()) { |
| 468 | + // Set verbosity for command line build |
| 469 | + PreferencesData.set("build.verbose", "" + parser.isDoVerboseBuild()); |
| 470 | + PreferencesData.set("upload.verbose", "" + parser.isDoVerboseUpload()); |
| 471 | + |
| 472 | + // Make sure these verbosity preferences are only for the |
| 473 | + // current session |
| 474 | + PreferencesData.setDoSave(false); |
| 475 | + |
| 476 | + if (parser.isUploadMode()) { |
| 477 | + |
| 478 | + if (parser.getFilenames().size() != 1) |
| 479 | + { |
| 480 | + showError(_("Multiple files not supported"), _("Only one file at time suported with --upload option"), null); |
| 481 | + } |
| 482 | + |
| 483 | + List<String> warningsAccumulator = new LinkedList<String>(); |
| 484 | + boolean success = false; |
| 485 | + try { |
| 486 | + // costruttore di Editor carica lo sketch usando handleOpenInternal() che fa |
| 487 | + // la new di Sketch che chiama load() nel suo costruttore |
| 488 | + // In questo punto questo si traduce in: |
| 489 | + // SketchData data = new SketchData(file); |
| 490 | + // File tempBuildFolder = getBuildFolder(); |
| 491 | + // data.load(); |
| 492 | + SketchData data = new SketchData(absoluteFile(parser.getFilenames().get(0))); |
| 493 | + File tempBuildFolder = getBuildFolder(); |
| 494 | + data.load(); |
| 495 | + |
| 496 | + // Sketch.exportApplet() |
| 497 | + // - chiama Sketch.prepare() che chiama Sketch.ensureExistence() |
| 498 | + // - chiama Sketch.build(verbose=false) che chiama Sketch.ensureExistence(), imposta il progressListener e chiama Compiler.build() |
| 499 | + // - chiama Sketch.upload() (cfr. dopo...) |
| 500 | + if (!data.getFolder().exists()) showError(_("No sketch"), _("Can't find the sketch in the specified path"), null); |
| 501 | + String suggestedClassName = Compiler.build(data, tempBuildFolder.getAbsolutePath(), tempBuildFolder, null, false); |
| 502 | + if (suggestedClassName == null) showError(_("Error while verifying"), _("An error occurred while verifying the sketch"), null); |
| 503 | + |
| 504 | + // - chiama Sketch.upload() ... to be continued ... |
| 505 | + Uploader uploader = Compiler.getUploaderByPreferences(); |
| 506 | + if (uploader.requiresAuthorization() && !PreferencesData.has(uploader.getAuthorizationKey())) showError(_("..."), _("..."), null); |
| 507 | + try { |
| 508 | + success = Compiler.upload(data, uploader, tempBuildFolder.getAbsolutePath(), suggestedClassName, false, warningsAccumulator); |
| 509 | + } finally { |
| 510 | + if (uploader.requiresAuthorization() && !success) { |
| 511 | + PreferencesData.remove(uploader.getAuthorizationKey()); |
| 512 | + } |
| 513 | + } |
| 514 | + } catch (Exception e) { |
| 515 | + showError(_("Error while verifying/uploading"), _("An error occurred while verifying/uploading the sketch"), e); |
| 516 | + } |
| 517 | + for (String warning : warningsAccumulator) { |
| 518 | + System.out.print(_("Warning")); |
| 519 | + System.out.print(": "); |
| 520 | + System.out.println(warning); |
| 521 | + } |
| 522 | + if (!success) showError(_("Error while uploading"), _("An error occurred while uploading the sketch"), null); |
| 523 | + } else { |
| 524 | + |
| 525 | + for (String path : parser.getFilenames()) |
| 526 | + { |
| 527 | + try { |
| 528 | + // costruttore di Editor carica lo sketch usando handleOpenInternal() che fa |
| 529 | + // la new di Sketch che chiama load() nel suo costruttore |
| 530 | + // In questo punto questo si traduce in: |
| 531 | + // SketchData data = new SketchData(file); |
| 532 | + // File tempBuildFolder = getBuildFolder(); |
| 533 | + // data.load(); |
| 534 | + SketchData data = new SketchData(absoluteFile(path)); |
| 535 | + File tempBuildFolder = getBuildFolder(); |
| 536 | + data.load(); |
| 537 | + |
| 538 | + // metodo Sketch.prepare() chiama Sketch.ensureExistence() |
| 539 | + // Sketch.build(verbose) chiama Sketch.ensureExistence() e poi imposta il progressListener e, finalmente, chiama Compiler.build() |
| 540 | + // In questo punto questo si traduce in: |
| 541 | + // if (!data.getFolder().exists()) showError(...); |
| 542 | + // String ... = Compiler.build(data, tempBuildFolder.getAbsolutePath(), tempBuildFolder, null, verbose); |
| 543 | + if (!data.getFolder().exists()) showError(_("No sketch"), _("Can't find the sketch in the specified path"), null); |
| 544 | + String suggestedClassName = Compiler.build(data, tempBuildFolder.getAbsolutePath(), tempBuildFolder, null, parser.isDoVerboseBuild()); |
| 545 | + if (suggestedClassName == null) showError(_("Error while verifying"), _("An error occurred while verifying the sketch"), null); |
| 546 | + } catch (Exception e) { |
| 547 | + showError(_("Error while verifying"), _("An error occurred while verifying the sketch"), e); |
| 548 | + } |
| 549 | + } |
| 550 | + |
| 551 | + } |
| 552 | + |
| 553 | + // No errors exit gracefully |
| 554 | + System.exit(0); |
| 555 | + } |
| 556 | + else if (parser.isGetPrefMode()) { |
| 557 | + String value = PreferencesData.get(parser.getGetPref(), null); |
| 558 | + if (value != null) { |
| 559 | + System.out.println(value); |
| 560 | + System.exit(0); |
| 561 | + } else { |
| 562 | + System.exit(4); |
| 563 | + } |
| 564 | + } |
| 565 | + } |
| 566 | + |
410 | 567 | static public void initLogger() {
|
411 | 568 | System.setProperty(LogFactoryImpl.LOG_PROPERTY, NoOpLog.class.getCanonicalName());
|
412 | 569 | Logger.getLogger("javax.jmdns").setLevel(Level.OFF);
|
@@ -509,6 +666,18 @@ static public String loadFile(File file) throws IOException {
|
509 | 666 | return PApplet.join(contents, "\n");
|
510 | 667 | }
|
511 | 668 |
|
| 669 | + static public void main(String args[]) throws Exception { |
| 670 | + initPlatform(); |
| 671 | + |
| 672 | + initPortableFolder(); |
| 673 | + |
| 674 | + PreferencesData.init(null); |
| 675 | + |
| 676 | + prescanParameters(args); |
| 677 | + |
| 678 | + init(args); |
| 679 | + } |
| 680 | + |
512 | 681 | static public void onBoardOrPortChange() {
|
513 | 682 | TargetPlatform targetPlatform = getTargetPlatform();
|
514 | 683 | if (targetPlatform == null)
|
|
0 commit comments