-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathios-cargo
executable file
·219 lines (183 loc) · 8.55 KB
/
ios-cargo
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
#!/usr/bin/env python3
import argparse
import subprocess
import tomllib
import os
import shutil
import zipfile
import tempfile
import json
def parse_cargo_toml():
with open('Cargo.toml', 'rb') as f:
cargo_toml = tomllib.load(f)
app_name = cargo_toml['package']['metadata']['bundle']['name']
app_id = cargo_toml['package']['metadata']['bundle']['identifier']
return app_name, app_id
def ipa(args):
print("Releasing the build...")
args.release = True
build(args)
app_name, _ = parse_cargo_toml()
build_type = 'release'
target = get_target(args)
cargo_target_dir = os.getenv('CARGO_TARGET_DIR')
if cargo_target_dir:
base_target_dir = cargo_target_dir
else:
base_target_dir = 'target'
app_path = os.path.join(base_target_dir, target, build_type, 'bundle', 'ios', f'{app_name}.app')
temp_dir = tempfile.mkdtemp()
payload_dir = os.path.join(temp_dir, "Payload")
os.makedirs(payload_dir)
shutil.copytree(app_path, os.path.join(payload_dir, f'{app_name}.app'))
ipa_path = f'{app_name}.ipa'
with zipfile.ZipFile(ipa_path, 'w', zipfile.ZIP_DEFLATED) as ipa_file:
for root, dirs, files in os.walk(payload_dir):
for file in files:
file_path = os.path.join(root, file)
ipa_file.write(file_path, os.path.relpath(file_path, os.path.dirname(payload_dir)))
shutil.rmtree(payload_dir)
print(f"Created {ipa_path}")
def post_process_info_plist(plist_path, ipad):
try:
with open('insert.plist', 'r') as insert_file:
insert_content = insert_file.read()
except:
insert_content = ""
with open(plist_path, 'r') as plist_file:
plist_content = plist_file.read()
if ipad:
ipad_content = """
<key>UIDeviceFamily</key>
<array>
<integer>1</integer> <!-- iPhone -->
<integer>2</integer> <!-- iPad -->
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
"""
insert_content += ipad_content
modified_content = plist_content.replace('</dict>', f'{insert_content}\n</dict>')
with open(plist_path, 'w') as plist_file:
plist_file.write(modified_content)
def get_target(args):
target = 'aarch64-apple-ios'
if args.x86:
target = 'x86_64-apple-ios'
elif args.sim:
target = 'aarch64-apple-ios-sim'
if args.target:
target = args.target
return target
def build(args):
target = get_target(args)
command = ['cargo', 'bundle', '--target', target]
if args.release:
command.append('--release')
print(f"Running command: {' '.join(command)}")
subprocess.run(command, check=True)
app_name, _ = parse_cargo_toml()
build_type = 'release' if args.release else 'debug'
cargo_target_dir = os.getenv('CARGO_TARGET_DIR')
if cargo_target_dir:
base_target_dir = cargo_target_dir
else:
base_target_dir = 'target'
plist_path = os.path.join(base_target_dir, target, build_type, 'bundle', 'ios', f'{app_name}.app', 'Info.plist')
post_process_info_plist(plist_path, args.ipad)
def get_booted_device():
result = subprocess.run(['xcrun', 'simctl', 'list', 'devices', '--json'], capture_output=True, text=True, check=True)
devices = json.loads(result.stdout)
for runtime in devices['devices']:
for dev in devices['devices'][runtime]:
if dev['state'] == 'Booted':
return dev['udid']
return None
def boot_device(device):
print(f"Booting device {device}...")
subprocess.run(['xcrun', 'simctl', 'boot', device], check=True)
print(f"Device {device} booted.")
def get_newest_iphone_udid():
result = subprocess.run(['xcrun', 'simctl', 'list', 'devices', '--json'], capture_output=True, text=True, check=True)
devices = json.loads(result.stdout)
for runtime in devices['devices']:
for dev in devices['devices'][runtime]:
if 'iPhone' in dev['name'] and dev['isAvailable'] and 'SE' not in dev['name']:
return dev['udid']
raise Exception("No available iPhone simulators found")
def run_build(args):
app_name, app_id = parse_cargo_toml()
build_type = 'release' if args.release else 'debug'
target = get_target(args)
cargo_target_dir = os.getenv('CARGO_TARGET_DIR')
if cargo_target_dir:
base_target_dir = cargo_target_dir
else:
base_target_dir = 'target'
app_path = os.path.join(base_target_dir, target, build_type, 'bundle', 'ios', f'{app_name}.app')
if args.device == "booted":
if not get_booted_device():
specific_device_udid = get_newest_iphone_udid()
boot_device(specific_device_udid)
args.device = specific_device_udid
install_command = [
'xcrun', 'simctl', 'install', args.device, app_path
]
launch_command = ['xcrun', 'simctl', 'launch', '--console', args.device, app_id]
print(f"Running command: {' '.join(install_command)}")
subprocess.run(install_command, check=True)
print(f"Running command: {' '.join(launch_command)}")
subprocess.run(launch_command, check=True)
def run(args):
print("Running the build process...")
build(args)
print("Running the build...")
run_build(args)
def main():
parser = argparse.ArgumentParser(description='A script with build, run, run-build, and release subcommands.')
subparsers = parser.add_subparsers(dest='command', required=True)
build_parser = subparsers.add_parser('build', help='Build the project')
build_parser.add_argument('--x86', action='store_true', help='Use x86 target')
build_parser.add_argument('--sim', action='store_true', help='Use simulator target')
build_parser.add_argument('--target', type=str, help='Specify custom target')
build_parser.add_argument('--release', '-r', action='store_true', help='Build for release')
build_parser.add_argument('--ipad', action='store_true', help='Include iPad-specific Info.plist entries')
build_parser.set_defaults(func=build)
run_parser = subparsers.add_parser('run', help='Build and run the project')
run_parser.add_argument('--x86', action='store_true', help='Use x86 target')
run_parser.add_argument('--sim', action='store_true', help='Use simulator target')
run_parser.add_argument('--target', type=str, help='Specify custom target')
run_parser.add_argument('--release', '-r', action='store_true', help='Build for release')
run_parser.add_argument('--ipad', action='store_true', help='Include iPad-specific Info.plist entries')
run_parser.add_argument('--device', type=str, default='booted', help='Specify the target device')
run_parser.set_defaults(func=run)
run_build_parser = subparsers.add_parser('run-build', help='Runs already built project')
run_build_parser.add_argument('--x86', action='store_true', help='Use x86 target')
run_build_parser.add_argument('--sim', action='store_true', help='Use simulator target')
run_build_parser.add_argument('--target', type=str, help='Specify custom target')
run_build_parser.add_argument('--release', '-r', action='store_true', help='Build for release')
run_build_parser.add_argument('--device', type=str, default='booted', help='Specify the target device')
run_build_parser.add_argument('--ipad', action='store_true', help='Include iPad-specific Info.plist entries')
run_build_parser.set_defaults(func=run_build)
release_parser = subparsers.add_parser('ipa', help='Creates a ipa')
release_parser.add_argument('--x86', action='store_true', help='Use x86 target')
release_parser.add_argument('--sim', action='store_true', help='Use simulator target')
release_parser.add_argument('--target', type=str, help='Specify custom target')
release_parser.add_argument('--release', '-r', action='store_true', help='Build for release')
release_parser.add_argument('--ipad', action='store_true', help='Include iPad-specific Info.plist entries')
release_parser.set_defaults(func=ipa)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()