forked from platformio/platformio-core
Move "run" command to the root
This commit is contained in:
1 parent
dcc63da2ef
commit
52f8e98eed
10 files changed
+9
-9
No files matched your search
@@ -0,0 +1,13 @@
|
||||
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
@@ -0,0 +1,322 @@
|
||||
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import operator
|
||||
import os
|
||||
import shutil
|
||||
from multiprocessing import cpu_count
|
||||
from time import time
|
||||
|
||||
import click
|
||||
from tabulate import tabulate
|
||||
|
||||
from platformio import app, exception, fs, util
|
||||
from platformio.device.commands.monitor import device_monitor_cmd
|
||||
from platformio.project.config import ProjectConfig
|
||||
from platformio.project.helpers import find_project_dir_above, load_build_metadata
|
||||
from platformio.run.helpers import clean_build_dir, handle_legacy_libdeps
|
||||
from platformio.run.processor import EnvironmentProcessor
|
||||
from platformio.test.runners.base import CTX_META_TEST_IS_RUNNING
|
||||
|
||||
# pylint: disable=too-many-arguments,too-many-locals,too-many-branches
|
||||
|
||||
try:
|
||||
DEFAULT_JOB_NUMS = cpu_count()
|
||||
except NotImplementedError:
|
||||
DEFAULT_JOB_NUMS = 1
|
||||
|
||||
|
||||
@click.command("run", short_help="Run project targets (build, upload, clean, etc.)")
|
||||
@click.option("-e", "--environment", multiple=True)
|
||||
@click.option("-t", "--target", multiple=True)
|
||||
@click.option("--upload-port")
|
||||
@click.option(
|
||||
"-d",
|
||||
"--project-dir",
|
||||
default=os.getcwd,
|
||||
type=click.Path(
|
||||
exists=True, file_okay=True, dir_okay=True, writable=True, resolve_path=True
|
||||
),
|
||||
)
|
||||
@click.option(
|
||||
"-c",
|
||||
"--project-conf",
|
||||
type=click.Path(
|
||||
exists=True, file_okay=True, dir_okay=False, readable=True, resolve_path=True
|
||||
),
|
||||
)
|
||||
@click.option(
|
||||
"-j",
|
||||
"--jobs",
|
||||
type=int,
|
||||
default=DEFAULT_JOB_NUMS,
|
||||
help=(
|
||||
"Allow N jobs at once. "
|
||||
"Default is a number of CPUs in a system (N=%d)" % DEFAULT_JOB_NUMS
|
||||
),
|
||||
)
|
||||
@click.option(
|
||||
"-a",
|
||||
"--program-arg",
|
||||
"program_args",
|
||||
multiple=True,
|
||||
help="A program argument (multiple are allowed)",
|
||||
)
|
||||
@click.option("--disable-auto-clean", is_flag=True)
|
||||
@click.option("--list-targets", is_flag=True)
|
||||
@click.option("-s", "--silent", is_flag=True)
|
||||
@click.option("-v", "--verbose", is_flag=True)
|
||||
@click.pass_context
|
||||
def cli(
|
||||
ctx,
|
||||
environment,
|
||||
target,
|
||||
upload_port,
|
||||
project_dir,
|
||||
project_conf,
|
||||
jobs,
|
||||
program_args,
|
||||
disable_auto_clean,
|
||||
list_targets,
|
||||
silent,
|
||||
verbose,
|
||||
):
|
||||
app.set_session_var("custom_project_conf", project_conf)
|
||||
|
||||
# find project directory on upper level
|
||||
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
|
||||
|
||||
results = []
|
||||
with fs.cd(project_dir):
|
||||
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("platformio", "build_dir")
|
||||
try:
|
||||
clean_build_dir(build_dir, config)
|
||||
except: # pylint: disable=bare-except
|
||||
click.secho(
|
||||
"Can not remove temporary directory `%s`. Please remove "
|
||||
"it manually to avoid build issues" % build_dir,
|
||||
fg="yellow",
|
||||
)
|
||||
|
||||
handle_legacy_libdeps(project_dir, config)
|
||||
|
||||
default_envs = config.default_envs()
|
||||
for env in config.envs():
|
||||
skipenv = any(
|
||||
[
|
||||
environment and env not in environment,
|
||||
not environment and default_envs and env not in default_envs,
|
||||
]
|
||||
)
|
||||
if skipenv:
|
||||
results.append({"env": env})
|
||||
continue
|
||||
|
||||
# print empty line between multi environment project
|
||||
if not silent and any(r.get("succeeded") is not None for r in results):
|
||||
click.echo()
|
||||
|
||||
results.append(
|
||||
process_env(
|
||||
ctx,
|
||||
env,
|
||||
config,
|
||||
environment,
|
||||
target,
|
||||
upload_port,
|
||||
jobs,
|
||||
program_args,
|
||||
is_test_running,
|
||||
silent,
|
||||
verbose,
|
||||
)
|
||||
)
|
||||
|
||||
command_failed = any(r.get("succeeded") is False for r in results)
|
||||
|
||||
if not is_test_running and (command_failed or not silent) and len(results) > 1:
|
||||
print_processing_summary(results, verbose)
|
||||
|
||||
# Reset custom project config
|
||||
app.set_session_var("custom_project_conf", None)
|
||||
|
||||
if command_failed:
|
||||
raise exception.ReturnErrorCode(1)
|
||||
return True
|
||||
|
||||
|
||||
def process_env(
|
||||
ctx,
|
||||
name,
|
||||
config,
|
||||
environments,
|
||||
targets,
|
||||
upload_port,
|
||||
jobs,
|
||||
program_args,
|
||||
is_test_running,
|
||||
silent,
|
||||
verbose,
|
||||
):
|
||||
if not is_test_running and not silent:
|
||||
print_processing_header(name, config, verbose)
|
||||
|
||||
ep = EnvironmentProcessor(
|
||||
ctx,
|
||||
name,
|
||||
config,
|
||||
targets,
|
||||
upload_port,
|
||||
jobs,
|
||||
program_args,
|
||||
silent,
|
||||
verbose,
|
||||
)
|
||||
result = {"env": name, "duration": time(), "succeeded": ep.process()}
|
||||
result["duration"] = time() - result["duration"]
|
||||
|
||||
# print footer on error or when is not unit testing
|
||||
if not is_test_running and (not silent or not result["succeeded"]):
|
||||
print_processing_footer(result)
|
||||
|
||||
if (
|
||||
result["succeeded"]
|
||||
and "monitor" in ep.get_build_targets()
|
||||
and "nobuild" not in ep.get_build_targets()
|
||||
):
|
||||
ctx.invoke(
|
||||
device_monitor_cmd, environment=environments[0] if environments else None
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def print_processing_header(env, config, verbose=False):
|
||||
env_dump = []
|
||||
for k, v in config.items(env=env):
|
||||
if verbose or k in ("platform", "framework", "board"):
|
||||
env_dump.append("%s: %s" % (k, ", ".join(v) if isinstance(v, list) else v))
|
||||
click.echo(
|
||||
"Processing %s (%s)"
|
||||
% (click.style(env, fg="cyan", bold=True), "; ".join(env_dump))
|
||||
)
|
||||
terminal_width, _ = shutil.get_terminal_size()
|
||||
click.secho("-" * terminal_width, bold=True)
|
||||
|
||||
|
||||
def print_processing_footer(result):
|
||||
is_failed = not result.get("succeeded")
|
||||
util.print_labeled_bar(
|
||||
"[%s] Took %.2f seconds"
|
||||
% (
|
||||
(
|
||||
click.style("FAILED", fg="red", bold=True)
|
||||
if is_failed
|
||||
else click.style("SUCCESS", fg="green", bold=True)
|
||||
),
|
||||
result["duration"],
|
||||
),
|
||||
is_error=is_failed,
|
||||
)
|
||||
|
||||
|
||||
def print_processing_summary(results, verbose=False):
|
||||
tabular_data = []
|
||||
succeeded_nums = 0
|
||||
failed_nums = 0
|
||||
duration = 0
|
||||
|
||||
for result in results:
|
||||
duration += result.get("duration", 0)
|
||||
if result.get("succeeded") is False:
|
||||
failed_nums += 1
|
||||
status_str = click.style("FAILED", fg="red")
|
||||
elif result.get("succeeded") is None:
|
||||
if not verbose:
|
||||
continue
|
||||
status_str = "IGNORED"
|
||||
else:
|
||||
succeeded_nums += 1
|
||||
status_str = click.style("SUCCESS", fg="green")
|
||||
|
||||
tabular_data.append(
|
||||
(
|
||||
click.style(result["env"], fg="cyan"),
|
||||
status_str,
|
||||
util.humanize_duration_time(result.get("duration")),
|
||||
)
|
||||
)
|
||||
|
||||
click.echo()
|
||||
click.echo(
|
||||
tabulate(
|
||||
tabular_data,
|
||||
headers=[
|
||||
click.style(s, bold=True) for s in ("Environment", "Status", "Duration")
|
||||
],
|
||||
),
|
||||
err=failed_nums,
|
||||
)
|
||||
|
||||
util.print_labeled_bar(
|
||||
"%s%d succeeded in %s"
|
||||
% (
|
||||
"%d failed, " % failed_nums if failed_nums else "",
|
||||
succeeded_nums,
|
||||
util.humanize_duration_time(duration),
|
||||
),
|
||||
is_error=failed_nums,
|
||||
fg="red" if failed_nums else "green",
|
||||
)
|
||||
|
||||
|
||||
def print_target_list(envs):
|
||||
tabular_data = []
|
||||
for env, data in load_build_metadata(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")
|
||||
],
|
||||
),
|
||||
)
|
||||
@@ -0,0 +1,64 @@
|
||||
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from os import makedirs
|
||||
from os.path import isdir, isfile, join
|
||||
|
||||
import click
|
||||
|
||||
from platformio import fs
|
||||
from platformio.project.helpers import compute_project_checksum, get_project_dir
|
||||
|
||||
|
||||
def handle_legacy_libdeps(project_dir, config):
|
||||
legacy_libdeps_dir = join(project_dir, ".piolibdeps")
|
||||
if not isdir(legacy_libdeps_dir) or legacy_libdeps_dir == config.get(
|
||||
"platformio", "libdeps_dir"
|
||||
):
|
||||
return
|
||||
if not config.has_section("env"):
|
||||
config.add_section("env")
|
||||
lib_extra_dirs = config.get("env", "lib_extra_dirs", [])
|
||||
lib_extra_dirs.append(legacy_libdeps_dir)
|
||||
config.set("env", "lib_extra_dirs", lib_extra_dirs)
|
||||
click.secho(
|
||||
"DEPRECATED! A legacy library storage `{0}` has been found in a "
|
||||
"project. \nPlease declare project dependencies in `platformio.ini`"
|
||||
" file using `lib_deps` option and remove `{0}` folder."
|
||||
"\nMore details -> https://docs.platformio.org/page/projectconf/"
|
||||
"section_env_library.html#lib-deps".format(legacy_libdeps_dir),
|
||||
fg="yellow",
|
||||
)
|
||||
|
||||
|
||||
def clean_build_dir(build_dir, config):
|
||||
# remove legacy ".pioenvs" folder
|
||||
legacy_build_dir = join(get_project_dir(), ".pioenvs")
|
||||
if isdir(legacy_build_dir) and legacy_build_dir != build_dir:
|
||||
fs.rmtree(legacy_build_dir)
|
||||
|
||||
checksum_file = join(build_dir, "project.checksum")
|
||||
checksum = compute_project_checksum(config)
|
||||
|
||||
if isdir(build_dir):
|
||||
# check project structure
|
||||
if isfile(checksum_file):
|
||||
with open(checksum_file, encoding="utf8") as fp:
|
||||
if fp.read() == checksum:
|
||||
return
|
||||
fs.rmtree(build_dir)
|
||||
|
||||
makedirs(build_dir)
|
||||
with open(checksum_file, mode="w", encoding="utf8") as fp:
|
||||
fp.write(checksum)
|
||||
@@ -0,0 +1,94 @@
|
||||
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from platformio.package.commands.install import install_project_env_dependencies
|
||||
from platformio.platform.factory import PlatformFactory
|
||||
from platformio.project.exception import UndefinedEnvPlatformError
|
||||
from platformio.test.runners.base import CTX_META_TEST_RUNNING_NAME
|
||||
|
||||
# pylint: disable=too-many-instance-attributes
|
||||
|
||||
|
||||
class EnvironmentProcessor(object):
|
||||
def __init__( # pylint: disable=too-many-arguments
|
||||
self,
|
||||
cmd_ctx,
|
||||
name,
|
||||
config,
|
||||
targets,
|
||||
upload_port,
|
||||
jobs,
|
||||
program_args,
|
||||
silent,
|
||||
verbose,
|
||||
):
|
||||
self.cmd_ctx = cmd_ctx
|
||||
self.name = name
|
||||
self.config = config
|
||||
self.targets = [str(t) for t in targets]
|
||||
self.upload_port = upload_port
|
||||
self.jobs = jobs
|
||||
self.program_args = program_args
|
||||
self.silent = silent
|
||||
self.verbose = verbose
|
||||
self.options = config.items(env=name, as_dict=True)
|
||||
|
||||
def get_build_variables(self):
|
||||
variables = dict(
|
||||
pioenv=self.name,
|
||||
project_config=self.config.path,
|
||||
program_args=self.program_args,
|
||||
)
|
||||
|
||||
if CTX_META_TEST_RUNNING_NAME in self.cmd_ctx.meta:
|
||||
variables["piotest_running_name"] = self.cmd_ctx.meta[
|
||||
CTX_META_TEST_RUNNING_NAME
|
||||
]
|
||||
|
||||
if self.upload_port:
|
||||
# override upload port with a custom from CLI
|
||||
variables["upload_port"] = self.upload_port
|
||||
return variables
|
||||
|
||||
def get_build_targets(self):
|
||||
return (
|
||||
self.targets
|
||||
if self.targets
|
||||
else self.config.get("env:" + self.name, "targets", [])
|
||||
)
|
||||
|
||||
def process(self):
|
||||
if "platform" not in self.options:
|
||||
raise UndefinedEnvPlatformError(self.name)
|
||||
|
||||
build_vars = self.get_build_variables()
|
||||
build_targets = list(self.get_build_targets())
|
||||
|
||||
# skip monitor target, we call it above
|
||||
if "monitor" in build_targets:
|
||||
build_targets.remove("monitor")
|
||||
|
||||
if "clean" not in build_targets:
|
||||
install_project_env_dependencies(
|
||||
self.name,
|
||||
{
|
||||
"project_targets": build_targets,
|
||||
"piotest_running_name": build_vars.get("piotest_running_name"),
|
||||
},
|
||||
)
|
||||
|
||||
result = PlatformFactory.new(self.options["platform"], autoinstall=True).run(
|
||||
build_vars, build_targets, self.silent, self.verbose, self.jobs
|
||||
)
|
||||
return result["returncode"] == 0
|
||||
Reference in new issue
Block a user