|
| 1 | +from packaging.version import Version |
| 2 | + |
| 3 | +import questionary |
| 4 | + |
| 5 | +from commitizen import factory, out |
| 6 | +from commitizen.cz import registry |
| 7 | +from commitizen.config import BaseConfig, TomlConfig, IniConfig |
| 8 | +from commitizen.git import get_latest_tag, get_all_tags |
| 9 | +from commitizen.defaults import config_files |
| 10 | + |
| 11 | + |
| 12 | +class Init: |
| 13 | + def __init__(self, config: BaseConfig, *args): |
| 14 | + self.config: BaseConfig = config |
| 15 | + self.cz = factory.commiter_factory(self.config) |
| 16 | + |
| 17 | + def __call__(self): |
| 18 | + values_to_add = {} |
| 19 | + |
| 20 | + # No config file exist |
| 21 | + if not self.config.path: |
| 22 | + config_path = self._ask_config_path() |
| 23 | + |
| 24 | + if "toml" in config_path: |
| 25 | + self.config = TomlConfig(data="", path=config_path) |
| 26 | + else: |
| 27 | + self.config = IniConfig(data="", path=config_path) |
| 28 | + |
| 29 | + self.config.init_empty_config_file() |
| 30 | + |
| 31 | + values_to_add["name"] = self._ask_name() |
| 32 | + tag = self._ask_tag() |
| 33 | + values_to_add["version"] = Version(tag).public |
| 34 | + values_to_add["tag_format"] = self._ask_tag_format(tag) |
| 35 | + self._update_config_file(values_to_add) |
| 36 | + out.write("The configuration are all set.") |
| 37 | + else: |
| 38 | + # TODO: handle the case that config file exist but no value |
| 39 | + out.line(f"Config file {self.config.path} already exists") |
| 40 | + |
| 41 | + def _ask_config_path(self) -> str: |
| 42 | + name = questionary.select( |
| 43 | + "Please choose a supported config file: (default: pyproject.tml)", |
| 44 | + choices=config_files, |
| 45 | + default="pyproject.toml", |
| 46 | + style=self.cz.style, |
| 47 | + ).ask() |
| 48 | + return name |
| 49 | + |
| 50 | + def _ask_name(self) -> str: |
| 51 | + name = questionary.select( |
| 52 | + "Please choose a cz: (default: cz_conventional_commits)", |
| 53 | + choices=list(registry.keys()), |
| 54 | + default="cz_conventional_commits", |
| 55 | + style=self.cz.style, |
| 56 | + ).ask() |
| 57 | + return name |
| 58 | + |
| 59 | + def _ask_tag(self) -> str: |
| 60 | + latest_tag = get_latest_tag() |
| 61 | + if not latest_tag: |
| 62 | + out.error("No Existing Tag. Set tag to v0.0.1") |
| 63 | + return "0.0.1" |
| 64 | + |
| 65 | + is_correct_tag = questionary.confirm( |
| 66 | + f"Is {latest_tag} the latest tag?", style=self.cz.style, default=False |
| 67 | + ).ask() |
| 68 | + if not is_correct_tag: |
| 69 | + tags = get_all_tags() |
| 70 | + if not tags: |
| 71 | + out.error("No Existing Tag. Set tag to v0.0.1") |
| 72 | + return "0.0.1" |
| 73 | + |
| 74 | + latest_tag = questionary.select( |
| 75 | + "Please choose the latest tag: ", |
| 76 | + choices=get_all_tags(), |
| 77 | + style=self.cz.style, |
| 78 | + ).ask() |
| 79 | + |
| 80 | + if not latest_tag: |
| 81 | + out.error("Tag is required!") |
| 82 | + raise SystemExit() |
| 83 | + return latest_tag |
| 84 | + |
| 85 | + def _ask_tag_format(self, latest_tag) -> str: |
| 86 | + is_correct_format = False |
| 87 | + if latest_tag.startswith("v"): |
| 88 | + tag_format = r"v$version" |
| 89 | + is_correct_format = questionary.confirm( |
| 90 | + f'Is "{tag_format}" the correct tag format?', style=self.cz.style |
| 91 | + ).ask() |
| 92 | + |
| 93 | + if not is_correct_format: |
| 94 | + tag_format = questionary.text( |
| 95 | + 'Please enter the correct version format: (default: "$version")', |
| 96 | + style=self.cz.style, |
| 97 | + ).ask() |
| 98 | + |
| 99 | + if not tag_format: |
| 100 | + tag_format = "$version" |
| 101 | + return tag_format |
| 102 | + |
| 103 | + def _update_config_file(self, values): |
| 104 | + if not values: |
| 105 | + out.write("The configuration were all set. Nothing to add.") |
| 106 | + raise SystemExit() |
| 107 | + |
| 108 | + for key, value in values.items(): |
| 109 | + self.config.set_key(key, value) |
0 commit comments