Skip to content

Commit 9db8326

Browse files
committed
Add version management for maven projects
1 parent 212cfd4 commit 9db8326

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-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: 28 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,14 @@ 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+
properties = root.find("{http://maven.apache.org/POM/4.0.0}properties")
110+
r = properties.find("{http://maven.apache.org/POM/4.0.0}revision")
111+
r.text = str(new_version)
112+
tree.write(full_file_path, xml_declaration=True, encoding="utf-8", method="xml")
105113
else:
106114
raise BadParameter(f'Invalid version type "{file_type}"')
107115

@@ -115,6 +123,8 @@ def get_version(file="version.json"):
115123
return Version(toml.load(f)["project"]["version"])
116124
elif file_extension == ".json":
117125
return Version(json.load(f)["version"])
126+
elif file_extension == ".xml":
127+
return Version(read_version_from_pom())
118128
else:
119129
raise BadParameter(f'Invalid file format "{full_file_path}"')
120130
except FileNotFoundError:
@@ -127,7 +137,7 @@ def get_next_version(release_type, build_hash=None, file="version.json"):
127137
version = get_version(file)
128138
if release_type == ReleaseType.build:
129139
if not build_hash:
130-
raise BadParameter("Build hash i required for realease type build")
140+
raise BadParameter("Build hash is required for realease type build")
131141
return version.replace(build=build_hash[:5])
132142
elif release_type == ReleaseType.patch:
133143
return version.replace(build=None, patch=version.patch + 1)
@@ -150,7 +160,7 @@ def bump_version(version, files=["version.json"], file_type=None):
150160

151161

152162
def bump_to_next_version(
153-
release_type, build_hash=None, files=["version.json"], file_type=None
163+
release_type, build_hash=None, files=["version.json"], file_type=None
154164
):
155165
"""Bump version to the next version according to previous version, release type and build hash"""
156166

@@ -159,3 +169,18 @@ def bump_to_next_version(
159169

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

0 commit comments

Comments
 (0)