mirror of
https://github.com/platformio/platformio-core.git
synced 2025-07-29 17:47:14 +02:00
Init new project using new ProjectConfig API
This commit is contained in:
@ -165,5 +165,4 @@ def _copy_project_conf(build_dir, project_conf):
|
||||
config = ProjectConfig(project_conf, parse_extra=False)
|
||||
if config.has_section("platformio"):
|
||||
config.remove_section("platformio")
|
||||
with open(join(build_dir, "platformio.ini"), "wb") as fp:
|
||||
config.write(fp)
|
||||
config.save(join(build_dir, "platformio.ini"))
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
from os import getcwd, makedirs
|
||||
from os.path import isdir, isfile, join
|
||||
from shutil import copyfile
|
||||
|
||||
import click
|
||||
|
||||
@ -89,7 +88,8 @@ def cli(
|
||||
"platformio.ini", fg="cyan"))
|
||||
|
||||
is_new_project = not util.is_platformio_project(project_dir)
|
||||
init_base_project(project_dir)
|
||||
if is_new_project:
|
||||
init_base_project(project_dir)
|
||||
|
||||
if board:
|
||||
fill_project_envs(ctx, project_dir, board, project_option, env_prefix,
|
||||
@ -144,13 +144,7 @@ def get_best_envname(project_dir, boards=None):
|
||||
|
||||
|
||||
def init_base_project(project_dir):
|
||||
if util.is_platformio_project(project_dir):
|
||||
return
|
||||
|
||||
copyfile(
|
||||
join(util.get_source_dir(), "projectconftpl.ini"),
|
||||
join(project_dir, "platformio.ini"))
|
||||
|
||||
ProjectConfig(join(project_dir, "platformio.ini")).save()
|
||||
with util.cd(project_dir):
|
||||
dir_to_readme = [
|
||||
(util.get_projectsrc_dir(), None),
|
||||
@ -362,12 +356,9 @@ def init_cvs_ignore(project_dir):
|
||||
|
||||
def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix,
|
||||
force_download):
|
||||
content = []
|
||||
config = ProjectConfig(
|
||||
join(project_dir, "platformio.ini"), parse_extra=False)
|
||||
used_boards = []
|
||||
used_platforms = []
|
||||
|
||||
config_path = join(project_dir, "platformio.ini")
|
||||
config = util.load_project_config(config_path)
|
||||
for section in config.sections():
|
||||
cond = [
|
||||
section.startswith("env:"),
|
||||
@ -377,12 +368,15 @@ def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix,
|
||||
used_boards.append(config.get(section, "board"))
|
||||
|
||||
pm = PlatformManager()
|
||||
used_platforms = []
|
||||
modified = False
|
||||
for id_ in board_ids:
|
||||
board_config = pm.board_config(id_)
|
||||
used_platforms.append(board_config['platform'])
|
||||
if id_ in used_boards:
|
||||
continue
|
||||
used_boards.append(id_)
|
||||
modified = True
|
||||
|
||||
envopts = {"platform": board_config['platform'], "board": id_}
|
||||
# find default framework for board
|
||||
@ -396,22 +390,18 @@ def fill_project_envs(ctx, project_dir, board_ids, project_option, env_prefix,
|
||||
_name, _value = item.split("=", 1)
|
||||
envopts[_name.strip()] = _value.strip()
|
||||
|
||||
content.append("")
|
||||
content.append("[env:%s%s]" % (env_prefix, id_))
|
||||
for name, value in envopts.items():
|
||||
content.append("%s = %s" % (name, value))
|
||||
section = "env:%s%s" % (env_prefix, id_)
|
||||
config.add_section(section)
|
||||
|
||||
for option, value in envopts.items():
|
||||
config.set(section, option, value)
|
||||
|
||||
if force_download and used_platforms:
|
||||
_install_dependent_platforms(ctx, used_platforms)
|
||||
|
||||
if not content:
|
||||
return
|
||||
|
||||
with open(config_path, "a") as f:
|
||||
content.append("")
|
||||
f.write("\n".join(content))
|
||||
|
||||
ProjectConfig.reset_instances()
|
||||
if modified:
|
||||
config.save()
|
||||
config.reset_instances()
|
||||
|
||||
|
||||
def _install_dependent_platforms(ctx, platforms):
|
||||
|
@ -26,6 +26,18 @@ try:
|
||||
except ImportError:
|
||||
import configparser as ConfigParser
|
||||
|
||||
CONFIG_HEADER = """
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
"""
|
||||
|
||||
KNOWN_PLATFORMIO_OPTIONS = [
|
||||
"description",
|
||||
"env_default",
|
||||
@ -118,6 +130,7 @@ class ProjectConfig(object):
|
||||
|
||||
VARTPL_RE = re.compile(r"\$\{([^\.\}]+)\.([^\}]+)\}")
|
||||
|
||||
expand_interpolations = True
|
||||
_instances = {}
|
||||
_parser = None
|
||||
_parsed = []
|
||||
@ -140,8 +153,6 @@ class ProjectConfig(object):
|
||||
|
||||
@staticmethod
|
||||
def get_instance(path):
|
||||
if not isfile(path):
|
||||
raise exception.NotPlatformIOProject(path)
|
||||
if path not in ProjectConfig._instances:
|
||||
ProjectConfig._instances[path] = ProjectConfig(path)
|
||||
return ProjectConfig._instances[path]
|
||||
@ -150,13 +161,13 @@ class ProjectConfig(object):
|
||||
def reset_instances():
|
||||
ProjectConfig._instances = {}
|
||||
|
||||
def __init__(self, path, parse_extra=True):
|
||||
if not isfile(path):
|
||||
raise exception.NotPlatformIOProject(path)
|
||||
def __init__(self, path, parse_extra=True, expand_interpolations=True):
|
||||
self.path = path
|
||||
self.expand_interpolations = expand_interpolations
|
||||
self._parsed = []
|
||||
self._parser = ConfigParser.ConfigParser()
|
||||
self.read(path, parse_extra)
|
||||
if isfile(path):
|
||||
self.read(path, parse_extra)
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._parser, name)
|
||||
@ -197,6 +208,8 @@ class ProjectConfig(object):
|
||||
for option in self.options(section)]
|
||||
|
||||
def get(self, section, option):
|
||||
if not self.expand_interpolations:
|
||||
return self._parser.get(section, option)
|
||||
try:
|
||||
value = self._parser.get(section, option)
|
||||
except ConfigParser.Error as e:
|
||||
@ -227,6 +240,8 @@ class ProjectConfig(object):
|
||||
return self.parse_multi_values(self.get("platformio", "env_default"))
|
||||
|
||||
def validate(self, envs=None):
|
||||
if not isfile(self.path):
|
||||
raise exception.NotPlatformIOProject(self.path)
|
||||
# check envs
|
||||
known = set(self.envs())
|
||||
if not known:
|
||||
@ -246,7 +261,7 @@ class ProjectConfig(object):
|
||||
KNOWN_PLATFORMIO_OPTIONS)
|
||||
if unknown:
|
||||
warnings.add(
|
||||
"Ignore unknown `%s` options in `[platformio]` section" %
|
||||
"Ignore unknown `%s` options in section `[platformio]`" %
|
||||
", ".join(unknown))
|
||||
|
||||
# check [env:*] sections
|
||||
@ -257,7 +272,7 @@ class ProjectConfig(object):
|
||||
# obsolete
|
||||
if option in RENAMED_OPTIONS:
|
||||
warnings.add(
|
||||
"`%s` option in `[%s]` section is deprecated and will "
|
||||
"`%s` option in section `[%s]` is deprecated and will "
|
||||
"be removed in the next release! Please use `%s` "
|
||||
"instead" % (option, section, RENAMED_OPTIONS[option]))
|
||||
# rename on-the-fly
|
||||
@ -281,3 +296,9 @@ class ProjectConfig(object):
|
||||
click.secho("Warning! %s" % warning, fg="yellow")
|
||||
|
||||
return True
|
||||
|
||||
def save(self, path=None):
|
||||
with open(path or self.path, "wb") as fp:
|
||||
fp.write(CONFIG_HEADER.strip())
|
||||
fp.write("\n\n")
|
||||
self._parser.write(fp)
|
||||
|
@ -1,9 +0,0 @@
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
@ -179,15 +179,11 @@ def get_project_optional_dir(name, default=None):
|
||||
if var_name in os.environ:
|
||||
paths = os.getenv(var_name)
|
||||
else:
|
||||
try:
|
||||
config = ProjectConfig.get_instance(
|
||||
join(get_project_dir(), "platformio.ini"))
|
||||
if (config.has_section("platformio")
|
||||
and config.has_option("platformio", name)):
|
||||
paths = config.get("platformio", name)
|
||||
except exception.NotPlatformIOProject:
|
||||
pass
|
||||
|
||||
config = ProjectConfig.get_instance(
|
||||
join(get_project_dir(), "platformio.ini"))
|
||||
if (config.has_section("platformio")
|
||||
and config.has_option("platformio", name)):
|
||||
paths = config.get("platformio", name)
|
||||
if not paths:
|
||||
return default
|
||||
|
||||
|
Reference in New Issue
Block a user