-
-
Notifications
You must be signed in to change notification settings - Fork 297
feat(providers): add dart_provider #1564
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Mapping | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
from ruamel.yaml import YAML | ||
|
||
from commitizen.providers.base_provider import VersionProvider | ||
|
||
|
||
class DartProvider(VersionProvider): | ||
""" | ||
dart pubspec.yaml version management | ||
""" | ||
|
||
package_filename = "pubspec.yaml" | ||
|
||
def __init__(self, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
self._yaml = YAML() | ||
self._yaml.preserve_quotes = True | ||
self._yaml.indent(mapping=2, sequence=4, offset=2) | ||
|
||
@property | ||
def package_file(self) -> Path: | ||
return Path() / self.package_filename | ||
|
||
def get_version(self) -> str: | ||
print(self.package_file.read_text()) | ||
document = self._yaml.load(self.package_file.read_text()) | ||
return self.get(document) | ||
|
||
def set_version(self, version: str) -> None: | ||
document = self._yaml.load(self.package_file.read_text()) | ||
self.set(document, version) | ||
self._yaml.dump(document, self.package_file) | ||
|
||
def get(self, document: Mapping[str, str]) -> str: | ||
# Extract the version without the build number | ||
version = document["version"] | ||
return version.split("+")[0] if "+" in version else version | ||
|
||
def set(self, document: dict[str, Any], version: str) -> None: | ||
# To enforce to save the version in pubspec without quotes | ||
# even if the version could be interpreted as float by yaml | ||
# dump | ||
version_value: Any | ||
try: | ||
version_value = float(version) | ||
except ValueError: | ||
version_value = version | ||
document["version"] = version_value |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ dependencies = [ | |
"charset-normalizer (>=2.1.0,<4)", | ||
# Use the Python 3.11 and 3.12 compatible API: https://github.com/python/importlib_metadata#compatibility | ||
"importlib-metadata >=8.0.0,<8.7.0 ; python_version < '3.10'", | ||
"ruamel-yaml (>=0.18.14,<0.19.0)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pubspec.yaml file in Dart contains fields both with and without quotes. ruamel.yaml extends PyYAML by offering more configuration options, such as setting style preferences for quotes — in this case, preserving the original ones. |
||
] | ||
keywords = ["commitizen", "conventional", "commits", "git"] | ||
# See also: https://pypi.org/classifiers/ | ||
|
@@ -71,6 +72,7 @@ cargo = "commitizen.providers:CargoProvider" | |
commitizen = "commitizen.providers:CommitizenProvider" | ||
composer = "commitizen.providers:ComposerProvider" | ||
npm = "commitizen.providers:NpmProvider" | ||
dart = "commitizen.providers:DartProvider" | ||
pep621 = "commitizen.providers:Pep621Provider" | ||
poetry = "commitizen.providers:PoetryProvider" | ||
scm = "commitizen.providers:ScmProvider" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from textwrap import dedent | ||
|
||
import pytest | ||
|
||
from commitizen.config.base_config import BaseConfig | ||
from commitizen.providers import get_provider | ||
from commitizen.providers.dart_provider import DartProvider | ||
|
||
DART_YAML = """\ | ||
name: my_super_package | ||
description: A new Flutter project. | ||
|
||
# Bla, bla, bla | ||
publish_to: "none" # Remove this line if you wish to publish to pub.dev | ||
|
||
version: 0.1.0+1 | ||
|
||
environment: | ||
sdk: ^3.7.2 | ||
# note that this only enforces that the Flutter version is at least the value specified. It does not | ||
# force it to this specific version | ||
flutter: 3.29.2 | ||
|
||
# Bla, bla, bla | ||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
|
||
# Some package dependencies | ||
# network | ||
dio: ^5.8.0+1 | ||
|
||
# The following section is specific to Flutter packages. | ||
flutter: | ||
# The following line ensures that the Material Icons font is | ||
# included with your application, so that you can use the icons in | ||
# the material Icons class. | ||
uses-material-design: true | ||
|
||
# To add assets to your application, add an assets section, like this: | ||
assets: | ||
- assets/images/ | ||
""" | ||
|
||
DART_YAML_FLOAT_PARSEABLE_EXPECTED = """\ | ||
name: my_super_package | ||
description: A new Flutter project. | ||
|
||
# Bla, bla, bla | ||
publish_to: "none" # Remove this line if you wish to publish to pub.dev | ||
|
||
version: 42.1 | ||
|
||
environment: | ||
sdk: ^3.7.2 | ||
# note that this only enforces that the Flutter version is at least the value specified. It does not | ||
# force it to this specific version | ||
flutter: 3.29.2 | ||
|
||
# Bla, bla, bla | ||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
|
||
# Some package dependencies | ||
# network | ||
dio: ^5.8.0+1 | ||
|
||
# The following section is specific to Flutter packages. | ||
flutter: | ||
# The following line ensures that the Material Icons font is | ||
# included with your application, so that you can use the icons in | ||
# the material Icons class. | ||
uses-material-design: true | ||
|
||
# To add assets to your application, add an assets section, like this: | ||
assets: | ||
- assets/images/ | ||
""" | ||
|
||
DART_YAML_SEMVER_EXPECTED = """\ | ||
name: my_super_package | ||
description: A new Flutter project. | ||
|
||
# Bla, bla, bla | ||
publish_to: "none" # Remove this line if you wish to publish to pub.dev | ||
|
||
version: 2.3.4 | ||
|
||
environment: | ||
sdk: ^3.7.2 | ||
# note that this only enforces that the Flutter version is at least the value specified. It does not | ||
# force it to this specific version | ||
flutter: 3.29.2 | ||
|
||
# Bla, bla, bla | ||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
|
||
# Some package dependencies | ||
# network | ||
dio: ^5.8.0+1 | ||
|
||
# The following section is specific to Flutter packages. | ||
flutter: | ||
# The following line ensures that the Material Icons font is | ||
# included with your application, so that you can use the icons in | ||
# the material Icons class. | ||
uses-material-design: true | ||
|
||
# To add assets to your application, add an assets section, like this: | ||
assets: | ||
- assets/images/ | ||
""" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"content, expected", | ||
((DART_YAML, DART_YAML_FLOAT_PARSEABLE_EXPECTED),), | ||
) | ||
def test_dart_provider_with_float_parseable_new_version( | ||
config: BaseConfig, | ||
chdir: Path, | ||
content: str, | ||
expected: str, | ||
): | ||
filename = DartProvider.package_filename | ||
file = chdir / filename | ||
file.write_text(dedent(content)) | ||
config.settings["version_provider"] = "dart" | ||
|
||
provider = get_provider(config) | ||
assert isinstance(provider, DartProvider) | ||
assert provider.get_version() == "0.1.0" | ||
|
||
provider.set_version("42.1") | ||
assert file.read_text() == dedent(expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"content, expected", | ||
((DART_YAML, DART_YAML_SEMVER_EXPECTED),), | ||
) | ||
def test_dart_provider_with_semver_new_version( | ||
config: BaseConfig, | ||
chdir: Path, | ||
content: str, | ||
expected: str, | ||
): | ||
filename = DartProvider.package_filename | ||
file = chdir / filename | ||
file.write_text(dedent(content)) | ||
config.settings["version_provider"] = "dart" | ||
|
||
provider = get_provider(config) | ||
assert isinstance(provider, DartProvider) | ||
assert provider.get_version() == "0.1.0" | ||
|
||
provider.set_version("2.3.4") | ||
assert file.read_text() == dedent(expected) |