Files
platformio-core/platformio/ide/projectgenerator.py

164 lines
5.5 KiB
Python
Raw Normal View History

# Copyright 2014-2015 Ivan Kravets <me@ikravets.com>
#
# 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 json
import os
2015-09-04 20:35:56 +03:00
import re
from os.path import abspath, basename, expanduser, isdir, join, relpath
import bottle
import click
2015-12-08 18:42:50 +02:00
from platformio import exception, util
class ProjectGenerator(object):
2015-12-08 18:42:50 +02:00
def __init__(self, project_dir, ide, board):
self.project_dir = project_dir
self.ide = ide
self.board = board
2015-05-06 11:17:38 +01:00
self._tplvars = {}
self._gather_tplvars()
@staticmethod
def get_supported_ides():
tpls_dir = join(util.get_source_dir(), "ide", "tpls")
return sorted([d for d in os.listdir(tpls_dir)
2015-05-27 19:21:29 +03:00
if isdir(join(tpls_dir, d))])
@util.memoized
def get_project_env(self):
data = {"env_name": "PlatformIO"}
with util.cd(self.project_dir):
config = util.get_project_config()
for section in config.sections():
if not section.startswith("env:"):
continue
data = {"env_name": section[4:]}
for k, v in config.items(section):
data[k] = v
2015-12-08 18:42:50 +02:00
if self.board == data.get("board"):
break
return data
@util.memoized
def get_project_build_data(self):
data = {
"defines": [],
"includes": [],
"cxx_path": None
}
envdata = self.get_project_env()
if "env_name" not in envdata:
return data
result = util.exec_command(
["platformio", "-f", "run", "-t", "idedata",
"-e", envdata['env_name'], "-d", self.project_dir]
)
2015-12-08 18:42:50 +02:00
if result['returncode'] != 0 or '"includes":' not in result['out']:
2015-12-08 18:42:50 +02:00
raise exception.PlatformioException(
"\n".join([result['out'], result['err']]))
output = result['out']
2015-12-08 18:42:50 +02:00
start_index = output.index('\n{"')
stop_index = output.rindex('}')
data = json.loads(output[start_index + 1:stop_index + 1])
return data
def get_project_name(self):
return basename(self.project_dir)
def get_src_files(self):
result = []
with util.cd(self.project_dir):
for root, _, files in os.walk(util.get_projectsrc_dir()):
for f in files:
result.append(relpath(join(root, f)))
return result
@staticmethod
def get_main_src_file(src_files):
for f in src_files:
for ext in ("c", "cpp"):
if f.endswith(".%s" % ext):
return f
return None
def get_tpls(self):
tpls = []
tpls_dir = join(util.get_source_dir(), "ide", "tpls", self.ide)
for root, _, files in os.walk(tpls_dir):
for f in files:
if not f.endswith(".tpl"):
continue
_relpath = root.replace(tpls_dir, "")
if _relpath.startswith(os.sep):
2015-09-04 20:35:56 +03:00
_relpath = _relpath[1:]
tpls.append((_relpath, join(root, f)))
return tpls
def generate(self):
for _relpath, _path in self.get_tpls():
tpl_dir = self.project_dir
if _relpath:
tpl_dir = join(self.project_dir, _relpath)
if not isdir(tpl_dir):
os.makedirs(tpl_dir)
file_name = basename(_path)[:-4]
with open(join(tpl_dir, file_name), "w") as f:
f.write(self._render_tpl(_path).encode("utf8"))
def _render_tpl(self, tpl_path):
content = ""
with open(tpl_path) as f:
content = f.read()
return bottle.template(content, **self._tplvars)
def _gather_tplvars(self):
src_files = self.get_src_files()
main_src_file = self.get_main_src_file(src_files)
if not main_src_file and self.ide == "clion":
click.secho(
"Warning! Can not find main source file (*.c, *.cpp). So, "
"code auto-completion is disabled. Please add source files "
"to `src` directory and re-initialize project or edit "
"`CMakeLists.txt` file manually (`add_executable` command).",
fg="yellow")
2015-05-06 11:17:38 +01:00
self._tplvars.update(self.get_project_env())
self._tplvars.update(self.get_project_build_data())
2015-05-06 11:17:38 +01:00
self._tplvars.update({
2015-04-24 16:34:14 +01:00
"project_name": self.get_project_name(),
"src_files": src_files,
"main_src_file": main_src_file,
"user_home_dir": abspath(expanduser("~")),
"project_dir": self.project_dir,
"systype": util.get_systype(),
2015-09-05 22:43:12 +03:00
"platformio_path": self._fix_os_path(
util.where_is_program("platformio")),
"env_pathsep": os.pathsep,
2015-09-04 20:35:56 +03:00
"env_path": self._fix_os_path(os.getenv("PATH"))
})
2015-09-04 20:35:56 +03:00
@staticmethod
def _fix_os_path(path):
2015-09-04 23:48:57 +03:00
return (re.sub(r"[\\]+", '\\' * 4, path) if "windows" in
util.get_systype() else path)