diff --git a/test_flutter/flutter_template_tests/flutter_plugin_ffi_test.dart b/test_flutter/flutter_template_tests/flutter_plugin_ffi_test.dart index 06b9df74..aa867313 100644 --- a/test_flutter/flutter_template_tests/flutter_plugin_ffi_test.dart +++ b/test_flutter/flutter_template_tests/flutter_plugin_ffi_test.dart @@ -32,32 +32,30 @@ void main() { final bindingsGeneratedCopyUri = libDirUri.resolve('${projectName}_bindings_generated_copy.dart'); - await Task.serial([ - RunProcess( - executable: 'flutter', - arguments: [ - 'create', - '--template=plugin_ffi', - projectName, - ], - workingDirectory: tempDirUri, - ), - Copy( - source: bindingsGeneratedUri, - target: bindingsGeneratedCopyUri, - ), - RunProcess( - executable: 'flutter', - arguments: [ - 'pub', - 'run', - 'ffigen', - '--config', - 'ffigen.yaml', - ], - workingDirectory: projectDirUri, - ), - ]).run(); + await runProcess( + executable: 'flutter', + arguments: [ + 'create', + '--template=plugin_ffi', + projectName, + ], + workingDirectory: tempDirUri, + ); + await copyFile( + source: bindingsGeneratedUri, + target: bindingsGeneratedCopyUri, + ); + await runProcess( + executable: 'flutter', + arguments: [ + 'pub', + 'run', + 'ffigen', + '--config', + 'ffigen.yaml', + ], + workingDirectory: projectDirUri, + ); final originalBindings = await readFileAsString(bindingsGeneratedCopyUri); final regeneratedBindings = await readFileAsString(bindingsGeneratedUri); @@ -71,96 +69,56 @@ Future readFileAsString(Uri uri) async { return contents.replaceAll('\r', ''); } -abstract class Task { - Future run(); - - factory Task.serial(Iterable tasks) => _SerialTask(tasks); -} - -class _SerialTask implements Task { - final Iterable tasks; - - _SerialTask(this.tasks); - - @override - Future run() async { - for (final task in tasks) { - await task.run(); - } - } -} - -class RunProcess implements Task { - final List arguments; - final String executable; - final Uri? workingDirectory; - Map? environment; - final bool throwOnFailure; - - RunProcess({ - required this.arguments, - required this.executable, - this.workingDirectory, - this.environment, - this.throwOnFailure = true, +Future runProcess({ + required List arguments, + required String executable, + Uri? workingDirectory, + Map? environment, + bool throwOnFailure = true, +}) async { + // Excluding [workingDirectory]. + final String commandString = [ + if (workingDirectory != null) '(cd ${workingDirectory.path};', + ...?environment?.entries.map((entry) => '${entry.key}=${entry.value}'), + executable, + ...arguments.map((a) => a.contains(' ') ? "'$a'" : a), + if (workingDirectory != null) ')', + ].join(' '); + + final workingDirectoryString = workingDirectory?.toFilePath(); + + print('Running `$commandString`.'); + final process = await Process.start(executable, arguments, + runInShell: true, + includeParentEnvironment: true, + workingDirectory: workingDirectoryString, + environment: environment) + .then((process) { + process.stdout.transform(utf8.decoder).forEach((s) => print(' $s')); + process.stderr.transform(utf8.decoder).forEach((s) => print(' $s')); + return process; }); - - /// Excluding [workingDirectory]. - String get commandString => [ - if (workingDirectory != null) '(cd ${workingDirectory!.path};', - ...?environment?.entries.map((entry) => '${entry.key}=${entry.value}'), - executable, - ...arguments.map((a) => a.contains(' ') ? "'$a'" : a), - if (workingDirectory != null) ')', - ].join(' '); - - @override - Future run() async { - final workingDirectoryString = workingDirectory?.toFilePath(); - - print('Running `$commandString`.'); - final process = await Process.start(executable, arguments, - runInShell: true, - includeParentEnvironment: true, - workingDirectory: workingDirectoryString, - environment: environment) - .then((process) { - process.stdout.transform(utf8.decoder).forEach((s) => print(' $s')); - process.stderr.transform(utf8.decoder).forEach((s) => print(' $s')); - return process; - }); - final exitCode = await process.exitCode; - if (exitCode != 0) { - final message = - 'Command `$commandString` failed with exit code $exitCode.'; - print(message); - if (throwOnFailure) { - throw Exception(message); - } + final exitCode = await process.exitCode; + if (exitCode != 0) { + final message = 'Command `$commandString` failed with exit code $exitCode.'; + print(message); + if (throwOnFailure) { + throw Exception(message); } - print('Command `$commandString` done.'); } + print('Command `$commandString` done.'); } -class Copy implements Task { - final Uri source; - final Uri target; - - Copy({ - required this.source, - required this.target, - }); - - @override - Future run() async { - final file = File.fromUri(source); - if (!await file.exists()) { - final message = - "File not in expected location: '${source.toFilePath()}'."; - print(message); - throw Exception(message); - } - print('Copying ${source.toFilePath()} to ${target.toFilePath()}.'); - await file.copy(target.toFilePath()); +Future copyFile({ + required Uri source, + required Uri target, +}) async { + final file = File.fromUri(source); + if (!await file.exists()) { + final message = "File not in expected location: '${source.toFilePath()}'."; + print(message); + throw Exception(message); } + print('Copying ${source.toFilePath()} to ${target.toFilePath()}.'); + await file.copy(target.toFilePath()); }