mirror of
https://github.com/drewcassidy/yaclog.git
synced 2024-09-01 14:58:58 +00:00
Add cargo.toml support
Added a flag to update Rust Cargo.toml files when releasing a new version
This commit is contained in:
parent
c5030b6060
commit
8421d38164
@ -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
|
||||
|
@ -31,7 +31,9 @@ requires-python = ">= 3.8"
|
||||
dependencies = [
|
||||
"Click >= 7.0",
|
||||
"GitPython >= 3",
|
||||
"packaging >= 20"
|
||||
"packaging >= 20",
|
||||
"tomlkit >= 0.11"
|
||||
|
||||
]
|
||||
dynamic = ["version"]
|
||||
|
||||
|
@ -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 <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):
|
||||
|
||||
|
@ -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')}.")
|
||||
|
||||
|
33
yaclog/cli/cargo_toml.py
Normal file
33
yaclog/cli/cargo_toml.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user