-
Notifications
You must be signed in to change notification settings - Fork 9
/
plugin_tool.py
221 lines (168 loc) · 8.79 KB
/
plugin_tool.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
import argparse, re, os, shutil, sys
FRB_VERSION = "2.7.0"
PACKAGE_NAME = "simple_audio_flutter"
parser = argparse.ArgumentParser(
usage="Put this file in your root project directory and execute the commands.",
description="A tool to help with building a Flutter plugin with Rust."
)
parser.add_argument(
"-c", "--code-gen",
action="store_true",
help="Generates the FFI bridge using flutter_rust_bridge."
)
parser.add_argument(
"-b", "--build",
choices=["android", "linux", "windows", "ios", "macos"],
nargs="*",
help="Builds the Rust code. This will have to be run on Linux, Windows, and macOS if you want to target all platforms."
)
parser.add_argument(
"--bump-version",
type=str,
help="Bumps the version of the plugin to the given version. Ex. \"1.0.0\""
)
def code_gen():
print("Generating code with flutter_rust_bridge...\n")
pubspec = open("simple_audio_flutter/pubspec.yaml", "r+")
lines = pubspec.readlines()
for line in range(0, len(lines)):
if "# rust_lib_simple_audio" not in lines[line]:
continue
lines[line] = " rust_lib_simple_audio:\n"
lines[line + 1] = " path: rust_builder\n"
pubspec.seek(0)
pubspec.writelines(lines)
pubspec.truncate()
break
pubspec.close()
os.system(f"cargo install flutter_rust_bridge_codegen --version {FRB_VERSION}")
os.system("cargo install cargo-expand --version 1.0.70")
os.system("flutter_rust_bridge_codegen generate --config-file simple_audio_flutter/flutter_rust_bridge.yaml")
pubspec = open("simple_audio_flutter/pubspec.yaml", "r+")
lines = pubspec.readlines()
for line in range(0, len(lines)):
if "rust_lib_simple_audio" not in lines[line]:
continue
lines[line] = " # rust_lib_simple_audio:\n"
lines[line + 1] = " # path: rust_builder\n"
pubspec.seek(0)
pubspec.writelines(lines)
pubspec.truncate()
break
pubspec.close()
def build(targets: list[str]):
print("Building Rust code...\n")
is_linux = sys.platform == "linux"
is_windows = sys.platform == "win32"
is_mac = sys.platform == "darwin"
if (is_linux or is_windows or is_mac) and "android" in targets:
print("Building Android libraries...\n")
os.system("rustup target add aarch64-linux-android armv7-linux-androideabi x86_64-linux-android i686-linux-android")
os.system("cargo install cargo-ndk")
architectures = ["arm64-v8a", "armeabi-v7a", "x86", "x86_64"]
for architecture in architectures:
path = f"{PACKAGE_NAME}/android/src/main/jniLibs/{architecture}/lib{PACKAGE_NAME}.so"
if os.path.exists(path):
os.remove(path)
result = os.system(f"cargo ndk -t arm64-v8a -t armeabi-v7a -t x86 -t x86_64 -o {PACKAGE_NAME}/android/src/main/jniLibs build --release")
assert result == 0
if is_linux and "linux" in targets:
print("Building Linux libraries...\n")
os.system("rustup target add x86_64-unknown-linux-gnu")
result = os.system("cargo build --release --target x86_64-unknown-linux-gnu")
assert result == 0
if os.path.exists(f"{PACKAGE_NAME}/linux/lib{PACKAGE_NAME}.so"):
os.remove(f"{PACKAGE_NAME}/linux/lib{PACKAGE_NAME}.so")
shutil.move(f"target/x86_64-unknown-linux-gnu/release/lib{PACKAGE_NAME}.so", f"{PACKAGE_NAME}/linux")
if is_windows and "windows" in targets:
print("Building Windows libraries...\n")
os.system("rustup target add x86_64-pc-windows-msvc")
result = os.system("cargo build --release --target x86_64-pc-windows-msvc")
assert result == 0
if os.path.exists(f"{PACKAGE_NAME}/windows/{PACKAGE_NAME}.dll"):
os.remove(f"{PACKAGE_NAME}/windows/{PACKAGE_NAME}.dll")
shutil.move(f"target/x86_64-pc-windows-msvc/release/{PACKAGE_NAME}.dll", f"{PACKAGE_NAME}/windows")
if is_mac:
if "macos" in targets:
print("Building macOS libraries...\n")
# Build for macOS.
os.system("rustup target add aarch64-apple-darwin x86_64-apple-darwin")
result = os.system("cargo build --release --target aarch64-apple-darwin")
assert result == 0
result = os.system("cargo build --release --target x86_64-apple-darwin")
assert result == 0
os.system(f'lipo "target/aarch64-apple-darwin/release/lib{PACKAGE_NAME}.a" "target/x86_64-apple-darwin/release/lib{PACKAGE_NAME}.a" -output "lib{PACKAGE_NAME}.a" -create')
if os.path.exists(f"{PACKAGE_NAME}/macos/Libs/lib{PACKAGE_NAME}.a"):
os.remove(f"{PACKAGE_NAME}/macos/Libs/lib{PACKAGE_NAME}.a")
os.makedirs(f"{PACKAGE_NAME}/macos/Libs", exist_ok=True)
shutil.move(f"./lib{PACKAGE_NAME}.a", f"{PACKAGE_NAME}/macos/Libs")
if "ios" in targets:
# Build for iOS
print("Building iOS libraries...\n")
os.system("rustup target add aarch64-apple-ios aarch64-apple-ios-sim x86_64-apple-ios")
result = os.system("IPHONEOS_DEPLOYMENT_TARGET=10.0 cargo build --release --target aarch64-apple-ios")
assert result == 0
result = os.system(f"cargo build --release --target aarch64-apple-ios-sim")
assert result == 0
result = os.system("CMAKE_OSX_SYSROOT=$(xcrun --sdk iphonesimulator --show-sdk-path) cargo build --release --target x86_64-apple-ios")
assert result == 0
os.system(f'lipo "target/aarch64-apple-ios-sim/release/lib{PACKAGE_NAME}.a" "target/x86_64-apple-ios/release/lib{PACKAGE_NAME}.a" -output "lib{PACKAGE_NAME}.a" -create')
os.system(f"xcodebuild -create-xcframework -library target/aarch64-apple-ios/release/lib{PACKAGE_NAME}.a -library ./lib{PACKAGE_NAME}.a -output {PACKAGE_NAME}.xcframework")
os.remove(f"./lib{PACKAGE_NAME}.a")
if os.path.exists(f"{PACKAGE_NAME}/ios/Frameworks/{PACKAGE_NAME}.xcframework"):
shutil.rmtree(f"{PACKAGE_NAME}/ios/Frameworks/{PACKAGE_NAME}.xcframework")
os.makedirs(f"{PACKAGE_NAME}/ios/Frameworks", exist_ok=True)
shutil.move(f"./{PACKAGE_NAME}.xcframework", f"{PACKAGE_NAME}/ios/Frameworks")
def bump_version(version: str):
def replace_string_in_file(file, regex, replacement):
new_text = re.sub(regex, replacement, file.read(), count=1)
file.seek(0)
file.write(new_text)
file.seek(0)
# pubspec.yaml
pubspec = open("simple_audio_flutter/pubspec.yaml", "r+")
replace_string_in_file(pubspec, r"version: [\d|\.]+\s", f"version: {version}\n")
pubspec.close()
# Cargo.toml
cargo = open("simple_audio_flutter/rust/Cargo.toml", "r+")
replace_string_in_file(cargo, r'version = "[\d|\.]+"\s', f'version = "{version}"\n')
cargo.close()
# Cargo.toml in main Rust project
cargo = open("simple_audio/Cargo.toml", "r+")
replace_string_in_file(cargo, r'version = "[\d|\.]+"\s', f'version = "{version}"\n')
cargo.close()
# Android CMake
android_cmake = open("simple_audio_flutter/android/CMakeLists.txt", "r+")
replace_string_in_file(android_cmake, r'set\(Version "[\d|\.]+"\)\s', f'set(Version "{version}")\n')
android_cmake.close()
# Linux CMake
linux_cmake = open("simple_audio_flutter/linux/CMakeLists.txt", "r+")
replace_string_in_file(linux_cmake, r'set\(Version "[\d|\.]+"\)\s', f'set(Version "{version}")\n')
linux_cmake.close()
# Windows CMake
windows_cmake = open("simple_audio_flutter/windows/CMakeLists.txt", "r+")
replace_string_in_file(windows_cmake, r'set\(Version "[\d|\.]+"\)\s', f'set(Version "{version}")\n')
windows_cmake.close()
pubspec_text = open("simple_audio_flutter/pubspec.yaml", "r").read()
package_name = pubspec_text.split("name: ")[1].split("\n")[0].strip()
# macOS podspec
macos_podspec = open(f"simple_audio_flutter/macos/{package_name}.podspec", "r+")
replace_string_in_file(macos_podspec, r'version = "[\d|\.]+"\s', f'version = "{version}"\n')
replace_string_in_file(macos_podspec, r"s\.version\s+= '[\d|\.]+'\s", f"s.version = '{version}'\n")
macos_podspec.close()
# iOS podspec
ios_podspec = open(f"simple_audio_flutter/ios/{package_name}.podspec", "r+")
replace_string_in_file(ios_podspec, r'version = "[\d|\.]+"\s', f'version = "{version}"\n')
replace_string_in_file(ios_podspec, r"s\.version\s+= '[\d|\.]+'\s", f"s.version = '{version}'\n")
ios_podspec.close()
if __name__ == "__main__":
args = parser.parse_args()
if args.code_gen:
code_gen()
if args.build != None:
targets = args.build
if len(args.build) == 0:
targets = ["android", "linux", "windows", "ios", "macos"]
build(targets)
if args.bump_version:
bump_version(args.bump_version)