-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-ios-bindings.js
108 lines (90 loc) · 3.52 KB
/
setup-ios-bindings.js
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
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const execAsync = promisify(exec);
const directoriesToRemove = ['bindings', 'ios', 'target'];
const removeDirectories = () => {
directoriesToRemove.forEach((dir) => {
const dirPath = path.resolve('rust', dir);
if (fs.existsSync(dirPath)) {
fs.rmSync(dirPath, { recursive: true });
console.log(`Removed directory: ${dirPath}`);
}
});
};
const setupIosCommand = `
export IPHONEOS_DEPLOYMENT_TARGET=13.4
sed -i '' 's/crate_type = .*/crate_type = ["cdylib", "staticlib"]/' Cargo.toml && \\
cargo build --release && \\
cargo run --bin uniffi-bindgen generate --library ./target/release/libpubkycore.dylib --language swift --out-dir ./bindings && \\
rustup target add aarch64-apple-ios-sim aarch64-apple-ios && \\
cargo build --release --target=aarch64-apple-ios-sim && \\
cargo build --release --target=aarch64-apple-ios && \\
mv bindings/pubkycoreFFI.modulemap bindings/module.modulemap && \\
xcodebuild -create-xcframework -library ./target/aarch64-apple-ios-sim/release/libpubkycore.a -headers ./bindings -library ./target/aarch64-apple-ios/release/libpubkycore.a -headers ./bindings -output "ios/PubkyCore.xcframework"
`;
const originalDir = process.cwd();
const postSetupIos = async () => {
const rustBindingsPubkyCoreSwift = path.resolve(
'rust',
'bindings',
'pubkycore.swift'
);
const iosPubkyCoreSwift = path.resolve('ios', 'pubkycore.swift');
// Copy rust/bindings/pubkycore.swift file to ios/ directory
await fs.promises.copyFile(rustBindingsPubkyCoreSwift, iosPubkyCoreSwift);
console.log(`Copied ${rustBindingsPubkyCoreSwift} to ${iosPubkyCoreSwift}`);
// Delete rust/ios/PubkyCore.xcframework/ios-arm64/Headers/pubkycore.swift
const iosArm64HeadersPubkyCoreSwift = path.resolve(
'rust',
'ios',
'PubkyCore.xcframework',
'ios-arm64',
'Headers',
'pubkycore.swift'
);
if (fs.existsSync(iosArm64HeadersPubkyCoreSwift)) {
await fs.promises.unlink(iosArm64HeadersPubkyCoreSwift);
console.log(`Deleted ${iosArm64HeadersPubkyCoreSwift}`);
}
// Delete rust/ios/PubkyCore.xcframework/ios-arm64-simulator/Headers/pubkycore.swift
const iosArm64SimulatorHeadersPubkyCoreSwift = path.resolve(
'rust',
'ios',
'PubkyCore.xcframework',
'ios-arm64-simulator',
'Headers',
'pubkycore.swift'
);
if (fs.existsSync(iosArm64SimulatorHeadersPubkyCoreSwift)) {
await fs.promises.unlink(iosArm64SimulatorHeadersPubkyCoreSwift);
console.log(`Deleted ${iosArm64SimulatorHeadersPubkyCoreSwift}`);
}
const rustIos = path.resolve('rust', 'ios');
const frameworksDir = path.resolve('ios', 'Frameworks');
// Copy contents of rust/ios/ to Frameworks directory
await fs.promises.cp(rustIos, frameworksDir, {
recursive: true,
force: true,
});
console.log(`Copied contents of ${rustIos} to ${frameworksDir}`);
};
const runSetup = async () => {
try {
removeDirectories();
// Change the current working directory to the 'rust' directory
process.chdir('rust');
const { stdout, stderr } = await execAsync(setupIosCommand);
console.log(`Setup iOS command output: ${stdout}`);
if (stderr) {
console.error(`Setup iOS command stderr: ${stderr}`);
}
// Revert to the original directory after setupIosCommand
process.chdir(originalDir);
await postSetupIos();
} catch (error) {
console.error(`Error executing setup-ios command: ${error.message}`);
}
};
runSetup();