Skip to content

Commit

Permalink
Add cargo.toml support
Browse files Browse the repository at this point in the history
Added a flag to update Rust Cargo.toml files when releasing a new version
  • Loading branch information
drewcassidy committed Aug 14, 2022
1 parent c5030b6 commit 8421d38
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file

## Unreleased

### Added

- Added a flag to update Rust Cargo.toml files when releasing a new version


## Version 1.0.4 - 2022-04-08

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ requires-python = ">= 3.8"
dependencies = [
"Click >= 7.0",
"GitPython >= 3",
"packaging >= 20"
"packaging >= 20",
"tomlkit >= 0.11"

]
dynamic = ["version"]

Expand Down
25 changes: 25 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,31 @@ def test_commit(self):
self.assertIn(repo.head.commit.hexsha[0:7], result.output)
self.assertEqual(repo.tags[0].name, '1.0.0')

def test_cargo(self):
"""Test updating cargo.toml files"""
runner = CliRunner()
with runner.isolated_filesystem():
with open("Cargo.toml", "w") as fp:
fp.write((
'[package]\n'
'name = "dummy"\n'
'version = "0.3.4"\n'
'authors = ["Andrew Cassidy <drewcassidy@me.com>"]\n'
'description = "A dummy crate used for testing yaclog"\n'
'keywords = ["does", "not", "exist"]\n'
'edition = "2018"\n'
))

runner.invoke(cli, ['init']) # create the changelog
runner.invoke(cli, ['entry', '-b', 'entry number 1'])

result = runner.invoke(cli, ['release', 'Version 1.0.0', '-C'])
check_result(self, result)

with open("Cargo.toml", "r") as fp:
self.assertIn('version = "1.0.0"', fp.read())
# we're just going to trust tomlkit not to mangle everything else


class TestShow(unittest.TestCase):

Expand Down
19 changes: 13 additions & 6 deletions yaclog/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import datetime
import os.path
from ..cli import cargo_toml

import click
import git
Expand Down Expand Up @@ -199,9 +200,11 @@ def entry(obj: Changelog, bullets, paragraphs, section_name, version_name):
@click.option('-c', '--commit', is_flag=True,
help='Create a git commit tagged with the new version number. '
'If there are no changes to commit, the current commit will be tagged instead.')
@click.option('-C', '--cargo', '-🦀', is_flag=True,
help='Update the version in a Rust cargo.toml manifest file.')
@click.argument('version_name', metavar='VERSION', type=str, default=None, required=False)
@click.pass_obj
def release(obj: Changelog, version_name, rel_seg, pre_seg, commit):
def release(obj: Changelog, version_name, rel_seg, pre_seg, commit, cargo):
"""
Release VERSION, or a version incremented from the last release.
Expand All @@ -213,7 +216,7 @@ def release(obj: Changelog, version_name, rel_seg, pre_seg, commit):
other kinds of prerelease.
"""

if rel_seg is None and pre_seg is None and not version_name and not commit:
if rel_seg is None and pre_seg is None and not version_name and not commit and not cargo:
click.echo('Nothing to release!')
raise click.Abort

Expand Down Expand Up @@ -246,6 +249,14 @@ def release(obj: Changelog, version_name, rel_seg, pre_seg, commit):
obj.write()
click.echo(f"Renamed {click.style(old_name, fg='blue')} to {click.style(new_name, fg='blue')}")

short_version, *_ = yaclog.version.extract_version(cur_version.name)
if not short_version:
short_version = cur_version.name.replace(' ', '-')

if cargo:
cargo_toml.set_version("Cargo.toml", str(short_version))
click.echo("Updated Cargo.toml")

if commit:
repo = git.Repo(os.curdir)

Expand Down Expand Up @@ -277,10 +288,6 @@ def release(obj: Changelog, version_name, rel_seg, pre_seg, commit):
else:
commit = repo.head.commit

short_version, *_ = yaclog.version.extract_version(cur_version.name)
if not short_version:
short_version = cur_version.name.replace(' ', '-')

repo_tag = repo.create_tag(short_version, ref=commit, message=cur_version.body(False))
click.echo(f"Created tag {click.style(repo_tag.name, fg='green')}.")

Expand Down
33 changes: 33 additions & 0 deletions yaclog/cli/cargo_toml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# yaclog: yet another changelog tool
# Copyright (c) 2022. Andrew Cassidy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from tomlkit import dumps
from tomlkit import parse


def set_version(path, version):
"""
Set the version string in a cargo.toml file
:param path: path-like file location
:param version: version string to overwrite with
"""
with open(path, 'r+') as fp:
toml = parse(fp.read())
toml['package']['version'] = version
fp.seek(0)
fp.write(dumps(toml))
fp.truncate()

0 comments on commit 8421d38

Please sign in to comment.