|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +bool _hasCommandOnPath(String name) { |
| 8 | + final ProcessResult result = Process.runSync('which', <String>[name]); |
| 9 | + return result.exitCode == 0; |
| 10 | +} |
| 11 | + |
| 12 | +List<String> _findPairs(Set<String> as, Set<String> bs) { |
| 13 | + final List<String> result = <String>[]; |
| 14 | + for (final String a in as) { |
| 15 | + if (bs.contains(a)) { |
| 16 | + result.add(a); |
| 17 | + } else { |
| 18 | + print('Mix match file $a.'); |
| 19 | + } |
| 20 | + } |
| 21 | + for (final String b in bs) { |
| 22 | + if (!as.contains(b)) { |
| 23 | + print('Mix match file $b.'); |
| 24 | + } |
| 25 | + } |
| 26 | + return result; |
| 27 | +} |
| 28 | + |
| 29 | +String _basename(String path) { |
| 30 | + return path.split(Platform.pathSeparator).last; |
| 31 | +} |
| 32 | + |
| 33 | +Set<String> _grabPngFilenames(Directory dir) { |
| 34 | + return dir.listSync() |
| 35 | + .map((FileSystemEntity e) => _basename(e.path)) |
| 36 | + .where((String e) => e.endsWith('.png')) |
| 37 | + .toSet(); |
| 38 | +} |
| 39 | + |
| 40 | +/// The main entry point to the tool, execute it like `main`. Returns the |
| 41 | +/// `exitCode`. |
| 42 | +int run(List<String> args) { |
| 43 | + int returnCode = 0; |
| 44 | + if (!_hasCommandOnPath('compare')) { |
| 45 | + throw Exception(r'Could not find `compare` from ImageMagick on $PATH.'); |
| 46 | + } |
| 47 | + if (args.length != 2) { |
| 48 | + throw Exception('Usage: compare_goldens.dart <dir path> <dir path>'); |
| 49 | + } |
| 50 | + |
| 51 | + final Directory dirA = Directory(args[0]); |
| 52 | + if (!dirA.existsSync()) { |
| 53 | + throw Exception('Unable to find $dirA'); |
| 54 | + } |
| 55 | + final Directory dirB = Directory(args[1]); |
| 56 | + if (!dirB.existsSync()) { |
| 57 | + throw Exception('Unable to find $dirB'); |
| 58 | + } |
| 59 | + |
| 60 | + final Set<String> filesA = _grabPngFilenames(dirA); |
| 61 | + final Set<String> filesB = _grabPngFilenames(dirB); |
| 62 | + final List<String> pairs = _findPairs(filesA, filesB); |
| 63 | + |
| 64 | + if (filesA.length != pairs.length || filesB.length != pairs.length) { |
| 65 | + returnCode = 1; |
| 66 | + } |
| 67 | + |
| 68 | + int count = 0; |
| 69 | + for (final String name in pairs) { |
| 70 | + count += 1; |
| 71 | + final String pathA = <String>[dirA.path, name].join(Platform.pathSeparator); |
| 72 | + final String pathB = <String>[dirB.path, name].join(Platform.pathSeparator); |
| 73 | + final String output = 'diff_$name'; |
| 74 | + print('compare ($count / ${pairs.length}) $name'); |
| 75 | + final ProcessResult result = Process.runSync('compare', |
| 76 | + <String>['-metric', 'RMSE', '-fuzz', '5%', pathA, pathB, output]); |
| 77 | + if (result.exitCode != 0) { |
| 78 | + print('DIFF FOUND: saved to $output'); |
| 79 | + returnCode = 1; |
| 80 | + } else { |
| 81 | + File(output).deleteSync(); |
| 82 | + } |
| 83 | + } |
| 84 | + return returnCode; |
| 85 | +} |
0 commit comments