From 8421d3816454a1eb097fb481cf41130ed4dd6112 Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Sat, 13 Aug 2022 20:02:45 -0700 Subject: [PATCH] Add cargo.toml support Added a flag to update Rust Cargo.toml files when releasing a new version --- CHANGELOG.md | 7 +++++++ pyproject.toml | 4 +++- tests/test_cli.py | 25 +++++++++++++++++++++++++ yaclog/cli/__main__.py | 19 +++++++++++++------ yaclog/cli/cargo_toml.py | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 yaclog/cli/cargo_toml.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 47a8019..d1e6dd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pyproject.toml b/pyproject.toml index f00f1da..d63fc11 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,9 @@ requires-python = ">= 3.8" dependencies = [ "Click >= 7.0", "GitPython >= 3", - "packaging >= 20" + "packaging >= 20", + "tomlkit >= 0.11" + ] dynamic = ["version"] diff --git a/tests/test_cli.py b/tests/test_cli.py index e3d56d0..1886741 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -217,6 +217,31 @@ class TestRelease(unittest.TestCase): 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 "]\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): diff --git a/yaclog/cli/__main__.py b/yaclog/cli/__main__.py index b5a86c9..11d0d26 100644 --- a/yaclog/cli/__main__.py +++ b/yaclog/cli/__main__.py @@ -16,6 +16,7 @@ import datetime import os.path +from ..cli import cargo_toml import click import git @@ -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. @@ -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 @@ -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) @@ -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')}.") diff --git a/yaclog/cli/cargo_toml.py b/yaclog/cli/cargo_toml.py new file mode 100644 index 0000000..cef1fa0 --- /dev/null +++ b/yaclog/cli/cargo_toml.py @@ -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 . + +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()