-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-android-bindings.js
97 lines (80 loc) · 2.85 KB
/
setup-android-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
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const { promisify } = require('util');
const execAsync = promisify(exec);
const directoriesToRemove = ['app', '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 setupAndroidCommand = `
sed -i '' 's/crate\\_type = .\\*/crate\\_type = \\["cdylib"\\]/' Cargo.toml && \\
cargo build --release && \\
cargo install cargo-ndk && \\
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android && \\
cargo ndk -o ./app/src/main/jniLibs --manifest-path ./Cargo.toml -t armeabi-v7a -t arm64-v8a -t x86 -t x86_64 build --release && \\
cargo run --bin uniffi-bindgen generate --library ./target/release/libmobile.dylib --language kotlin --out-dir ./app/src/main/java/tech/forgen/todolist/rust
`;
const postSetupAndroid = async () => {
const rustMobileKt = path.resolve(
'rust',
'app',
'src',
'main',
'java',
'tech',
'forgen',
'todolist',
'rust',
'uniffi',
'mobile',
'mobile.kt'
);
const androidMobileKt = path.resolve(
'android',
'src',
'main',
'java',
'uniffi',
'mobile',
'mobile.kt'
);
// Create the destination directory if it doesn't exist
await fs.promises.mkdir(path.dirname(androidMobileKt), { recursive: true });
// Copy rust/app/src/main/java/tech/forgen/todolist/rust/uniffi/mobile/mobile.kt to android/src/main/java/uniffi/mobile/mobile.kt
await fs.promises.copyFile(rustMobileKt, androidMobileKt);
console.log(`Copied ${rustMobileKt} to ${androidMobileKt}`);
const rustJniLibs = path.resolve('rust', 'app', 'src', 'main', 'jniLibs');
const androidJniLibs = path.resolve('android', 'src', 'main', 'jniLibs');
// Copy rust/app/src/main/jniLibs to android/src/main/jniLibs
await fs.promises.cp(rustJniLibs, androidJniLibs, {
recursive: true,
force: true,
});
console.log(`Copied contents of ${rustJniLibs} to ${androidJniLibs}`);
};
const originalDir = process.cwd();
const runSetup = async () => {
try {
removeDirectories();
// Change the current working directory to the 'rust' directory
process.chdir('rust');
const { stdout, stderr } = await execAsync(setupAndroidCommand);
console.log(`Setup Android command output: ${stdout}`);
if (stderr) {
console.error(`Setup Android command stderr: ${stderr}`);
}
// Revert to the original directory after setupAndroidCommand execution
process.chdir(originalDir);
await postSetupAndroid();
} catch (error) {
console.error(`Error executing setup-android command: ${error.message}`);
}
};
runSetup();