From eeeed1ba08bd1781eb302ff663a216401ba83fd2 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Wed, 6 May 2015 17:29:58 +0100 Subject: [PATCH] =?UTF-8?q?Allow=20to=20run=20project=20with=20platformio?= =?UTF-8?q?=20run=20=E2=80=93project-dir=20option=20without=20changing=20t?= =?UTF-8?q?he=20current=20working=20directory=20//=20Resolve=20#192?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HISTORY.rst | 3 ++ docs/userguide/cmd_run.rst | 6 +++ platformio/commands/run.py | 81 +++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index b5063b09..2de01062 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -11,6 +11,9 @@ Release History (`issue #174 `_) * Added global ``-f, --force`` option which will force to accept any confirmation prompts (`issue #152 `_) +* Allowed to run project with `platformio run --project-dir `_ option without changing the current working + directory + (`issue #192 `_) * Allowed to add library dependencies for build environment using `install_libs `_ option in ``platformio.ini`` diff --git a/docs/userguide/cmd_run.rst b/docs/userguide/cmd_run.rst index 2dc833f4..9c170794 100644 --- a/docs/userguide/cmd_run.rst +++ b/docs/userguide/cmd_run.rst @@ -39,6 +39,12 @@ Process specified targets Upload port of embedded board. To print all available ports use :ref:`cmd_serialports` command +.. option:: + --build-dir + +Specify the path to project directory. By default, ``--build-dir`` is equal to +current working directory (``CWD``). + Examples -------- diff --git a/platformio/commands/run.py b/platformio/commands/run.py index 0a49d513..e29d1366 100644 --- a/platformio/commands/run.py +++ b/platformio/commands/run.py @@ -2,6 +2,7 @@ # See LICENSE for details. from datetime import datetime +from os import chdir, getcwd from os.path import getmtime, isdir, join from shutil import rmtree from time import time @@ -20,52 +21,60 @@ from platformio.platforms.base import PlatformFactory @click.option("--environment", "-e", multiple=True, metavar="") @click.option("--target", "-t", multiple=True, metavar="") @click.option("--upload-port", metavar="") +@click.option("--project-dir", default=getcwd, + type=click.Path(exists=True, file_okay=False, dir_okay=True, + writable=True, resolve_path=True)) @click.pass_context -def cli(ctx, environment, target, upload_port): +def cli(ctx, environment, target, upload_port, project_dir): + initial_cwd = getcwd() + chdir(project_dir) + try: + config = util.get_project_config() - config = util.get_project_config() + if not config.sections(): + raise exception.ProjectEnvsNotAvailable() - if not config.sections(): - raise exception.ProjectEnvsNotAvailable() + unknown = set(environment) - set([s[4:] for s in config.sections()]) + if unknown: + raise exception.UnknownEnvNames(", ".join(unknown)) - unknown = set(environment) - set([s[4:] for s in config.sections()]) - if unknown: - raise exception.UnknownEnvNames(", ".join(unknown)) + # remove ".pioenvs" if project config is modified + _pioenvs_dir = util.get_pioenvs_dir() + if (isdir(_pioenvs_dir) and + getmtime(join(util.get_project_dir(), "platformio.ini")) > + getmtime(_pioenvs_dir)): + rmtree(_pioenvs_dir) - # remove ".pioenvs" if project config is modified - _pioenvs_dir = util.get_pioenvs_dir() - if (isdir(_pioenvs_dir) and - getmtime(join(util.get_project_dir(), "platformio.ini")) > - getmtime(_pioenvs_dir)): - rmtree(_pioenvs_dir) + found_error = False + _first_done = False + for section in config.sections(): + # skip main configuration section + if section == "platformio": + continue + elif section[:4] != "env:": + raise exception.InvalidEnvName(section) - found_error = False - _first_done = False - for section in config.sections(): - # skip main configuration section - if section == "platformio": - continue - elif section[:4] != "env:": - raise exception.InvalidEnvName(section) + envname = section[4:] + if environment and envname not in environment: + # echo("Skipped %s environment" % style(envname, fg="yellow")) + continue - envname = section[4:] - if environment and envname not in environment: - # echo("Skipped %s environment" % style(envname, fg="yellow")) - continue + options = {} + for k, v in config.items(section): + options[k] = v - options = {} - for k, v in config.items(section): - options[k] = v + if _first_done: + click.echo() - if _first_done: - click.echo() + if not process_environment( + ctx, envname, options, target, upload_port): + found_error = True + _first_done = True - if not process_environment(ctx, envname, options, target, upload_port): - found_error = True - _first_done = True - - if found_error: - raise exception.ReturnErrorCode() + if found_error: + raise exception.ReturnErrorCode() + finally: + chdir(initial_cwd) def process_environment(ctx, name, options, targets, upload_port):