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-04-24 14:43:13 +01:00
|
|
|
from glob import glob
|
2015-05-07 17:21:44 +03:00
|
|
|
from os import listdir, walk
|
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-05-27 19:21:29 +03:00
|
|
|
return sorted([d for d in listdir(tpls_dir)
|
|
|
|
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):
|
|
|
|
envdata = self.get_project_env()
|
|
|
|
if "env_name" not in envdata:
|
|
|
|
return None
|
|
|
|
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
|
|
|
)
|
|
|
|
if result['returncode'] != 0 or '{"includes":' not in result['out']:
|
|
|
|
return None
|
|
|
|
|
|
|
|
output = result['out']
|
|
|
|
try:
|
2015-05-22 19:21:05 +03:00
|
|
|
start_index = output.index('\n{"includes":')
|
2015-05-22 19:27:33 +03:00
|
|
|
stop_index = output.rindex('}')
|
2015-05-22 19:21:05 +03:00
|
|
|
return json.loads(output[start_index + 1:stop_index + 1])
|
2015-05-22 19:08:51 +03:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
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):
|
|
|
|
for root, _, files in walk(util.get_projectsrc_dir()):
|
|
|
|
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):
|
|
|
|
tpls_dir = join(util.get_source_dir(), "ide", "tpls", self.ide)
|
|
|
|
return glob(join(tpls_dir, ".*.tpl")) + glob(join(tpls_dir, "*.tpl"))
|
|
|
|
|
|
|
|
def generate(self):
|
|
|
|
for tpl_path in self.get_tpls():
|
|
|
|
file_name = basename(tpl_path)[:-4]
|
|
|
|
with open(join(self.project_dir, file_name), "w") as f:
|
2015-04-24 17:17:24 +01:00
|
|
|
f.write(self._render_tpl(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-04-24 14:43:13 +01:00
|
|
|
|
2015-05-22 19:08:51 +03:00
|
|
|
build_data = 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-22 19:08:51 +03:00
|
|
|
"includes": (build_data['includes']
|
|
|
|
if build_data and "includes" in build_data else []),
|
|
|
|
"defines": (build_data['defines']
|
|
|
|
if build_data and "defines" in build_data else []),
|
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-05-07 17:21:44 +03:00
|
|
|
"project_dir": self.project_dir
|
2015-04-24 14:43:13 +01:00
|
|
|
})
|