|
| 1 | +// Copyright (c) 2025 SQLite Cloud, Inc. |
| 2 | +// Licensed under the Elastic License 2.0 (see LICENSE.md). |
| 3 | + |
| 4 | +import 'dart:io'; |
| 5 | + |
| 6 | +import 'package:code_assets/code_assets.dart'; |
| 7 | +import 'package:hooks/hooks.dart'; |
| 8 | +import 'package:path/path.dart' as p; |
| 9 | + |
| 10 | +void main(List<String> args) async { |
| 11 | + await build(args, (input, output) async { |
| 12 | + if (!input.config.buildCodeAssets) return; |
| 13 | + |
| 14 | + final codeConfig = input.config.code; |
| 15 | + final os = codeConfig.targetOS; |
| 16 | + final arch = codeConfig.targetArchitecture; |
| 17 | + |
| 18 | + final binaryPath = _resolveBinaryPath(os, arch, codeConfig); |
| 19 | + if (binaryPath == null) { |
| 20 | + throw UnsupportedError( |
| 21 | + 'sqlite_js does not support $os $arch.', |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + final nativeLibDir = p.join( |
| 26 | + input.packageRoot.toFilePath(), |
| 27 | + 'native_libraries', |
| 28 | + ); |
| 29 | + final file = File(p.join(nativeLibDir, binaryPath)); |
| 30 | + if (!file.existsSync()) { |
| 31 | + throw StateError( |
| 32 | + 'Pre-built binary not found: ${file.path}. ' |
| 33 | + 'Run the CI pipeline to populate native_libraries/.', |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + output.assets.code.add( |
| 38 | + CodeAsset( |
| 39 | + package: input.packageName, |
| 40 | + name: 'src/native/sqlite_js_extension.dart', |
| 41 | + linkMode: DynamicLoadingBundled(), |
| 42 | + file: file.uri, |
| 43 | + ), |
| 44 | + ); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +String? _resolveBinaryPath(OS os, Architecture arch, CodeConfig config) { |
| 49 | + if (os == OS.android) { |
| 50 | + return switch (arch) { |
| 51 | + Architecture.arm64 => 'android/js_android_arm64.so', |
| 52 | + Architecture.arm => 'android/js_android_arm.so', |
| 53 | + Architecture.x64 => 'android/js_android_x64.so', |
| 54 | + _ => null, |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + if (os == OS.iOS) { |
| 59 | + final sdk = config.iOS.targetSdk; |
| 60 | + if (sdk == IOSSdk.iPhoneOS) { |
| 61 | + return 'ios/js_ios_arm64.dylib'; |
| 62 | + } |
| 63 | + // Simulator: fat binary (arm64 + x64) |
| 64 | + return 'ios-sim/js_ios-sim.dylib'; |
| 65 | + } |
| 66 | + |
| 67 | + if (os == OS.macOS) { |
| 68 | + return switch (arch) { |
| 69 | + Architecture.arm64 => 'mac/js_mac_arm64.dylib', |
| 70 | + Architecture.x64 => 'mac/js_mac_x64.dylib', |
| 71 | + _ => null, |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + if (os == OS.linux) { |
| 76 | + return switch (arch) { |
| 77 | + Architecture.x64 => 'linux/js_linux_x64.so', |
| 78 | + Architecture.arm64 => 'linux/js_linux_arm64.so', |
| 79 | + _ => null, |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + if (os == OS.windows) { |
| 84 | + return switch (arch) { |
| 85 | + Architecture.x64 => 'windows/js_windows_x64.dll', |
| 86 | + _ => null, |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + return null; |
| 91 | +} |
0 commit comments