|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import json |
3 | 4 | from abc import ABC, abstractmethod |
4 | 5 | from pathlib import Path |
5 | | -from typing import ClassVar, cast |
| 6 | +from typing import Any, ClassVar, cast |
6 | 7 |
|
7 | 8 | import importlib_metadata as metadata |
8 | 9 | import tomlkit |
@@ -122,6 +123,46 @@ def set(self, document: tomlkit.TOMLDocument, version: str): |
122 | 123 | document["package"]["version"] = version # type: ignore |
123 | 124 |
|
124 | 125 |
|
| 126 | +class JsonProvider(FileProvider): |
| 127 | + """ |
| 128 | + Base class for JSON-based version providers |
| 129 | + """ |
| 130 | + |
| 131 | + indent: ClassVar[int] = 2 |
| 132 | + |
| 133 | + def get_version(self) -> str: |
| 134 | + document = json.loads(self.file.read_text()) |
| 135 | + return self.get(document) |
| 136 | + |
| 137 | + def set_version(self, version: str): |
| 138 | + document = json.loads(self.file.read_text()) |
| 139 | + self.set(document, version) |
| 140 | + self.file.write_text(json.dumps(document, indent=self.indent) + "\n") |
| 141 | + |
| 142 | + def get(self, document: dict[str, Any]) -> str: |
| 143 | + return document["version"] # type: ignore |
| 144 | + |
| 145 | + def set(self, document: dict[str, Any], version: str): |
| 146 | + document["version"] = version |
| 147 | + |
| 148 | + |
| 149 | +class NpmProvider(JsonProvider): |
| 150 | + """ |
| 151 | + npm package.json version management |
| 152 | + """ |
| 153 | + |
| 154 | + filename = "package.json" |
| 155 | + |
| 156 | + |
| 157 | +class ComposerProvider(JsonProvider): |
| 158 | + """ |
| 159 | + Composer version management |
| 160 | + """ |
| 161 | + |
| 162 | + filename = "composer.json" |
| 163 | + indent = 4 |
| 164 | + |
| 165 | + |
125 | 166 | def get_provider(config: BaseConfig) -> VersionProvider: |
126 | 167 | """ |
127 | 168 | Get the version provider as defined in the configuration |
|
0 commit comments