-
Notifications
You must be signed in to change notification settings - Fork 95
/
dev.py
392 lines (313 loc) · 11.8 KB
/
dev.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""
This is a script to help with the automation of common development tasks.
It requires 'fire' to be installed for the command line automation (i.e.: pip install fire).
Some example commands:
python -m dev set-version 0.0.2
python -m dev check-tag-version
python -m dev vendor-robocorp-ls-core
"""
import os
import sys
import traceback
__file__ = os.path.abspath(__file__)
if not os.path.exists(os.path.join(os.path.abspath("."), "dev.py")):
raise RuntimeError('Please execute commands from the directory containing "dev.py"')
try:
import robocorp_code
except ImportError:
# I.e.: add relative path (the cwd must be the directory containing this file).
sys.path.append("src")
import robocorp_code
robocorp_code.import_robocorp_ls_core()
def _fix_contents_version(contents, version):
import re
contents = re.sub(
r"(version\s*=\s*)\"\d+\.\d+\.\d+", r'\1"%s' % (version,), contents
)
contents = re.sub(
r"(__version__\s*=\s*)\"\d+\.\d+\.\d+", r'\1"%s' % (version,), contents
)
contents = re.sub(
r"(\"version\"\s*:\s*)\"\d+\.\d+\.\d+", r'\1"%s' % (version,), contents
)
return contents
def _fix_rcc_contents_version(contents, version):
import re
assert version.startswith("v"), f'{version} must start with "v"'
# RCC_VERSION = "v11.5.5"
# const RCC_VERSION = "v11.5.5";
contents = re.sub(
r"(RCC_VERSION\s*=\s*)\"v\d+\.\d+\.\d+", r'\1"%s' % (version,), contents
)
return contents
class Dev(object):
def set_version(self, version):
"""
Sets a new version for robocorp-code in all the needed files.
"""
def update_version(version, filepath):
with open(filepath, "r") as stream:
contents = stream.read()
new_contents = _fix_contents_version(contents, version)
assert contents != new_contents
with open(filepath, "w") as stream:
stream.write(new_contents)
update_version(version, os.path.join(".", "package.json"))
update_version(version, os.path.join(".", "pyproject.toml"))
update_version(
version, os.path.join(".", "src", "robocorp_code", "__init__.py")
)
def set_rcc_version(self, version):
"""
Sets the new RCC version to be used.
"""
def update_version(version, filepath):
with open(filepath, "r") as stream:
contents = stream.read()
new_contents = _fix_rcc_contents_version(contents, version)
assert (
contents != new_contents
), "Nothing changed after applying new version."
with open(filepath, "w") as stream:
stream.write(new_contents)
update_version(version, os.path.join(".", "src", "robocorp_code", "rcc.py"))
update_version(version, os.path.join(".", "vscode-client", "src", "rcc.ts"))
print(
f"New RCC version set.\nErase the rcc executable from {os.path.abspath(os.path.join('.', 'bin'))} to re-download locally."
)
def get_tag(self):
import subprocess
# i.e.: Gets the last tagged version
cmd = "git describe --tags --abbrev=0 --match robocorp-code*".split()
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, stderr = popen.communicate()
# Something as: b'robocorp-code-0.0.1'
stdout = stdout.decode("utf-8")
stdout = stdout.strip()
return stdout
def check_tag_version(self):
"""
Checks if the current tag matches the latest version (exits with 1 if it
does not match and with 0 if it does match).
"""
import subprocess
version = self.get_tag()
version = version[version.rfind("-") + 1 :]
if robocorp_code.__version__ == version:
sys.stderr.write("Version matches (%s) (exit(0))\n" % (version,))
sys.exit(0)
else:
sys.stderr.write(
"Version does not match (found in sources: %s != tag: %s) (exit(1))\n"
% (robocorp_code.__version__, version)
)
sys.exit(1)
def remove_vendor_robocorp_ls_core(self):
import shutil
import time
vendored_dir = os.path.join(
os.path.dirname(__file__),
"src",
"robocorp_code",
"vendored",
"robocorp_ls_core",
)
try:
shutil.rmtree(vendored_dir)
time.sleep(0.5)
except:
if os.path.exists(vendored_dir):
traceback.print_exc()
return vendored_dir
def vendor_robocorp_ls_core(self):
"""
Vendors robocorp_ls_core into robocorp_code/vendored.
"""
import shutil
src_core = os.path.join(
os.path.dirname(__file__),
"..",
"robocorp-python-ls-core",
"src",
"robocorp_ls_core",
)
vendored_dir = self.remove_vendor_robocorp_ls_core()
print("Copying from: %s to %s" % (src_core, vendored_dir))
shutil.copytree(src_core, vendored_dir)
print("Finished vendoring.")
def codegen(self):
"""
Generates code (to add actions, settings, etc).
In particular, generates the package.json and auxiliary files with
constants in the code.
"""
try:
import codegen_package
except ImportError:
# I.e.: add relative path (the cwd must be the directory containing this file).
sys.path.append("codegen")
import codegen_package
codegen_package.main()
def fix_readme(self):
"""
Updates the links in the README.md to match the current tagged version.
To be called during release.
"""
import re
readme = os.path.join(os.path.dirname(__file__), "README.md")
with open(readme, "r") as f:
content = f.read()
tag = self.get_tag()
if not tag:
raise AssertionError(
"Could not get tag! (are you checking out with full tag history?)"
)
new_content = re.sub(
r"\(docs/",
rf"(https://github.com/robocorp/robotframework-lsp/tree/{tag}/robocorp-code/docs/",
content,
)
new_content = re.sub(
r"\(images/",
rf"(https://raw.githubusercontent.com/robocorp/robotframework-lsp/{tag}/robocorp-code/images/",
content,
)
new_content = new_content.replace(
"Apache 2.0",
"[Robocorp License Agreement (pdf)](https://cdn.robocorp.com/legal/Robocorp-EULA-v1.0.pdf)",
)
assert "apache" not in new_content.lower()
with open(readme, "w") as f:
f.write(new_content)
def generate_license_file(self):
import subprocess
import tempfile
import time
from robocorp_code.rcc import download_rcc
rcc_location = os.path.join(tempfile.mkdtemp(), "rcc.exe")
download_rcc(rcc_location)
time.sleep(0.2)
print(f"Downloaded rcc to: {rcc_location}")
assert os.path.exists(rcc_location)
readme = os.path.join(os.path.dirname(__file__), "LICENSE.txt")
with open(readme, "w") as f:
output = subprocess.check_output([rcc_location, "man", "license"])
decoded = output.decode("utf-8")
assert "Robocorp End User License Agreement" in decoded
assert (
"This EULA is the final, complete and exclusive agreement of the parties"
in decoded
)
f.write(decoded)
def download_rcc(self, plat):
assert plat in ("win32", "linux", "darwin")
import stat
import time
from robocorp_code.rcc import download_rcc
root = os.path.dirname(__file__)
bin_dir = os.path.join(root, "bin")
assert os.path.exists(bin_dir)
rcc_location_win = os.path.join(bin_dir, "rcc.exe")
if os.path.exists(rcc_location_win):
os.chmod(rcc_location_win, stat.S_IWRITE)
os.remove(rcc_location_win)
time.sleep(0.1)
rcc_location_linux_darwin = os.path.join(bin_dir, "rcc")
if os.path.exists(rcc_location_linux_darwin):
os.chmod(rcc_location_linux_darwin, stat.S_IWRITE)
os.remove(rcc_location_linux_darwin)
time.sleep(0.1)
assert not os.path.exists(rcc_location_win)
assert not os.path.exists(rcc_location_linux_darwin)
if plat == "win32":
rcc_location = rcc_location_win
else:
rcc_location = rcc_location_linux_darwin
download_rcc(rcc_location, force=True, sys_platform=plat)
time.sleep(0.2)
print(f"Downloaded rcc to: {rcc_location}")
assert os.path.exists(rcc_location)
def local_install(self):
"""
Packages both Robotframework Language Server and Robocorp Code and installs
them in Visual Studio Code.
"""
import subprocess
print("Making local install")
from pathlib import Path
root = Path(__file__).parent.parent
def run(args, shell=False):
print("---", " ".join(args))
return subprocess.check_call(args, cwd=curdir, shell=shell)
def get_version():
import json
p = Path(curdir / "package.json")
contents = json.loads(p.read_text())
return contents["version"]
print("--- installing RobotFramework Language Server")
curdir = root / "robotframework-ls"
run("python -m dev vendor_robocorp_ls_core".split())
run("vsce package".split(), shell=sys.platform == "win32")
run(
f"code --install-extension robotframework-lsp-{get_version()}.vsix".split(),
shell=sys.platform == "win32",
)
run("python -m dev remove_vendor_robocorp_ls_core".split())
print("\n--- installing Robocorp Code")
curdir = root / "robocorp-code"
run("python -m dev vendor_robocorp_ls_core".split())
run("vsce package".split(), shell=sys.platform == "win32")
run(
f"code --install-extension robocorp-code-{get_version()}.vsix".split(),
shell=sys.platform == "win32",
)
run("python -m dev remove_vendor_robocorp_ls_core".split())
def test_lines():
"""
Check that the replace matches what we expect.
Things we must match:
version="0.0.1"
"version": "0.0.1",
__version__ = "0.0.1"
"""
from robocorp_ls_core.unittest_tools.compare import compare_lines
contents = _fix_contents_version(
"""
version="0.0.198"
version = "0.0.1"
"version": "0.0.1",
"version":"0.0.1",
"version" :"0.0.1",
__version__ = "0.0.1"
""",
"3.7.1",
)
expected = """
version="3.7.1"
version = "3.7.1"
"version": "3.7.1",
"version":"3.7.1",
"version" :"3.7.1",
__version__ = "3.7.1"
"""
compare_lines(contents.splitlines(), expected.splitlines())
if __name__ == "__main__":
TEST = False
if TEST:
test_lines()
else:
try:
import fire
except ImportError:
sys.stderr.write(
'\nError. "fire" library not found.\nPlease install with "pip install fire" (or activate the proper env).\n'
)
else:
# Workaround so that fire always prints the output.
# See: https://github.com/google/python-fire/issues/188
def Display(lines, out):
text = "\n".join(lines) + "\n"
out.write(text)
from fire import core
core.Display = Display
fire.Fire(Dev())