forked from platformio/platformio-core
Export ProjectConfig instance to templates generator // Issue #2824
This commit is contained in:
@@ -102,8 +102,7 @@ def cli(
|
|||||||
ide is not None)
|
ide is not None)
|
||||||
|
|
||||||
if ide:
|
if ide:
|
||||||
pg = ProjectGenerator(project_dir, ide,
|
pg = ProjectGenerator(project_dir, ide, board)
|
||||||
get_best_envname(project_dir, board))
|
|
||||||
pg.generate()
|
pg.generate()
|
||||||
|
|
||||||
if is_new_project:
|
if is_new_project:
|
||||||
@@ -131,29 +130,6 @@ def cli(
|
|||||||
fg="green")
|
fg="green")
|
||||||
|
|
||||||
|
|
||||||
def get_best_envname(project_dir, boards=None):
|
|
||||||
config = ProjectConfig.get_instance(join(project_dir, "platformio.ini"))
|
|
||||||
config.validate()
|
|
||||||
|
|
||||||
envname = None
|
|
||||||
default_envs = config.default_envs()
|
|
||||||
if default_envs:
|
|
||||||
envname = default_envs[0]
|
|
||||||
if not boards:
|
|
||||||
return envname
|
|
||||||
|
|
||||||
for env in config.envs():
|
|
||||||
if not boards:
|
|
||||||
return env
|
|
||||||
if not envname:
|
|
||||||
envname = env
|
|
||||||
items = config.items(env=env, as_dict=True)
|
|
||||||
if "board" in items and items.get("board") in boards:
|
|
||||||
return env
|
|
||||||
|
|
||||||
return envname
|
|
||||||
|
|
||||||
|
|
||||||
def init_base_project(project_dir):
|
def init_base_project(project_dir):
|
||||||
ProjectConfig(join(project_dir, "platformio.ini")).save()
|
ProjectConfig(join(project_dir, "platformio.ini")).save()
|
||||||
with fs.cd(project_dir):
|
with fs.cd(project_dir):
|
||||||
|
@@ -32,10 +32,13 @@ from platformio.project.helpers import (get_project_lib_dir,
|
|||||||
|
|
||||||
class ProjectGenerator(object):
|
class ProjectGenerator(object):
|
||||||
|
|
||||||
def __init__(self, project_dir, ide, env_name):
|
def __init__(self, project_dir, ide, boards):
|
||||||
|
self.config = ProjectConfig.get_instance(
|
||||||
|
join(project_dir, "platformio.ini"))
|
||||||
|
self.config.validate()
|
||||||
self.project_dir = project_dir
|
self.project_dir = project_dir
|
||||||
self.ide = str(ide)
|
self.ide = str(ide)
|
||||||
self.env_name = str(env_name)
|
self.env_name = str(self.get_best_envname(boards))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_supported_ides():
|
def get_supported_ides():
|
||||||
@@ -43,33 +46,54 @@ class ProjectGenerator(object):
|
|||||||
return sorted(
|
return sorted(
|
||||||
[d for d in os.listdir(tpls_dir) if isdir(join(tpls_dir, d))])
|
[d for d in os.listdir(tpls_dir) if isdir(join(tpls_dir, d))])
|
||||||
|
|
||||||
|
def get_best_envname(self, boards=None):
|
||||||
|
envname = None
|
||||||
|
default_envs = self.config.default_envs()
|
||||||
|
if default_envs:
|
||||||
|
envname = default_envs[0]
|
||||||
|
if not boards:
|
||||||
|
return envname
|
||||||
|
|
||||||
|
for env in self.config.envs():
|
||||||
|
if not boards:
|
||||||
|
return env
|
||||||
|
if not envname:
|
||||||
|
envname = env
|
||||||
|
items = self.config.items(env=env, as_dict=True)
|
||||||
|
if "board" in items and items.get("board") in boards:
|
||||||
|
return env
|
||||||
|
|
||||||
|
return envname
|
||||||
|
|
||||||
def _load_tplvars(self):
|
def _load_tplvars(self):
|
||||||
tpl_vars = {"env_name": self.env_name}
|
tpl_vars = {
|
||||||
|
"config": self.config,
|
||||||
|
"systype": util.get_systype(),
|
||||||
|
"project_name": basename(self.project_dir),
|
||||||
|
"project_dir": self.project_dir,
|
||||||
|
"env_name": self.env_name,
|
||||||
|
"user_home_dir": abspath(expanduser("~")),
|
||||||
|
"platformio_path": self._fix_os_path(
|
||||||
|
sys.argv[0] if isfile(sys.argv[0])
|
||||||
|
else where_is_program("platformio")),
|
||||||
|
"env_path": self._fix_os_path(os.getenv("PATH")),
|
||||||
|
"env_pathsep": os.pathsep
|
||||||
|
} # yapf: disable
|
||||||
|
|
||||||
# default env configuration
|
# default env configuration
|
||||||
tpl_vars.update(
|
tpl_vars.update(self.config.items(env=self.env_name, as_dict=True))
|
||||||
ProjectConfig.get_instance(join(
|
|
||||||
self.project_dir, "platformio.ini")).items(env=self.env_name,
|
|
||||||
as_dict=True))
|
|
||||||
# build data
|
# build data
|
||||||
tpl_vars.update(
|
tpl_vars.update(
|
||||||
load_project_ide_data(self.project_dir, self.env_name) or {})
|
load_project_ide_data(self.project_dir, self.env_name) or {})
|
||||||
|
|
||||||
with fs.cd(self.project_dir):
|
with fs.cd(self.project_dir):
|
||||||
tpl_vars.update({
|
tpl_vars.update({
|
||||||
"project_name": basename(self.project_dir),
|
|
||||||
"src_files": self.get_src_files(),
|
"src_files": self.get_src_files(),
|
||||||
"user_home_dir": abspath(expanduser("~")),
|
|
||||||
"project_dir": self.project_dir,
|
|
||||||
"project_src_dir": get_project_src_dir(),
|
"project_src_dir": get_project_src_dir(),
|
||||||
"project_lib_dir": get_project_lib_dir(),
|
"project_lib_dir": get_project_lib_dir(),
|
||||||
"project_libdeps_dir": join(
|
"project_libdeps_dir": join(
|
||||||
get_project_libdeps_dir(), self.env_name),
|
get_project_libdeps_dir(), self.env_name)
|
||||||
"systype": util.get_systype(),
|
|
||||||
"platformio_path": self._fix_os_path(
|
|
||||||
sys.argv[0] if isfile(sys.argv[0])
|
|
||||||
else where_is_program("platformio")),
|
|
||||||
"env_pathsep": os.pathsep,
|
|
||||||
"env_path": self._fix_os_path(os.getenv("PATH"))
|
|
||||||
}) # yapf: disable
|
}) # yapf: disable
|
||||||
return tpl_vars
|
return tpl_vars
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user