|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import os |
4 | | -import shutil |
5 | 3 | from pathlib import Path |
6 | 4 | from typing import Any, NamedTuple |
7 | 5 |
|
8 | 6 | import questionary |
9 | 7 | import yaml |
10 | 8 |
|
11 | | -from commitizen import cmd, factory, out |
| 9 | +from commitizen import cmd, factory, out, project_info |
12 | 10 | from commitizen.__version__ import __version__ |
13 | 11 | from commitizen.config import ( |
14 | 12 | BaseConfig, |
@@ -69,64 +67,12 @@ def title(self) -> str: |
69 | 67 | ) |
70 | 68 |
|
71 | 69 |
|
72 | | -class ProjectInfo: |
73 | | - """Discover information about the current folder.""" |
74 | | - |
75 | | - @property |
76 | | - def has_pyproject(self) -> bool: |
77 | | - return os.path.isfile("pyproject.toml") |
78 | | - |
79 | | - @property |
80 | | - def has_uv_lock(self) -> bool: |
81 | | - return os.path.isfile("uv.lock") |
82 | | - |
83 | | - @property |
84 | | - def has_setup(self) -> bool: |
85 | | - return os.path.isfile("setup.py") |
86 | | - |
87 | | - @property |
88 | | - def has_pre_commit_config(self) -> bool: |
89 | | - return os.path.isfile(".pre-commit-config.yaml") |
90 | | - |
91 | | - @property |
92 | | - def is_python_uv(self) -> bool: |
93 | | - return self.has_pyproject and self.has_uv_lock |
94 | | - |
95 | | - @property |
96 | | - def is_python_poetry(self) -> bool: |
97 | | - if not self.has_pyproject: |
98 | | - return False |
99 | | - with open("pyproject.toml") as f: |
100 | | - return "[tool.poetry]" in f.read() |
101 | | - |
102 | | - @property |
103 | | - def is_python(self) -> bool: |
104 | | - return self.has_pyproject or self.has_setup |
105 | | - |
106 | | - @property |
107 | | - def is_rust_cargo(self) -> bool: |
108 | | - return os.path.isfile("Cargo.toml") |
109 | | - |
110 | | - @property |
111 | | - def is_npm_package(self) -> bool: |
112 | | - return os.path.isfile("package.json") |
113 | | - |
114 | | - @property |
115 | | - def is_php_composer(self) -> bool: |
116 | | - return os.path.isfile("composer.json") |
117 | | - |
118 | | - @property |
119 | | - def is_pre_commit_installed(self) -> bool: |
120 | | - return bool(shutil.which("pre-commit")) |
121 | | - |
122 | | - |
123 | 70 | class Init: |
124 | 71 | _PRE_COMMIT_CONFIG_PATH = ".pre-commit-config.yaml" |
125 | 72 |
|
126 | 73 | def __init__(self, config: BaseConfig, *args: object) -> None: |
127 | 74 | self.config: BaseConfig = config |
128 | 75 | self.cz = factory.committer_factory(self.config) |
129 | | - self.project_info = ProjectInfo() |
130 | 76 |
|
131 | 77 | def __call__(self) -> None: |
132 | 78 | if self.config.path: |
@@ -172,7 +118,7 @@ def __call__(self) -> None: |
172 | 118 | ) as config_file: |
173 | 119 | yaml.safe_dump(config_data, stream=config_file) |
174 | 120 |
|
175 | | - if not self.project_info.is_pre_commit_installed: |
| 121 | + if not project_info.is_pre_commit_installed(): |
176 | 122 | raise InitFailedError( |
177 | 123 | "Failed to install pre-commit hook.\n" |
178 | 124 | "pre-commit is not installed in current environment." |
@@ -208,14 +154,10 @@ def __call__(self) -> None: |
208 | 154 | out.success("Configuration complete 🚀") |
209 | 155 |
|
210 | 156 | def _ask_config_path(self) -> Path: |
211 | | - default_path = ( |
212 | | - "pyproject.toml" if self.project_info.has_pyproject else ".cz.toml" |
213 | | - ) |
214 | | - |
215 | 157 | filename: str = questionary.select( |
216 | 158 | "Please choose a supported config file: ", |
217 | 159 | choices=CONFIG_FILES, |
218 | | - default=default_path, |
| 160 | + default=project_info.get_default_config_filename(), |
219 | 161 | style=self.cz.style, |
220 | 162 | ).unsafe_ask() |
221 | 163 | return Path(filename) |
@@ -280,37 +222,17 @@ def _ask_version_provider(self) -> str: |
280 | 222 | "Choose the source of the version:", |
281 | 223 | choices=_VERSION_PROVIDER_CHOICES, |
282 | 224 | style=self.cz.style, |
283 | | - default=self._default_version_provider, |
| 225 | + default=project_info.get_default_version_provider(), |
284 | 226 | ).unsafe_ask() |
285 | 227 | return version_provider |
286 | 228 |
|
287 | | - @property |
288 | | - def _default_version_provider(self) -> str: |
289 | | - if self.project_info.is_python: |
290 | | - if self.project_info.is_python_poetry: |
291 | | - return "poetry" |
292 | | - if self.project_info.is_python_uv: |
293 | | - return "uv" |
294 | | - return "pep621" |
295 | | - |
296 | | - if self.project_info.is_rust_cargo: |
297 | | - return "cargo" |
298 | | - if self.project_info.is_npm_package: |
299 | | - return "npm" |
300 | | - if self.project_info.is_php_composer: |
301 | | - return "composer" |
302 | | - |
303 | | - return "commitizen" |
304 | | - |
305 | 229 | def _ask_version_scheme(self) -> str: |
306 | 230 | """Ask for setting: version_scheme""" |
307 | | - default_scheme = "pep440" if self.project_info.is_python else "semver" |
308 | | - |
309 | 231 | scheme: str = questionary.select( |
310 | 232 | "Choose version scheme: ", |
311 | 233 | choices=KNOWN_SCHEMES, |
312 | 234 | style=self.cz.style, |
313 | | - default=default_scheme, |
| 235 | + default=project_info.get_default_version_scheme(), |
314 | 236 | ).unsafe_ask() |
315 | 237 | return scheme |
316 | 238 |
|
@@ -344,8 +266,7 @@ def _get_config_data(self) -> dict[str, Any]: |
344 | 266 | ], |
345 | 267 | } |
346 | 268 |
|
347 | | - if not self.project_info.has_pre_commit_config: |
348 | | - # .pre-commit-config.yaml does not exist |
| 269 | + if not Path(".pre-commit-config.yaml").is_file(): |
349 | 270 | return {"repos": [CZ_HOOK_CONFIG]} |
350 | 271 |
|
351 | 272 | with open( |
|
0 commit comments