Skip to content
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

Use pyproject.toml as the configuration file #70

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,39 @@ tbump = "tbump.main:main"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"


[tool.tbump.version]
current = "6.1.1"
regex = '''
(?P<major>\d+)
\.
(?P<minor>\d+)
\.
(?P<patch>\d+)
'''

[tool.tbump.git]
message_template = "Bump to {new_version}"
tag_template = "v{new_version}"


[[tool.tbump.file]]
src = "pyproject.toml"
search = 'version = "{current_version}"'


[[tool.tbump.file]]
src = "tbump/main.py"

[[tool.tbump.before_commit]]
name = "Run CI"
cmd = "./lint.sh && poetry run pytest"

[[tool.tbump.before_commit]]
name = "Check Changelog"
cmd = "grep -q {new_version} Changelog.rst"

[[tool.tbump.after_push]]
name = "Publish to pypi"
cmd = "tools/publish.sh"
38 changes: 0 additions & 38 deletions tbump.toml

This file was deleted.

17 changes: 15 additions & 2 deletions tbump/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def validate_re(regex: str) -> str:
schema.Optional("github_url"): str,
}
)
return cast(Config, tbump_schema.validate(config))
validated_config = tbump_schema.validate(config)
return cast(Config, validated_config)


def validate_config(cfg: Config) -> None:
Expand Down Expand Up @@ -126,7 +127,19 @@ def validate_config(cfg: Config) -> None:


