diff --git a/.changeset/delete_fewer_files_with_overwrite.md b/.changeset/delete_fewer_files_with_overwrite.md new file mode 100644 index 000000000..c2e273a39 --- /dev/null +++ b/.changeset/delete_fewer_files_with_overwrite.md @@ -0,0 +1,13 @@ +--- +default: major +--- + +# Delete fewer files with `--overwrite` + +`--overwrite` will no longer delete the entire output directory before regenerating. Instead, it will only delete +specific, known directories within that directory. Right now, that is only the generated `models` and `api` directories. + +Other generated files, like `README.md`, will be overwritten. Extra files and directories outside of those listed above +will be left untouched, so you can any extra modules or files around while still updating `pyproject.toml` automatically. + +Closes #1105. diff --git a/end_to_end_tests/test_end_to_end.py b/end_to_end_tests/test_end_to_end.py index 2452c3acd..124b801d2 100644 --- a/end_to_end_tests/test_end_to_end.py +++ b/end_to_end_tests/test_end_to_end.py @@ -13,7 +13,7 @@ def _compare_directories( record: Path, test_subject: Path, - expected_differences: dict[Path, str], + expected_differences: Optional[dict[Path, str]] = None, expected_missing: Optional[set[str]] = None, ignore: list[str] = None, depth=0, @@ -298,11 +298,11 @@ def test_update_integration_tests(): config_path = source_path / "config.yaml" _run_command( "generate", - extra_args=["--meta=none", "--overwrite", f"--output-path={source_path / 'integration_tests'}"], + extra_args=["--overwrite", "--meta=pdm", f"--output-path={temp_dir}"], url=url, config_path=config_path ) - _compare_directories(temp_dir, source_path, expected_differences={}) + _compare_directories(source_path, temp_dir, ignore=["pyproject.toml"]) import mypy.api out, err, status = mypy.api.run([str(temp_dir), "--strict"]) diff --git a/integration-tests/.gitignore b/integration-tests/.gitignore index ed29cb977..79a2c3d73 100644 --- a/integration-tests/.gitignore +++ b/integration-tests/.gitignore @@ -20,4 +20,4 @@ dmypy.json .idea/ /coverage.xml -/.coverage \ No newline at end of file +/.coverage diff --git a/integration-tests/integration_tests/py.typed b/integration-tests/integration_tests/py.typed new file mode 100644 index 000000000..1aad32711 --- /dev/null +++ b/integration-tests/integration_tests/py.typed @@ -0,0 +1 @@ +# Marker file for PEP 561 \ No newline at end of file diff --git a/integration-tests/pyproject.toml b/integration-tests/pyproject.toml index 307e9e936..9eaacea87 100644 --- a/integration-tests/pyproject.toml +++ b/integration-tests/pyproject.toml @@ -4,12 +4,12 @@ version = "0.0.1" description = "A client library for accessing OpenAPI Test Server" authors = [] readme = "README.md" +requires-python = ">=3.9,<4.0" dependencies = [ "httpx>=0.20.0,<0.29.0", "attrs>=21.3.0", "python-dateutil>=2.8.0", ] -requires-python = ">=3.8,<4.0" [tool.pdm] distribution = true diff --git a/openapi_python_client/__init__.py b/openapi_python_client/__init__.py index 2225d2008..af6944ae4 100644 --- a/openapi_python_client/__init__.py +++ b/openapi_python_client/__init__.py @@ -108,13 +108,11 @@ def build(self) -> Sequence[GeneratorError]: """Create the project from templates""" print(f"Generating {self.project_dir}") - if self.config.overwrite: - shutil.rmtree(self.project_dir, ignore_errors=True) - try: self.project_dir.mkdir() except FileExistsError: - return [GeneratorError(detail="Directory already exists. Delete it or use the --overwrite option.")] + if not self.config.overwrite: + return [GeneratorError(detail="Directory already exists. Delete it or use the --overwrite option.")] self._create_package() self._build_metadata() self._build_models() @@ -158,7 +156,7 @@ def _get_errors(self) -> list[GeneratorError]: def _create_package(self) -> None: if self.package_dir != self.project_dir: - self.package_dir.mkdir() + self.package_dir.mkdir(exist_ok=True) # Package __init__.py package_init = self.package_dir / "__init__.py" @@ -214,6 +212,7 @@ def _build_setup_py(self) -> None: def _build_models(self) -> None: # Generate models models_dir = self.package_dir / "models" + shutil.rmtree(models_dir, ignore_errors=True) models_dir.mkdir() models_init = models_dir / "__init__.py" imports = [] @@ -259,6 +258,7 @@ def _build_api(self) -> None: # Generate endpoints api_dir = self.package_dir / "api" + shutil.rmtree(api_dir, ignore_errors=True) api_dir.mkdir() api_init_path = api_dir / "__init__.py" api_init_template = self.env.get_template("api_init.py.jinja") diff --git a/pyproject.toml b/pyproject.toml index a311d020d..4e7c63010 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -130,7 +130,7 @@ composite = ["test --cov openapi_python_client tests --cov-report=term-missing"] [tool.pdm.scripts.regen_integration] shell = """ -openapi-python-client generate --overwrite --url https://raw.githubusercontent.com/openapi-generators/openapi-test-server/main/openapi.json --config integration-tests/config.yaml --meta none --output-path integration-tests/integration_tests \ +openapi-python-client generate --overwrite --url https://raw.githubusercontent.com/openapi-generators/openapi-test-server/main/openapi.json --config integration-tests/config.yaml --meta pdm --output-path integration-tests \ """ [build-system]