Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Other projects: support PEP 621 and TS/JS package.json for finding version #100

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelogs/fragments/100-more-other-projects.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
minor_changes:
- Allow to extract other project versions for Python projects from PEP 621 conformant ``pyproject.toml``
(https://github.com/ansible-community/antsibull-changelog/pull/100).
- Allow to extract other project versions for JavaScript / TypeScript projects from ``package.json``
(https://github.com/ansible-community/antsibull-changelog/pull/100).
21 changes: 21 additions & 0 deletions src/antsibull_changelog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import argparse
import datetime
import json
import os
import sys
import traceback
Expand Down Expand Up @@ -448,15 +449,31 @@ def _get_pyproject_toml_version(project_toml_path: str) -> str | None:
with open(project_toml_path, 'r', encoding='utf-8') as f:
data = (toml if HAS_TOML else tomli).loads(f.read())
gotmax23 marked this conversation as resolved.
Show resolved Hide resolved

# PEP 621 project metadata (https://peps.python.org/pep-0621/#version)
project_data = data.get('project')
if isinstance(project_data, dict) and project_data.get('version'):
return project_data.get('version')

tool_config = data.get('tool') or {}

# Python Poetry (https://python-poetry.org/docs/pyproject/#version)
if 'poetry' in tool_config:
poetry_config = tool_config['poetry']
return poetry_config.get('version')

return None


def _get_package_json_version(package_json_path: str) -> str | None:
'''
Try to extract version from package.json.
'''
with open(package_json_path, 'r', encoding='utf-8') as f:
data = json.load(f)

return data.get('version')


def _get_project_version(paths: PathsConfig) -> str | None:
'''
Try to extract version for other projects.
Expand All @@ -465,6 +482,10 @@ def _get_project_version(paths: PathsConfig) -> str | None:
if os.path.isfile(project_toml_path):
return _get_pyproject_toml_version(project_toml_path)

package_json_path = os.path.join(paths.base_dir, 'package.json')
if os.path.isfile(package_json_path):
return _get_package_json_version(package_json_path)

return None


Expand Down