def parse(cfg_path: Path) -> Config:
parsed = tomlkit.loads(cfg_path.text())
# tbump.toml used to contain "flat" keys while pyproject.toml contains nested keys.
# For example:
#
# [[file]]
# vs
# [[tool.tbump.file]]
#
# In order to minimize the number of changes that are needed for this switch
# we parse `pyproject.toml` and use it to create a new `tomlkit.Document()` with
# a flat structure
parsed_pyproject = tomlkit.loads(cfg_path.text())["tool"]["tbump"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty clever but it will crash with KeyError if [tool] or [[tool.tbump]] section is messing

Copy link
Contributor Author

@pmav99 pmav99 Sep 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but let's not forget that the PR was about implementing Option 2, i.e. only support pyproject.toml. In this case, tool.tbump should always be present :)))

That being said, handling the KeyError would probably make sense anyway, since the user might make a typo or something.

parsed = tomlkit.document()
parsed.update(parsed_pyproject)
parsed = validate_basic_schema(parsed)
current_version = parsed["version"]["current"]
git_message_template = parsed["git"]["message_template"]
Expand Down
4 changes: 2 additions & 2 deletions tbump/file_bumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def get_patches(self, new_version: str) -> List[Patch]:
self.new_groups = self.parse_version(self.new_version)
change_requests = self.compute_change_requests()
tbump_toml_change = ChangeRequest(
"tbump.toml", self.current_version, new_version
"pyproject.toml", self.current_version, new_version
)
change_requests.append(tbump_toml_change)
patches = []
Expand Down Expand Up @@ -272,7 +272,7 @@ def compute_change_request_for_file(self, file: tbump.config.File) -> ChangeRequ
def bump_files(new_version: str, repo_path: Path = None) -> None:
repo_path = repo_path or Path(".")
bumper = FileBumper(repo_path)
cfg = tbump.config.parse(repo_path / "tbump.toml")
cfg = tbump.config.parse(repo_path / "pyproject.toml")
bumper.set_config(cfg)
patches = bumper.get_patches(new_version=new_version)
n = len(patches)
Expand Down
17 changes: 9 additions & 8 deletions tbump/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ def find_files(working_path: Path, current_version: str) -> List[str]:


def init(working_path: Path, *, current_version: str) -> None:
""" Interactively creates a new tbump.toml """
""" Interactively creates a new pyproject.toml """
ui.info_1("Generating tbump config file")
tbump_path = working_path / "tbump.toml"
tbump_path = working_path / "pyproject.toml"
if tbump_path.exists():
ui.fatal(tbump_path, "already exists")
template = textwrap.dedent(
"""\
# Uncomment this if your project is hosted on GitHub:
# [tool.tbump]
# github_url = https://github.com/<user or organization>/<project>/

[version]
[tool.tbump.version]
current = "@current_version@"

# Example of a semver regexp.
Expand All @@ -44,7 +45,7 @@ def init(working_path: Path, *, current_version: str) -> None:
(?P<patch>\\d+)
'''

[git]
[tool.tbump.git]
message_template = "Bump to {new_version}"
tag_template = "v{new_version}"
"""
Expand All @@ -53,9 +54,9 @@ def init(working_path: Path, *, current_version: str) -> None:
file_template = textwrap.dedent(
"""
# For each file to patch, add a [[file]] config section containing
# the path of the file, relative to the tbump.toml location.
[[file]]
src = "..."
# the path of the file, relative to the pyproject.toml location.
[[tool.tbump.file]]
src = "pyproject.toml"
"""
)

Expand All @@ -81,4 +82,4 @@ def init(working_path: Path, *, current_version: str) -> None:
to_write += file_template
to_write += hooks_template
tbump_path.write_text(to_write)
ui.info_2(ui.check, "Generated tbump.toml")
ui.info_2(ui.check, "Generated pyproject.toml")
2 changes: 1 addition & 1 deletion tbump/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def run(cmd: List[str]) -> None:


def parse_config(working_path: Path) -> Config:
tbump_path = working_path / "tbump.toml"
tbump_path = working_path / "pyproject.toml"
try:
config = tbump.config.parse(tbump_path)
except IOError as io_error:
Expand Down
12 changes: 6 additions & 6 deletions tbump/test/data/tbump.toml → tbump/test/data/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[version]
[tool.tbump.version]
current = "1.2.41-alpha-1"
regex = '''
(?P<major>\d+)
Expand All @@ -14,21 +14,21 @@ regex = '''
)?
'''

[git]
[tool.tbump.git]
message_template = "Bump to {new_version}"
tag_template = "v{new_version}"

[[file]]
[[tool.tbump.file]]
src = "package.json"
search = '"version": "{current_version}"'

[[file]]
[[tool.tbump.file]]
src = "VERSION"

[[file]]
[[tool.tbump.file]]
src = "pub.js"
version_template = "{major}.{minor}.{patch}"

[[file]]
[[tool.tbump.file]]
src = "glob*.?"
search = 'version_[a-z]+ = "{current_version}"'
34 changes: 17 additions & 17 deletions tbump/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


def test_happy_parse(test_data_path: Path) -> None:
config = tbump.config.parse(test_data_path / "tbump.toml")
config = tbump.config.parse(test_data_path / "pyproject.toml")
foo_json = tbump.config.File(
src="package.json", search='"version": "{current_version}"'
)
Expand Down Expand Up @@ -52,7 +52,7 @@ def assert_validation_error(config: Config, expected_message: str) -> None:

@pytest.fixture # type: ignore
def test_config(test_data_path: Path) -> Config:
return tbump.config.parse(test_data_path / "tbump.toml")
return tbump.config.parse(test_data_path / "pyproject.toml")


def test_invalid_commit_message(test_config: Config) -> None:
Expand Down Expand Up @@ -83,15 +83,15 @@ def test_current_version_does_not_match_expected_regex(test_config: Config) -> N
def test_invalid_regex() -> None:
contents = textwrap.dedent(
r"""
[version]
[tool.tbump.version]
current = '1.42a1'
regex = '(unbalanced'

[git]
[tool.tbump.git]
message_template = "Bump to {new_version}"
tag_template = "v{new_version}"

[[file]]
[[tool.tbump.file]]
src = "VERSION"
"""
)
Expand All @@ -112,25 +112,25 @@ def test_invalid_custom_template(test_config: Config) -> None:


def test_parse_hooks(tmp_path: Path) -> None:
toml_path = tmp_path / "tbump.toml"
toml_path = tmp_path / "pyproject.toml"
toml_path.write_text(
r"""
[version]
[tool.tbump.version]
current = "1.2.3"
regex = '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'

[git]
[tool.tbump.git]
message_template = "Bump to {new_version}"
tag_template = "v{new_version}"

[[file]]
[[tool.tbump.file]]
src = "pub.js"

[[before_commit]]
[[tool.tbump.before_commit]]
name = "Check changelog"
cmd = "grep -q {new_version} Changelog.md"

[[after_push]]
[[tool.tbump.after_push]]
name = "After push"
cmd = "cargo publish"
"""
Expand All @@ -148,25 +148,25 @@ def test_parse_hooks(tmp_path: Path) -> None:


def test_retro_compat_hooks(tmp_path: Path) -> None:
toml_path = tmp_path / "tbump.toml"
toml_path = tmp_path / "pyproject.toml"
toml_path.write_text(
r"""
[version]
[tool.tbump.version]
current = "1.2.3"
regex = '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'

[git]
[tool.tbump.git]
message_template = "Bump to {new_version}"
tag_template = "v{new_version}"

[[file]]
[[tool.tbump.file]]
src = "pub.js"

[[hook]]
[[tool.tbump.hook]]
name = "very old name"
cmd = "old command"

[[before_push]]
[[tool.tbump.before_push]]
name = "deprecated name"
cmd = "deprecated command"
"""
Expand Down
Loading