Пример #1
0
    def from_new(cls, root_path: Path) -> "MbedProgramFiles":
        """Create MbedProgramFiles from a new directory.

        A "new directory" in this context means it doesn't already contain an Mbed program.

        Args:
            root_path: The directory in which to create the program data files.

        Raises:
            ValueError: A program .mbed or mbed-os.lib file already exists at this path.
        """
        app_config = root_path / APP_CONFIG_FILE_NAME
        mbed_os_ref = root_path / MBED_OS_REFERENCE_FILE_NAME
        cmakelists_file = root_path / CMAKELISTS_FILE_NAME
        main_cpp = root_path / MAIN_CPP_FILE_NAME
        gitignore = root_path / ".gitignore"
        cmake_build_dir = root_path / BUILD_DIR
        custom_targets_json = root_path / CUSTOM_TARGETS_JSON_FILE_NAME

        if mbed_os_ref.exists():
            raise ValueError(f"Program already exists at path {root_path}.")

        app_config.write_text(json.dumps(DEFAULT_APP_CONFIG, indent=4))
        mbed_os_ref.write_text(
            f"{MBED_OS_REFERENCE_URL}#{MBED_OS_REFERENCE_ID}")
        render_cmakelists_template(cmakelists_file, root_path.stem)
        render_main_cpp_template(main_cpp)
        render_gitignore_template(gitignore)
        return cls(
            app_config_file=app_config,
            mbed_os_ref=mbed_os_ref,
            cmakelists_file=cmakelists_file,
            cmake_build_dir=cmake_build_dir,
            custom_targets_json=custom_targets_json,
        )
Пример #2
0
    def from_existing(cls, root_path: Path) -> "MbedProgramFiles":
        """Create MbedProgramFiles from a directory containing an existing program.

        Args:
            root_path: The path containing the MbedProgramFiles.
        """
        app_config: Optional[Path]
        app_config = root_path / APP_CONFIG_FILE_NAME
        if not app_config.exists():
            logger.info(
                "This program does not contain an mbed_app.json config file.")
            app_config = None

        mbed_os_file = root_path / MBED_OS_REFERENCE_FILE_NAME

        cmakelists_file = root_path / CMAKELISTS_FILE_NAME
        if not cmakelists_file.exists():
            logger.warning(
                "No CMakeLists.txt found in the program root. Creating it...")
            render_cmakelists_template(cmakelists_file, root_path.stem)

        return cls(
            app_config_file=app_config,
            mbed_os_ref=mbed_os_file,
            cmakelists_file=cmakelists_file,
            cmake_config_file=root_path / CMAKE_CONFIG_FILE_PATH,
            cmake_build_dir=root_path / CMAKE_BUILD_DIR,
        )
Пример #3
0
    def test_renders_cmakelists_template(self, mock_datetime, tmp_path):
        the_year = 3999
        mock_datetime.datetime.now.return_value.year = the_year
        program_name = "mytestprogram"
        file_path = Path(tmp_path, "mytestpath")

        render_cmakelists_template(file_path, program_name)
        output = file_path.read_text()

        assert str(the_year) in output
        assert program_name in output
Пример #4
0
    def test_renders_cmakelists_template(self, mock_datetime):
        with TemporaryDirectory() as tmpdir:
            the_year = 3999
            mock_datetime.datetime.now.return_value.year = the_year
            program_name = "mytestprogram"
            file_path = Path(tmpdir, "mytestpath")

            render_cmakelists_template(file_path, program_name)

            output = file_path.read_text()
            self.assertIn(str(the_year), output)
            self.assertIn(program_name, output)