-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscripts.py
More file actions
executable file
·519 lines (454 loc) · 18.4 KB
/
Copy pathscripts.py
File metadata and controls
executable file
·519 lines (454 loc) · 18.4 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/usr/bin/env python3
"""This script provides a command-line interface for building, testing, and managing the Flutter and Rust parts of the project."""
import subprocess
import argparse
import platform
import os
from typing import List
DART_FRB = "lib/src/rust/frb_generated.dart"
DART_FRB_IO = "lib/src/rust/frb_generated.io.dart"
DART_FRB_WEB = "lib/src/rust/frb_generated.web.dart"
RUST_FRB_IO = "rust/src/frb_generated.io.rs"
RUST_FRB = "rust/src/frb_generated.rs"
RUST_FRB_WEB = "rust/src/frb_generated.web.rs"
FRB_FILES = [DART_FRB, DART_FRB_IO, DART_FRB_WEB, RUST_FRB_IO, RUST_FRB, RUST_FRB_WEB]
DMG_CONFIG = "packages/config.json"
OUTPUT_DMG = "packages/mdd.dmg"
FRB_INSTALL_NAME = "flutter_rust_bridge_codegen@^2.0.0-dev.0"
IOS_PODS_FILES = "ios/Podfile ios/Podfile.lock ios/Pods/"
MACOS_PODS_FILES = "macos/Podfile macos/Podfile.lock macos/Pods/"
class Build:
"""Build commands for the project."""
def __init__(self):
"""Initialize the Build class."""
pass
def build_all(self) -> None:
"""Build the project for all supported platforms."""
os_name: str = platform.system()
print(f"Building for all platforms on {os_name}...")
try:
if platform.system() == "Windows":
self.build_windows()
elif platform.system() == "Darwin":
self.build_android()
self.build_ios()
self.build_macos()
elif platform.system() == "Linux":
self.build_linux()
else:
print("Unsupported platform")
except Exception as e:
print(f"Error building project for all platforms: {str(e)}")
def build_android(self) -> None:
"""Build the project for Android."""
print("Building for Android...")
try:
self.build_bundle()
self.build_apk()
print("Project built successfully\n")
except Exception as e:
print("Error building project for android:", str(e))
def build_apk(self) -> None:
"""Build the Android APK."""
print("Building apk for Android...")
try:
subprocess.run(["flutter", "build", "apk", "--release", "--split-per-abi"])
print("Project built successfully\n")
except Exception as e:
print("Error building project for android:", str(e))
def build_bundle(self) -> None:
"""Build the Android App Bundle."""
print("Building appbundle for Android...")
try:
subprocess.run(["flutter", "build", "appbundle", "--release"])
print("Project built successfully\n")
except Exception as e:
print("Error building project for android:", str(e))
def build_ios(self) -> None:
"""Build the project for iOS."""
print("Building for iOS...")
try:
subprocess.run(["flutter", "build", "ios", "--release"])
print("Project built successfully\n")
except Exception as e:
print("Error building project for ios:", str(e))
def build_macos(self) -> None:
"""Build the project for macOS."""
print("Building for macOS...")
try:
subprocess.run(["flutter", "build", "macos", "--release"])
print("Project built successfully\n")
self._remove_dmg()
self._create_dmg()
self._open_dmg()
except Exception as e:
print("Error building project for macos:", str(e))
def build_linux(self) -> None:
"""Build the project for Linux."""
print("Building for Linux...")
try:
subprocess.run(["flutter", "build", "linux", "--release"])
print("Project built successfully\n")
except Exception as e:
print("Error building project for linux:", str(e))
def build_windows(self) -> None:
"""Build the project for Windows."""
print("Building for Windows...")
try:
subprocess.run(["flutter", "build", "windows", "--release"], shell=True)
print("Project built successfully\n")
except Exception as e:
print("Error building project for windows:", str(e))
return
def _create_dmg(self) -> None:
"""Create a DMG file for the macOS build."""
print("Creating dmg...")
try:
subprocess.run(["appdmg", DMG_CONFIG, OUTPUT_DMG])
print("Dmg created successfully\n")
except Exception as e:
print("Error creating dmg:", str(e))
def _remove_dmg(self) -> None:
"""Remove the DMG file if it exists."""
print("Removing dmg...")
try:
if os.path.exists(OUTPUT_DMG):
os.remove(OUTPUT_DMG)
print("Dmg removed successfully\n")
except OSError as e:
print("Error removing dmg:", str(e))
def _open_dmg(self) -> None:
"""Open the DMG file."""
print("Opening dmg...")
try:
subprocess.run(["open", OUTPUT_DMG])
print("Dmg opened successfully\n")
except Exception as e:
print("Error opening dmg:", str(e))
class BuildRust:
"""Build commands for the Rust part of the project."""
def __init__(self):
"""Initialize the BuildRust class."""
pass
def install_frb_executable(self) -> None:
"""Install the flutter_rust_bridge executable."""
print("Installing frb executable...")
try:
subprocess.run(["cargo", "install", FRB_INSTALL_NAME])
print("Frb executable installed successfully\n")
except Exception as e:
print("Error installing frb executable:", str(e))
return
def generate_frb_code(self, is_clean: bool) -> None:
"""Generate the frb code.
Args:
is_clean (bool): Whether to remove old frb code before generating.
"""
print("Generating frb code...")
try:
if is_clean:
self.remove_old_frb_code()
subprocess.run(["flutter_rust_bridge_codegen", "generate"])
print("Rust code generated successfully\n")
except Exception as e:
print("Error generating frb code:", str(e))
return
def clean_frb_files(self) -> None:
"""Clean the frb files."""
print("Cleaning frb code...")
try:
self.remove_old_frb_code()
subprocess.run(["rm", "-rf", "rust_builder"])
subprocess.run(["rm", "-rf", "flutter_rust_bridge.yaml"])
subprocess.run(["rm", "-rf", "integration_test"])
print("Frb code cleaned successfully\n")
except Exception as e:
print("Error cleaning frb code:", str(e))
return
def update_frb_executable(self) -> None:
"""Update the frb executable."""
print("Updating frb executable...")
try:
subprocess.run(["cargo", "install", FRB_INSTALL_NAME])
self.update_rust_dependencies()
print("Frb executable updated successfully\n")
except Exception as e:
print("Error updating frb executable:", str(e))
return
def remove_old_frb_code(self) -> None:
"""Remove old frb code."""
print("Removing old frb code...")
try:
for file in FRB_FILES:
if os.path.exists(file):
os.remove(file)
print(f"Removed {file}")
print("Old frb code removed successfully\n")
except Exception as e:
print("Error removing old frb code:", str(e))
return
def check_rust_dependencies(self) -> None:
"""Check the Rust dependencies."""
print("Checking rust dependencies...")
try:
subprocess.run(["cargo", "check"], cwd="rust")
print("Rust dependencies checked successfully\n")
except Exception as e:
print("Error checking rust dependencies:", str(e))
return
def update_rust_dependencies(self) -> None:
"""Update the Rust dependencies."""
print("Updating rust dependencies...")
try:
subprocess.run(["cargo", "update"], cwd="rust")
print("Rust dependencies updated successfully\n")
except Exception as e:
print("Error updating rust dependencies:", str(e))
return
class FlutterUtils:
"""Utilities for the Flutter project."""
def __init__(self):
"""Initialize the FlutterUtils class."""
pass
def clean_project(self) -> None:
"""Clean the Flutter project."""
print("Cleaning project...")
try:
subprocess.run(["flutter", "clean"])
print("Project cleaned successfully\n")
except Exception as e:
print("Error cleaning project:", str(e))
return
def fix_dart_code(self) -> None:
"""Fix the Dart code."""
print("Fixing dart code...")
try:
subprocess.run(["dart", "fix", "--apply"])
print("Dart code fixed successfully\n")
except Exception as e:
print("Error fixing dart code:", str(e))
return
def update_flutter_dependencies(self) -> None:
"""Update the Flutter dependencies."""
print("Updating flutter dependencies...")
try:
subprocess.run(["flutter", "pub", "upgrade"])
print("Flutter dependencies updated successfully\n")
except Exception as e:
print("Error updating flutter dependencies:", str(e))
return
def clean_pods(self) -> None:
"""Clean the iOS and macOS pods."""
print("Cleaning pods...")
try:
subprocess.run(["flutter", "clean"])
subprocess.run(["rm", "-rf", IOS_PODS_FILES])
subprocess.run(["rm", "-rf", MACOS_PODS_FILES])
subprocess.run(["rm", "-rf", "ios/.symlinks"])
subprocess.run(["rm", "-rf", "ios/Flutter/Flutter.framework"])
subprocess.run(["rm", "-rf", "macos/Flutter/Flutter.framework"])
subprocess.run(["rm", "-rf", "macos/Flutter/Flutter.podspec"])
print("Pods cleaned successfully\n")
except Exception as e:
print("Error cleaning pods:", str(e))
return
class BuildDocs:
"""Build commands for the documentation."""
def __init__(self):
"""Initialize the BuildDocs class."""
pass
def run(self) -> None:
"""Run the documentation website."""
print("Building documentation...")
command: List[str] = ["yarn", "start"]
try:
if platform.system() == "Windows":
subprocess.run(command, cwd="website", shell=True)
else:
subprocess.run(command, cwd="website")
print("Documentation built successfully\n")
except Exception as e:
print("Error building documentation:", str(e))
return
def build(self) -> None:
"""Build the documentation website."""
print("Building documentation...")
command: List[str] = ["yarn", "build"]
try:
if platform.system() == "Windows":
subprocess.run(command, cwd="website", shell=True)
else:
subprocess.run(command, cwd="website")
print("Documentation built successfully\n")
except Exception as e:
print("Error building documentation:", str(e))
return
def upgrade(self) -> None:
"""Upgrade the documentation dependencies."""
print("Upgrading documentation...")
command: List[str] = [
"yarn",
"upgrade",
"@docusaurus/core@latest",
"@docusaurus/preset-classic@latest",
"@docusaurus/module-type-aliases@latest",
"@docusaurus/tsconfig@latest",
"@docusaurus/types@latest",
]
try:
if platform.system() == "Windows":
subprocess.run(command, cwd="website", shell=True)
else:
subprocess.run(command, cwd="website")
print("Documentation upgraded successfully\n")
except Exception as e:
print("Error upgrading documentation:", str(e))
return
class Args:
"""Argument parser for the script."""
def __init__(self):
"""Initialize the Args class."""
pass
def get_args(self) -> argparse.Namespace:
"""Get the arguments from the command line."""
parser = argparse.ArgumentParser(
description="Various build scripts for the project"
)
subparsers = parser.add_subparsers(
dest="command", help="Commands for build scripts"
)
self.get_flutter_build_args(subparsers)
self.get_flutter_utils_args(subparsers)
self.get_rust_build_args(subparsers)
self.get_doc_build_args(subparsers)
return parser.parse_args()
def get_flutter_build_args(self, args: argparse.Namespace) -> None:
"""Get the arguments for the Flutter build commands."""
parser = args.add_parser("build", help="Build project")
parser.add_argument("--all", action="store_true", help="Build all platforms")
parser.add_argument("--android", action="store_true", help="Build android")
parser.add_argument("--apk", action="store_true", help="Build apk")
parser.add_argument("--bundle", action="store_true", help="Build bundle")
parser.add_argument("--ios", action="store_true", help="Build ios")
parser.add_argument("--linux", action="store_true", help="Build linux")
parser.add_argument("--macos", action="store_true", help="Build macos")
parser.add_argument("--windows", action="store_true", help="Build windows")
def get_flutter_utils_args(self, args: argparse.Namespace) -> None:
"""Get the arguments for the Flutter utility commands."""
parser = args.add_parser("utils", help="Utilities for Flutter project")
parser.add_argument("--clean", action="store_true", help="Clean project")
parser.add_argument("--fix", action="store_true", help="Fix dart code")
parser.add_argument(
"--update", action="store_true", help="Update flutter dependencies"
)
parser.add_argument("--clean-pods", action="store_true", help="Clean pods")
def get_rust_build_args(self, args: argparse.Namespace) -> None:
"""Get the arguments for the Rust build commands."""
parser = args.add_parser("frb", help="Build options for Rust project")
parser.add_argument("--generate", action="store_true", help="Generate frb code")
parser.add_argument(
"--check", action="store_true", help="Check rust dependencies"
)
parser.add_argument(
"--update", action="store_true", help="Update rust dependencies"
)
parser.add_argument(
"--upgrade", action="store_true", help="Upgrade frb executable"
)
parser.add_argument("--clean", action="store_true", help="Clean project")
parser.add_argument("--clean-all", action="store_true", help="Clean FRB files")
parser.add_argument("--install", action="store_true", help="Install frb executable")
def get_doc_build_args(self, args: argparse.Namespace) -> None:
"""Get the arguments for the documentation build commands."""
parser = args.add_parser("docs", help="Build documentation")
parser.add_argument("--run", action="store_true", help="Run documentation")
parser.add_argument("--build", action="store_true", help="Build documentation")
parser.add_argument(
"--upgrade", action="store_true", help="Upgrade documentation"
)
class Parser:
"""Parser for the script arguments."""
def __init__(self, args: argparse.Namespace):
"""Initialize the Parser class."""
self.args = args
def parse_build_args(self) -> None:
"""Parse the build arguments."""
build = Build()
if self.args.android:
build.build_android()
elif self.args.apk:
build.build_apk()
elif self.args.bundle:
build.build_bundle()
elif self.args.ios:
build.build_ios()
elif self.args.linux:
build.build_linux()
elif self.args.macos:
build.build_macos()
elif self.args.windows:
build.build_windows()
elif self.args.all:
build.build_all()
else:
print("No platform selected")
def parse_flutter_utils_args(self) -> None:
"""Parse the Flutter utility arguments."""
utils = FlutterUtils()
if self.args.clean:
utils.clean_project()
elif self.args.fix:
utils.fix_dart_code()
elif self.args.update:
utils.update_flutter_dependencies()
elif self.args.clean_pods:
utils.clean_pods()
else:
print("No utility option selected")
return
def parse_rust_build_args(self) -> None:
"""Parse the Rust build arguments."""
rust = BuildRust()
if self.args.generate:
rust.generate_frb_code(self.args.clean)
elif self.args.check:
rust.check_rust_dependencies()
elif self.args.update:
rust.update_rust_dependencies()
elif self.args.upgrade:
rust.update_frb_executable()
elif self.args.install:
rust.install_frb_executable()
elif self.args.clean_all:
rust.clean_frb_files()
else:
print("No build option selected")
return
def parse_doc_build_args(self) -> None:
"""Parse the documentation build arguments."""
docs = BuildDocs()
if self.args.run:
docs.run()
elif self.args.build:
docs.build()
elif self.args.upgrade:
docs.upgrade()
else:
print("No documentation option selected")
return
def main() -> None:
"""Main function for the script."""
args = Args().get_args()
parser = Parser(args)
if args.command == "build":
parser.parse_build_args()
elif args.command == "utils":
parser.parse_flutter_utils_args()
elif args.command == "frb":
parser.parse_rust_build_args()
elif args.command == "docs":
parser.parse_doc_build_args()
else:
print("No command selected")
if __name__ == "__main__":
main()