-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-remote-android-bindings.js
82 lines (70 loc) · 2.76 KB
/
setup-remote-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
const fs = require('fs').promises;
const path = require('path');
const simpleGit = require('simple-git');
// Configuration
const repoOwner = 'pubky';
const repoName = 'pubky-core-ffi';
const branch = 'main';
const ktPath = 'bindings/android/pubkycore.kt';
const ktDestinationPath = 'android/src/main/java/uniffi/pubkycore/';
const jniPath = 'bindings/android/jniLibs';
const jniDestinationPath = 'android/src/main/jniLibs/';
const tempDir = 'temp';
async function runSetup() {
try {
console.log('Removing existing files...');
// Remove destination directories if they exist & Clean up any lingering temporary directory
await Promise.all([
fs.rm(ktDestinationPath, { recursive: true, force: true }),
fs.rm(jniDestinationPath, { recursive: true, force: true }),
fs.rm(tempDir, { recursive: true, force: true }),
]);
console.log('Creating directories...');
// Create destination directories if they don't exist
await Promise.all([
fs.mkdir(ktDestinationPath, { recursive: true }),
fs.mkdir(jniDestinationPath, { recursive: true }),
]);
// Initialize Git
const git = simpleGit();
console.log('Cloning repository...');
// Clone the repository sparsely
await git.clone(
`https://github.com/${repoOwner}/${repoName}.git`,
tempDir,
['--depth', '1', '--filter=blob:none', '--sparse', `--branch=${branch}`]
);
// Change directory to the cloned repository
const tempGit = simpleGit(tempDir);
console.log('Setting up sparse checkout...');
// Set sparse-checkout to include only the required directory
await tempGit.raw(['sparse-checkout', 'set', 'bindings/android']);
console.log('Copying Kotlin file...');
// Copy Kotlin file to destination
const ktSourcePath = path.join(tempDir, ktPath);
const ktTargetPath = path.join(ktDestinationPath, 'pubkycore.kt');
await fs.copyFile(ktSourcePath, ktTargetPath);
console.log('Copying JNI libraries...');
// Copy JNI libraries directory
const jniSourcePath = path.join(tempDir, jniPath);
const jniTargetPath = jniDestinationPath;
await fs.cp(jniSourcePath, jniTargetPath, { recursive: true });
console.log('Cleaning up...');
// Clean up temporary directory
await fs.rm(tempDir, { recursive: true, force: true });
console.log('Android files downloaded and copied successfully!');
} catch (error) {
console.error('Error during setup:', error);
// Try to clean up temp directory if it exists
try {
await fs.rm(tempDir, { recursive: true, force: true });
} catch (cleanupError) {
console.error('Failed to clean up temporary directory:', cleanupError);
}
process.exit(1);
}
}
runSetup().catch((error) => {
console.error('Unhandled error:', error);
process.exit(1);
});