-
-
Notifications
You must be signed in to change notification settings - Fork 300
Init command #102
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
woile
merged 18 commits into
commitizen-tools:master
from
Lee-W:init_command_on_new_config
Dec 30, 2019
Merged
Init command #102
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bc624ee
feat(git): add get_latest_tag, get_all_tags
Lee-W 4533798
feat(commands/init): Implment init command
Lee-W f9f80e0
feat(cli): add init command into cli
Lee-W ca5d598
fix(commands/init): fix logic error
Lee-W 1f8a8df
test(commands/init): add test case for init command
Lee-W 9f88748
style(all): black
Lee-W 37e82be
feat(commands/init): add name into init question
Lee-W 8ea9618
feat(commands/init): Implment init command
Lee-W b7c8df2
fix(commands/init): fix logic error
Lee-W ad2bb02
fix(commands/init): fix init command broken due to newly refactored c…
Lee-W e65087c
style(commands): blackify
Lee-W 001d848
feat(config): add init_empty_config_file
Lee-W 2bacb27
fix(commands/init): fix init commands cannot create config file
Lee-W 332c2f4
style(commands/init): blackify
Lee-W 4faf523
fix(commands/init): fix test_init_when_config_already_exists
Lee-W 32fe3e7
fix(commands/init): read version in the format "x.y.z" instead of "vx…
Lee-W 94e5120
refactor(commands/init): add default for tag_format
Lee-W 64b56d8
docs(index): add init command
Lee-W File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from packaging.version import Version | ||
|
||
import questionary | ||
|
||
from commitizen import factory, out | ||
from commitizen.cz import registry | ||
from commitizen.config import BaseConfig, TomlConfig, IniConfig | ||
from commitizen.git import get_latest_tag, get_all_tags | ||
from commitizen.defaults import config_files | ||
|
||
|
||
class Init: | ||
def __init__(self, config: BaseConfig, *args): | ||
self.config: BaseConfig = config | ||
self.cz = factory.commiter_factory(self.config) | ||
|
||
def __call__(self): | ||
values_to_add = {} | ||
|
||
# No config file exist | ||
if not self.config.path: | ||
config_path = self._ask_config_path() | ||
|
||
if "toml" in config_path: | ||
self.config = TomlConfig(data="", path=config_path) | ||
else: | ||
self.config = IniConfig(data="", path=config_path) | ||
|
||
self.config.init_empty_config_file() | ||
|
||
values_to_add["name"] = self._ask_name() | ||
tag = self._ask_tag() | ||
values_to_add["version"] = Version(tag).public | ||
values_to_add["tag_format"] = self._ask_tag_format(tag) | ||
self._update_config_file(values_to_add) | ||
out.write("The configuration are all set.") | ||
else: | ||
# TODO: handle the case that config file exist but no value | ||
out.line(f"Config file {self.config.path} already exists") | ||
|
||
def _ask_config_path(self) -> str: | ||
name = questionary.select( | ||
"Please choose a supported config file: (default: pyproject.tml)", | ||
choices=config_files, | ||
default="pyproject.toml", | ||
style=self.cz.style, | ||
).ask() | ||
return name | ||
|
||
def _ask_name(self) -> str: | ||
name = questionary.select( | ||
"Please choose a cz: (default: cz_conventional_commits)", | ||
choices=list(registry.keys()), | ||
default="cz_conventional_commits", | ||
style=self.cz.style, | ||
).ask() | ||
return name | ||
|
||
def _ask_tag(self) -> str: | ||
latest_tag = get_latest_tag() | ||
if not latest_tag: | ||
out.error("No Existing Tag. Set tag to v0.0.1") | ||
return "0.0.1" | ||
|
||
is_correct_tag = questionary.confirm( | ||
f"Is {latest_tag} the latest tag?", style=self.cz.style, default=False | ||
).ask() | ||
if not is_correct_tag: | ||
tags = get_all_tags() | ||
if not tags: | ||
out.error("No Existing Tag. Set tag to v0.0.1") | ||
return "0.0.1" | ||
|
||
latest_tag = questionary.select( | ||
"Please choose the latest tag: ", | ||
choices=get_all_tags(), | ||
style=self.cz.style, | ||
).ask() | ||
|
||
if not latest_tag: | ||
out.error("Tag is required!") | ||
raise SystemExit() | ||
return latest_tag | ||
|
||
def _ask_tag_format(self, latest_tag) -> str: | ||
is_correct_format = False | ||
if latest_tag.startswith("v"): | ||
tag_format = r"v$version" | ||
is_correct_format = questionary.confirm( | ||
f'Is "{tag_format}" the correct tag format?', style=self.cz.style | ||
).ask() | ||
|
||
if not is_correct_format: | ||
tag_format = questionary.text( | ||
'Please enter the correct version format: (default: "$version")', | ||
style=self.cz.style, | ||
).ask() | ||
|
||
if not tag_format: | ||
tag_format = "$version" | ||
return tag_format | ||
|
||
def _update_config_file(self, values): | ||
if not values: | ||
out.write("The configuration were all set. Nothing to add.") | ||
raise SystemExit() | ||
|
||
for key, value in values.items(): | ||
self.config.set_key(key, value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.