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 1 commit
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 form ``package.json``
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
(https://github.com/ansible-community/antsibull-changelog/pull/100).
20 changes: 20 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,30 @@ 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)
if data.get('version'):
return data.get('version')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For PEP 621 metadata, version is part of the project table (data['project']['verision']). It's not a top level key.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops, that's embarrasing. Fixed in 88f13ee.


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, 'rb') as f:
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
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 +481,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