From 8f80489184f77790cf5bae66014ba2350d239aba Mon Sep 17 00:00:00 2001 From: Katherine Whitlock Date: Thu, 16 Jan 2025 17:14:54 -0500 Subject: [PATCH] Factor PlatformIO buildgen out of writer.py --- esphome/__main__.py | 6 ++- esphome/build_gen/__init__.py | 0 esphome/build_gen/platformio.py | 78 +++++++++++++++++++++++++++++++++ esphome/core/__init__.py | 12 ++--- esphome/writer.py | 77 +------------------------------- 5 files changed, 89 insertions(+), 84 deletions(-) create mode 100644 esphome/build_gen/__init__.py create mode 100644 esphome/build_gen/platformio.py diff --git a/esphome/__main__.py b/esphome/__main__.py index 2a0bd8f2b3..493f0f5741 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -66,7 +66,7 @@ def choose_prompt(options, purpose: str = None): return options[0][1] safe_print( - f'Found multiple options{f" for {purpose}" if purpose else ""}, please choose one:' + f"Found multiple options{f' for {purpose}' if purpose else ''}, please choose one:" ) for i, (desc, _) in enumerate(options): safe_print(f" [{i + 1}] {desc}") @@ -225,7 +225,9 @@ def generate_cpp_contents(config): def write_cpp_file(): - writer.write_platformio_project() + from esphome.build_gen import platformio + + platformio.write_project() code_s = indent(CORE.cpp_main_section) writer.write_cpp(code_s) diff --git a/esphome/build_gen/__init__.py b/esphome/build_gen/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/build_gen/platformio.py b/esphome/build_gen/platformio.py new file mode 100644 index 0000000000..45b9c43e11 --- /dev/null +++ b/esphome/build_gen/platformio.py @@ -0,0 +1,78 @@ +import os +from typing import Union + +from esphome.const import ENV_NOGITIGNORE, __version__ +from esphome.core import CORE +from esphome.helpers import get_bool_env, mkdir_p, read_file, write_file_if_changed +from esphome.writer import find_begin_end, update_storage_json, write_gitignore + +INI_AUTO_GENERATE_BEGIN = "; ========== AUTO GENERATED CODE BEGIN ===========" +INI_AUTO_GENERATE_END = "; =========== AUTO GENERATED CODE END ============" + +INI_BASE_FORMAT = ( + """; Auto generated code by esphome + +[common] +lib_deps = +build_flags = +upload_flags = + +""", + """ + +""", +) + + +def format_ini(data: dict[str, Union[str, list[str]]]) -> str: + content = "" + for key, value in sorted(data.items()): + if isinstance(value, list): + content += f"{key} =\n" + for x in value: + content += f" {x}\n" + else: + content += f"{key} = {value}\n" + return content + + +def get_ini_content(): + CORE.add_platformio_option( + "lib_deps", + [x.as_lib_dep for x in CORE.platformio_libraries] + ["${common.lib_deps}"], + ) + # Sort to avoid changing build flags order + CORE.add_platformio_option("build_flags", sorted(CORE.build_flags)) + + content = "[platformio]\n" + content += f"description = ESPHome {__version__}\n" + + content += f"[env:{CORE.name}]\n" + content += format_ini(CORE.platformio_options) + + return content + + +def write_ini(content): + update_storage_json() + path = CORE.relative_build_path("platformio.ini") + + if os.path.isfile(path): + text = read_file(path) + content_format = find_begin_end( + text, INI_AUTO_GENERATE_BEGIN, INI_AUTO_GENERATE_END + ) + else: + content_format = INI_BASE_FORMAT + full_file = f"{content_format[0] + INI_AUTO_GENERATE_BEGIN}\n{content}" + full_file += INI_AUTO_GENERATE_END + content_format[1] + write_file_if_changed(path, full_file) + + +def write_project(): + mkdir_p(CORE.build_path) + + content = get_ini_content() + if not get_bool_env(ENV_NOGITIGNORE): + write_gitignore() + write_ini(content) diff --git a/esphome/core/__init__.py b/esphome/core/__init__.py index f26c3da483..21617b24b1 100644 --- a/esphome/core/__init__.py +++ b/esphome/core/__init__.py @@ -504,7 +504,7 @@ class EsphomeCore: # A list of statements to insert in the global block (includes and global variables) self.global_statements: list[Statement] = [] # A set of platformio libraries to add to the project - self.libraries: list[Library] = [] + self.platformio_libraries: list[Library] = [] # A set of build flags to set in the platformio project self.build_flags: set[str] = set() # A set of defines to set for the compile process in esphome/core/defines.h @@ -536,7 +536,7 @@ class EsphomeCore: self.variables = {} self.main_statements = [] self.global_statements = [] - self.libraries = [] + self.platformio_libraries = [] self.build_flags = set() self.defines = set() self.platformio_options = {} @@ -707,7 +707,7 @@ class EsphomeCore: raise ValueError( f"Library {library} must be instance of Library, not {type(library)}" ) - for other in self.libraries[:]: + for other in self.platformio_libraries[:]: if other.name is None or library.name is None: continue library_name = ( @@ -729,7 +729,7 @@ class EsphomeCore: if library.repository is not None: # This is more specific since its using a repository - self.libraries.remove(other) + self.platformio_libraries.remove(other) continue if library.version is None: @@ -737,7 +737,7 @@ class EsphomeCore: break if other.version is None: # Found more specific version requirement - self.libraries.remove(other) + self.platformio_libraries.remove(other) continue if other.version == library.version: break @@ -748,7 +748,7 @@ class EsphomeCore: ) else: _LOGGER.debug("Adding library: %s", library) - self.libraries.append(library) + self.platformio_libraries.append(library) return library def add_build_flag(self, build_flag): diff --git a/esphome/writer.py b/esphome/writer.py index 90446ae4b1..74c19164ee 100644 --- a/esphome/writer.py +++ b/esphome/writer.py @@ -3,12 +3,10 @@ import logging import os from pathlib import Path import re -from typing import Union from esphome import loader from esphome.config import iter_component_configs, iter_components from esphome.const import ( - ENV_NOGITIGNORE, HEADER_FILE_EXTENSIONS, PLATFORM_ESP32, SOURCE_FILE_EXTENSIONS, @@ -17,8 +15,6 @@ from esphome.const import ( from esphome.core import CORE, EsphomeError from esphome.helpers import ( copy_file_if_changed, - get_bool_env, - mkdir_p, read_file, walk_files, write_file_if_changed, @@ -31,8 +27,6 @@ CPP_AUTO_GENERATE_BEGIN = "// ========== AUTO GENERATED CODE BEGIN ===========" CPP_AUTO_GENERATE_END = "// =========== AUTO GENERATED CODE END ============" CPP_INCLUDE_BEGIN = "// ========== AUTO GENERATED INCLUDE BLOCK BEGIN ===========" CPP_INCLUDE_END = "// ========== AUTO GENERATED INCLUDE BLOCK END ===========" -INI_AUTO_GENERATE_BEGIN = "; ========== AUTO GENERATED CODE BEGIN ===========" -INI_AUTO_GENERATE_END = "; =========== AUTO GENERATED CODE END ============" CPP_BASE_FORMAT = ( """// Auto generated code by esphome @@ -51,20 +45,6 @@ void loop() { """, ) -INI_BASE_FORMAT = ( - """; Auto generated code by esphome - -[common] -lib_deps = -build_flags = -upload_flags = - -""", - """ - -""", -) - UPLOAD_SPEED_OVERRIDE = { "esp210": 57600, } @@ -132,34 +112,6 @@ def update_storage_json(): new.save(path) -def format_ini(data: dict[str, Union[str, list[str]]]) -> str: - content = "" - for key, value in sorted(data.items()): - if isinstance(value, list): - content += f"{key} =\n" - for x in value: - content += f" {x}\n" - else: - content += f"{key} = {value}\n" - return content - - -def get_ini_content(): - CORE.add_platformio_option( - "lib_deps", [x.as_lib_dep for x in CORE.libraries] + ["${common.lib_deps}"] - ) - # Sort to avoid changing build flags order - CORE.add_platformio_option("build_flags", sorted(CORE.build_flags)) - - content = "[platformio]\n" - content += f"description = ESPHome {__version__}\n" - - content += f"[env:{CORE.name}]\n" - content += format_ini(CORE.platformio_options) - - return content - - def find_begin_end(text, begin_s, end_s): begin_index = text.find(begin_s) if begin_index == -1: @@ -187,34 +139,7 @@ def find_begin_end(text, begin_s, end_s): return text[:begin_index], text[(end_index + len(end_s)) :] -def write_platformio_ini(content): - update_storage_json() - path = CORE.relative_build_path("platformio.ini") - - if os.path.isfile(path): - text = read_file(path) - content_format = find_begin_end( - text, INI_AUTO_GENERATE_BEGIN, INI_AUTO_GENERATE_END - ) - else: - content_format = INI_BASE_FORMAT - full_file = f"{content_format[0] + INI_AUTO_GENERATE_BEGIN}\n{content}" - full_file += INI_AUTO_GENERATE_END + content_format[1] - write_file_if_changed(path, full_file) - - -def write_platformio_project(): - mkdir_p(CORE.build_path) - - content = get_ini_content() - if not get_bool_env(ENV_NOGITIGNORE): - write_gitignore() - write_platformio_ini(content) - - -DEFINES_H_FORMAT = ( - ESPHOME_H_FORMAT -) = """\ +DEFINES_H_FORMAT = ESPHOME_H_FORMAT = """\ #pragma once #include "esphome/core/macros.h" {}