Improve handling of multi-file `*.ino/pde` sketches // Resolve #130

This commit is contained in:
Ivan Kravets
2015-03-21 23:08:36 +02:00
parent b82f9da290
commit 7f43df45c5
3 changed files with 52 additions and 34 deletions

View File

@ -1,6 +1,13 @@
Release History
===============
1.3.0 (2015-??-??)
------------------
* Improved handling of multi-file ``*.ino/pde`` sketches
(`issue #130 <https://github.com/ivankravets/platformio/issues/130>`_)
1.2.0 (2015-03-20)
------------------

View File

@ -1,7 +1,7 @@
# Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.
VERSION = (1, 2, 0)
VERSION = (1, 3, "0.dev0")
__version__ = ".".join([str(s) for s in VERSION])
__title__ = "platformio"

View File

@ -275,43 +275,54 @@ def BuildDependentLibraries(env, src_dir): # pylint: disable=R0914
def ConvertInoToCpp(env):
def delete_tmpcpp(files):
for f in files:
remove(f)
PROTOTYPE_RE = re.compile(
r"""^(
(?:\s*[a-z_\d]+){1,2} # return type
\s+[a-z_\d]+\s* # name of prototype
\([a-z_,\.\*\&\[\]\s\d]*\) # arguments
)\s*\{ # must end with {
""",
re.X | re.M | re.I
)
tmpcpp = []
items = (env.Glob(join("$PROJECTSRC_DIR", "*.ino")) +
env.Glob(join("$PROJECTSRC_DIR", "*.pde")))
for item in items:
cppfile = item.get_path()[:-3] + "cpp"
if isfile(cppfile):
continue
ino_contents = item.get_text_contents()
DETECTMAIN_RE = re.compile(r"void\s+(setup|loop)\s*\(", re.M | re.I)
prototypes = re.findall(
r"""^(
(?:\s*[a-z_\d]+){1,2} # return type
\s+[a-z_\d]+\s* # name of prototype
\([a-z_,\.\*\&\[\]\s\d]*\) # args
)\s*\{ # must end with {
""",
ino_contents,
re.X | re.M | re.I
)
prototypes = [p.strip() for p in prototypes]
# print prototypes
def delete_tmpcpp_file(file_):
remove(file_)
# create new temporary C++ valid file
with open(cppfile, "w") as f:
f.write("#include <Arduino.h>\n")
if prototypes:
f.write("%s;\n" % ";\n".join(prototypes))
f.write('#line 1 "%s"\n' % basename(item.path))
f.write(ino_contents)
tmpcpp.append(cppfile)
def is_main_ino(contents):
return DETECTMAIN_RE.search(contents)
if tmpcpp:
atexit.register(delete_tmpcpp, tmpcpp)
ino_nodes = (env.Glob(join("$PROJECTSRC_DIR", "*.ino")) +
env.Glob(join("$PROJECTSRC_DIR", "*.pde")))
prototypes = []
data = []
for node in ino_nodes:
ino_contents = node.get_text_contents()
prototypes += PROTOTYPE_RE.findall(ino_contents)
item = (basename(node.get_path()), ino_contents)
if is_main_ino(ino_contents):
data = [item] + data
else:
data.append(item)
if not data:
return
# create new temporary C++ valid file
tmpcpp_file = join(env.subst("$PROJECTSRC_DIR"), "piomain.cpp")
with open(tmpcpp_file, "w") as f:
f.write("#include <Arduino.h>\n")
if prototypes:
f.write("%s;" % ";\n".join(prototypes))
for name, contents in data:
f.write('\n#line 1 "%s"\n' % name)
f.write(contents)
atexit.register(delete_tmpcpp_file, tmpcpp_file)
def exists(_):