|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:native_assets_cli/native_assets_cli.dart'; |
| 4 | +import 'package:native_toolchain_rust_common/native_toolchain_rust_common.dart'; |
| 5 | +import 'package:path/path.dart' as p; |
| 6 | +import 'package:test/test.dart'; |
| 7 | + |
| 8 | +void copyPathSync(String from, String to) { |
| 9 | + Directory(to).createSync(recursive: true); |
| 10 | + for (final file in Directory(from).listSync(recursive: false)) { |
| 11 | + if (file.uri.pathSegments.contains('.dart_tool') || |
| 12 | + file.uri.pathSegments.contains('build')) { |
| 13 | + continue; |
| 14 | + } |
| 15 | + |
| 16 | + final copyTo = p.join(to, p.relative(file.path, from: from)); |
| 17 | + if (file is Directory) { |
| 18 | + copyPathSync(file.path, copyTo); |
| 19 | + } else if (file is File) { |
| 20 | + File(file.path).copySync(copyTo); |
| 21 | + } else if (file is Link) { |
| 22 | + Link(copyTo).createSync(file.targetSync(), recursive: true); |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +Future<void> withFlutterExampleProject( |
| 28 | + Future<void> Function(Uri) testFn, |
| 29 | +) async { |
| 30 | + final tempDir = Directory.systemTemp.createTempSync('buildTest'); |
| 31 | + try { |
| 32 | + final workspaceRoot = Directory.current.uri.resolve('../'); |
| 33 | + |
| 34 | + final exampleRoot = workspaceRoot.resolve('example/flutter_package'); |
| 35 | + |
| 36 | + copyPathSync(exampleRoot.toFilePath(), tempDir.path); |
| 37 | + |
| 38 | + final pubspecOverrides = """ |
| 39 | +dependency_overrides: |
| 40 | + flutter_package: |
| 41 | + path: ../ |
| 42 | + native_toolchain_rust: |
| 43 | + path: ${workspaceRoot.resolve('native_toolchain_rust').toFilePath()} |
| 44 | + native_toolchain_rust_common: |
| 45 | + path: ${workspaceRoot.resolve('native_toolchain_rust_common').toFilePath()} |
| 46 | + rustup: |
| 47 | + path: ${workspaceRoot.resolve('rustup').toFilePath()} |
| 48 | + """; |
| 49 | + |
| 50 | + File(tempDir.uri.resolve('example/pubspec_overrides.yaml').toFilePath()) |
| 51 | + .writeAsStringSync(pubspecOverrides); |
| 52 | + |
| 53 | + final exampleUri = tempDir.uri.resolve('example/'); |
| 54 | + |
| 55 | + await runCommand( |
| 56 | + 'flutter', |
| 57 | + ['clean'], |
| 58 | + workingDirectory: exampleUri.toFilePath(), |
| 59 | + ); |
| 60 | + |
| 61 | + await runCommand( |
| 62 | + 'flutter', |
| 63 | + ['pub', 'get'], |
| 64 | + workingDirectory: exampleUri.toFilePath(), |
| 65 | + ); |
| 66 | + |
| 67 | + await testFn(exampleUri); |
| 68 | + } finally { |
| 69 | + int attempt = 0; |
| 70 | + while (true) { |
| 71 | + try { |
| 72 | + await tempDir.delete(recursive: true); |
| 73 | + break; |
| 74 | + } catch (e) { |
| 75 | + if (attempt > 5) { |
| 76 | + rethrow; |
| 77 | + } |
| 78 | + attempt++; |
| 79 | + // Windows being windows. |
| 80 | + print('Failed to clean temp installer temp dir: $e'); |
| 81 | + await Future.delayed(const Duration(seconds: 1)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void main() async { |
| 88 | + test( |
| 89 | + 'macOS project', |
| 90 | + () async { |
| 91 | + await withFlutterExampleProject((uri) async { |
| 92 | + for (final config in ['Debug', 'Profile', 'Release']) { |
| 93 | + await runCommand( |
| 94 | + 'flutter', |
| 95 | + ['build', 'macos', '--${config.toLowerCase()}'], |
| 96 | + workingDirectory: uri.toFilePath(), |
| 97 | + ); |
| 98 | + |
| 99 | + // Check if the library is built |
| 100 | + final library = File.fromUri(uri.resolve( |
| 101 | + 'build/macos/Build/Products/$config/example.app/Contents/Frameworks/flutter_ffi_plugin.framework/Versions/A/flutter_ffi_plugin')); |
| 102 | + expect(library.existsSync(), isTrue); |
| 103 | + } |
| 104 | + }); |
| 105 | + }, |
| 106 | + timeout: const Timeout(Duration(minutes: 10)), |
| 107 | + skip: !Platform.isMacOS, |
| 108 | + ); |
| 109 | + test( |
| 110 | + 'iOS project', |
| 111 | + () async { |
| 112 | + await withFlutterExampleProject((uri) async { |
| 113 | + for (final config in ['Debug', 'Profile', 'Release']) { |
| 114 | + await runCommand( |
| 115 | + 'flutter', |
| 116 | + ['build', 'ios', '--${config.toLowerCase()}', '--no-codesign'], |
| 117 | + workingDirectory: uri.toFilePath(), |
| 118 | + ); |
| 119 | + |
| 120 | + // Check if the library is built |
| 121 | + final library = File.fromUri(uri.resolve( |
| 122 | + 'build/ios/$config-iphoneos/Runner.app/Frameworks/flutter_ffi_plugin.framework/flutter_ffi_plugin', |
| 123 | + )); |
| 124 | + expect(library.existsSync(), isTrue); |
| 125 | + } |
| 126 | + }); |
| 127 | + }, |
| 128 | + timeout: const Timeout(Duration(minutes: 10)), |
| 129 | + skip: !Platform.isMacOS, |
| 130 | + ); |
| 131 | + test( |
| 132 | + 'windows project', |
| 133 | + () async { |
| 134 | + await withFlutterExampleProject((uri) async { |
| 135 | + for (final config in ['Debug', 'Profile', 'Release']) { |
| 136 | + await runCommand( |
| 137 | + 'flutter', |
| 138 | + ['build', 'windows', '--${config.toLowerCase()}'], |
| 139 | + workingDirectory: uri.toFilePath(), |
| 140 | + ); |
| 141 | + |
| 142 | + // Check if the library is built |
| 143 | + final library = File.fromUri(uri.resolve( |
| 144 | + 'build/windows/${Architecture.current}/runner/$config/flutter_ffi_plugin.dll', |
| 145 | + )); |
| 146 | + expect(library.existsSync(), isTrue); |
| 147 | + } |
| 148 | + }); |
| 149 | + }, |
| 150 | + timeout: const Timeout(Duration(minutes: 10)), |
| 151 | + skip: !Platform.isWindows, |
| 152 | + ); |
| 153 | + test( |
| 154 | + 'linux project', |
| 155 | + () async { |
| 156 | + await withFlutterExampleProject((uri) async { |
| 157 | + for (final config in ['debug', 'profile', 'release']) { |
| 158 | + await runCommand( |
| 159 | + 'flutter', |
| 160 | + ['build', 'linux', '--$config'], |
| 161 | + workingDirectory: uri.toFilePath(), |
| 162 | + ); |
| 163 | + |
| 164 | + // Check if the library is built |
| 165 | + final library = File.fromUri(uri.resolve( |
| 166 | + 'build/linux/${Architecture.current}/$config/bundle/lib/libflutter_ffi_plugin.so', |
| 167 | + )); |
| 168 | + expect(library.existsSync(), isTrue); |
| 169 | + } |
| 170 | + }); |
| 171 | + }, |
| 172 | + timeout: const Timeout(Duration(minutes: 10)), |
| 173 | + skip: !Platform.isLinux, |
| 174 | + ); |
| 175 | + test( |
| 176 | + 'android project', |
| 177 | + () async { |
| 178 | + await withFlutterExampleProject((uri) async { |
| 179 | + for (final config in ['debug', 'profile', 'release']) { |
| 180 | + await runCommand( |
| 181 | + 'flutter', |
| 182 | + ['build', 'apk', '--$config'], |
| 183 | + workingDirectory: uri.toFilePath(), |
| 184 | + ); |
| 185 | + |
| 186 | + // Check if the library is built |
| 187 | + final library = File.fromUri(uri.resolve( |
| 188 | + 'build/app/intermediates/merged_jni_libs/$config/out/arm64-v8a/libflutter_ffi_plugin.so', |
| 189 | + )); |
| 190 | + expect(library.existsSync(), isTrue); |
| 191 | + } |
| 192 | + }); |
| 193 | + }, |
| 194 | + timeout: const Timeout(Duration(minutes: 10)), |
| 195 | + ); |
| 196 | +} |
0 commit comments