Load "idedata" configuration from a dumped file

This commit is contained in:
Ivan Kravets
2021-03-19 13:46:27 +02:00
parent 9ede20a367
commit eebdf04357
3 changed files with 38 additions and 22 deletions

View File

@ -224,10 +224,11 @@ if "idedata" in COMMAND_LINE_TARGETS:
Import("projenv") Import("projenv")
except: # pylint: disable=bare-except except: # pylint: disable=bare-except
projenv = env projenv = env
click.echo( data = projenv.DumpIDEData(env)
"\n%s\n" # dump to file for the further reading by project.helpers.load_project_ide_data
% json.dumps(projenv.DumpIDEData(env)) # pylint: disable=undefined-variable with open(projenv.subst(os.path.join("$BUILD_DIR", "idedata.json")), "w") as fp:
) json.dump(data, fp)
click.echo("\n%s\n" % json.dumps(data)) # pylint: disable=undefined-variable
env.Exit(0) env.Exit(0)
if "sizedata" in COMMAND_LINE_TARGETS: if "sizedata" in COMMAND_LINE_TARGETS:

View File

@ -384,7 +384,7 @@ class ProjectConfigDirsMixin(object):
if result is None: if result is None:
return None return None
project_dir = os.getcwd() project_dir = os.path.dirname(self.path)
# patterns # patterns
if "$PROJECT_HASH" in result: if "$PROJECT_HASH" in result:

View File

@ -135,17 +135,29 @@ def compute_project_checksum(config):
return checksum.hexdigest() return checksum.hexdigest()
def load_project_ide_data(project_dir, env_or_envs): def load_project_ide_data(project_dir, env_or_envs, cache=False):
assert env_or_envs
env_names = env_or_envs
if not isinstance(env_names, list):
env_names = [env_names]
result = _load_cached_project_ide_data(project_dir, env_names) if cache else {}
missed_env_names = set(env_names) - set(result.keys())
if missed_env_names:
result.update(_load_project_ide_data(project_dir, missed_env_names))
if not isinstance(env_or_envs, list) and env_or_envs in result:
return result[env_or_envs]
return result or None
def _load_project_ide_data(project_dir, env_names):
# pylint: disable=import-outside-toplevel # pylint: disable=import-outside-toplevel
from platformio.commands.run.command import cli as cmd_run from platformio.commands.run.command import cli as cmd_run
assert env_or_envs
envs = env_or_envs
if not isinstance(envs, list):
envs = [envs]
args = ["--project-dir", project_dir, "--target", "idedata"] args = ["--project-dir", project_dir, "--target", "idedata"]
for env in envs: for name in env_names:
args.extend(["-e", env]) args.extend(["-e", name])
result = CliRunner().invoke(cmd_run, args) result = CliRunner().invoke(cmd_run, args)
if result.exit_code != 0 and not isinstance( if result.exit_code != 0 and not isinstance(
result.exception, exception.ReturnErrorCode result.exception, exception.ReturnErrorCode
@ -153,14 +165,17 @@ def load_project_ide_data(project_dir, env_or_envs):
raise result.exception raise result.exception
if '"includes":' not in result.output: if '"includes":' not in result.output:
raise exception.PlatformioException(result.output) raise exception.PlatformioException(result.output)
return _load_cached_project_ide_data(project_dir, env_names)
data = {}
for line in result.output.split("\n"): def _load_cached_project_ide_data(project_dir, env_names):
line = line.strip() build_dir = ProjectConfig.get_instance(
if line.startswith('{"') and line.endswith("}") and "env_name" in line: join(project_dir, "platformio.ini")
_data = json.loads(line) ).get_optional_dir("build")
if "env_name" in _data: result = {}
data[_data["env_name"]] = _data for name in env_names:
if not isinstance(env_or_envs, list) and env_or_envs in data: if not os.path.isfile(os.path.join(build_dir, name, "idedata.json")):
return data[env_or_envs] continue
return data or None with open(os.path.join(build_dir, name, "idedata.json")) as fp:
result[name] = json.load(fp)
return result