2014-06-03 21:27:36 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
2014-06-07 13:34:31 +03:00
|
|
|
from click import command, echo, option, secho, style
|
2014-06-03 21:27:36 +03:00
|
|
|
|
2014-07-27 22:39:41 +03:00
|
|
|
from platformio.exception import (InvalidEnvName, ProjectEnvsNotAvaialable,
|
|
|
|
UndefinedEnvPlatform, UnknownEnvNames)
|
2014-06-12 23:29:47 +03:00
|
|
|
from platformio.platforms._base import PlatformFactory
|
2014-06-07 13:34:31 +03:00
|
|
|
from platformio.util import get_project_config
|
2014-06-03 21:27:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
@command("run", short_help="Process project environments")
|
2014-06-12 21:17:45 +03:00
|
|
|
@option("--environment", "-e", multiple=True, metavar="<environment>")
|
|
|
|
@option("--target", "-t", multiple=True, metavar="<target>")
|
2014-07-27 22:39:41 +03:00
|
|
|
@option("--upload-port", metavar="<upload port>")
|
|
|
|
def cli(environment, target, upload_port):
|
2014-06-07 13:34:31 +03:00
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
config = get_project_config()
|
2014-06-12 21:17:45 +03:00
|
|
|
|
|
|
|
if not config.sections():
|
2014-06-16 12:07:37 +03:00
|
|
|
raise ProjectEnvsNotAvaialable()
|
2014-06-12 21:17:45 +03:00
|
|
|
|
2014-07-27 22:39:41 +03:00
|
|
|
envnames = [s[4:] for s in config.sections()]
|
|
|
|
unknown = set(environment) - set(envnames)
|
|
|
|
if unknown:
|
|
|
|
raise UnknownEnvNames(", ".join(unknown))
|
|
|
|
|
2014-06-03 21:27:36 +03:00
|
|
|
for section in config.sections():
|
|
|
|
if section[:4] != "env:":
|
2014-07-27 22:39:41 +03:00
|
|
|
raise InvalidEnvName(section)
|
2014-06-03 21:27:36 +03:00
|
|
|
|
2014-06-07 13:34:31 +03:00
|
|
|
envname = section[4:]
|
2014-06-03 21:27:36 +03:00
|
|
|
if environment and envname not in environment:
|
2014-07-27 22:39:41 +03:00
|
|
|
# echo("Skipped %s environment" % style(envname, fg="yellow"))
|
2014-06-03 21:27:36 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
echo("Processing %s environment:" % style(envname, fg="cyan"))
|
2014-07-27 22:39:41 +03:00
|
|
|
|
|
|
|
variables = ["PIOENV=" + envname]
|
|
|
|
if upload_port:
|
|
|
|
variables.append("UPLOAD_PORT=%s" % upload_port)
|
|
|
|
for k, v in config.items(section):
|
|
|
|
k = k.upper()
|
|
|
|
if k == "TARGETS" or (k == "UPLOAD_PORT" and upload_port):
|
|
|
|
continue
|
|
|
|
variables.append("%s=%s" % (k.upper(), v))
|
2014-06-03 21:27:36 +03:00
|
|
|
|
|
|
|
envtargets = []
|
|
|
|
if target:
|
|
|
|
envtargets = [t for t in target]
|
|
|
|
elif config.has_option(section, "targets"):
|
|
|
|
envtargets = config.get(section, "targets").split()
|
|
|
|
|
2014-06-07 13:34:31 +03:00
|
|
|
if not config.has_option(section, "platform"):
|
|
|
|
raise UndefinedEnvPlatform(envname)
|
|
|
|
|
|
|
|
p = PlatformFactory().newPlatform(config.get(section, "platform"))
|
|
|
|
result = p.run(variables, envtargets)
|
|
|
|
secho(result['out'], fg="green")
|
|
|
|
secho(result['err'], fg="red")
|