diff --git a/platformio/__main__.py b/platformio/__main__.py index f114362d..d1cc6852 100644 --- a/platformio/__main__.py +++ b/platformio/__main__.py @@ -1,82 +1,41 @@ # Copyright (C) Ivan Kravets # See LICENSE for details. +from os import listdir +from os.path import join from sys import exit -import click -from clint.textui import colored, indent, puts +from click import command, MultiCommand, version_option -from platformio.util import get_project_config, run_builder +from platformio import __version__ +from platformio.util import get_source_dir + + +class PlatformioCLI(MultiCommand): + + def list_commands(self, ctx): + cmds = [] + for filename in listdir(join(get_source_dir(), "commands")): + if filename.startswith("__init__"): + continue + if filename.endswith(".py"): + cmds.append(filename[:-3]) + cmds.sort() + return cmds + + def get_command(self, ctx, name): + mod = __import__("platformio.commands." + name, None, None, ["cli"]) + return mod.cli + + +@command(cls=PlatformioCLI) +@version_option(__version__) +def cli(): + pass -@click.group() def main(): - pass - - -@main.group() -def init(): - """ Initialize new platformio based project """ - pass - - -@main.group() -def install(): - """ Install new platforms """ - pass - - -@main.group() -def list(): - """ List installed platforms """ - pass - - -@main.command() -@click.option("--environment", "-e", multiple=True) -@click.option("--target", "-t", multiple=True) -def run(environment, target): - """Process project environments """ - - config = get_project_config() - for section in config.sections(): - if section[:4] != "env:": - continue - envname = section[4:] - - if environment and envname not in environment: - puts("Skipped %s environment" % colored.yellow(envname)) - continue - - puts("Processing %s environment:" % colored.cyan(envname)) - variables = ["%s=%s" % (o.upper(), v) for o, v in config.items(section) - if o != "targets"] - variables.append("PIOENV=" + envname) - - envtargets = [] - if target: - envtargets = [t for t in target] - elif config.has_option(section, "targets"): - envtargets = config.get(section, "targets").split() - - result = run_builder(variables, envtargets) - - # print result - with indent(4, quote=colored.white(".")): - puts(colored.green(result['out'])) - puts(colored.red(result['err'])) - - -@main.group() -def search(): - """ Search for new platforms """ - pass - - -@main.group() -def show(): - """ Show information about installed platforms """ - pass + cli() if __name__ == "__main__": diff --git a/platformio/commands/__init__.py b/platformio/commands/__init__.py new file mode 100644 index 00000000..ca6f0304 --- /dev/null +++ b/platformio/commands/__init__.py @@ -0,0 +1,2 @@ +# Copyright (C) Ivan Kravets +# See LICENSE for details. diff --git a/platformio/commands/run.py b/platformio/commands/run.py new file mode 100644 index 00000000..b626ad0b --- /dev/null +++ b/platformio/commands/run.py @@ -0,0 +1,36 @@ +# Copyright (C) Ivan Kravets +# See LICENSE for details. + +from click import command, echo, option, style + +from platformio.util import get_project_config, run_builder, textindent + + +@command("run", short_help="Process project environments") +@option("--environment", "-e", multiple=True) +@option("--target", "-t", multiple=True) +def cli(environment, target): + config = get_project_config() + for section in config.sections(): + if section[:4] != "env:": + continue + envname = section[4:] + + if environment and envname not in environment: + echo("Skipped %s environment" % style(envname, fg="yellow")) + continue + + echo("Processing %s environment:" % style(envname, fg="cyan")) + variables = ["%s=%s" % (o.upper(), v) for o, v in config.items(section) + if o != "targets"] + variables.append("PIOENV=" + envname) + + envtargets = [] + if target: + envtargets = [t for t in target] + elif config.has_option(section, "targets"): + envtargets = config.get(section, "targets").split() + + result = run_builder(variables, envtargets) + echo(textindent(style(result['out'], fg="green"), ". ")) + echo(textindent(style(result['err'], fg="red"), ". ")) diff --git a/platformio/util.py b/platformio/util.py index 1d9ceabc..6e6e9803 100644 --- a/platformio/util.py +++ b/platformio/util.py @@ -5,6 +5,7 @@ from os import getcwd from os.path import dirname, expanduser, isfile, join, realpath from subprocess import PIPE, Popen from sys import exit +from textwrap import fill try: from configparser import ConfigParser @@ -39,6 +40,11 @@ def get_project_config(): return get_project_config._cache +def textindent(text, quote): + return fill(text, initial_indent=quote, + subsequent_indent=quote, width=120) + + def exec_command(args): p = Popen(args, stdout=PIPE, stderr=PIPE) out, err = p.communicate() diff --git a/setup.py b/setup.py index 0dd4696b..9660c110 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,6 @@ setup( license=__license__, install_requires=[ "click", - "clint", "pyserial", # "SCons" ],