Skip to content

Commit 5479bbe

Browse files
authored
Merge pull request #120 from stulaj/maven-versioning
Maven versioning
2 parents 212cfd4 + 7c7d300 commit 5479bbe

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Docker and docker-compose is required for most of `pydev project` commands.
1818

1919
### Bindfs
2020

21-
If you want to use pydev command `pydev project bind-library` you have to install `bindfs` for mounting location to the right directory. For exymple you can mount directory with library to a docker shared volume.
21+
If you want to use pydev command `pydev project bind-library` you have to install `bindfs` for mounting location to the right directory. For example you can mount directory with library to a docker shared volume.
2222

23-
Instalation command for linux/macOS:
23+
Installation command for linux/macOS:
2424
```bash
2525
apt-get install bindfs
2626
yum install bindfs
@@ -215,7 +215,7 @@ Helpers for automatic update version in the version file.
215215

216216
#### Commands
217217
* `pydev version bump-to-next` - bump version in the JSON file (or files) and print it
218-
* `pydev version print-version` - return current project version according to version JSON file
218+
* `pydev version print` - return current project version according to version JSON file
219219
* `pydev version print-next` - return next version according to input release type, build hash and version JSON file
220220

221221
#### Configuration

developers_chamber/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class VersionFileType(str, Enum):
2929
toml = "toml"
3030
json = "json"
3131
npm = "npm"
32+
xml = "xml"
3233

3334
def __str__(self):
3435
return self.value

developers_chamber/version_utils.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import re
44

5+
import xml.etree.ElementTree as ET
56
import toml
67
from click import BadParameter
78

@@ -17,7 +18,6 @@ class InvalidVersion(Exception):
1718

1819

1920
class Version:
20-
2121
VERSION_RE = re.compile(r"^{}$".format(VERSION_PATTERN))
2222

2323
def __init__(self, version_str):
@@ -102,6 +102,13 @@ def _write_version_to_file(file, new_version, file_type=None):
102102
lf.truncate()
103103
json.dump(file_data, f, indent=2)
104104
json.dump(lock_file_data, lf, indent=2)
105+
elif file_type == VersionFileType.xml:
106+
ET.register_namespace("", "http://maven.apache.org/POM/4.0.0")
107+
tree = ET.parse(f)
108+
root = tree.getroot()
109+
version = root.find("{http://maven.apache.org/POM/4.0.0}version")
110+
version.text = str(new_version)
111+
tree.write(full_file_path, xml_declaration=True, encoding="utf-8", method="xml")
105112
else:
106113
raise BadParameter(f'Invalid version type "{file_type}"')
107114

@@ -115,6 +122,8 @@ def get_version(file="version.json"):
115122
return Version(toml.load(f)["project"]["version"])
116123
elif file_extension == ".json":
117124
return Version(json.load(f)["version"])
125+
elif file_extension == ".xml":
126+
return Version(read_version_from_pom())
118127
else:
119128
raise BadParameter(f'Invalid file format "{full_file_path}"')
120129
except FileNotFoundError:
@@ -127,7 +136,7 @@ def get_next_version(release_type, build_hash=None, file="version.json"):
127136
version = get_version(file)
128137
if release_type == ReleaseType.build:
129138
if not build_hash:
130-
raise BadParameter("Build hash i required for realease type build")
139+
raise BadParameter("Build hash is required for realease type build")
131140
return version.replace(build=build_hash[:5])
132141
elif release_type == ReleaseType.patch:
133142
return version.replace(build=None, patch=version.patch + 1)
@@ -150,7 +159,7 @@ def bump_version(version, files=["version.json"], file_type=None):
150159

151160

152161
def bump_to_next_version(
153-
release_type, build_hash=None, files=["version.json"], file_type=None
162+
release_type, build_hash=None, files=["version.json"], file_type=None
154163
):
155164
"""Bump version to the next version according to previous version, release type and build hash"""
156165

@@ -159,3 +168,17 @@ def bump_to_next_version(
159168

160169
next_version = get_next_version(release_type, build_hash, files[0])
161170
return bump_version(next_version, files, file_type)
171+
172+
173+
def read_version_from_pom(file="pom.xml"):
174+
full_file_path = os.path.join(os.getcwd(), file)
175+
if not os.path.isfile(full_file_path):
176+
raise BadParameter("File {} was not found".format(full_file_path))
177+
178+
try:
179+
tree = ET.parse(full_file_path)
180+
root = tree.getroot()
181+
version = root.find("{http://maven.apache.org/POM/4.0.0}version")
182+
return version.text
183+
except KeyError:
184+
raise BadParameter("Could not find revision in pom.xml")

0 commit comments

Comments
 (0)