|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io' as io; |
| 3 | +import 'package:args/command_runner.dart'; |
| 4 | +import 'package:build_cmd/repo_finder.dart'; |
| 5 | +import 'package:io/ansi.dart'; |
| 6 | +import 'package:path/path.dart' as path; |
| 7 | +import 'package:process/process.dart'; |
| 8 | + |
| 9 | +/// Relative header paths (in both repositories) |
| 10 | +final containers = 'mediapipe/tasks/c/components/containers'; |
| 11 | +final processors = 'mediapipe/tasks/c/components/processors'; |
| 12 | +final core = 'mediapipe/tasks/c/core'; |
| 13 | +final tc = 'mediapipe/tasks/c/text/text_classifier'; |
| 14 | + |
| 15 | +/// google/flutter-mediapipe package paths |
| 16 | +final corePackage = 'packages/mediapipe-core/third_party'; |
| 17 | +final textPackage = 'packages/mediapipe-task-text/third_party'; |
| 18 | + |
| 19 | +/// First string is its relative location in both repositories, |
| 20 | +/// Second string is its package location in `google/flutter-mediapipe`, |
| 21 | +/// Third string is the file name |
| 22 | +/// Fourth param is an optional function to modify the file |
| 23 | +List<(String, String, String, Function(io.File)?)> headerPaths = [ |
| 24 | + (containers, corePackage, 'category.h', null), |
| 25 | + (containers, corePackage, 'classification_result.h', null), |
| 26 | + (core, corePackage, 'base_options.h', null), |
| 27 | + (processors, corePackage, 'classifier_options.h', null), |
| 28 | + (tc, textPackage, 'text_classifier.h', relativeIncludes), |
| 29 | +]; |
| 30 | + |
| 31 | +class SyncHeadersCommand extends Command with RepoFinderMixin { |
| 32 | + @override |
| 33 | + String description = 'Syncs header files to google/flutter-mediapipe'; |
| 34 | + @override |
| 35 | + String name = 'headers'; |
| 36 | + |
| 37 | + SyncHeadersCommand() { |
| 38 | + argParser.addFlag( |
| 39 | + 'overwrite', |
| 40 | + abbr: 'o', |
| 41 | + defaultsTo: true, |
| 42 | + help: 'If true, will overwrite existing header files ' |
| 43 | + 'at destination locations.', |
| 44 | + ); |
| 45 | + addSourceOption(argParser); |
| 46 | + } |
| 47 | + |
| 48 | + @override |
| 49 | + Future<void> run() async { |
| 50 | + final io.Directory flutterMediaPipeDirectory = findFlutterMediaPipeRoot(); |
| 51 | + final io.Directory mediaPipeDirectory = findMediaPipeRoot( |
| 52 | + flutterMediaPipeDirectory, |
| 53 | + argResults!['source'], |
| 54 | + ); |
| 55 | + |
| 56 | + final config = Options( |
| 57 | + allowOverwrite: argResults!['overwrite'], |
| 58 | + mediaPipeDir: mediaPipeDirectory, |
| 59 | + flutterMediaPipeDir: flutterMediaPipeDirectory, |
| 60 | + ); |
| 61 | + |
| 62 | + await copyHeaders(config); |
| 63 | + } |
| 64 | + |
| 65 | + Future<void> copyHeaders(Options config) async { |
| 66 | + final mgr = LocalProcessManager(); |
| 67 | + for (final tup in headerPaths) { |
| 68 | + final headerFile = io.File(path.joinAll( |
| 69 | + [config.mediaPipeDir.absolute.path, tup.$1, tup.$3], |
| 70 | + )); |
| 71 | + if (!headerFile.existsSync()) { |
| 72 | + io.stderr.writeln( |
| 73 | + 'Expected to find ${headerFile.path}, but ' |
| 74 | + 'file does not exist.', |
| 75 | + ); |
| 76 | + io.exit(1); |
| 77 | + } |
| 78 | + final destinationPath = path.joinAll( |
| 79 | + [config.flutterMediaPipeDir.absolute.path, tup.$2, tup.$1, tup.$3], |
| 80 | + ); |
| 81 | + final destinationFile = io.File(destinationPath); |
| 82 | + if (destinationFile.existsSync() && !config.allowOverwrite) { |
| 83 | + io.stdout.writeln( |
| 84 | + 'Warning: Not overwriting existing file at $destinationPath. ' |
| 85 | + 'Skipping ${tup.$3}.', |
| 86 | + ); |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + ensureFolders(io.File(destinationPath)); |
| 91 | + |
| 92 | + final process = await mgr.start([ |
| 93 | + 'cp', |
| 94 | + headerFile.path, |
| 95 | + destinationPath, |
| 96 | + ], runInShell: true); |
| 97 | + int processExitCode = await process.exitCode; |
| 98 | + if (processExitCode != 0) { |
| 99 | + final processStdErr = utf8.decoder.convert( |
| 100 | + (await process.stderr.toList()) |
| 101 | + .fold<List<int>>([], (arr, el) => arr..addAll(el))); |
| 102 | + io.stderr.write(wrapWith(processStdErr, [red])); |
| 103 | + |
| 104 | + final processStdOut = utf8.decoder.convert( |
| 105 | + (await process.stdout.toList()) |
| 106 | + .fold<List<int>>([], (arr, el) => arr..addAll(el))); |
| 107 | + io.stderr.write(wrapWith(processStdOut, [red])); |
| 108 | + io.exit(processExitCode); |
| 109 | + } else { |
| 110 | + io.stderr.writeln(wrapWith('Copied ${tup.$3}', [green])); |
| 111 | + } |
| 112 | + |
| 113 | + // Call the final modification function, if supplied |
| 114 | + if (tup.$4 != null) { |
| 115 | + tup.$4!.call(destinationFile); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// Builds any missing folders between the file and the root of the repository |
| 121 | + void ensureFolders(io.File file) { |
| 122 | + io.Directory parent = file.parent; |
| 123 | + List<io.Directory> dirsToCreate = []; |
| 124 | + while (!parent.existsSync()) { |
| 125 | + dirsToCreate.add(parent); |
| 126 | + parent = parent.parent; |
| 127 | + } |
| 128 | + for (io.Directory dir in dirsToCreate.reversed) { |
| 129 | + dir.createSync(); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +class Options { |
| 135 | + const Options({ |
| 136 | + required this.allowOverwrite, |
| 137 | + required this.mediaPipeDir, |
| 138 | + required this.flutterMediaPipeDir, |
| 139 | + }); |
| 140 | + |
| 141 | + final bool allowOverwrite; |
| 142 | + final io.Directory mediaPipeDir; |
| 143 | + final io.Directory flutterMediaPipeDir; |
| 144 | +} |
| 145 | + |
| 146 | +void relativeIncludes(io.File textClassifierHeader) { |
| 147 | + assert(textClassifierHeader.path.endsWith('text_classifier.h')); |
| 148 | + String contents = textClassifierHeader.readAsStringSync(); |
| 149 | + |
| 150 | + Map<String, String> rewrites = { |
| 151 | + 'mediapipe/tasks/c/components/containers/classification_result.h': |
| 152 | + '../../../../../../../mediapipe-core/third_party/mediapipe/tasks/c/components/containers/classification_result.h', |
| 153 | + 'mediapipe/tasks/c/components/processors/classifier_options.h': |
| 154 | + '../../../../../../../mediapipe-core/third_party/mediapipe/tasks/c/components/processors/classifier_options.h', |
| 155 | + 'mediapipe/tasks/c/core/base_options.h': |
| 156 | + '../../../../../../../mediapipe-core/third_party/mediapipe/tasks/c/core/base_options.h', |
| 157 | + }; |
| 158 | + |
| 159 | + for (final rewrite in rewrites.entries) { |
| 160 | + contents = contents.replaceAll(rewrite.key, rewrite.value); |
| 161 | + } |
| 162 | + textClassifierHeader.writeAsStringSync(contents); |
| 163 | +} |
0 commit comments