From af9e1f9e3a25fb0ea5cfa00dc4b557456c9ae7d7 Mon Sep 17 00:00:00 2001 From: Yohsuke FURUTA Date: Wed, 2 May 2018 22:03:53 +0900 Subject: [PATCH 1/3] CLI: Add --version to CLI option I added to get the Arduino IDE version from the command line It will allow to check easily if the new Arduino is already installed. This feature makes it easier to build external systems linked to specific versions of Arduino. 1. I added `--version` action, which shows version name and exit 1. Currently, VERSION_NAME_LONG (like `1.8.5`, `1.9.0-beta`, `1.8.6 Hourly Build XXX`, etc...) is used. Because I want to know its version number and stable/beta/hourly. 2. Finish with `0`. Because it is `SUCCESSFLLY FINISHED`. 2. Updated man page. --- app/test/processing/app/CommandLineTest.java | 22 ++++++++++++++++++- .../app/helpers/CommandlineParser.java | 7 +++++- build/shared/manpage.adoc | 5 +++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/test/processing/app/CommandLineTest.java b/app/test/processing/app/CommandLineTest.java index a2532ff8116..31f63e99e7a 100644 --- a/app/test/processing/app/CommandLineTest.java +++ b/app/test/processing/app/CommandLineTest.java @@ -34,6 +34,7 @@ import java.io.File; import org.apache.commons.compress.utils.IOUtils; +import org.fest.assertions.Assertions; import org.junit.Before; import org.junit.Test; @@ -126,5 +127,24 @@ public void testCommandLinePreferencesSave() throws Exception { prefs = new PreferencesMap(prefFile); assertEquals("preference should be saved", "xxx", prefs.get("test_pref")); -} + } + + @Test + public void testCommandLineVersion() throws Exception { + Runtime rt = Runtime.getRuntime(); + Process pr = rt.exec(new String[]{ + arduinoPath.getAbsolutePath(), + "--version", + }); + pr.waitFor(); + + Assertions.assertThat(pr.exitValue()) + .as("Process will finish with exit code 0 in --version") + .isEqualTo(0); + Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream()))) + .matches("Arduino: \\d+\\.\\d+\\.\\d+.*\n"); + Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream()))) + .as("Currently, STDERR is not used in --version.") + .isEqualTo(""); + } } diff --git a/arduino-core/src/processing/app/helpers/CommandlineParser.java b/arduino-core/src/processing/app/helpers/CommandlineParser.java index 17e1bc86d2e..9cb04f5b6c5 100644 --- a/arduino-core/src/processing/app/helpers/CommandlineParser.java +++ b/arduino-core/src/processing/app/helpers/CommandlineParser.java @@ -16,7 +16,7 @@ public class CommandlineParser { private enum ACTION { - GUI, NOOP, VERIFY("--verify"), UPLOAD("--upload"), GET_PREF("--get-pref"), INSTALL_BOARD("--install-boards"), INSTALL_LIBRARY("--install-library"); + GUI, NOOP, VERIFY("--verify"), UPLOAD("--upload"), GET_PREF("--get-pref"), INSTALL_BOARD("--install-boards"), INSTALL_LIBRARY("--install-library"), VERSION("--version"); final String value; @@ -52,6 +52,7 @@ public CommandlineParser(String[] args) { actions.put("--get-pref", ACTION.GET_PREF); actions.put("--install-boards", ACTION.INSTALL_BOARD); actions.put("--install-library", ACTION.INSTALL_LIBRARY); + actions.put("--version", ACTION.VERSION); } public void parseArgumentsPhase1() { @@ -84,6 +85,10 @@ public void parseArgumentsPhase1() { } libraryToInstall = args[i]; } + if (a == ACTION.VERSION) { + BaseNoGui.showMessage("Arduino", BaseNoGui.VERSION_NAME_LONG); + System.exit(0); + } action = a; continue; } diff --git a/build/shared/manpage.adoc b/build/shared/manpage.adoc index 47847d89d43..1d0f4cba816 100644 --- a/build/shared/manpage.adoc +++ b/build/shared/manpage.adoc @@ -33,6 +33,8 @@ SYNOPSIS *arduino* [*--install-library* __library name__[:__version__][,__library name__[:__version__],__library name__[:__version__]] +*arduino* [*--version*] + DESCRIPTION ----------- The 'arduino' integrated development environment allows editing, @@ -83,6 +85,9 @@ ACTIONS Fetches available libraries list and install the specified one. If __version__ is omitted, the latest is installed. If a library with the same version is already installed, nothing is installed and program exits with exit code 1. If a library with a different version is already installed, it's replaced. Multiple libraries can be specified, separated by a comma. +*--version*:: + Print the version information and exit. + OPTIONS ------- *--board* __package__:__arch__:__board__[:__parameters__]:: From 6137e7a4cecf5d5da0b07050ed8dc2538d9adc63 Mon Sep 17 00:00:00 2001 From: Yohsuke FURUTA Date: Thu, 3 May 2018 01:06:17 +0900 Subject: [PATCH 2/3] Split "parse" and "action". Move print action to probably suitable place. This commit will fix the behavior of multiple actions about --version. --- app/src/processing/app/Base.java | 3 +++ app/test/processing/app/CommandLineTest.java | 5 +---- .../src/processing/app/helpers/CommandlineParser.java | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 1752e1dc824..4a9480a87cd 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -483,6 +483,9 @@ public Base(String[] args) throws Exception { System.exit(0); } else if (parser.isGetPrefMode()) { BaseNoGui.dumpPrefs(parser); + } else if (parser.isVersionMode()) { + System.out.print("Arduino: " + BaseNoGui.VERSION_NAME_LONG); + System.exit(0); } } diff --git a/app/test/processing/app/CommandLineTest.java b/app/test/processing/app/CommandLineTest.java index 31f63e99e7a..8470264779b 100644 --- a/app/test/processing/app/CommandLineTest.java +++ b/app/test/processing/app/CommandLineTest.java @@ -142,9 +142,6 @@ public void testCommandLineVersion() throws Exception { .as("Process will finish with exit code 0 in --version") .isEqualTo(0); Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream()))) - .matches("Arduino: \\d+\\.\\d+\\.\\d+.*\n"); - Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream()))) - .as("Currently, STDERR is not used in --version.") - .isEqualTo(""); + .matches("Arduino: \\d+\\.\\d+\\.\\d+.*"); } } diff --git a/arduino-core/src/processing/app/helpers/CommandlineParser.java b/arduino-core/src/processing/app/helpers/CommandlineParser.java index 9cb04f5b6c5..19216a32065 100644 --- a/arduino-core/src/processing/app/helpers/CommandlineParser.java +++ b/arduino-core/src/processing/app/helpers/CommandlineParser.java @@ -85,10 +85,6 @@ public void parseArgumentsPhase1() { } libraryToInstall = args[i]; } - if (a == ACTION.VERSION) { - BaseNoGui.showMessage("Arduino", BaseNoGui.VERSION_NAME_LONG); - System.exit(0); - } action = a; continue; } @@ -345,6 +341,10 @@ public boolean isInstallLibrary() { return action == ACTION.INSTALL_LIBRARY; } + public boolean isVersionMode() { + return action == ACTION.VERSION; + } + public String getBoardToInstall() { return this.boardToInstall; } From 0b4c2f212c4884ad357ffb8dcf7d2e5b00534391 Mon Sep 17 00:00:00 2001 From: Yohsuke FURUTA Date: Thu, 3 May 2018 01:06:48 +0900 Subject: [PATCH 3/3] add testcase --- app/test/processing/app/CommandLineTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/test/processing/app/CommandLineTest.java b/app/test/processing/app/CommandLineTest.java index 8470264779b..bf8eb8904c1 100644 --- a/app/test/processing/app/CommandLineTest.java +++ b/app/test/processing/app/CommandLineTest.java @@ -144,4 +144,19 @@ public void testCommandLineVersion() throws Exception { Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream()))) .matches("Arduino: \\d+\\.\\d+\\.\\d+.*"); } + + @Test + public void testCommandLineMultipleAction() throws Exception { + Runtime rt = Runtime.getRuntime(); + Process pr = rt.exec(new String[]{ + arduinoPath.getAbsolutePath(), + "--version", + "--verify", + }); + pr.waitFor(); + + Assertions.assertThat(pr.exitValue()) + .as("Multiple Action will be rejected") + .isEqualTo(3); + } }