-
Notifications
You must be signed in to change notification settings - Fork 34
/
deploy.py
202 lines (179 loc) · 7 KB
/
deploy.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from subprocess import call
import subprocess
from win32api import GetFileVersionInfo, LOWORD, HIWORD
import zipfile
import os
import argparse
import re
import sys
import shutil
import json
import hashlib
from bs4 import BeautifulSoup
import datetime
from pygit2 import Repository, Config, Signature, GIT_OBJ_COMMIT
script_dir = os.path.dirname(os.path.realpath(__file__))
rc_path = os.path.join (script_dir, 'src/plugin/DSpellCheck.rc')
def get_version_number (filename):
info = GetFileVersionInfo (filename, "\\")
ms = info['FileVersionMS']
ls = info['FileVersionLS']
return '{}.{}.{}.{}'.format (HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls))
version_str = re.compile (r'(\s*VALUE "FileVersion", ")([0-9.]*)(")')
rc_encoding = 'utf-16-le'
def get_rc_version ():
with open(rc_path, encoding=rc_encoding) as f:
for line in f:
m = version_str.match (line)
if m:
return m.group (2)
comma_version_str = [
re.compile (r'(\s*VALUE "ProductVersion", ")([0-9,]*)(")'),
re.compile (r'(\s*FILEVERSION\s*)([0-9,]*)()'),
re.compile (r'(\s*PRODUCTVERSION\s*)([0-9,]*)()')
]
def replace_rc_version (version):
source_rc_path = rc_path
target_rc_path = rc_path + '.tmp'
with open(target_rc_path, 'w', encoding='utf-16-le') as fw, open(source_rc_path, 'r', encoding=rc_encoding) as f:
for line in f:
m = version_str.match (line)
if m:
line = re.sub (version_str, r'\g<1>{}\3'.format ('.'.join (version)), line)
for pattern in comma_version_str:
m = pattern.match (line)
if m:
line = re.sub (pattern, r'\g<1>{}\3'.format (','.join (version) + ',0'), line)
fw.write (line)
os.remove (source_rc_path)
os.rename (target_rc_path, source_rc_path)
def zip(src, dst):
zf = zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED)
zf.write(src, os.path.basename(src))
zf.close()
parser = argparse.ArgumentParser()
parser.add_argument('--new-minor', action="store_true", dest="new_minor", help="Deploy new minor version", default=False)
parser.add_argument('--new-major', action="store_true", dest="new_major", help="Deploy new major version", default=False)
parser.add_argument('--update-pm', action="store_true", dest="update_pm", help="Update plugin manager (done automatically if new minor version is set)", default=False)
parser.add_argument('--update-only-rc', action="store_true", dest="update_only_rc", help="Update only resource files if build should be done later", default=False)
parser.add_argument('--add-tag', action="store_true", dest="add_tag", help="Add tag for version", default=False)
parser.add_argument('-v', '--verbose', action="store_true", dest="verbose", help="Verbose output", default=False)
options = parser.parse_args()
ver = get_rc_version ().split ('.')
ver_str = '.'.join (ver)
def create_commit(repo, msg):
index = repo.index
index.add_all ()
index.write ()
config = Config.get_global_config()
author = Signature(config['user.name'], config['user.email'])
commiter = author
tree = index.write_tree()
repo.create_commit ('HEAD', author, commiter, msg, tree, [repo.head.target])
def add_version_commit():
repo = Repository (script_dir)
create_commit (repo, 'Update to {}'.format (ver_str))
config = Config.get_global_config()
author = Signature(config['user.name'], config['user.email'])
def add_version_tag ():
repo = Repository (script_dir)
config = Config.get_global_config()
author = Signature(config['user.name'], config['user.email'])
repo.create_tag('v{}'.format (ver_str), repo.revparse_single('HEAD').id, GIT_OBJ_COMMIT, author, 'v{}'.format (ver_str))
def to_msvc_arch(arch):
if arch == 'x64':
return 'x64'
elif arch == 'x86':
return 'Win32'
else:
raise ValueError('Unexpected architecture')
new_ver_is_added = False
if options.new_minor:
ver[-1]=str (int (ver[-1]) + 1)
new_ver_is_added = True
if options.new_major:
ver = get_rc_version ().split ('.')
ver[-2]=str (int (ver[-2]) + 1)
ver[-1]='0'
new_ver_is_added=True
if new_ver_is_added:
replace_rc_version (ver)
ver_str = '.'.join (ver)
print ('Version increased to {}'.format (ver_str))
add_version_commit()
if options.update_only_rc:
exit(0)
if new_ver_is_added or options.add_tag:
add_version_tag()
x64_binary_path = ''
x86_binary_path = ''
x64_zip_path = ''
x86_zip_path = ''
for arch in ['x64', 'x86']:
dir = 'build-deploy-msvc2022-{}'.format (arch)
FNULL = open(os.devnull, 'w')
if call(['cmake', script_dir, '-A', to_msvc_arch(arch)], stdout= (None if options.verbose else FNULL), cwd=dir) != 0:
print ('Error: Cmake error')
sys.exit (1)
build_args = ['cmake', '--build', dir, '--config', 'RelWithDebInfo']
print ('Building {} version...'.format (arch))
binary_path = os.path.join (dir, 'RelWithDebInfo', 'DSpellCheck.dll')
if arch == 'x64':
x64_binary_path = binary_path
else:
x86_binary_path = binary_path
pdb_path = os.path.join (dir, 'RelWithDebInfo', 'DSpellCheck.pdb')
if call(build_args, stdout= (None if options.verbose else FNULL)) != 0:
print ('Error: Build error')
sys.exit (1)
out_path = os.path.join ('out', str (get_version_number (binary_path)))
target_zip_path = os.path.join (out_path, 'DSpellCheck_{}.zip'.format (arch))
target_pdb_path = os.path.join (out_path,
'DSpellCheck_{}.pdb'.format (arch))
if not os.path.isdir (out_path):
os.makedirs (out_path)
print ('Deploying to {}...'.format (target_zip_path))
if os.path.isfile (target_zip_path):
print ('Error: File {} already exists. Remove it explicitly if you want to overwrite.'
.format (target_zip_path))
sys.exit (1)
zip (binary_path, target_zip_path)
if arch == 'x64':
x64_zip_path = target_zip_path
else:
x86_zip_path = target_zip_path
print ('Copying PDB...')
shutil.copy (pdb_path, target_pdb_path)
if options.new_minor or options.update_pm:
ver = get_rc_version ()
if 'NPP_PLUGIN_LIST_PATH' in os.environ:
plugin_list_path = os.environ['NPP_PLUGIN_LIST_PATH']
print ('Applying change to nppPluginList source directory...')
for t in [('src/pl.x64.json', x64_zip_path, 'x64'), ('src/pl.x86.json', x86_zip_path, 'x86')]:
plugins_json_path = os.path.join (plugin_list_path, t[0])
json_data = None
with open(plugins_json_path, encoding='utf-8') as data_file:
json_data = json.loads(data_file.read ())
plugin_node = None
for node in json_data["npp-plugins"]:
if node["display-name"] == "DSpellCheck":
plugin_node = node
break
plugin_node["version"] = ver
plugin_node["id"] = hashlib.sha256(open(t[1], 'rb').read()).hexdigest()
plugin_node["repository"] = 'https://github.com/Predelnik/DSpellCheck/releases/download/v{}/DSpellCheck_{}.zip'.format (ver, t[2])
with open (plugins_json_path, "w", encoding='utf-8') as fp:
json.dump (json_data, fp, indent='\t', ensure_ascii=False)
fp.write ('\n')
print ('Creating commit in nppPluginList repository...')
create_commit (Repository(plugin_list_path), 'Update DSpellCheck to {}'.format (ver))
else:
print ('%NPP_PLUGIN_LIST_PATH% is not set up, nothing to update')
successString = 'Success!'
try:
from colorama import init, Fore, Style
init ()
successString = Fore.GREEN + Style.BRIGHT + successString
except:
pass
print (successString)