Allow to run project with platformio run –project-dir option without changing the current working directory // Resolve #192

This commit is contained in:
Ivan Kravets
2015-05-06 17:29:58 +01:00
parent 04f246ea9d
commit eeeed1ba08
3 changed files with 54 additions and 36 deletions

View File

@ -11,6 +11,9 @@ Release History
(`issue #174 <https://github.com/platformio/platformio/issues/174>`_) (`issue #174 <https://github.com/platformio/platformio/issues/174>`_)
* Added global ``-f, --force`` option which will force to accept any * Added global ``-f, --force`` option which will force to accept any
confirmation prompts (`issue #152 <https://github.com/platformio/platformio/issues/152>`_) confirmation prompts (`issue #152 <https://github.com/platformio/platformio/issues/152>`_)
* Allowed to run project with `platformio run --project-dir <http://docs.platformio.org/en/latest/userguide/cmd_run.html#cmdoption--project-dir>`_ option without changing the current working
directory
(`issue #192 <https://github.com/platformio/platformio/issues/192>`_)
* Allowed to add library dependencies for build environment using * Allowed to add library dependencies for build environment using
`install_libs <http://docs.platformio.org/en/latest/projectconf.html#install-libs>`_ `install_libs <http://docs.platformio.org/en/latest/projectconf.html#install-libs>`_
option in ``platformio.ini`` option in ``platformio.ini``

View File

@ -39,6 +39,12 @@ Process specified targets
Upload port of embedded board. To print all available ports use Upload port of embedded board. To print all available ports use
:ref:`cmd_serialports` command :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 Examples
-------- --------

View File

@ -2,6 +2,7 @@
# See LICENSE for details. # See LICENSE for details.
from datetime import datetime from datetime import datetime
from os import chdir, getcwd
from os.path import getmtime, isdir, join from os.path import getmtime, isdir, join
from shutil import rmtree from shutil import rmtree
from time import time from time import time
@ -20,52 +21,60 @@ from platformio.platforms.base import PlatformFactory
@click.option("--environment", "-e", multiple=True, metavar="<environment>") @click.option("--environment", "-e", multiple=True, metavar="<environment>")
@click.option("--target", "-t", multiple=True, metavar="<target>") @click.option("--target", "-t", multiple=True, metavar="<target>")
@click.option("--upload-port", metavar="<upload port>") @click.option("--upload-port", metavar="<upload port>")
@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 @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(): unknown = set(environment) - set([s[4:] for s in config.sections()])
raise exception.ProjectEnvsNotAvailable() if unknown:
raise exception.UnknownEnvNames(", ".join(unknown))
unknown = set(environment) - set([s[4:] for s in config.sections()]) # remove ".pioenvs" if project config is modified
if unknown: _pioenvs_dir = util.get_pioenvs_dir()
raise exception.UnknownEnvNames(", ".join(unknown)) 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 found_error = False
_pioenvs_dir = util.get_pioenvs_dir() _first_done = False
if (isdir(_pioenvs_dir) and for section in config.sections():
getmtime(join(util.get_project_dir(), "platformio.ini")) > # skip main configuration section
getmtime(_pioenvs_dir)): if section == "platformio":
rmtree(_pioenvs_dir) continue
elif section[:4] != "env:":
raise exception.InvalidEnvName(section)
found_error = False envname = section[4:]
_first_done = False if environment and envname not in environment:
for section in config.sections(): # echo("Skipped %s environment" % style(envname, fg="yellow"))
# skip main configuration section continue
if section == "platformio":
continue
elif section[:4] != "env:":
raise exception.InvalidEnvName(section)
envname = section[4:] options = {}
if environment and envname not in environment: for k, v in config.items(section):
# echo("Skipped %s environment" % style(envname, fg="yellow")) options[k] = v
continue
options = {} if _first_done:
for k, v in config.items(section): click.echo()
options[k] = v
if _first_done: if not process_environment(
click.echo() ctx, envname, options, target, upload_port):
found_error = True
_first_done = True
if not process_environment(ctx, envname, options, target, upload_port): if found_error:
found_error = True raise exception.ReturnErrorCode()
_first_done = True finally:
chdir(initial_cwd)
if found_error:
raise exception.ReturnErrorCode()
def process_environment(ctx, name, options, targets, upload_port): def process_environment(ctx, name, options, targets, upload_port):