|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import os |
4 | | -import shutil |
| 3 | +from pathlib import Path |
5 | 4 | from typing import Any, NamedTuple |
6 | 5 |
|
7 | 6 | import questionary |
8 | 7 | import yaml |
9 | 8 |
|
10 | | -from commitizen import cmd, factory, out |
| 9 | +from commitizen import cmd, factory, out, project_info |
11 | 10 | from commitizen.__version__ import __version__ |
12 | 11 | from commitizen.config import BaseConfig, JsonConfig, TomlConfig, YAMLConfig |
13 | 12 | from commitizen.cz import registry |
@@ -65,65 +64,13 @@ def title(self) -> str: |
65 | 64 | ) |
66 | 65 |
|
67 | 66 |
|
68 | | -class ProjectInfo: |
69 | | - """Discover information about the current folder.""" |
70 | | - |
71 | | - @property |
72 | | - def has_pyproject(self) -> bool: |
73 | | - return os.path.isfile("pyproject.toml") |
74 | | - |
75 | | - @property |
76 | | - def has_uv_lock(self) -> bool: |
77 | | - return os.path.isfile("uv.lock") |
78 | | - |
79 | | - @property |
80 | | - def has_setup(self) -> bool: |
81 | | - return os.path.isfile("setup.py") |
82 | | - |
83 | | - @property |
84 | | - def has_pre_commit_config(self) -> bool: |
85 | | - return os.path.isfile(".pre-commit-config.yaml") |
86 | | - |
87 | | - @property |
88 | | - def is_python_uv(self) -> bool: |
89 | | - return self.has_pyproject and self.has_uv_lock |
90 | | - |
91 | | - @property |
92 | | - def is_python_poetry(self) -> bool: |
93 | | - if not self.has_pyproject: |
94 | | - return False |
95 | | - with open("pyproject.toml") as f: |
96 | | - return "[tool.poetry]" in f.read() |
97 | | - |
98 | | - @property |
99 | | - def is_python(self) -> bool: |
100 | | - return self.has_pyproject or self.has_setup |
101 | | - |
102 | | - @property |
103 | | - def is_rust_cargo(self) -> bool: |
104 | | - return os.path.isfile("Cargo.toml") |
105 | | - |
106 | | - @property |
107 | | - def is_npm_package(self) -> bool: |
108 | | - return os.path.isfile("package.json") |
109 | | - |
110 | | - @property |
111 | | - def is_php_composer(self) -> bool: |
112 | | - return os.path.isfile("composer.json") |
113 | | - |
114 | | - @property |
115 | | - def is_pre_commit_installed(self) -> bool: |
116 | | - return bool(shutil.which("pre-commit")) |
117 | | - |
118 | | - |
119 | 67 | class Init: |
120 | 68 | _PRE_COMMIT_CONFIG_PATH = ".pre-commit-config.yaml" |
121 | 69 |
|
122 | 70 | def __init__(self, config: BaseConfig, *args: object) -> None: |
123 | 71 | self.config: BaseConfig = config |
124 | 72 | self.encoding = config.settings["encoding"] |
125 | 73 | self.cz = factory.committer_factory(self.config) |
126 | | - self.project_info = ProjectInfo() |
127 | 74 |
|
128 | 75 | def __call__(self) -> None: |
129 | 76 | if self.config.path: |
@@ -167,7 +114,7 @@ def __call__(self) -> None: |
167 | 114 | ) as config_file: |
168 | 115 | yaml.safe_dump(config_data, stream=config_file) |
169 | 116 |
|
170 | | - if not self.project_info.is_pre_commit_installed: |
| 117 | + if not project_info.is_pre_commit_installed(): |
171 | 118 | raise InitFailedError( |
172 | 119 | "Failed to install pre-commit hook.\n" |
173 | 120 | "pre-commit is not installed in current environment." |
@@ -215,14 +162,10 @@ def __call__(self) -> None: |
215 | 162 | out.success("Configuration complete 🚀") |
216 | 163 |
|
217 | 164 | def _ask_config_path(self) -> str: |
218 | | - default_path = ( |
219 | | - "pyproject.toml" if self.project_info.has_pyproject else ".cz.toml" |
220 | | - ) |
221 | | - |
222 | 165 | name: str = questionary.select( |
223 | 166 | "Please choose a supported config file: ", |
224 | 167 | choices=CONFIG_FILES, |
225 | | - default=default_path, |
| 168 | + default=project_info.get_default_config_filename(), |
226 | 169 | style=self.cz.style, |
227 | 170 | ).unsafe_ask() |
228 | 171 | return name |
@@ -287,37 +230,17 @@ def _ask_version_provider(self) -> str: |
287 | 230 | "Choose the source of the version:", |
288 | 231 | choices=_VERSION_PROVIDER_CHOICES, |
289 | 232 | style=self.cz.style, |
290 | | - default=self._default_version_provider, |
| 233 | + default=project_info.get_default_version_provider(), |
291 | 234 | ).unsafe_ask() |
292 | 235 | return version_provider |
293 | 236 |
|
294 | | - @property |
295 | | - def _default_version_provider(self) -> str: |
296 | | - if self.project_info.is_python: |
297 | | - if self.project_info.is_python_poetry: |
298 | | - return "poetry" |
299 | | - if self.project_info.is_python_uv: |
300 | | - return "uv" |
301 | | - return "pep621" |
302 | | - |
303 | | - if self.project_info.is_rust_cargo: |
304 | | - return "cargo" |
305 | | - if self.project_info.is_npm_package: |
306 | | - return "npm" |
307 | | - if self.project_info.is_php_composer: |
308 | | - return "composer" |
309 | | - |
310 | | - return "commitizen" |
311 | | - |
312 | 237 | def _ask_version_scheme(self) -> str: |
313 | 238 | """Ask for setting: version_scheme""" |
314 | | - default_scheme = "pep440" if self.project_info.is_python else "semver" |
315 | | - |
316 | 239 | scheme: str = questionary.select( |
317 | 240 | "Choose version scheme: ", |
318 | 241 | choices=KNOWN_SCHEMES, |
319 | 242 | style=self.cz.style, |
320 | | - default=default_scheme, |
| 243 | + default=project_info.get_default_version_scheme(), |
321 | 244 | ).unsafe_ask() |
322 | 245 | return scheme |
323 | 246 |
|
@@ -351,8 +274,7 @@ def _get_config_data(self) -> dict[str, Any]: |
351 | 274 | ], |
352 | 275 | } |
353 | 276 |
|
354 | | - if not self.project_info.has_pre_commit_config: |
355 | | - # .pre-commit-config.yaml does not exist |
| 277 | + if not Path(".pre-commit-config.yaml").is_file(): |
356 | 278 | return {"repos": [CZ_HOOK_CONFIG]} |
357 | 279 |
|
358 | 280 | with open(self._PRE_COMMIT_CONFIG_PATH, encoding=self.encoding) as config_file: |
|
0 commit comments