2015-04-24 14:43:13 +01:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
2015-05-22 19:08:51 +03:00
|
|
|
import json
|
2015-09-04 19:31:59 +03:00
|
|
|
import os
|
2015-09-04 20:35:56 +03:00
|
|
|
import re
|
2015-06-27 21:18:33 +03:00
|
|
|
from os.path import abspath, basename, expanduser, isdir, join, relpath
|
2015-04-24 14:43:13 +01:00
|
|
|
|
|
|
|
import bottle
|
|
|
|
|
|
|
|
from platformio import util
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectGenerator(object):
|
|
|
|
|
2015-06-27 15:13:27 +03:00
|
|
|
def __init__(self, project_dir, ide, board=None):
|
2015-04-24 14:43:13 +01:00
|
|
|
self.project_dir = project_dir
|
|
|
|
self.ide = ide
|
2015-06-27 15:13:27 +03:00
|
|
|
self.board = board
|
2015-05-06 11:17:38 +01:00
|
|
|
self._tplvars = {}
|
|
|
|
|
|
|
|
self._gather_tplvars()
|
2015-04-24 14:43:13 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_supported_ides():
|
|
|
|
tpls_dir = join(util.get_source_dir(), "ide", "tpls")
|
2015-09-04 19:31:59 +03:00
|
|
|
return sorted([d for d in os.listdir(tpls_dir)
|
2015-05-27 19:21:29 +03:00
|
|
|
if isdir(join(tpls_dir, d))])
|
2015-04-24 14:43:13 +01:00
|
|
|
|
2015-06-27 15:13:27 +03:00
|
|
|
@util.memoized
|
2015-05-25 23:26:35 +03:00
|
|
|
def get_project_env(self):
|
2015-05-26 22:55:59 +03:00
|
|
|
data = {"env_name": "PlatformIO"}
|
2015-05-25 23:26:35 +03:00
|
|
|
with util.cd(self.project_dir):
|
|
|
|
config = util.get_project_config()
|
|
|
|
for section in config.sections():
|
|
|
|
if not section.startswith("env:"):
|
|
|
|
continue
|
2015-06-27 15:13:27 +03:00
|
|
|
data = {"env_name": section[4:]}
|
2015-05-25 23:26:35 +03:00
|
|
|
for k, v in config.items(section):
|
|
|
|
data[k] = v
|
2015-06-27 15:13:27 +03:00
|
|
|
if self.board and self.board == data.get("board"):
|
|
|
|
break
|
2015-04-24 14:43:13 +01:00
|
|
|
return data
|
|
|
|
|
2015-05-22 19:08:51 +03:00
|
|
|
@util.memoized
|
|
|
|
def get_project_build_data(self):
|
2015-09-04 19:31:59 +03:00
|
|
|
data = {
|
|
|
|
"defines": [],
|
|
|
|
"includes": [],
|
|
|
|
"cxx_path": None
|
|
|
|
}
|
2015-05-22 19:08:51 +03:00
|
|
|
envdata = self.get_project_env()
|
|
|
|
if "env_name" not in envdata:
|
2015-09-04 19:31:59 +03:00
|
|
|
return data
|
2015-05-22 19:08:51 +03:00
|
|
|
result = util.exec_command(
|
2015-05-25 23:33:38 +03:00
|
|
|
["platformio", "run", "-t", "idedata", "-e", envdata['env_name'],
|
|
|
|
"--project-dir", self.project_dir]
|
2015-05-22 19:08:51 +03:00
|
|
|
)
|
2015-09-04 19:31:59 +03:00
|
|
|
if result['returncode'] != 0 or '"includes":' not in result['out']:
|
|
|
|
return data
|
2015-05-22 19:08:51 +03:00
|
|
|
|
|
|
|
output = result['out']
|
|
|
|
try:
|
2015-09-04 19:31:59 +03:00
|
|
|
start_index = output.index('\n{"')
|
2015-05-22 19:27:33 +03:00
|
|
|
stop_index = output.rindex('}')
|
2015-09-04 19:31:59 +03:00
|
|
|
data = json.loads(output[start_index + 1:stop_index + 1])
|
2015-05-22 19:08:51 +03:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2015-09-04 19:31:59 +03:00
|
|
|
return data
|
2015-05-22 19:08:51 +03:00
|
|
|
|
2015-04-24 14:43:13 +01:00
|
|
|
def get_project_name(self):
|
|
|
|
return basename(self.project_dir)
|
|
|
|
|
2015-06-27 21:18:33 +03:00
|
|
|
def get_srcfiles(self):
|
2015-05-07 17:21:44 +03:00
|
|
|
result = []
|
2015-06-27 21:18:33 +03:00
|
|
|
with util.cd(self.project_dir):
|
2015-09-04 19:31:59 +03:00
|
|
|
for root, _, files in os.walk(util.get_projectsrc_dir()):
|
2015-06-27 21:18:33 +03:00
|
|
|
for f in files:
|
|
|
|
result.append(relpath(join(root, f)))
|
2015-05-07 17:21:44 +03:00
|
|
|
return result
|
|
|
|
|
2015-04-24 14:43:13 +01:00
|
|
|
def get_tpls(self):
|
2015-07-15 19:34:10 +03:00
|
|
|
tpls = []
|
2015-04-24 14:43:13 +01:00
|
|
|
tpls_dir = join(util.get_source_dir(), "ide", "tpls", self.ide)
|
2015-09-04 19:31:59 +03:00
|
|
|
for root, _, files in os.walk(tpls_dir):
|
2015-07-15 19:34:10 +03:00
|
|
|
for f in files:
|
2015-09-04 19:31:59 +03:00
|
|
|
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:]
|
2015-09-04 19:31:59 +03:00
|
|
|
tpls.append((_relpath, join(root, f)))
|
2015-07-15 19:34:10 +03:00
|
|
|
return tpls
|
2015-04-24 14:43:13 +01:00
|
|
|
|
|
|
|
def generate(self):
|
2015-07-15 19:34:10 +03:00
|
|
|
for _relpath, _path in self.get_tpls():
|
|
|
|
tpl_dir = self.project_dir
|
|
|
|
if _relpath:
|
2015-09-04 19:31:59 +03:00
|
|
|
tpl_dir = join(self.project_dir, _relpath)
|
2015-07-15 19:34:10 +03:00
|
|
|
if not isdir(tpl_dir):
|
2015-09-04 19:31:59 +03:00
|
|
|
os.makedirs(tpl_dir)
|
2015-07-15 19:34:10 +03:00
|
|
|
|
|
|
|
file_name = basename(_path)[:-4]
|
|
|
|
with open(join(tpl_dir, file_name), "w") as f:
|
|
|
|
f.write(self._render_tpl(_path).encode("utf8"))
|
2015-04-24 14:43:13 +01:00
|
|
|
|
|
|
|
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):
|
2015-05-06 11:17:38 +01:00
|
|
|
self._tplvars.update(self.get_project_env())
|
2015-09-04 19:31:59 +03:00
|
|
|
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(),
|
2015-05-07 17:21:44 +03:00
|
|
|
"srcfiles": self.get_srcfiles(),
|
2015-06-27 21:20:44 +03:00
|
|
|
"user_home_dir": abspath(expanduser("~")),
|
2015-08-20 14:52:03 +03:00
|
|
|
"project_dir": self.project_dir,
|
2015-09-04 19:31:59 +03:00
|
|
|
"systype": util.get_systype(),
|
2015-09-04 20:35:56 +03:00
|
|
|
"platformio_path": self._fix_os_path(
|
|
|
|
util.where_is_program("platformio")),
|
2015-09-04 19:31:59 +03:00
|
|
|
"env_pathsep": os.pathsep,
|
2015-09-04 20:35:56 +03:00
|
|
|
"env_path": self._fix_os_path(os.getenv("PATH"))
|
2015-04-24 14:43:13 +01:00
|
|
|
})
|
2015-09-04 20:35:56 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _fix_os_path(path):
|
|
|
|
return re.sub(r"[\\]+", "\\\\", path)
|