|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'dart:convert'; |
| 4 | + |
| 5 | +import 'package:args/command_runner.dart'; |
| 6 | +import 'package:path/path.dart' as p; |
| 7 | +import 'package:http/http.dart' as http; |
| 8 | + |
| 9 | +void main(List<String> args) { |
| 10 | + Directory packagesDir = new Directory( |
| 11 | + p.join(p.dirname(p.dirname(p.fromUri(Platform.script))), 'packages')); |
| 12 | + |
| 13 | + new CommandRunner('cli', 'Various productivity utils.') |
| 14 | + ..addCommand(new TestCommand(packagesDir)) |
| 15 | + ..addCommand(new AnalyzeCommand(packagesDir)) |
| 16 | + ..addCommand(new FormatCommand(packagesDir)) |
| 17 | + ..addCommand(new BuildCommand(packagesDir)) |
| 18 | + ..run(args); |
| 19 | +} |
| 20 | + |
| 21 | +class TestCommand extends Command { |
| 22 | + TestCommand(this.packagesDir); |
| 23 | + |
| 24 | + final Directory packagesDir; |
| 25 | + |
| 26 | + final name = 'test'; |
| 27 | + final description = 'Runs the tests for all packages.'; |
| 28 | + |
| 29 | + Future run() async { |
| 30 | + List<String> failingPackages = <String>[]; |
| 31 | + for (Directory packageDir in _listAllPackages(packagesDir)) { |
| 32 | + String packageName = p.relative(packageDir.path, from: packagesDir.path); |
| 33 | + if (!new Directory(p.join(packageDir.path, 'test')).existsSync()) { |
| 34 | + print('\nSKIPPING $packageName - no test subdirectory'); |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + print('\nRUNNING $packageName tests...'); |
| 39 | + Process process = await Process.start('flutter', ['test', '--color'], |
| 40 | + workingDirectory: packageDir.path); |
| 41 | + stdout.addStream(process.stdout); |
| 42 | + stderr.addStream(process.stderr); |
| 43 | + if (await process.exitCode != 0) { |
| 44 | + failingPackages.add(packageName); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + print('\n\n'); |
| 49 | + if (failingPackages.isNotEmpty) { |
| 50 | + print('Test for the following packages are failing (see above):'); |
| 51 | + failingPackages.forEach((String package) { |
| 52 | + print(' * $package'); |
| 53 | + }); |
| 54 | + } else { |
| 55 | + print('All tests are passing!'); |
| 56 | + } |
| 57 | + exit(failingPackages.length); |
| 58 | + } |
| 59 | + |
| 60 | + Iterable<Directory> _listAllPackages(Directory root) => root |
| 61 | + .listSync(recursive: true) |
| 62 | + .where((FileSystemEntity entity) => |
| 63 | + entity is File && p.basename(entity.path) == 'pubspec.yaml') |
| 64 | + .map((FileSystemEntity entity) => entity.parent); |
| 65 | +} |
| 66 | + |
| 67 | +class AnalyzeCommand extends Command { |
| 68 | + AnalyzeCommand(this.packagesDir); |
| 69 | + |
| 70 | + final Directory packagesDir; |
| 71 | + |
| 72 | + final name = 'analyze'; |
| 73 | + final description = 'Analyzes all packages.'; |
| 74 | + |
| 75 | + Future run() async { |
| 76 | + print('TODO(goderbauer): Implement command when ' |
| 77 | + 'https://github.com/flutter/flutter/issues/10015 is resolved.'); |
| 78 | + exit(1); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +class FormatCommand extends Command { |
| 83 | + FormatCommand(this.packagesDir) { |
| 84 | + argParser.addFlag('travis', hide: true); |
| 85 | + } |
| 86 | + |
| 87 | + final Directory packagesDir; |
| 88 | + |
| 89 | + final name = 'format'; |
| 90 | + final description = 'Formats the code of all packages.'; |
| 91 | + |
| 92 | + Future run() async { |
| 93 | + String javaFormatterPath = p.join(p.dirname(p.fromUri(Platform.script)), |
| 94 | + 'google-java-format-1.3-all-deps.jar'); |
| 95 | + File javaFormatterFile = new File(javaFormatterPath); |
| 96 | + |
| 97 | + if (!javaFormatterFile.existsSync()) { |
| 98 | + print('Downloading Google Java Format...'); |
| 99 | + http.Response response = await http.get( |
| 100 | + 'https://github.com/google/google-java-format/releases/download/google-java-format-1.3/google-java-format-1.3-all-deps.jar'); |
| 101 | + javaFormatterFile.writeAsBytesSync(response.bodyBytes); |
| 102 | + } |
| 103 | + |
| 104 | + print('Formatting all .dart files...'); |
| 105 | + Process.runSync('flutter', ['format'], workingDirectory: packagesDir.path); |
| 106 | + |
| 107 | + print('Formatting all .java files...'); |
| 108 | + Iterable<String> javaFiles = _getFilesWithExtension(packagesDir, '.java'); |
| 109 | + Process.runSync( |
| 110 | + 'java', ['-jar', javaFormatterPath, '--replace']..addAll(javaFiles), |
| 111 | + workingDirectory: packagesDir.path); |
| 112 | + |
| 113 | + print('Formatting all .m and .h files...'); |
| 114 | + Iterable<String> hFiles = _getFilesWithExtension(packagesDir, '.h'); |
| 115 | + Iterable<String> mFiles = _getFilesWithExtension(packagesDir, '.m'); |
| 116 | + Process.runSync('clang-format', |
| 117 | + ['-i', '--style=Google']..addAll(hFiles)..addAll(mFiles), |
| 118 | + workingDirectory: packagesDir.path); |
| 119 | + |
| 120 | + if (argResults['travis']) { |
| 121 | + ProcessResult modifiedFiles = Process.runSync( |
| 122 | + 'git', ['ls-files', '--modified'], |
| 123 | + workingDirectory: packagesDir.path); |
| 124 | + if (modifiedFiles.stdout.isNotEmpty) { |
| 125 | + ProcessResult diff = Process.runSync('git', ['diff', '--color'], |
| 126 | + workingDirectory: packagesDir.path); |
| 127 | + print(diff.stdout); |
| 128 | + |
| 129 | + print('\nThese files are not formatted correctly (see diff above):'); |
| 130 | + LineSplitter |
| 131 | + .split(modifiedFiles.stdout) |
| 132 | + .map((String line) => ' $line') |
| 133 | + .forEach(print); |
| 134 | + print('\nTo fix run "pub get && dart cli.dart format" inside scripts/'); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + Iterable<String> _getFilesWithExtension(Directory dir, String extension) => |
| 140 | + dir |
| 141 | + .listSync(recursive: true) |
| 142 | + .where((FileSystemEntity entity) => |
| 143 | + entity is File && p.extension(entity.path) == extension) |
| 144 | + .map((FileSystemEntity entity) => entity.path); |
| 145 | +} |
| 146 | + |
| 147 | +class BuildCommand extends Command { |
| 148 | + BuildCommand(this.packagesDir) { |
| 149 | + argParser.addFlag('ipa', defaultsTo: Platform.isMacOS); |
| 150 | + argParser.addFlag('apk', defaultsTo: !Platform.isMacOS); |
| 151 | + } |
| 152 | + |
| 153 | + final Directory packagesDir; |
| 154 | + |
| 155 | + final name = 'build'; |
| 156 | + final description = |
| 157 | + 'Builds all example apps. By default, an IPA is build on Mac and an APK ' |
| 158 | + 'on all other platforms.'; |
| 159 | + |
| 160 | + Future run() async { |
| 161 | + List<String> failingPackages = <String>[]; |
| 162 | + for (Directory example in _getExamplePackages(packagesDir)) { |
| 163 | + String packageName = p.relative(example.path, from: packagesDir.path); |
| 164 | + |
| 165 | + if (argResults['ipa']) { |
| 166 | + print('\nBUILDING IPA for $packageName'); |
| 167 | + Process process = await Process.start( |
| 168 | + 'flutter', ['build', 'ios', '--no-codesign'], |
| 169 | + workingDirectory: example.path); |
| 170 | + stdout.addStream(process.stdout); |
| 171 | + stderr.addStream(process.stderr); |
| 172 | + if (await process.exitCode != 0) { |
| 173 | + failingPackages.add('$packageName (ipa)'); |
| 174 | + } |
| 175 | + } else { |
| 176 | + print('\nSkipping IPA for $packageName (run with --ipa to build this)'); |
| 177 | + } |
| 178 | + |
| 179 | + if (argResults['apk']) { |
| 180 | + print('\nBUILDING APK for $packageName'); |
| 181 | + Process process = await Process.start('flutter', ['build', 'apk'], |
| 182 | + workingDirectory: example.path); |
| 183 | + stdout.addStream(process.stdout); |
| 184 | + stderr.addStream(process.stderr); |
| 185 | + if (await process.exitCode != 0) { |
| 186 | + failingPackages.add('$packageName (apk)'); |
| 187 | + } |
| 188 | + } else { |
| 189 | + print('\nSkipping APK for $packageName (run with --apk to build this)'); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + print('\n\n'); |
| 194 | + if (failingPackages.isNotEmpty) { |
| 195 | + print('The following build are failing (see above for details):'); |
| 196 | + failingPackages.forEach((String package) { |
| 197 | + print(' * $package'); |
| 198 | + }); |
| 199 | + } else { |
| 200 | + print('All builds successful!'); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + Iterable<Directory> _getExamplePackages(Directory dir) => dir |
| 205 | + .listSync(recursive: true) |
| 206 | + .where((FileSystemEntity entity) => |
| 207 | + entity is Directory && p.basename(entity.path) == 'example') |
| 208 | + .where((Directory dir) => dir.listSync().any((FileSystemEntity entity) => |
| 209 | + entity is File && p.basename(entity.path) == 'pubspec.yaml')); |
| 210 | +} |
0 commit comments