diff --git a/compilesketches/compilesketches.py b/compilesketches/compilesketches.py index 23467799..2c2da1f3 100644 --- a/compilesketches/compilesketches.py +++ b/compilesketches/compilesketches.py @@ -762,6 +762,26 @@ def install_libraries(self): if len(library_list.download) > 0: self.install_libraries_from_download(library_list=library_list.download) + def get_dependencies_from_properties_file(self, properties_file_path): + """extract library names from `depends` key""" + dependencies = [] + with open(properties_file_path, "r") as file: + content = file.read() + match = re.search(r"depends=(.*)", content) + if match: + # only works with "," (comma) deliminator + depends = match.group(1) + if depends: + dependencies = depends.split(",") + return dependencies + + def get_library_dependencies(self, library_path): + """if library.properties is present, extract dependencies""" + properties_file_path = os.path.join(library_path, "library.properties") + if os.path.exists(properties_file_path): + return self.get_dependencies_from_properties_file(properties_file_path) + return [] + def install_libraries_from_library_manager(self, library_list): """Install libraries using the Arduino Library Manager @@ -778,6 +798,10 @@ def install_libraries_from_library_manager(self, library_list): lib_install_command.append(self.get_manager_dependency_name(library)) self.run_arduino_cli_command(command=lib_install_command, enable_output=self.get_run_command_output_level()) + dependencies = self.get_library_dependencies(os.environ["GITHUB_REPOSITORY"]) + for depends_library in dependencies: + lib_install_command.extend(depends_library) + def install_libraries_from_path(self, library_list): """Install libraries from local paths diff --git a/compilesketches/tests/test_compilesketches.py b/compilesketches/tests/test_compilesketches.py index 8604ecb3..2a07dae0 100644 --- a/compilesketches/tests/test_compilesketches.py +++ b/compilesketches/tests/test_compilesketches.py @@ -3182,3 +3182,67 @@ def rev_parse(self): mocker.patch.object(Repo, "rev_parse", return_value="push-head-sha") assert compilesketches.get_head_commit_hash() == expected_hash + + +# Automated library parsing from the library.properties file +def test_get_dependencies_from_properties_file_with_dependencies(): + properties_file = os.path.join(os.getcwd(), "library.properties") + with open(properties_file, "w") as file: + file.write("depends=Library1,Library2,Library3") + + compilesketches_object = get_compilesketches_object() + dependencies = compilesketches_object.get_dependencies_from_properties_file(properties_file) + + assert dependencies == ["Library1", "Library2", "Library3"] + + +# Empty library.properties file should not return any dependencies +def test_get_dependencies_from_properties_file_without_dependencies(): + properties_file = os.path.join(os.getcwd(), "library.properties") + with open(properties_file, "w") as file: + file.write("depends=") + + compilesketches_object = get_compilesketches_object() + dependencies = compilesketches_object.get_dependencies_from_properties_file(properties_file) + + assert dependencies == [] + + +# No depends key inside library.properties, should not return any dependencies +def test_get_dependencies_from_properties_file_no_depends_key(): + properties_file = os.path.join(os.getcwd(), "library.properties") + with open(properties_file, "w") as file: + file.write("key=value") + + compilesketches_object = get_compilesketches_object() + dependencies = compilesketches_object.get_dependencies_from_properties_file(properties_file) + + assert dependencies == [] + + +# dependencies correctly extracted from a properties file within a library +def test_get_library_dependencies_with_properties_file(): + library_path = os.path.join(os.getcwd(), "library") + os.makedirs(library_path, exist_ok=True) + properties_file = os.path.join(library_path, "library.properties") + with open(properties_file, "w") as file: + file.write("depends=Library1,Library2,Library3") + + compilesketches_object = get_compilesketches_object() + dependencies = compilesketches_object.get_library_dependencies(library_path) + + assert dependencies == ["Library1", "Library2", "Library3"] + + +# no dependencies when the library does not contain a properties file +def test_get_library_dependencies_without_properties_file(): + library_path = os.path.join(os.getcwd(), "library") + os.makedirs(library_path, exist_ok=True) + properties_file = os.path.join(library_path, "library.properties") + if os.path.exists(properties_file): + os.remove(properties_file) # properties file is removed + + compilesketches_object = get_compilesketches_object() + dependencies = compilesketches_object.get_library_dependencies(library_path) + + assert dependencies == []