From 9f05519ccd98e621e10e22593f7a84278bbec073 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Mon, 22 Jun 2020 19:53:31 +0300 Subject: [PATCH] =?UTF-8?q?List=20available=20project=20targets=20with=20a?= =?UTF-8?q?=20new=20"platformio=20run=20=E2=80=93list-targets"=20command?= =?UTF-8?q?=20//=20Resolve=20#3544?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HISTORY.rst | 1 + docs | 2 +- platformio/commands/run/command.py | 45 ++++++++++++++++++++++++++---- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 709cc9b7..2e9af05c 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -23,6 +23,7 @@ PlatformIO Core 4 - Python callback as a target (use the power of Python interpreter and PlatformIO Build API) +* List available project targets (including dev-platform specific and custom targets) with a new `platformio run --list-targets `__ command (`issue #3544 `_) * Added support for "globstar/`**`" (recursive) pattern for the different commands and configuration options (`platformio ci `__, `src_filter `__, `check_patterns `__, `library.json > srcFilter `__). Python 3.5+ is required. * Added a new ``-e, --environment`` option to `platformio project init `__ command that helps to update a PlatformIO project using existing environment * Fixed an issue with PIO Unit Testing when running multiple environments (`issue #3523 `_) diff --git a/docs b/docs index 53c8b74e..e2ed4006 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 53c8b74e709cae5e4678d41e491af118edd426ea +Subproject commit e2ed4006983b5400dee022def8774b13e15466ee diff --git a/platformio/commands/run/command.py b/platformio/commands/run/command.py index 378eaf0d..c2142723 100644 --- a/platformio/commands/run/command.py +++ b/platformio/commands/run/command.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import operator +import os from multiprocessing import cpu_count -from os import getcwd -from os.path import isfile from time import time import click @@ -26,7 +26,7 @@ from platformio.commands.run.helpers import clean_build_dir, handle_legacy_libde from platformio.commands.run.processor import EnvironmentProcessor from platformio.commands.test.processor import CTX_META_TEST_IS_RUNNING from platformio.project.config import ProjectConfig -from platformio.project.helpers import find_project_dir_above +from platformio.project.helpers import find_project_dir_above, load_project_ide_data # pylint: disable=too-many-arguments,too-many-locals,too-many-branches @@ -43,7 +43,7 @@ except NotImplementedError: @click.option( "-d", "--project-dir", - default=getcwd, + default=os.getcwd, type=click.Path( exists=True, file_okay=True, dir_okay=True, writable=True, resolve_path=True ), @@ -68,6 +68,7 @@ except NotImplementedError: @click.option("-s", "--silent", is_flag=True) @click.option("-v", "--verbose", is_flag=True) @click.option("--disable-auto-clean", is_flag=True) +@click.option("--list-targets", is_flag=True) @click.pass_context def cli( ctx, @@ -80,11 +81,12 @@ def cli( silent, verbose, disable_auto_clean, + list_targets, ): app.set_session_var("custom_project_conf", project_conf) # find project directory on upper level - if isfile(project_dir): + if os.path.isfile(project_dir): project_dir = find_project_dir_above(project_dir) is_test_running = CTX_META_TEST_IS_RUNNING in ctx.meta @@ -93,6 +95,9 @@ def cli( config = ProjectConfig.get_instance(project_conf) config.validate(environment) + if list_targets: + return print_target_list(list(environment) or config.envs()) + # clean obsolete build dir if not disable_auto_clean: build_dir = config.get_optional_dir("build") @@ -261,3 +266,33 @@ def print_processing_summary(results): is_error=failed_nums, fg="red" if failed_nums else "green", ) + + +def print_target_list(envs): + tabular_data = [] + for env, data in load_project_ide_data(os.getcwd(), envs).items(): + tabular_data.extend( + sorted( + [ + ( + click.style(env, fg="cyan"), + t["group"], + click.style(t.get("name"), fg="yellow"), + t["title"], + t.get("description"), + ) + for t in data.get("targets", []) + ], + key=operator.itemgetter(1, 2), + ) + ) + tabular_data.append((None, None, None, None, None)) + click.echo( + tabulate( + tabular_data, + headers=[ + click.style(s, bold=True) + for s in ("Environment", "Group", "Name", "Title", "Description") + ], + ), + )