# SPDX-FileCopyrightText: 2023 Carmen Bianca BAKKER <carmen@carmenbianca.eu>## SPDX-License-Identifier: EUPL-1.2+"""Code related to ``protokolo init``."""frompathlibimportPathfrom._utilimportcleandoc_nlfrom.typesimportStrPath,SupportedMarkupCHANGELOG_MD="""# Change logThis change log follows the [Keep a Changelog](http://keepachangelog.com/).recommendations. Every release contains the following sections:- `Added` for new features.- `Changed` for changes in existing functionality.- `Deprecated` for soon-to-be removed features.- `Removed` for now removed features.- `Fixed` for any bug fixes.- `Security` in case of vulnerabilities.<!-- protokolo-section-tag -->"""CHANGELOG_RST="""Change log==========This change log follows the `Keep a Changelog <http://keepachangelog.com/>`_recommendations. Every release contains the following sections:- ``Added`` for new features.- ``Changed`` for changes in existing functionality.- ``Deprecated`` for soon-to-be removed features.- ``Removed`` for now removed features.- ``Fixed`` for any bug fixes.- ``Security`` in case of vulnerabilities... protokolo-section-tag"""
[docs]defcreate_changelog(changelog:StrPath,markup:SupportedMarkup|None)->None:"""Create a changelog file"""changelog=Path(changelog).resolve()# Make certain the directory of CHANGELOG existschangelog.parent.mkdir(parents=True,exist_ok=True)# Make certain CHANGELOG exists, but do not overwrite it.ifnotchangelog.exists():text=CHANGELOG_MDifmarkup=="restructuredtext":text=CHANGELOG_RSTchangelog.write_text(text,encoding="utf-8")
[docs]defcreate_keep_a_changelog(directory:StrPath)->None:"""Create a skeleton structure of changelog.d at *directory*."""directory=Path(directory)directory.mkdir(parents=True,exist_ok=True)ifnot(directory/".protokolo.toml").exists():(directory/".protokolo.toml").write_text(cleandoc_nl(""" [protokolo.section] title = "${version} - ${date}" level = 2 """),encoding="utf-8",)subdirs=[{"dirname":"added","title":"Added","order":1},{"dirname":"changed","title":"Changed","order":2},{"dirname":"deprecated","title":"Deprecated","order":3},{"dirname":"removed","title":"Removed","order":4},{"dirname":"fixed","title":"Fixed","order":5},{"dirname":"security","title":"Security","order":6},]forsubdirinsubdirs:(directory/str(subdir["dirname"])).mkdir(exist_ok=True)protokolo_toml=directory/str(subdir["dirname"])/".protokolo.toml"ifnotprotokolo_toml.exists():protokolo_toml.write_text(cleandoc_nl(""" [protokolo.section] title = "{title}" order = {order} """).format(title=subdir["title"],order=subdir["order"]),encoding="utf-8",)
[docs]defcreate_root_toml(changelog:StrPath,markup:SupportedMarkup|None,directory:StrPath)->None:"""Create a ``.protokolo.toml`` file in the current working directory."""ifmarkupisNone:markup="markdown"Path(".protokolo.toml").write_text(cleandoc_nl(""" [protokolo] changelog = "{changelog}" markup = "{markup}" directory = "{directory}" """).format(changelog=changelog,markup=markup,directory=directory),encoding="utf-8",)