2
2
3
3
import os
4
4
import shutil
5
+ from pathlib import Path
5
6
from typing import Any , NamedTuple
6
7
7
8
import questionary
8
9
import yaml
9
10
10
11
from commitizen import cmd , factory , out
11
12
from commitizen .__version__ import __version__
12
- from commitizen .config import BaseConfig , JsonConfig , TomlConfig , YAMLConfig
13
+ from commitizen .config import (
14
+ BaseConfig ,
15
+ create_config ,
16
+ )
13
17
from commitizen .cz import registry
14
18
from commitizen .defaults import CONFIG_FILES , DEFAULT_SETTINGS
15
19
from commitizen .exceptions import InitFailedError , NoAnswersError
@@ -153,14 +157,6 @@ def __call__(self) -> None:
153
157
except KeyboardInterrupt :
154
158
raise InitFailedError ("Stopped by user" )
155
159
156
- # Initialize configuration
157
- if "toml" in config_path :
158
- self .config = TomlConfig (data = "" , path = config_path )
159
- elif "json" in config_path :
160
- self .config = JsonConfig (data = "{}" , path = config_path )
161
- elif "yaml" in config_path :
162
- self .config = YAMLConfig (data = "" , path = config_path )
163
-
164
160
# Collect hook data
165
161
hook_types = questionary .checkbox (
166
162
"What types of pre-commit hook you want to install? (Leave blank if you don't want to install)" ,
@@ -175,37 +171,33 @@ def __call__(self) -> None:
175
171
except InitFailedError as e :
176
172
raise InitFailedError (f"Failed to install pre-commit hook.\n { e } " )
177
173
178
- # Create and initialize config
179
- self .config .init_empty_config_content ()
180
-
181
- self .config .set_key ("name" , cz_name )
182
- self .config .set_key ("tag_format" , tag_format )
183
- self .config .set_key ("version_scheme" , version_scheme )
184
- if version_provider == "commitizen" :
185
- self .config .set_key ("version" , version .public )
186
- else :
187
- self .config .set_key ("version_provider" , version_provider )
188
- if update_changelog_on_bump :
189
- self .config .set_key ("update_changelog_on_bump" , update_changelog_on_bump )
190
- if major_version_zero :
191
- self .config .set_key ("major_version_zero" , major_version_zero )
174
+ _write_config_to_file (
175
+ path = config_path ,
176
+ cz_name = cz_name ,
177
+ version_provider = version_provider ,
178
+ version_scheme = version_scheme ,
179
+ version = version ,
180
+ tag_format = tag_format ,
181
+ update_changelog_on_bump = update_changelog_on_bump ,
182
+ major_version_zero = major_version_zero ,
183
+ )
192
184
193
185
out .write ("\n You can bump the version running:\n " )
194
186
out .info ("\t cz bump\n " )
195
187
out .success ("Configuration complete 🚀" )
196
188
197
- def _ask_config_path (self ) -> str :
189
+ def _ask_config_path (self ) -> Path :
198
190
default_path = (
199
191
"pyproject.toml" if self .project_info .has_pyproject else ".cz.toml"
200
192
)
201
193
202
- name : str = questionary .select (
194
+ filename : str = questionary .select (
203
195
"Please choose a supported config file: " ,
204
196
choices = CONFIG_FILES ,
205
197
default = default_path ,
206
198
style = self .cz .style ,
207
199
).unsafe_ask ()
208
- return name
200
+ return Path ( filename )
209
201
210
202
def _ask_name (self ) -> str :
211
203
name : str = questionary .select (
@@ -383,3 +375,30 @@ def _install_pre_commit_hook(self, hook_types: list[str] | None = None) -> None:
383
375
hook_types = ["commit-msg" , "pre-push" ]
384
376
self ._exec_install_pre_commit_hook (hook_types )
385
377
out .write ("commitizen pre-commit hook is now installed in your '.git'\n " )
378
+
379
+
380
+ def _write_config_to_file (
381
+ * ,
382
+ path : Path ,
383
+ cz_name : str ,
384
+ version_provider : str ,
385
+ version_scheme : str ,
386
+ version : Version ,
387
+ tag_format : str ,
388
+ update_changelog_on_bump : bool ,
389
+ major_version_zero : bool ,
390
+ ) -> None :
391
+ out_config = create_config (path = path )
392
+ out_config .init_empty_config_content ()
393
+
394
+ out_config .set_key ("name" , cz_name )
395
+ out_config .set_key ("tag_format" , tag_format )
396
+ out_config .set_key ("version_scheme" , version_scheme )
397
+ if version_provider == "commitizen" :
398
+ out_config .set_key ("version" , version .public )
399
+ else :
400
+ out_config .set_key ("version_provider" , version_provider )
401
+ if update_changelog_on_bump :
402
+ out_config .set_key ("update_changelog_on_bump" , update_changelog_on_bump )
403
+ if major_version_zero :
404
+ out_config .set_key ("major_version_zero" , major_version_zero )
0 commit comments