-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-ci.py
354 lines (302 loc) · 12.1 KB
/
run-ci.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
from typing import Dict, List, Optional
import argparse
import os
import re
import shutil
import sys
import tempfile
import tankerci
import tankerci.conan
from tankerci.conan import TankerSource
import tankerci.context
import tankerci.cpp
import tankerci.gcp
import tankerci.git
import tankerci.gitlab
import cli_ui as ui
from path import Path
PROFILES = [
"ios-armv7-release",
"ios-armv7s-release",
"ios-armv8-release",
"ios-x86-release",
"ios-x86_64-release",
]
def _copy_folder_content(src_path: Path, dest_path: Path) -> None:
ui.info_1("Moving content of", src_path, "to", dest_path)
for src_dir in src_path.dirs():
dest_dir = dest_path / src_dir.basename()
dest_dir.rmtree_p()
ui.info_2(src_dir, "->", dest_dir)
src_dir.copytree(dest_dir)
for src_file in src_path.files():
dest_file = dest_path / src_file.basename()
dest_file.remove_p()
ui.info_2(src_file, "->", dest_file)
src_file.copy2(dest_file)
class Builder:
def __init__(self, *, src_path: Path, profiles: List[str]):
self.src_path = src_path
self.pod_path = self.src_path / "Tanker"
self.conan_path = self.pod_path / "conan"
self.libraries_path = self.pod_path / "Libraries"
self.headers_path = self.pod_path / "Headers"
self.example_path = self.pod_path / "Example"
self.profiles = profiles
def generate_podspec(self) -> None:
static_libs = self.get_static_libs()
in_path = self.src_path / "Tanker/Tanker.in.podspec"
contents = in_path.text()
link_flags = [
f"-l{x.name[3:-2]}" for x in static_libs
] # strip 'lib' prefix and '.a' suffix
contents = contents.replace("@static_libs_link_flags@", " ".join(link_flags))
out_path = self.pod_path / "Tanker.podspec"
out_path.write_text(contents)
ui.info_2("Generated", out_path)
def get_static_libs(self) -> List[Path]:
libs_path = self.libraries_path
return libs_path.glob("*.a") # type: ignore
def get_build_path(self, profile: str) -> Path:
return self.conan_path / profile
def get_all_dependency_libs(self) -> Dict[str, List[Path]]:
all_libs: Dict[str, List[Path]] = dict()
for profile in self.profiles:
deps = tankerci.conan.get_dependencies_libs(
self.get_build_path(profile) / "conanbuildinfo.json"
)
for _, libs in deps.items():
if not libs:
continue
for lib in libs:
all_libs.setdefault(lib.name, []).extend([lib])
return all_libs
def generate_fat_libraries(self) -> None:
ui.info_1("Generating fat libraries")
self.libraries_path.rmtree_p()
self.libraries_path.makedirs_p()
for lib_name, libs in self.get_all_dependency_libs().items():
output = self.libraries_path / lib_name
tankerci.run(
"lipo", "-create", "-output", output, *libs, cwd=self.conan_path
)
def copy_headers(self) -> None:
first_profile = list(self.profiles)[0]
# we assume that all profiles have the same includes
conan_info = self.get_build_path(first_profile) / "conanbuildinfo.json"
include_paths = tankerci.conan.get_dependencies_include_paths(conan_info)
for src_include_path in include_paths["tanker"]:
_copy_folder_content(src_include_path, self.headers_path)
def handle_sdk_deps(self, *, tanker_source: TankerSource) -> None:
ui.info_1("copying sdk-native for profiles: ", self.profiles)
self.generate_fat_libraries()
self.copy_headers()
def handle_ios_deps(self) -> None:
ui.info_2("Installing Tanker pod dependencies")
tankerci.run("pod", "deintegrate", cwd=self.example_path)
tankerci.run("pod", "install", "--repo-update", cwd=self.example_path)
def build_and_test_pod(self) -> None:
ui.info_2("building pod and launching tests")
tankerci.run(
"pod",
"lib",
"lint",
"--verbose",
"--allow-warnings",
self.pod_path / "Tanker.podspec",
)
class PodPublisher:
def __init__(self, *, src_path: Path) -> None:
self.src_path = src_path
self.dest_path = self.src_path / "artifacts"
self.dest_path.rmtree_p()
def copy_static_libs(self) -> None:
ui.info_1("Copying static libs")
libraries_path = self.src_path / "Tanker/Libraries"
dest_path = self.dest_path / "Libraries"
ui.info_2(libraries_path, "->", dest_path)
libraries_path.copytree(dest_path)
def copy_sources(self) -> None:
ui.info_1("Copying sources")
sources_path = self.src_path / "Tanker/Sources"
ui.info_2(sources_path, "->", self.dest_path)
sources_path.copytree(self.dest_path / "Sources")
export_list_src = self.src_path / "Tanker/export_symbols.list"
export_list_dest = self.dest_path / "export_symbols.list"
ui.info_2(export_list_src, "->", export_list_dest)
export_list_src.copy(export_list_dest)
def copy_top_files(self) -> None:
ui.info_1("Copying top files")
for name in ("Tanker/LICENSE", "Tanker/Tanker.podspec"):
src = self.src_path / name
ui.info_2(src, "->", self.dest_path)
src.copy(self.dest_path)
def copy_headers(self) -> None:
ui.info_1("Copying headers")
headers_path = self.src_path / "Tanker" / "Headers"
dest_path = self.dest_path / "Headers"
ui.info_2(headers_path, "->", dest_path)
headers_path.copytree(dest_path)
def copy_test_sources(self) -> None:
# trick cocoapods copy the Dummy.m to avoid error during validation
ui.info_1("Copying dummy test file")
dummy_test_path = self.src_path / "Tanker/Tests/Dummy.m"
dest_path = self.dest_path / "Tests"
dest_path.makedirs_p()
ui.info_2(dummy_test_path, "->", dest_path)
dummy_test_path.copy(dest_path)
def get_version_from_spec(self) -> str:
contents = (self.src_path / "Tanker/Tanker.podspec").text()
for line in contents.splitlines():
match = re.match(r"^\s+s\.version\s+=\s+'(.*?)'", line)
if match:
return match.groups()[0] # type: ignore
sys.exit("Could not find version from Tanker.podspec")
def generate_archive(self) -> Path:
version = self.get_version_from_spec()
ui.info_1("Generating archive, version:", version)
archive_name = "tanker-ios-sdk-%s.tar.gz" % version
with self.dest_path:
tankerci.run("tar cfvz %s *" % archive_name, shell=True)
shutil.copy(archive_name, self.src_path)
res = self.src_path / archive_name
ui.info_2("Generated", res)
return res
def upload_archive(self, archive_path: Path) -> None:
tankerci.gcp.GcpProject("tanker-prod").auth()
tankerci.run("gsutil", "cp", archive_path, "gs://cocoapods.tanker.io/ios/")
def build_pod(self) -> None:
# fmt: off
tankerci.run(
"pod", "spec", "lint", "Tanker/Tanker.podspec",
"--verbose",
"--allow-warnings",
"--skip-tests",
cwd=self.src_path,
)
# fmt: on
def publish_pod(self) -> None:
# fmt: off
tankerci.run(
"pod", "repo", "push", "tanker", "Tanker/Tanker.podspec",
"--skip-tests",
"--verbose",
"--allow-warnings",
cwd=self.src_path,
)
# fmt: on
def publish(self) -> None:
with tempfile.TemporaryDirectory() as tmpdir:
self.dest_path = Path(tmpdir)
self.copy_top_files()
self.copy_static_libs()
self.copy_sources()
self.copy_headers()
self.copy_test_sources()
archive = self.generate_archive()
self.upload_archive(archive)
self.build_pod()
self.publish_pod()
def prepare(
tanker_source: TankerSource, update: bool, tanker_ref: Optional[str]
) -> Builder:
artifact_path = Path.getcwd() / "package"
tanker_deployed_ref = tanker_ref
if tanker_source == TankerSource.UPSTREAM:
profiles = [d.basename() for d in artifact_path.dirs()]
else:
profiles = PROFILES
if tanker_source == TankerSource.DEPLOYED and not tanker_deployed_ref:
tanker_deployed_ref = "tanker/latest-stable@"
tankerci.conan.install_tanker_source(
tanker_source,
output_path=Path("Tanker/conan"),
profiles=profiles,
update=update,
tanker_deployed_ref=tanker_deployed_ref,
)
builder = Builder(src_path=Path.getcwd(), profiles=profiles)
builder.handle_sdk_deps(tanker_source=tanker_source)
builder.generate_podspec()
builder.handle_ios_deps()
return builder
def build_and_test(
*, tanker_source: TankerSource, tanker_ref: Optional[str] = None
) -> None:
builder = prepare(tanker_source, False, tanker_ref)
builder.build_and_test_pod()
def deploy(*, version: str, tanker_ref: str) -> None:
tankerci.bump_files(version)
build_and_test(tanker_source=TankerSource.DEPLOYED, tanker_ref=tanker_ref)
src_path = Path.getcwd()
pod_publisher = PodPublisher(src_path=src_path)
pod_publisher.publish()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--isolate-conan-user-home",
action="store_true",
dest="home_isolation",
default=False,
)
subparsers = parser.add_subparsers(title="subcommands", dest="command")
reset_branch_parser = subparsers.add_parser("reset-branch")
reset_branch_parser.add_argument("branch")
download_artifacts_parser = subparsers.add_parser("download-artifacts")
download_artifacts_parser.add_argument("--project-id", required=True)
download_artifacts_parser.add_argument("--pipeline-id", required=True)
download_artifacts_parser.add_argument("--job-name", required=True)
build_and_test_parser = subparsers.add_parser("build-and-test")
build_and_test_parser.add_argument(
"--use-tanker",
type=tankerci.conan.TankerSource,
default=tankerci.conan.TankerSource.EDITABLE,
dest="tanker_source",
)
build_and_test_parser.add_argument("--tanker-ref")
prepare_parser = subparsers.add_parser("prepare")
prepare_parser.add_argument(
"--use-tanker",
type=tankerci.conan.TankerSource,
default=tankerci.conan.TankerSource.EDITABLE,
dest="tanker_source",
)
prepare_parser.add_argument("--tanker-ref")
prepare_parser.add_argument(
"--update", action="store_true", default=False, dest="update",
)
deploy_parser = subparsers.add_parser("deploy")
deploy_parser.add_argument("--version", required=True)
deploy_parser.add_argument("--tanker-ref", required=True)
subparsers.add_parser("mirror")
args = parser.parse_args()
if args.home_isolation:
tankerci.conan.set_home_isolation()
tankerci.conan.update_config()
if args.command == "build-and-test":
build_and_test(
tanker_source=args.tanker_source, tanker_ref=args.tanker_ref,
)
elif args.command == "prepare":
prepare(args.tanker_source, args.update, args.tanker_ref)
elif args.command == "deploy":
deploy(version=args.version, tanker_ref=args.tanker_ref)
elif args.command == "mirror":
tankerci.git.mirror(github_url="[email protected]:TankerHQ/sdk-ios")
elif args.command == "reset-branch":
fallback = os.environ["CI_COMMIT_REF_NAME"]
ref = tankerci.git.find_ref(
Path.getcwd(), [f"origin/{args.branch}", f"origin/{fallback}"]
)
tankerci.git.reset(Path.getcwd(), ref)
elif args.command == "download-artifacts":
tankerci.gitlab.download_artifacts(
project_id=args.project_id,
pipeline_id=args.pipeline_id,
job_name=args.job_name,
)
else:
parser.print_help()
sys.exit()
if __name__ == "__main__":
main()