Skip to content

Don't fully delete output directories #1183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/delete_fewer_files_with_overwrite.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 3 additions & 3 deletions end_to_end_tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ dmypy.json
.idea/

/coverage.xml
/.coverage
/.coverage
1 change: 1 addition & 0 deletions integration-tests/integration_tests/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Marker file for PEP 561
2 changes: 1 addition & 1 deletion integration-tests/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions openapi_python_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading