forked from robocorp/robotframework-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.py
More file actions
176 lines (138 loc) · 4.72 KB
/
dev.py
File metadata and controls
176 lines (138 loc) · 4.72 KB
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
"""
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 sys
import os
__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"')
import fire
try:
import example_vscode
except ImportError:
# I.e.: add relative path (the cwd must be the directory containing this file).
sys.path.append("src")
import example_vscode
example_vscode.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
class Dev(object):
def set_version(self, version):
"""
Sets a new version for robotframework-lsp 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)
if 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(".", "src", "setup.py"))
update_version(
version, os.path.join(".", "src", "example_vscode", "__init__.py")
)
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
# i.e.: Gets the last tagged version
cmd = "git describe --tags --abbrev=0".split()
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, stderr = popen.communicate()
# Something as: b'robotframework-lsp-0.0.1'
if sys.version_info[0] >= 3:
stdout = stdout.decode("utf-8")
version = stdout.strip()
version = version[version.rfind("-") + 1 :]
if example_vscode.__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"
% (example_vscode.__version__, version)
)
sys.exit(1)
def vendor_robocorp_ls_core(self):
"""
Vendors robocorp_ls_core into example_vscode/vendored.
"""
import shutil
src_core = os.path.join(
os.path.dirname(__file__),
"..",
"robocorp-python-ls-core",
"src",
"robocorp_ls_core",
)
vendored_dir = os.path.join(
os.path.dirname(__file__),
"src",
"example_vscode",
"vendored",
"robocorp_ls_core",
)
print("Copying from: %s to %s" % (src_core, vendored_dir))
shutil.copytree(src_core, vendored_dir)
print("Finished vendoring.")
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:
# 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())