# -*- coding: utf-8 -*-
from __future__ import annotations

from invoke import Context, task


def get_version(ctx: Context) -> str:
    result = ctx.run('poetry version -s --no-ansi', hide='out')
    assert result is not None, 'Hm?'

    return result.stdout.strip()


@task
def bump_version(ctx: Context, next_version: str, build: str | None = None):
    current_version = get_version(ctx)

    print(f'Bumping version: `{current_version}` -> `{next_version}`')

    for file_to_version in ctx.config.get('files_to_version', []):
        with open(file_to_version, 'r', encoding='utf-8') as content_f:
            content = content_f.read()

        content = content.replace(current_version, next_version)

        with open(file_to_version, 'w', encoding='utf-8') as content_f:
            content_f.write(content)
