Added `env.StringifyMacro(value)` helper function for the Advanced Scripting

This commit is contained in:
Ivan Kravets
2022-06-17 12:25:52 +03:00
parent 42690d3fa7
commit 7a01da7039
3 changed files with 53 additions and 1 deletions

View File

@ -32,6 +32,7 @@ PlatformIO Core 6
* Allowed to ``Import("projenv")`` in a library extra script (`issue #4305 <https://github.com/platformio/platformio-core/issues/4305>`_)
* Improved a serial port finder for a board with predefined HWIDs
* Added ``env.StringifyMacro(value)`` helper function for the `Advanced Scripting <https://docs.platformio.org/en/latest/scripting/index.html>`__
6.0.2 (2022-06-01)
~~~~~~~~~~~~~~~~~~

View File

@ -264,6 +264,10 @@ def ProcessUnFlags(env, flags):
env[key].remove(current)
def StringifyMacro(env, value):
return '\\"%s\\"' % value.replace('"', '\\\\\\"')
def MatchSourceFiles(env, src_dir, src_filter=None):
src_filter = env.subst(src_filter) if src_filter else None
src_filter = src_filter or SRC_FILTER_DEFAULT
@ -368,6 +372,7 @@ def generate(env):
env.AddMethod(ParseFlagsExtended)
env.AddMethod(ProcessFlags)
env.AddMethod(ProcessUnFlags)
env.AddMethod(StringifyMacro)
env.AddMethod(MatchSourceFiles)
env.AddMethod(CollectBuildFiles)
env.AddMethod(AddBuildMiddleware)

View File

@ -252,5 +252,51 @@ platform = native
lib_deps = symlink://../External
"""
)
result = clirunner.invoke(cmd_run, ["--project-dir", str(project_dir), "--verbose"])
result = clirunner.invoke(cmd_run, ["--project-dir", str(project_dir)])
validate_cliresult(result)
def test_stringification(clirunner, validate_cliresult, tmp_path: Path):
project_dir = tmp_path / "project"
src_dir = project_dir / "src"
src_dir.mkdir(parents=True)
(src_dir / "main.c").write_text(
"""
#include <stdio.h>
int main(void) {
printf("MACRO_1=<%s>\\n", MACRO_1);
printf("MACRO_2=<%s>\\n", MACRO_2);
printf("MACRO_3=<%s>\\n", MACRO_3);
printf("MACRO_4=<%s>\\n", MACRO_4);
return(0);
}
"""
)
(project_dir / "platformio.ini").write_text(
"""
[env:native]
platform = native
extra_scripts = script.py
build_flags =
'-DMACRO_1="Hello World!"'
'-DMACRO_2="Text is \\\\"Quoted\\\\""'
"""
)
(project_dir / "script.py").write_text(
"""
Import("projenv")
projenv.Append(CPPDEFINES=[
("MACRO_3", projenv.StringifyMacro('Hello "World"! Isn\\'t true?')),
("MACRO_4", projenv.StringifyMacro("Special chars: ',(,),[,],:"))
])
"""
)
result = clirunner.invoke(
cmd_run, ["--project-dir", str(project_dir), "-t", "exec"]
)
validate_cliresult(result)
assert "MACRO_1=<Hello World!>" in result.output
assert 'MACRO_2=<Text is "Quoted">' in result.output
assert 'MACRO_3=<Hello "World"! Isn\'t true?>' in result.output
assert 'MACRO_4=<Special chars: \',(,),[,],:>' in result.output