forked from il-katta/ACUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projedit.py
117 lines (95 loc) · 3.52 KB
/
projedit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
'''
setta la versione o incrementa l'ultimo numero di versione ( build version ) nei file .csproj per progetti ".NET Standard"
o nei file AssemblyInfo nei progetti ".NET Framework"
usage:
projedit.py netstandard path/to/project.csproj [<new version number>]
projedit.py vbframework path/to/AssemblyInfo.vb [<new version number>]
projedit.py csframework path/to/AssemblyInfo.cs [<new version number>]
'''
import sys
import re
import fileinput
from xml.etree import ElementTree
def xml_change_value_if_exists(proj, path, value):
node = proj.find(path)
if node is None:
return
node.text = value
def is_int(str_):
for n in str_:
if n not in '1234567890':
return False
return True
def increase_version_str(current_version):
build_n = current_version.split('.')[-1]
if not build_n.isdigit():
raise Exception("current build version {} is not a number".format(build_n))
return '.'.join(current_version.split('.')[:-1]) + '.' + str(int(build_n) + 1)
def netstandard_set_version(proj, path, new_version=None):
node = proj.find(path)
if node is None:
return
if new_version is None:
current_version = node.text
new_version = increase_version_str(current_version)
xml_change_value_if_exists(proj, path, new_version)
return new_version
def find_and_replace(file_name, regexp, new_version=None):
re_assembly_file = re.compile(regexp)
with fileinput.FileInput(file_name, inplace=True, backup='.bak') as file:
for line in file:
if re_assembly_file.match(line):
current_version = re_assembly_file.findall(line).pop()
if new_version is None:
new_version = increase_version_str(current_version)
print(line.replace(current_version, new_version), end='')
else:
print(line, end='')
return new_version
def netstandard_main(file_name, new_version=None):
proj = ElementTree.parse(file_name)
version = None
try:
version = netstandard_set_version(proj, 'PropertyGroup/Version', new_version)
except Exception as e:
print(str(e))
try:
new_version = netstandard_set_version(proj, 'PropertyGroup/PackageVersion', new_version)
if new_version:
version = new_version
except Exception as e:
print(str(e))
if version is None:
print("no changes")
sys.exit(1)
proj.write(file_name)
print(version, end='')
def vbframework_main(file_name, new_version=None):
new_version = find_and_replace(file_name, r'^\<Assembly: AssemblyFileVersion\("(.*)"\)\>', new_version)
if new_version is None:
print("no changes")
sys.exit(1)
print(new_version, end='')
def csframework_main(file_name, new_version=None):
new_version = find_and_replace(file_name, r'^\[assembly: AssemblyFileVersion\("(.*)"\)]$', new_version)
if new_version is None:
print("no changes")
sys.exit(1)
print(new_version, end='')
# main
if __name__ == '__main__':
edit_type = sys.argv[1]
file_name = sys.argv[2]
if len(sys.argv) == 4:
new_version = sys.argv[3]
else:
new_version = None
if edit_type == 'netstandard':
netstandard_main(file_name, new_version)
elif edit_type == 'vbframework':
vbframework_main(file_name, new_version)
elif edit_type == 'csframework':
csframework_main(file_name, new_version)
else:
sys.exit(255)