Skip to content

Commit 8c4a2eb

Browse files
authored
Project version batch replacement script (apache#441)
* update python indent_size * Project version number batch replacement script
1 parent 21f65fc commit 8c4a2eb

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ indent_size = 2
1919
indent_size = 2
2020

2121
[*.py]
22-
indent_size = 2
22+
indent_size = 4
2323

2424
[*.sh]
2525
indent_size = 2

script/update_version.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
# update streamx project version, requires python-3.8+
5+
# use case: python3 update_ver.py <expect_ver> <project_path>
6+
# example: python3 update_ver.py 1.2.1
7+
8+
# @author : Al-assad
9+
# @date : 2021/11/17
10+
11+
import sys
12+
import os
13+
import xml.etree.ElementTree as ET
14+
from os.path import join as os_path
15+
import pprint
16+
17+
expect_ver = '1.2.1'
18+
project_path = '../'
19+
20+
if len(sys.argv) >= 2:
21+
expect_ver = sys.argv[1]
22+
if len(sys.arg) >= 3:
23+
project_path = sys.argv[2]
24+
25+
# find all pom.xml in project
26+
poms = []
27+
for path, dirs, files in os.walk(project_path):
28+
if path == os_path('.git'):
29+
continue
30+
for file in files:
31+
if file != 'pom.xml':
32+
continue
33+
poms.append(os_path(path, file))
34+
35+
ns = '{http://maven.apache.org/POM/4.0.0}'
36+
pre_version = ''
37+
# update the version in pom.xml
38+
for pom in poms:
39+
tree = ET.parse(pom, parser=ET.XMLParser(target=ET.TreeBuilder(insert_comments=True)))
40+
root = tree.getroot()
41+
group_id = root.find(ns + 'groupId')
42+
if group_id is not None and group_id.text != 'com.streamxhub.streamx':
43+
continue
44+
version_id = root.find(ns + 'version')
45+
if version_id is not None:
46+
pre_version = version_id.text
47+
version_id.text = expect_ver
48+
parent = root.find(ns + 'parent')
49+
if parent is not None:
50+
if parent.find(ns + 'groupId').text == 'com.streamxhub.streamx':
51+
parent_version = parent.find(ns + 'version')
52+
if parent_version is not None:
53+
parent_version.text = expect_ver
54+
ET.register_namespace('', 'http://maven.apache.org/POM/4.0.0')
55+
# no use the xml_declaration content create by ET like <?xml version='1.0' encoding='utf-8'?>
56+
tree.write(pom, encoding='UTF-8', xml_declaration=False)
57+
with open(pom, 'r+') as f:
58+
c = f.read()
59+
f.seek(0, 0)
60+
f.write('<?xml version="1.0" encoding="UTF-8"?>\n' + c)
61+
print('update version in {} to {}'.format(pom, expect_ver))
62+
63+
# update version in print log content
64+
config_const_file = os_path(project_path, 'streamx-common/src/main/scala/com/streamxhub/streamx/common/conf/ConfigConst.scala')
65+
f_content = []
66+
with open(config_const_file, 'r') as f:
67+
f_content = f.readlines()
68+
for i in range(len(f_content)):
69+
line = f_content[i]
70+
if line.strip().startswith('println(" Ver'):
71+
f_content[i] = line.replace(pre_version, expect_ver)
72+
break
73+
74+
with open(config_const_file, 'w') as f:
75+
f.writelines(f_content)
76+
print('update version in {} to {}'.format(config_const_file, expect_ver))
77+
78+
print('done :)')

0 commit comments

Comments
 (0)